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

Function TestInjectJSONProperties

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

Source from the content-addressed store, hash-verified

819}
820
821func TestInjectJSONProperties(t *testing.T) {
822 newKV := KVPair{
823 Key: "newval",
824 Val: 123,
825 }
826
827 tests := []struct {
828 input string
829 expectedOutput string
830 expectedErr string
831 }{
832 {
833 input: ``,
834 expectedErr: `not a JSON object`,
835 },
836 {
837 input: `null`,
838 expectedErr: `not a JSON object`,
839 },
840 {
841 input: "123",
842 expectedErr: `not a JSON object`,
843 },
844 {
845 input: "{}",
846 expectedOutput: `{"newval":123}`,
847 },
848 {
849 input: `{"key":"val"}`,
850 expectedOutput: `{"key":"val","newval":123}`,
851 },
852 {
853 input: `{"newval":"old"}`,
854 expectedOutput: `{"newval":"old","newval":123}`,
855 },
856 {
857 input: ` {"key":"val"} `,
858 expectedOutput: `{"key":"val","newval":123}`,
859 },
860 }
861
862 for _, test := range tests {
863 t.Run(test.input, func(tt *testing.T) {
864 output, err := InjectJSONProperties([]byte(test.input), newKV)
865 if test.expectedErr != "" {
866 require.Errorf(tt, err, test.expectedErr, "expected error did not match")
867 return
868 } else {
869 require.NoError(tt, err, "unexpected error")
870 }
871
872 assert.Equal(tt, test.expectedOutput, string(output))
873 assert.NoError(tt, JSONUnmarshal(output, &map[string]interface{}{}), "produced invalid JSON")
874 })
875 }
876}
877
878func TestInjectJSONPropertiesDiffTypes(t *testing.T) {

Callers

nothing calls this directly

Calls 5

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

Tested by

no test coverage detected