MCPcopy Create free account
hub / github.com/pocketbase/pocketbase / TestRecordUnmarshalJSONField

Function TestRecordUnmarshalJSONField

core/record_model_test.go:1119–1173  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1117}
1118
1119func TestRecordUnmarshalJSONField(t *testing.T) {
1120 t.Parallel()
1121
1122 collection := core.NewBaseCollection("test")
1123 collection.Fields.Add(&core.JSONField{Name: "field"})
1124
1125 record := core.NewRecord(collection)
1126
1127 var testPointer *string
1128 var testStr string
1129 var testInt int
1130 var testBool bool
1131 var testSlice []int
1132 var testMap map[string]any
1133
1134 scenarios := []struct {
1135 value any
1136 destination any
1137 expectError bool
1138 expectedJSON string
1139 }{
1140 {nil, testPointer, false, `null`},
1141 {nil, testStr, false, `""`},
1142 {"", testStr, false, `""`},
1143 {1, testInt, false, `1`},
1144 {true, testBool, false, `true`},
1145 {[]int{1, 2, 3}, testSlice, false, `[1,2,3]`},
1146 {map[string]any{"test": 123}, testMap, false, `{"test":123}`},
1147 // json encoded values
1148 {`null`, testPointer, false, `null`},
1149 {`true`, testBool, false, `true`},
1150 {`456`, testInt, false, `456`},
1151 {`"test"`, testStr, false, `"test"`},
1152 {`[4,5,6]`, testSlice, false, `[4,5,6]`},
1153 {`{"test":456}`, testMap, false, `{"test":456}`},
1154 }
1155
1156 for i, s := range scenarios {
1157 t.Run(fmt.Sprintf("%d_%#v", i, s.value), func(t *testing.T) {
1158 record.Set("field", s.value)
1159
1160 err := record.UnmarshalJSONField("field", &s.destination)
1161 hasErr := err != nil
1162
1163 if hasErr != s.expectError {
1164 t.Fatalf("Expected hasErr %v, got %v", s.expectError, hasErr)
1165 }
1166
1167 raw, _ := json.Marshal(s.destination)
1168 if v := string(raw); v != s.expectedJSON {
1169 t.Fatalf("Expected %q, got %q", s.expectedJSON, v)
1170 }
1171 })
1172 }
1173}
1174
1175func TestRecordFindFileFieldByFile(t *testing.T) {
1176 t.Parallel()

Callers

nothing calls this directly

Calls 6

SetMethod · 0.95
UnmarshalJSONFieldMethod · 0.95
NewBaseCollectionFunction · 0.92
NewRecordFunction · 0.92
AddMethod · 0.45
RunMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…