lens @ 5168a9476f0e83264ecafc85bc9145e8bdcbb8dc

  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	testCases := []struct {
 16		name       string
 17		createUser *repository.CreateUser
 18		want       *repository.User
 19	}{
 20		{
 21			name: "Normal",
 22			createUser: &repository.CreateUser{
 23				Username: "new_username",
 24				Name:     "new_name",
 25			},
 26			want: &repository.User{
 27				ID:       1, // since the database is being created I can assume 1
 28				Username: "new_username",
 29				Name:     "new_name",
 30			},
 31		},
 32	}
 33
 34	for _, tc := range testCases {
 35		t.Run(tc.name, func(t *testing.T) {
 36			db, tearDown := setup(t)
 37			defer tearDown()
 38
 39			userRepository := NewUserRepository(db)
 40			id, err := userRepository.Create(context.Background(), tc.createUser)
 41			if err != nil {
 42				t.Fatalf("Error creating: %s", err.Error())
 43			}
 44
 45			got, err := userRepository.Get(context.Background(), id)
 46			if err != nil {
 47				t.Fatalf("Error getting: %s", err.Error())
 48			}
 49
 50			if diff := cmp.Diff(tc.want, got); diff != "" {
 51				t.Errorf("%s() mismatch (-want +got):\n%s", "Update", diff)
 52			}
 53		})
 54	}
 55}
 56
 57func TestUpdate(t *testing.T) {
 58	testCases := []struct {
 59		name       string
 60		createUser *repository.CreateUser
 61		want       *repository.User
 62	}{
 63		{
 64			name: "Normal creation",
 65			createUser: &repository.CreateUser{
 66				Username: "username",
 67				Name:     "name",
 68				IsAdmin:  true,
 69				Path:     "/tmp/new_folder",
 70			},
 71			want: &repository.User{
 72				ID:       1, // since the database is being created I can assume 1
 73				Username: "new_username",
 74				Name:     "new_name",
 75				IsAdmin:  true,
 76				Path:     "/tmp/new_folder",
 77			},
 78		},
 79	}
 80
 81	for _, tc := range testCases {
 82		t.Run(tc.name, func(t *testing.T) {
 83			db, tearDown := setup(t)
 84			defer tearDown()
 85
 86			userRepository := NewUserRepository(db)
 87
 88			id, err := userRepository.Create(context.Background(), tc.createUser)
 89			if err != nil {
 90				t.Fatalf("Error creating user: %s", err.Error())
 91			}
 92
 93			err = userRepository.Update(context.Background(), id, &repository.UpdateUser{
 94				Username: "new_username",
 95				Name:     "new_name",
 96			})
 97			if err != nil {
 98				t.Fatalf("Error update user: %s", err.Error())
 99			}
100
101			got, err := userRepository.Get(context.Background(), 1)
102			if err != nil {
103				t.Fatalf("Error getting user: %s", err.Error())
104			}
105
106			if diff := cmp.Diff(tc.want, got); diff != "" {
107				t.Errorf("%s() mismatch (-want +got):\n%s", "Update", diff)
108			}
109
110		})
111	}
112}