MCPcopy
hub / github.com/dgraph-io/dgraph / TestEncode

Function TestEncode

query/fastjson_test.go:71–142  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

69}
70
71func TestEncode(t *testing.T) {
72 enc := newEncoder()
73
74 t.Run("with uid list predicate", func(t *testing.T) {
75 root := enc.newNode(0)
76 friendNode1 := enc.newNode(enc.idForAttr("friend"))
77 require.NoError(t, enc.AddValue(friendNode1, enc.idForAttr("name"),
78 types.Val{Tid: types.StringID, Value: "alice"}))
79 friendNode2 := enc.newNode(enc.idForAttr("friend"))
80 require.NoError(t, enc.AddValue(friendNode2, enc.idForAttr("name"),
81 types.Val{Tid: types.StringID, Value: "bob"}))
82
83 enc.AddListChild(root, friendNode1)
84 enc.AddListChild(root, friendNode2)
85
86 enc.buf.Reset()
87 require.NoError(t, enc.encode(root))
88 testutil.CompareJSON(t, `
89 {
90 "friend":[
91 {
92 "name":"alice"
93 },
94 {
95 "name":"bob"
96 }
97 ]
98 }
99 `, enc.buf.String())
100 })
101
102 t.Run("with value list predicate", func(t *testing.T) {
103 root := enc.newNode(0)
104 require.NoError(t, enc.AddValue(root, enc.idForAttr("name"),
105 types.Val{Tid: types.StringID, Value: "alice"}))
106 require.NoError(t, enc.AddValue(root, enc.idForAttr("name"),
107 types.Val{Tid: types.StringID, Value: "bob"}))
108
109 enc.buf.Reset()
110 require.NoError(t, enc.encode(root))
111 testutil.CompareJSON(t, `
112 {
113 "name":[
114 "alice",
115 "bob"
116 ]
117 }
118 `, enc.buf.String())
119 })
120
121 t.Run("with uid predicate", func(t *testing.T) {
122 root := enc.newNode(0)
123
124 person := enc.newNode(enc.idForAttr("person"))
125 require.NoError(t, enc.AddValue(person, enc.idForAttr("name"), types.Val{Tid: types.StringID, Value: "alice"}))
126 require.NoError(t, enc.AddValue(person, enc.idForAttr("age"), types.Val{Tid: types.IntID, Value: 25}))
127 enc.AddListChild(root, person)
128

Callers

nothing calls this directly

Calls 10

CompareJSONFunction · 0.92
newEncoderFunction · 0.85
newNodeMethod · 0.80
idForAttrMethod · 0.80
AddValueMethod · 0.80
AddListChildMethod · 0.80
RunMethod · 0.45
ResetMethod · 0.45
encodeMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected