MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / TestInjectJSONPropertiesFromBytes

Function TestInjectJSONPropertiesFromBytes

base/util_test.go:1079–1137  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1077}
1078
1079func TestInjectJSONPropertiesFromBytes(t *testing.T) {
1080 newKVBytes := KVPairBytes{
1081 Key: "newval",
1082 Val: []byte(`{"abc":123,"nums":["one","two","three"],"test":true}`),
1083 }
1084
1085 tests := []struct {
1086 input string
1087 expectedOutput string
1088 expectedErr string
1089 }{
1090 {
1091 input: ``,
1092 expectedErr: `not a JSON object`,
1093 },
1094 {
1095 input: `null`,
1096 expectedErr: `not a JSON object`,
1097 },
1098 {
1099 input: "123",
1100 expectedErr: `not a JSON object`,
1101 },
1102 {
1103 input: "{}",
1104 expectedOutput: `{"newval":{"abc":123,"nums":["one","two","three"],"test":true}}`,
1105 },
1106 {
1107 input: `{"key":"val"}`,
1108 expectedOutput: `{"key":"val","newval":{"abc":123,"nums":["one","two","three"],"test":true}}`,
1109 },
1110 {
1111 input: `{"newval":"old"}`,
1112 expectedOutput: `{"newval":"old","newval":{"abc":123,"nums":["one","two","three"],"test":true}}`,
1113 },
1114 {
1115 input: ` {"key":"val"} `,
1116 expectedOutput: `{"key":"val","newval":{"abc":123,"nums":["one","two","three"],"test":true}}`,
1117 },
1118 }
1119
1120 for _, test := range tests {
1121 t.Run(test.input, func(tt *testing.T) {
1122 output, err := InjectJSONPropertiesFromBytes([]byte(test.input), newKVBytes)
1123 if test.expectedErr != "" {
1124 require.Errorf(tt, err, test.expectedErr, "expected error did not match")
1125 return
1126 } else {
1127 require.NoError(tt, err, "unexpected error")
1128 }
1129
1130 assert.Equal(tt, test.expectedOutput, string(output))
1131
1132 var m map[string]interface{}
1133 err = JSONUnmarshal(output, &m)
1134 assert.NoError(tt, err, "produced invalid JSON")
1135 })
1136 }

Callers

nothing calls this directly

Calls 5

ErrorfMethod · 0.80
JSONUnmarshalFunction · 0.70
RunMethod · 0.65
EqualMethod · 0.45

Tested by

no test coverage detected