MCPcopy
hub / github.com/dosco/graphjin / TestResponseProcessor_SingleObject

Function TestResponseProcessor_SingleObject

core/cache_response_test.go:286–334  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

284}
285
286func TestResponseProcessor_SingleObject(t *testing.T) {
287 // Create qcode with one selection for "users" table
288 qc := &qcode.QCode{
289 Selects: []qcode.Select{
290 {
291 Field: qcode.Field{
292 ID: 0,
293 ParentID: -1,
294 FieldName: "users",
295 },
296 Table: "users",
297 Ti: sdata.DBTable{Name: "users"},
298 },
299 },
300 }
301 rp := NewResponseProcessor(qc)
302
303 input := []byte(`{"data": {"users": {"id": 1, "name": "John", "__gj_id": 1}}}`)
304 cleaned, refs, err := rp.ProcessForCache(input)
305 if err != nil {
306 t.Errorf("unexpected error: %v", err)
307 }
308
309 // Should have extracted one ref
310 if len(refs) != 1 {
311 t.Errorf("expected 1 ref, got %d", len(refs))
312 } else {
313 if refs[0].Table != "users" {
314 t.Errorf("expected table 'users', got %q", refs[0].Table)
315 }
316 if refs[0].ID != "1" {
317 t.Errorf("expected ID '1', got %q", refs[0].ID)
318 }
319 }
320
321 // Should have removed __gj_id from output
322 var result map[string]interface{}
323 if err := json.Unmarshal(cleaned, &result); err != nil {
324 t.Errorf("failed to parse cleaned response: %v", err)
325 }
326 dataMap := result["data"].(map[string]interface{})
327 usersMap := dataMap["users"].(map[string]interface{})
328 if _, hasGjId := usersMap["__gj_id"]; hasGjId {
329 t.Errorf("__gj_id should have been removed from response")
330 }
331 if usersMap["id"] != float64(1) {
332 t.Errorf("id field should be preserved")
333 }
334}
335
336func BenchmarkStripCacheTrackingFields(b *testing.B) {
337 data := []byte(`{"data":{"users":{"__gj_id":1,"id":1,"name":"Ada","posts":[{"__gj_id":10,"id":10,"title":"First"},{"id":11,"title":"Second","__gj_id":11}]}}}`)

Callers

nothing calls this directly

Calls 2

ProcessForCacheMethod · 0.95
NewResponseProcessorFunction · 0.85

Tested by

no test coverage detected