From c1eaa3e40d58008b3da1b0df241f36e75c8292af Mon Sep 17 00:00:00 2001 From: steebchen Date: Wed, 8 Nov 2023 17:30:42 +0700 Subject: [PATCH 1/8] refactor(test/enums): use table test for enums --- test/features/enums/enums_test.go | 116 +++++++++++++++++------------- test/features/enums/schema.prisma | 6 +- 2 files changed, 71 insertions(+), 51 deletions(-) diff --git a/test/features/enums/enums_test.go b/test/features/enums/enums_test.go index f70ebbac..864f1dfa 100644 --- a/test/features/enums/enums_test.go +++ b/test/features/enums/enums_test.go @@ -4,56 +4,76 @@ import ( "context" "testing" - "github.com/stretchr/testify/assert" - "github.com/steebchen/prisma-client-go/test" + "github.com/stretchr/testify/assert" ) +type cx = context.Context +type Func func(t *testing.T, client *PrismaClient, ctx cx) + func TestEnums(t *testing.T) { - // TODO - test.RunParallel(t, []test.Database{test.MySQL, test.PostgreSQL, test.MongoDB}, func(t *testing.T, db test.Database, ctx context.Context) { - client := NewClient() - - mockDB := test.Start(t, db, client.Engine, []string{}) - defer test.End(t, db, client.Engine, mockDB) - - admin := RoleAdmin - mod := RoleModerator - expected := &UserModel{ - InnerUser: InnerUser{ - ID: "123", - Role: admin, - RoleOpt: &mod, - Stuff1: StuffCASING, - Stuff2: StuffDifferent, - Stuff3: StuffHaHa, - }, - } - - created, err := client.User.CreateOne( - User.Role.Set(RoleAdmin), - User.Stuff1.Set(StuffCASING), - User.Stuff2.Set(StuffDifferent), - User.Stuff3.Set(StuffHaHa), - User.ID.Set("123"), - User.RoleOpt.Set(RoleModerator), - ).Exec(ctx) - if err != nil { - t.Fatalf("fail %s", err) - } - - assert.Equal(t, expected, created) - - actual, err := client.User.FindMany( - User.Role.Equals(RoleAdmin), - User.Role.In([]Role{RoleAdmin}), - User.RoleOpt.Equals(RoleModerator), - User.RoleOpt.In([]Role{RoleModerator}), - ).Exec(ctx) - if err != nil { - t.Fatalf("fail %s", err) - } - - assert.Equal(t, []UserModel{*expected}, actual) - }) + t.Parallel() + + tests := []struct { + name string + before []string + run Func + }{{ + name: "create", + run: func(t *testing.T, client *PrismaClient, ctx cx) { + admin := RoleAdmin + mod := RoleModerator + + stuffCASING := StuffCASING + stuffDifferent := StuffDifferent + stuffHaHa := StuffHaHa + expected := &UserModel{ + InnerUser: InnerUser{ + ID: "123", + Role: admin, + RoleOpt: &mod, + Stuff1: &stuffCASING, + Stuff2: &stuffDifferent, + Stuff3: &stuffHaHa, + }, + } + + created, err := client.User.CreateOne( + User.Role.Set(RoleAdmin), + User.Stuff1.Set(StuffCASING), + User.Stuff2.Set(StuffDifferent), + User.Stuff3.Set(StuffHaHa), + User.ID.Set("123"), + User.RoleOpt.Set(RoleModerator), + ).Exec(ctx) + if err != nil { + t.Fatalf("fail %s", err) + } + + assert.Equal(t, expected, created) + + actual, err := client.User.FindMany( + User.Role.Equals(RoleAdmin), + User.Role.In([]Role{RoleAdmin}), + User.RoleOpt.Equals(RoleModerator), + User.RoleOpt.In([]Role{RoleModerator}), + ).Exec(ctx) + if err != nil { + t.Fatalf("fail %s", err) + } + + assert.Equal(t, []UserModel{*expected}, actual) + }, + }} + for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + test.RunSerial(t, []test.Database{test.MySQL, test.PostgreSQL, test.MongoDB}, func(t *testing.T, db test.Database, ctx context.Context) { + client := NewClient() + mockDBName := test.Start(t, db, client.Engine, tt.before) + defer test.End(t, db, client.Engine, mockDBName) + tt.run(t, client, context.Background()) + }) + }) + } } diff --git a/test/features/enums/schema.prisma b/test/features/enums/schema.prisma index b3a3f63f..e36c66e5 100644 --- a/test/features/enums/schema.prisma +++ b/test/features/enums/schema.prisma @@ -14,9 +14,9 @@ model User { id String @id @default(cuid()) @map("_id") role Role roleOpt Role? - stuff1 Stuff - stuff2 Stuff - stuff3 Stuff + stuff1 Stuff? + stuff2 Stuff? + stuff3 Stuff? } enum Role { From 2fa8d43d1356bf40233027be4c24277dbeb064a2 Mon Sep 17 00:00:00 2001 From: steebchen Date: Wed, 8 Nov 2023 17:30:42 +0700 Subject: [PATCH 2/8] refactor(test/enums): use table test for enums --- test/features/enums/enums_test.go | 116 +++++++++++++++++------------- test/features/enums/schema.prisma | 6 +- 2 files changed, 71 insertions(+), 51 deletions(-) diff --git a/test/features/enums/enums_test.go b/test/features/enums/enums_test.go index f70ebbac..864f1dfa 100644 --- a/test/features/enums/enums_test.go +++ b/test/features/enums/enums_test.go @@ -4,56 +4,76 @@ import ( "context" "testing" - "github.com/stretchr/testify/assert" - "github.com/steebchen/prisma-client-go/test" + "github.com/stretchr/testify/assert" ) +type cx = context.Context +type Func func(t *testing.T, client *PrismaClient, ctx cx) + func TestEnums(t *testing.T) { - // TODO - test.RunParallel(t, []test.Database{test.MySQL, test.PostgreSQL, test.MongoDB}, func(t *testing.T, db test.Database, ctx context.Context) { - client := NewClient() - - mockDB := test.Start(t, db, client.Engine, []string{}) - defer test.End(t, db, client.Engine, mockDB) - - admin := RoleAdmin - mod := RoleModerator - expected := &UserModel{ - InnerUser: InnerUser{ - ID: "123", - Role: admin, - RoleOpt: &mod, - Stuff1: StuffCASING, - Stuff2: StuffDifferent, - Stuff3: StuffHaHa, - }, - } - - created, err := client.User.CreateOne( - User.Role.Set(RoleAdmin), - User.Stuff1.Set(StuffCASING), - User.Stuff2.Set(StuffDifferent), - User.Stuff3.Set(StuffHaHa), - User.ID.Set("123"), - User.RoleOpt.Set(RoleModerator), - ).Exec(ctx) - if err != nil { - t.Fatalf("fail %s", err) - } - - assert.Equal(t, expected, created) - - actual, err := client.User.FindMany( - User.Role.Equals(RoleAdmin), - User.Role.In([]Role{RoleAdmin}), - User.RoleOpt.Equals(RoleModerator), - User.RoleOpt.In([]Role{RoleModerator}), - ).Exec(ctx) - if err != nil { - t.Fatalf("fail %s", err) - } - - assert.Equal(t, []UserModel{*expected}, actual) - }) + t.Parallel() + + tests := []struct { + name string + before []string + run Func + }{{ + name: "create", + run: func(t *testing.T, client *PrismaClient, ctx cx) { + admin := RoleAdmin + mod := RoleModerator + + stuffCASING := StuffCASING + stuffDifferent := StuffDifferent + stuffHaHa := StuffHaHa + expected := &UserModel{ + InnerUser: InnerUser{ + ID: "123", + Role: admin, + RoleOpt: &mod, + Stuff1: &stuffCASING, + Stuff2: &stuffDifferent, + Stuff3: &stuffHaHa, + }, + } + + created, err := client.User.CreateOne( + User.Role.Set(RoleAdmin), + User.Stuff1.Set(StuffCASING), + User.Stuff2.Set(StuffDifferent), + User.Stuff3.Set(StuffHaHa), + User.ID.Set("123"), + User.RoleOpt.Set(RoleModerator), + ).Exec(ctx) + if err != nil { + t.Fatalf("fail %s", err) + } + + assert.Equal(t, expected, created) + + actual, err := client.User.FindMany( + User.Role.Equals(RoleAdmin), + User.Role.In([]Role{RoleAdmin}), + User.RoleOpt.Equals(RoleModerator), + User.RoleOpt.In([]Role{RoleModerator}), + ).Exec(ctx) + if err != nil { + t.Fatalf("fail %s", err) + } + + assert.Equal(t, []UserModel{*expected}, actual) + }, + }} + for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + test.RunSerial(t, []test.Database{test.MySQL, test.PostgreSQL, test.MongoDB}, func(t *testing.T, db test.Database, ctx context.Context) { + client := NewClient() + mockDBName := test.Start(t, db, client.Engine, tt.before) + defer test.End(t, db, client.Engine, mockDBName) + tt.run(t, client, context.Background()) + }) + }) + } } diff --git a/test/features/enums/schema.prisma b/test/features/enums/schema.prisma index b3a3f63f..e36c66e5 100644 --- a/test/features/enums/schema.prisma +++ b/test/features/enums/schema.prisma @@ -14,9 +14,9 @@ model User { id String @id @default(cuid()) @map("_id") role Role roleOpt Role? - stuff1 Stuff - stuff2 Stuff - stuff3 Stuff + stuff1 Stuff? + stuff2 Stuff? + stuff3 Stuff? } enum Role { From 16abc0308372ae347a17e218bdc34b452b69960d Mon Sep 17 00:00:00 2001 From: steebchen Date: Wed, 8 Nov 2023 17:52:05 +0700 Subject: [PATCH 3/8] wip test --- runtime/builder/builder.go | 8 +- test/features/enums/enums_test.go | 127 +++++++++++++++++++++++++++++- 2 files changed, 130 insertions(+), 5 deletions(-) diff --git a/runtime/builder/builder.go b/runtime/builder/builder.go index 6a7f3ad2..54a24d5a 100644 --- a/runtime/builder/builder.go +++ b/runtime/builder/builder.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "log" "strings" "time" @@ -32,10 +33,10 @@ type Field struct { // The Name of the field. Name string - // List saves whether the fields is a list of items + // List saves whether the fields are a list of items List bool - // WrapList saves whether the a list field should be wrapped in an object + // WrapList saves whether the field should be wrapped in an individual object WrapList bool // Value contains the field value. if nil, fields will contain a subselection. @@ -199,6 +200,9 @@ func (q Query) buildFields(list bool, wrapList bool, fields []Field) string { final = append(final, *uniques[name]) } + d, _ := json.MarshalIndent(final, "", " ") + log.Printf("final: %s", d) + for _, f := range final { if wrapList { builder.WriteString("{") diff --git a/test/features/enums/enums_test.go b/test/features/enums/enums_test.go index 864f1dfa..74e2a042 100644 --- a/test/features/enums/enums_test.go +++ b/test/features/enums/enums_test.go @@ -5,7 +5,7 @@ import ( "testing" "github.com/steebchen/prisma-client-go/test" - "github.com/stretchr/testify/assert" + "github.com/steebchen/prisma-client-go/test/helpers/massert" ) type cx = context.Context @@ -50,7 +50,7 @@ func TestEnums(t *testing.T) { t.Fatalf("fail %s", err) } - assert.Equal(t, expected, created) + massert.Equal(t, expected, created) actual, err := client.User.FindMany( User.Role.Equals(RoleAdmin), @@ -62,7 +62,128 @@ func TestEnums(t *testing.T) { t.Fatalf("fail %s", err) } - assert.Equal(t, []UserModel{*expected}, actual) + massert.Equal(t, []UserModel{*expected}, actual) + }, + }, { + name: "many or", + run: func(t *testing.T, client *PrismaClient, ctx cx) { + _, err := client.User.CreateOne( + User.Role.Set(RoleAdmin), + User.ID.Set("123"), + ).Exec(ctx) + if err != nil { + t.Fatalf("fail %s", err) + } + + _, err = client.User.CreateOne( + User.Role.Set(RoleModerator), + User.ID.Set("456"), + ).Exec(ctx) + if err != nil { + t.Fatalf("fail %s", err) + } + + _, err = client.User.CreateOne( + User.Role.Set(RoleUser), + User.ID.Set("789"), + ).Exec(ctx) + if err != nil { + t.Fatalf("fail %s", err) + } + + actual, err := client.User.FindMany( + User.Or( + User.And( + User.Role.Equals(RoleModerator), + ), + User.And( + User.Role.Equals(RoleAdmin), + ), + ), + ).Exec(ctx) + if err != nil { + t.Fatalf("fail %s", err) + } + + massert.Equal(t, []UserModel{ + { + InnerUser: InnerUser{ + ID: "123", + Role: RoleAdmin, + }, + }, + { + InnerUser: InnerUser{ + ID: "456", + Role: RoleModerator, + }, + }, + }, actual) + }, + }, { + name: "many or", + run: func(t *testing.T, client *PrismaClient, ctx cx) { + _, err := client.User.CreateOne( + User.Role.Set(RoleAdmin), + User.ID.Set("123"), + ).Exec(ctx) + if err != nil { + t.Fatalf("fail %s", err) + } + + _, err = client.User.CreateOne( + User.Role.Set(RoleModerator), + User.ID.Set("456"), + ).Exec(ctx) + if err != nil { + t.Fatalf("fail %s", err) + } + + _, err = client.User.CreateOne( + User.Role.Set(RoleUser), + User.ID.Set("789"), + ).Exec(ctx) + if err != nil { + t.Fatalf("fail %s", err) + } + + actual, err := client.User.FindMany( + User.Or( + User.And( + User.Role.Equals(RoleUser), + ), + User.And( + User.Role.Equals(RoleAdmin), + ), + User.And( + User.Role.Equals(RoleModerator), + ), + ), + ).Exec(ctx) + if err != nil { + t.Fatalf("fail %s", err) + } + + massert.Equal(t, []UserModel{ + { + InnerUser: InnerUser{ + ID: "123", + Role: RoleAdmin, + }, + }, + { + InnerUser: InnerUser{ + ID: "456", + Role: RoleModerator, + }, + }, + { + InnerUser: InnerUser{ + ID: "789", + Role: RoleUser, + }, + }, + }, actual) }, }} for _, tt := range tests { From cea9a5c88a139adfe79691c5e492fe12ed9098ff Mon Sep 17 00:00:00 2001 From: steebchen Date: Wed, 8 Nov 2023 17:58:39 +0700 Subject: [PATCH 4/8] lint fixes --- test/features/enums/enums_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/features/enums/enums_test.go b/test/features/enums/enums_test.go index 864f1dfa..037da8ae 100644 --- a/test/features/enums/enums_test.go +++ b/test/features/enums/enums_test.go @@ -4,8 +4,9 @@ import ( "context" "testing" - "github.com/steebchen/prisma-client-go/test" "github.com/stretchr/testify/assert" + + "github.com/steebchen/prisma-client-go/test" ) type cx = context.Context From c002123d04d0681262181f16e71397261ad1b2e4 Mon Sep 17 00:00:00 2001 From: steebchen Date: Wed, 8 Nov 2023 18:01:35 +0700 Subject: [PATCH 5/8] undo --- runtime/builder/builder.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/runtime/builder/builder.go b/runtime/builder/builder.go index 54a24d5a..5eb72995 100644 --- a/runtime/builder/builder.go +++ b/runtime/builder/builder.go @@ -4,7 +4,6 @@ import ( "context" "encoding/json" "fmt" - "log" "strings" "time" @@ -200,9 +199,6 @@ func (q Query) buildFields(list bool, wrapList bool, fields []Field) string { final = append(final, *uniques[name]) } - d, _ := json.MarshalIndent(final, "", " ") - log.Printf("final: %s", d) - for _, f := range final { if wrapList { builder.WriteString("{") From 0e8a3282cf48e1973f1609468a87728386417eab Mon Sep 17 00:00:00 2001 From: steebchen Date: Wed, 8 Nov 2023 18:12:36 +0700 Subject: [PATCH 6/8] whoops --- test/features/enums/enums_test.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/test/features/enums/enums_test.go b/test/features/enums/enums_test.go index 4bce1635..74e2a042 100644 --- a/test/features/enums/enums_test.go +++ b/test/features/enums/enums_test.go @@ -11,9 +11,6 @@ import ( type cx = context.Context type Func func(t *testing.T, client *PrismaClient, ctx cx) -type cx = context.Context -type Func func(t *testing.T, client *PrismaClient, ctx cx) - func TestEnums(t *testing.T) { t.Parallel() From beb19b8aeead9e27c025c7b5cb17134272718d0b Mon Sep 17 00:00:00 2001 From: steebchen Date: Wed, 8 Nov 2023 18:17:00 +0700 Subject: [PATCH 7/8] adapt tests --- test/features/enums/enums_test.go | 71 ++----------------------------- 1 file changed, 4 insertions(+), 67 deletions(-) diff --git a/test/features/enums/enums_test.go b/test/features/enums/enums_test.go index 74e2a042..58d6f7e6 100644 --- a/test/features/enums/enums_test.go +++ b/test/features/enums/enums_test.go @@ -52,7 +52,7 @@ func TestEnums(t *testing.T) { massert.Equal(t, expected, created) - actual, err := client.User.FindMany( + actual, err := client.User.FindFirst( User.Role.Equals(RoleAdmin), User.Role.In([]Role{RoleAdmin}), User.RoleOpt.Equals(RoleModerator), @@ -62,63 +62,7 @@ func TestEnums(t *testing.T) { t.Fatalf("fail %s", err) } - massert.Equal(t, []UserModel{*expected}, actual) - }, - }, { - name: "many or", - run: func(t *testing.T, client *PrismaClient, ctx cx) { - _, err := client.User.CreateOne( - User.Role.Set(RoleAdmin), - User.ID.Set("123"), - ).Exec(ctx) - if err != nil { - t.Fatalf("fail %s", err) - } - - _, err = client.User.CreateOne( - User.Role.Set(RoleModerator), - User.ID.Set("456"), - ).Exec(ctx) - if err != nil { - t.Fatalf("fail %s", err) - } - - _, err = client.User.CreateOne( - User.Role.Set(RoleUser), - User.ID.Set("789"), - ).Exec(ctx) - if err != nil { - t.Fatalf("fail %s", err) - } - - actual, err := client.User.FindMany( - User.Or( - User.And( - User.Role.Equals(RoleModerator), - ), - User.And( - User.Role.Equals(RoleAdmin), - ), - ), - ).Exec(ctx) - if err != nil { - t.Fatalf("fail %s", err) - } - - massert.Equal(t, []UserModel{ - { - InnerUser: InnerUser{ - ID: "123", - Role: RoleAdmin, - }, - }, - { - InnerUser: InnerUser{ - ID: "456", - Role: RoleModerator, - }, - }, - }, actual) + massert.Equal(t, expected, actual) }, }, { name: "many or", @@ -155,10 +99,9 @@ func TestEnums(t *testing.T) { User.And( User.Role.Equals(RoleAdmin), ), - User.And( - User.Role.Equals(RoleModerator), - ), ), + ).OrderBy( + User.ID.Order(SortOrderAsc), ).Exec(ctx) if err != nil { t.Fatalf("fail %s", err) @@ -171,12 +114,6 @@ func TestEnums(t *testing.T) { Role: RoleAdmin, }, }, - { - InnerUser: InnerUser{ - ID: "456", - Role: RoleModerator, - }, - }, { InnerUser: InnerUser{ ID: "789", From cfb235fdb5e5cfe6c01536d79d7b8d51840a8505 Mon Sep 17 00:00:00 2001 From: steebchen Date: Wed, 8 Nov 2023 18:19:43 +0700 Subject: [PATCH 8/8] add failing test --- test/features/enums/enums_test.go | 56 ++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/test/features/enums/enums_test.go b/test/features/enums/enums_test.go index 58d6f7e6..9097c03c 100644 --- a/test/features/enums/enums_test.go +++ b/test/features/enums/enums_test.go @@ -65,7 +65,7 @@ func TestEnums(t *testing.T) { massert.Equal(t, expected, actual) }, }, { - name: "many or", + name: "many or with and wrapper", run: func(t *testing.T, client *PrismaClient, ctx cx) { _, err := client.User.CreateOne( User.Role.Set(RoleAdmin), @@ -107,6 +107,60 @@ func TestEnums(t *testing.T) { t.Fatalf("fail %s", err) } + massert.Equal(t, []UserModel{ + { + InnerUser: InnerUser{ + ID: "123", + Role: RoleAdmin, + }, + }, + { + InnerUser: InnerUser{ + ID: "789", + Role: RoleUser, + }, + }, + }, actual) + }, + }, { + name: "many or direct", + run: func(t *testing.T, client *PrismaClient, ctx cx) { + _, err := client.User.CreateOne( + User.Role.Set(RoleAdmin), + User.ID.Set("123"), + ).Exec(ctx) + if err != nil { + t.Fatalf("fail %s", err) + } + + _, err = client.User.CreateOne( + User.Role.Set(RoleModerator), + User.ID.Set("456"), + ).Exec(ctx) + if err != nil { + t.Fatalf("fail %s", err) + } + + _, err = client.User.CreateOne( + User.Role.Set(RoleUser), + User.ID.Set("789"), + ).Exec(ctx) + if err != nil { + t.Fatalf("fail %s", err) + } + + actual, err := client.User.FindMany( + User.Or( + User.Role.Equals(RoleUser), + User.Role.Equals(RoleAdmin), + ), + ).OrderBy( + User.ID.Order(SortOrderAsc), + ).Exec(ctx) + if err != nil { + t.Fatalf("fail %s", err) + } + massert.Equal(t, []UserModel{ { InnerUser: InnerUser{