MCPcopy
hub / github.com/pocketbase/pocketbase / TestJSONFieldPrepareValue

Function TestJSONFieldPrepareValue

core/field_json_test.go:31–80  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

29}
30
31func TestJSONFieldPrepareValue(t *testing.T) {
32 app, _ := tests.NewTestApp()
33 defer app.Cleanup()
34
35 f := &core.JSONField{}
36 record := core.NewRecord(core.NewBaseCollection("test"))
37
38 scenarios := []struct {
39 raw any
40 expected string
41 }{
42 {"null", `null`},
43 {"", `""`},
44 {"true", `true`},
45 {"false", `false`},
46 {"test", `"test"`},
47 {"123", `123`},
48 {"-456", `-456`},
49 {"[1,2,3]", `[1,2,3]`},
50 {"[1,2,3", `"[1,2,3"`},
51 {`{"a":1,"b":2}`, `{"a":1,"b":2}`},
52 {`{"a":1,"b":2`, `"{\"a\":1,\"b\":2"`},
53 {[]int{1, 2, 3}, `[1,2,3]`},
54 {map[string]int{"a": 1, "b": 2}, `{"a":1,"b":2}`},
55 {nil, `null`},
56 {false, `false`},
57 {true, `true`},
58 {-78, `-78`},
59 {123.456, `123.456`},
60 }
61
62 for i, s := range scenarios {
63 t.Run(fmt.Sprintf("%d_%#v", i, s.raw), func(t *testing.T) {
64 v, err := f.PrepareValue(record, s.raw)
65 if err != nil {
66 t.Fatal(err)
67 }
68
69 raw, ok := v.(types.JSONRaw)
70 if !ok {
71 t.Fatalf("Expected string instance, got %T", v)
72 }
73 rawStr := raw.String()
74
75 if rawStr != s.expected {
76 t.Fatalf("Expected\n%#v\ngot\n%#v", s.expected, rawStr)
77 }
78 })
79 }
80}
81
82func TestJSONFieldValidateValue(t *testing.T) {
83 app, _ := tests.NewTestApp()

Callers

nothing calls this directly

Calls 7

CleanupMethod · 0.95
PrepareValueMethod · 0.95
NewTestAppFunction · 0.92
NewRecordFunction · 0.92
NewBaseCollectionFunction · 0.92
RunMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…