-
Notifications
You must be signed in to change notification settings - Fork 5
/
caching_tag_test.go
66 lines (58 loc) · 2.6 KB
/
caching_tag_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package gbox
import (
"fmt"
"testing"
"github.com/jensneuse/graphql-go-tools/pkg/graphql"
"github.com/stretchr/testify/require"
)
func TestCachingTagAnalyzer_AnalyzeResult_WithoutTypeKeys(t *testing.T) {
cr := newTestCachingRequest()
tags := make(cachingTags)
analyzer := newCachingTagAnalyzer(cr, nil)
err := analyzer.AnalyzeResult([]byte(`{"data": {"users":[{"name":"A"}]}}`), nil, tags)
sh, _ := cr.schema.Hash()
require.NoError(t, err)
require.Equal(t, tags.Types().ToSlice(), []string{"type:Query", "type:User"})
require.Equal(t, []string{fmt.Sprintf(cachingTagSchemaHashPattern, sh)}, tags.SchemaHash().ToSlice())
require.Equal(t, tags.TypeFields().ToSlice(), []string{"field:Query:users", "field:User:name"})
require.Equal(t, tags.TypeKeys().ToSlice(), []string{})
require.Equal(t, tags.Operation().ToSlice(), []string{fmt.Sprintf(cachingTagOperationPattern, cr.gqlRequest.OperationName)})
}
func TestCachingTagAnalyzer_AnalyzeResult_WithTypeKeys(t *testing.T) {
cr := newTestCachingRequest()
tags := make(cachingTags)
analyzer := newCachingTagAnalyzer(cr, graphql.RequestTypes{
"User": graphql.RequestFields{
"name": struct{}{},
},
})
err := analyzer.AnalyzeResult([]byte(`{"data": {"users":[{"name":"A"}]}}`), nil, tags)
sh, _ := cr.schema.Hash()
require.NoError(t, err)
require.Equal(t, tags.Types().ToSlice(), []string{"type:Query", "type:User"})
require.Equal(t, []string{fmt.Sprintf(cachingTagSchemaHashPattern, sh)}, tags.SchemaHash().ToSlice())
require.Equal(t, tags.TypeFields().ToSlice(), []string{"field:Query:users", "field:User:name"})
require.Equal(t, tags.TypeKeys().ToSlice(), []string{"key:User:name:A"})
require.Equal(t, tags.Operation().ToSlice(), []string{fmt.Sprintf(cachingTagOperationPattern, cr.gqlRequest.OperationName)})
}
func TestCachingTagAnalyzer_AnalyzeResult_OnlyTypes(t *testing.T) {
cr := newTestCachingRequest()
tags := make(cachingTags)
analyzer := newCachingTagAnalyzer(cr, graphql.RequestTypes{
"User": graphql.RequestFields{
"name": struct{}{},
},
})
err := analyzer.AnalyzeResult(
[]byte(`{"data": {"users":[{"name":"A"}]}}`),
map[string]struct{}{"Unknown": {}},
tags,
)
sh, _ := cr.schema.Hash()
require.NoError(t, err)
require.Equal(t, tags.Types().ToSlice(), []string{"type:Query"})
require.Equal(t, []string{fmt.Sprintf(cachingTagSchemaHashPattern, sh)}, tags.SchemaHash().ToSlice())
require.Equal(t, tags.TypeFields().ToSlice(), []string{"field:Query:users"})
require.Equal(t, tags.TypeKeys().ToSlice(), []string{})
require.Equal(t, tags.Operation().ToSlice(), []string{fmt.Sprintf(cachingTagOperationPattern, cr.gqlRequest.OperationName)})
}