aboutsummaryrefslogtreecommitdiff
path: root/pkg
diff options
context:
space:
mode:
authorGabriel Arakaki Giovanini <mail@gabrielgio.me>2023-07-24 21:11:35 +0200
committerGabriel Arakaki Giovanini <mail@gabrielgio.me>2023-07-24 21:11:35 +0200
commit2a986064a2657c9091a31a941ce0d004191b8453 (patch)
treebca26e42e1eba24c66057d9e9ede9d9c7f7940bc /pkg
parent3b9d27649a31e5af3fb137ff5b3378e7b8f7b9ae (diff)
downloadlens-2a986064a2657c9091a31a941ce0d004191b8453.tar.gz
lens-2a986064a2657c9091a31a941ce0d004191b8453.tar.bz2
lens-2a986064a2657c9091a31a941ce0d004191b8453.zip
ref: Move to boxed tests
Diffstat (limited to 'pkg')
-rw-r--r--pkg/database/sql/user_test.go138
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)
+ })
}
}