diff options
Diffstat (limited to 'pkg/database/sql')
-rw-r--r-- | pkg/database/sql/user_test.go | 138 |
1 files changed, 85 insertions, 53 deletions
diff --git a/pkg/database/sql/user_test.go b/pkg/database/sql/user_test.go index db43676..fda7440 100644 --- a/pkg/database/sql/user_test.go +++ b/pkg/database/sql/user_test.go @@ -12,69 +12,101 @@ import ( ) func TestCreate(t *testing.T) { - t.Parallel() - db, tearDown := setup(t) - defer tearDown() - - userRepository := NewUserRepository(db) - - id, err := userRepository.Create(context.Background(), &repository.CreateUser{ - Username: "new_username", - Name: "new_name", - }) - if err != nil { - t.Fatalf("Error creating: %s", err.Error()) + testCases := []struct { + name string + createUser *repository.CreateUser + want *repository.User + }{ + { + name: "Normal", + createUser: &repository.CreateUser{ + Username: "new_username", + Name: "new_name", + }, + want: &repository.User{ + ID: 1, // since the database is being created I can assume 1 + Username: "new_username", + Name: "new_name", + }, + }, } - got, err := userRepository.Get(context.Background(), id) - if err != nil { - t.Fatalf("Error getting: %s", err.Error()) - } - want := &repository.User{ - ID: id, - Username: "new_username", - Name: "new_name", - } + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + db, tearDown := setup(t) + defer tearDown() + + userRepository := NewUserRepository(db) + id, err := userRepository.Create(context.Background(), tc.createUser) + if err != nil { + t.Fatalf("Error creating: %s", err.Error()) + } - if diff := cmp.Diff(want, got); diff != "" { - t.Errorf("%s() mismatch (-want +got):\n%s", "Update", diff) + got, err := userRepository.Get(context.Background(), id) + if err != nil { + t.Fatalf("Error getting: %s", err.Error()) + } + + if diff := cmp.Diff(tc.want, got); diff != "" { + t.Errorf("%s() mismatch (-want +got):\n%s", "Update", diff) + } + }) } } func TestUpdate(t *testing.T) { - t.Parallel() - db, tearDown := setup(t) - defer tearDown() - - userRepository := NewUserRepository(db) - - id, err := userRepository.Create(context.Background(), &repository.CreateUser{ - Username: "username", - Name: "name", - }) - if err != nil { - t.Fatalf("Error creating user: %s", err.Error()) + testCases := []struct { + name string + createUser *repository.CreateUser + want *repository.User + }{ + { + name: "Normal creation", + createUser: &repository.CreateUser{ + Username: "username", + Name: "name", + IsAdmin: true, + Path: "/tmp/new_folder", + }, + want: &repository.User{ + ID: 1, // since the database is being created I can assume 1 + Username: "new_username", + Name: "new_name", + IsAdmin: true, + Path: "/tmp/new_folder", + }, + }, } - err = userRepository.Update(context.Background(), id, &repository.UpdateUser{ - Username: "new_username", - Name: "new_name", - }) - if err != nil { - t.Fatalf("Error update user: %s", err.Error()) - } + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + db, tearDown := setup(t) + defer tearDown() - got, err := userRepository.Get(context.Background(), 1) - if err != nil { - t.Fatalf("Error getting user: %s", err.Error()) - } - want := &repository.User{ - ID: id, - Username: "new_username", - Name: "new_name", - } + userRepository := NewUserRepository(db) + + id, err := userRepository.Create(context.Background(), tc.createUser) + if err != nil { + t.Fatalf("Error creating user: %s", err.Error()) + } + + err = userRepository.Update(context.Background(), id, &repository.UpdateUser{ + Username: "new_username", + Name: "new_name", + }) + if err != nil { + t.Fatalf("Error update user: %s", err.Error()) + } + + got, err := userRepository.Get(context.Background(), 1) + if err != nil { + t.Fatalf("Error getting user: %s", err.Error()) + } + + if diff := cmp.Diff(tc.want, got); diff != "" { + t.Errorf("%s() mismatch (-want +got):\n%s", "Update", diff) + } - if diff := cmp.Diff(want, got); diff != "" { - t.Errorf("%s() mismatch (-want +got):\n%s", "Update", diff) + }) } } |