-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
171 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
package graph | ||
|
||
import ( | ||
"fmt" | ||
"sort" | ||
"testing" | ||
|
||
"github.com/benweint/gquil/pkg/model" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/vektah/gqlparser/v2/ast" | ||
) | ||
|
||
func TestApplyFieldFilters(t *testing.T) { | ||
exampleDefs := model.DefinitionList{ | ||
{ | ||
Name: "Alpha", | ||
Kind: ast.Object, | ||
Fields: model.FieldDefinitionList{ | ||
{ | ||
Name: "x", | ||
}, | ||
{ | ||
Name: "y", | ||
}, | ||
{ | ||
Name: "z", | ||
}, | ||
}, | ||
}, | ||
{ | ||
Name: "Beta", | ||
Kind: ast.Object, | ||
Fields: model.FieldDefinitionList{ | ||
{ | ||
Name: "z", | ||
}, | ||
{ | ||
Name: "a", | ||
}, | ||
}, | ||
}, | ||
{ | ||
Name: "Theta", | ||
Kind: ast.Object, | ||
Fields: model.FieldDefinitionList{ | ||
{ | ||
Name: "x", | ||
}, | ||
{ | ||
Name: "y", | ||
}, | ||
}, | ||
}, | ||
{ | ||
Name: "InputA", | ||
Kind: ast.InputObject, | ||
InputFields: model.InputValueDefinitionList{ | ||
{ | ||
Name: "a", | ||
}, | ||
{ | ||
Name: "b", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range []struct { | ||
name string | ||
defs model.DefinitionList | ||
roots []string | ||
expectedFields []string | ||
}{ | ||
{ | ||
name: "single type", | ||
defs: exampleDefs, | ||
roots: []string{"Alpha"}, | ||
expectedFields: []string{ | ||
"Alpha.x", | ||
"Alpha.y", | ||
"Alpha.z", | ||
}, | ||
}, | ||
{ | ||
name: "multiple types", | ||
defs: exampleDefs, | ||
roots: []string{"Alpha", "Beta"}, | ||
expectedFields: []string{ | ||
"Alpha.x", | ||
"Alpha.y", | ||
"Alpha.z", | ||
"Beta.a", | ||
"Beta.z", | ||
}, | ||
}, | ||
{ | ||
name: "single field on multiple types", | ||
defs: exampleDefs, | ||
roots: []string{"Alpha.x", "Beta.z"}, | ||
expectedFields: []string{ | ||
"Alpha.x", | ||
"Beta.z", | ||
}, | ||
}, | ||
{ | ||
name: "multiple fields on the same type", | ||
defs: exampleDefs, | ||
roots: []string{ | ||
"Alpha.x", | ||
"Alpha.z", | ||
}, | ||
expectedFields: []string{ | ||
"Alpha.x", | ||
"Alpha.z", | ||
}, | ||
}, | ||
{ | ||
name: "field on type plus whole type", | ||
defs: exampleDefs, | ||
roots: []string{ | ||
"Alpha.x", | ||
"Alpha", | ||
}, | ||
expectedFields: []string{ | ||
"Alpha.x", | ||
"Alpha.y", | ||
"Alpha.z", | ||
}, | ||
}, | ||
{ | ||
name: "input field", | ||
defs: exampleDefs, | ||
roots: []string{ | ||
"InputA.a", | ||
}, | ||
expectedFields: []string{ | ||
"InputA.a", | ||
}, | ||
}, | ||
{ | ||
name: "input type", | ||
defs: exampleDefs, | ||
roots: []string{ | ||
"InputA", | ||
}, | ||
expectedFields: []string{ | ||
"InputA.a", | ||
"InputA.b", | ||
}, | ||
}, | ||
} { | ||
t.Run(tc.name, func(t *testing.T) { | ||
filtered := applyFieldFilters(exampleDefs, tc.roots) | ||
|
||
var actualFieldNames []string | ||
for _, def := range filtered { | ||
for _, field := range def.Fields { | ||
fieldName := fmt.Sprintf("%s.%s", def.Name, field.Name) | ||
actualFieldNames = append(actualFieldNames, fieldName) | ||
} | ||
for _, field := range def.InputFields { | ||
fieldName := fmt.Sprintf("%s.%s", def.Name, field.Name) | ||
actualFieldNames = append(actualFieldNames, fieldName) | ||
} | ||
} | ||
sort.Sort(sort.StringSlice(actualFieldNames)) | ||
|
||
assert.Equal(t, tc.expectedFields, actualFieldNames) | ||
}) | ||
} | ||
} |