Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(test/enums): use table test for enums #1090

Merged
merged 3 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 67 additions & 46 deletions test/features/enums/enums_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,51 +9,72 @@ import (
"github.com/steebchen/prisma-client-go/test"
)

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())
})
})
}
}
6 changes: 3 additions & 3 deletions test/features/enums/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down