lens @ 52276ba99540d6c61195772ccdd50944e96f4959

 1//go:build integration
 2
 3package sql
 4
 5import (
 6	"context"
 7	"testing"
 8
 9	"github.com/google/go-cmp/cmp"
10
11	"git.sr.ht/~gabrielgio/img/pkg/database/repository"
12)
13
14func TestCreate(t *testing.T) {
15	t.Parallel()
16	db, tearDown := setup(t)
17	defer tearDown()
18
19	userRepository := NewUserRepository(db)
20
21	id, err := userRepository.Create(context.Background(), &repository.CreateUser{
22		Username: "new_username",
23		Name:     "new_name",
24	})
25	if err != nil {
26		t.Fatalf("Error creating: %s", err.Error())
27	}
28
29	got, err := userRepository.Get(context.Background(), id)
30	if err != nil {
31		t.Fatalf("Error getting: %s", err.Error())
32	}
33	want := &repository.User{
34		ID:       id,
35		Username: "new_username",
36		Name:     "new_name",
37	}
38
39	if diff := cmp.Diff(want, got); diff != "" {
40		t.Errorf("%s() mismatch (-want +got):\n%s", "Update", diff)
41	}
42}
43
44func TestUpdate(t *testing.T) {
45	t.Parallel()
46	db, tearDown := setup(t)
47	defer tearDown()
48
49	userRepository := NewUserRepository(db)
50
51	id, err := userRepository.Create(context.Background(), &repository.CreateUser{
52		Username: "username",
53		Name:     "name",
54	})
55	if err != nil {
56		t.Fatalf("Error creating user: %s", err.Error())
57	}
58
59	err = userRepository.Update(context.Background(), id, &repository.UpdateUser{
60		Username: "new_username",
61		Name:     "new_name",
62	})
63	if err != nil {
64		t.Fatalf("Error update user: %s", err.Error())
65	}
66
67	got, err := userRepository.Get(context.Background(), 1)
68	if err != nil {
69		t.Fatalf("Error getting user: %s", err.Error())
70	}
71	want := &repository.User{
72		ID:       id,
73		Username: "new_username",
74		Name:     "new_name",
75	}
76
77	if diff := cmp.Diff(want, got); diff != "" {
78		t.Errorf("%s() mismatch (-want +got):\n%s", "Update", diff)
79	}
80}