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

test(enums): cover enums with operations #1091

Closed
wants to merge 11 commits into from
4 changes: 2 additions & 2 deletions runtime/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,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.
Expand Down
121 changes: 116 additions & 5 deletions test/features/enums/enums_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import (
"context"
"testing"

"github.com/stretchr/testify/assert"

"github.com/steebchen/prisma-client-go/test"
"github.com/steebchen/prisma-client-go/test/helpers/massert"
)

type cx = context.Context
Expand Down Expand Up @@ -51,9 +50,9 @@ 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(
actual, err := client.User.FindFirst(
User.Role.Equals(RoleAdmin),
User.Role.In([]Role{RoleAdmin}),
User.RoleOpt.Equals(RoleModerator),
Expand All @@ -63,7 +62,119 @@ func TestEnums(t *testing.T) {
t.Fatalf("fail %s", err)
}

assert.Equal(t, []UserModel{*expected}, actual)
massert.Equal(t, expected, actual)
},
}, {
name: "many or with and wrapper",
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),
),
),
).OrderBy(
User.ID.Order(SortOrderAsc),
).Exec(ctx)
if err != nil {
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{
ID: "123",
Role: RoleAdmin,
},
},
{
InnerUser: InnerUser{
ID: "789",
Role: RoleUser,
},
},
}, actual)
},
}}
for _, tt := range tests {
Expand Down
Loading