Skip to content

Commit

Permalink
check all fields, do not limit to OR
Browse files Browse the repository at this point in the history
  • Loading branch information
steebchen committed Nov 9, 2023
1 parent f340320 commit 4cfc479
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 14 deletions.
24 changes: 17 additions & 7 deletions runtime/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"log"
"strings"
"time"

Expand Down Expand Up @@ -187,7 +188,7 @@ func (q Query) buildOutputs(outputs []Output) (string, error) {
return builder.String(), nil
}

var ErrOrWithoutAnd = fmt.Errorf("OR can only be used with ANDs")
var ErrDuplicateField = fmt.Errorf("duplicate field")

func (q Query) buildFields(list bool, wrapList bool, fields []Field) (string, error) {
var builder strings.Builder
Expand Down Expand Up @@ -226,12 +227,8 @@ func (q Query) buildFields(list bool, wrapList bool, fields []Field) (string, er
}

for _, f := range final {
if f.Name == "OR" {
for _, field := range f.Fields {
if field.Name != "AND" {
return "", ErrOrWithoutAnd
}
}
if err := checkFields(f, f.Fields); err != nil {
return "", err
}

if wrapList {
Expand Down Expand Up @@ -280,6 +277,19 @@ func (q Query) buildFields(list bool, wrapList bool, fields []Field) (string, er
return builder.String(), nil
}

func checkFields(parent Field, fields []Field) error {
uniqueObjectFields := make(map[string]Field)
for _, f := range fields {
if f.Value != nil && !f.List && !parent.List {
if _, ok := uniqueObjectFields[f.Name]; ok {
return fmt.Errorf("%w: %q", ErrDuplicateField, f.Name)
}
uniqueObjectFields[f.Name] = f
}
}
return nil
}

func (q Query) Exec(ctx context.Context, into interface{}) error {
str, err := q.Build()
if err != nil {
Expand Down
22 changes: 20 additions & 2 deletions test/features/enums/enums_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package enums

import (
"context"
"errors"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -160,8 +161,25 @@ func TestEnums(t *testing.T) {
).OrderBy(
User.ID.Order(SortOrderAsc),
).Exec(ctx)

assert.Equal(t, builder.ErrOrWithoutAnd, err)
//if err != nil {
// t.Fatalf("fail %s", err)
//}

assert.Equal(t, builder.ErrDuplicateField, errors.Unwrap(err))
//massert.Equal(t, []UserModel{
// {
// InnerUser: InnerUser{
// ID: "123",
// Role: RoleAdmin,
// },
// },
// {
// InnerUser: InnerUser{
// ID: "789",
// Role: RoleUser,
// },
// },
//}, actual)
},
}}
for _, tt := range tests {
Expand Down
30 changes: 25 additions & 5 deletions test/projects/basic/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package basic
import (
"context"
"encoding/json"
"errors"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -652,18 +653,36 @@ func TestBasic(t *testing.T) {
}
`},
run: func(t *testing.T, client *PrismaClient, ctx cx) {
_, err := client.User.FindMany(
actual, err := client.User.FindMany(
User.Or(
User.Email.Equals("email1"),
User.ID.Equals("id2"),
),
).OrderBy(
User.ID.Order(SortOrderAsc),
).Exec(ctx)
assert.Equal(t, builder.ErrOrWithoutAnd, err)
if err != nil {
t.Fatalf("fail %s", err)
}

expected := []UserModel{{
InnerUser: InnerUser{
ID: "id1",
Email: "email1",
Username: "a",
},
}, {
InnerUser: InnerUser{
ID: "id2",
Email: "email2",
Username: "b",
},
}}

assert.Equal(t, expected, actual)
},
}, {
name: "OR operations complex",
name: "OR operations complex with and",
// language=GraphQL
before: []string{`
mutation {
Expand Down Expand Up @@ -772,7 +791,7 @@ func TestBasic(t *testing.T) {
assert.Equal(t, expected, actual)
},
}, {
name: "OR operations complex fail",
name: "OR operations complex no wrap",
// language=GraphQL
before: []string{`
mutation {
Expand Down Expand Up @@ -837,7 +856,8 @@ func TestBasic(t *testing.T) {
).OrderBy(
User.ID.Order(SortOrderAsc),
).Exec(ctx)
assert.Equal(t, builder.ErrOrWithoutAnd, err)

assert.Equal(t, builder.ErrDuplicateField, errors.Unwrap(err))
},
}, {
name: "id in",
Expand Down

0 comments on commit 4cfc479

Please sign in to comment.