github.com/go-kivik/kivik/v4@v4.3.2/mockdb/helpers_test.go (about) 1 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 2 // use this file except in compliance with the License. You may obtain a copy of 3 // the License at 4 // 5 // http://www.apache.org/licenses/LICENSE-2.0 6 // 7 // Unless required by applicable law or agreed to in writing, software 8 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 // License for the specific language governing permissions and limitations under 11 // the License. 12 13 package mockdb 14 15 import ( 16 "encoding/json" 17 "testing" 18 19 "gitlab.com/flimzy/testy" 20 21 "github.com/go-kivik/kivik/v4/driver" 22 ) 23 24 func TestDocument(t *testing.T) { 25 type tst struct { 26 i interface{} 27 expected *driver.Document 28 content interface{} 29 err string 30 } 31 tests := testy.NewTable() 32 tests.Add("simple doc", tst{ 33 i: map[string]string{"foo": "bar"}, 34 expected: &driver.Document{}, 35 content: []byte(`{"foo":"bar"}`), 36 }) 37 tests.Add("Unmarshalable", tst{ 38 i: func() {}, 39 err: "json: unsupported type: func()", 40 }) 41 tests.Add("raw string", tst{ 42 i: `{"foo":"bar"}`, 43 expected: &driver.Document{}, 44 content: []byte(`{"foo":"bar"}`), 45 }) 46 tests.Add("raw bytes", tst{ 47 i: []byte(`{"foo":"bar"}`), 48 expected: &driver.Document{}, 49 content: []byte(`{"foo":"bar"}`), 50 }) 51 tests.Add("json.RawMessage", tst{ 52 i: json.RawMessage(`{"foo":"bar"}`), 53 expected: &driver.Document{}, 54 content: []byte(`{"foo":"bar"}`), 55 }) 56 tests.Add("rev", tst{ 57 i: `{"_rev":"1-xxx"}`, 58 expected: &driver.Document{ 59 Rev: "1-xxx", 60 }, 61 content: []byte(`{"_rev":"1-xxx"}`), 62 }) 63 64 tests.Run(t, func(t *testing.T, test tst) { 65 result, err := Document(test.i) 66 if !testy.ErrorMatches(test.err, err) { 67 t.Errorf("Unexpected error: %s", err) 68 } 69 if err != nil { 70 return 71 } 72 if d := testy.DiffAsJSON(test.content, result.Body); d != nil { 73 t.Errorf("Unexpected content:\n%s\n", d) 74 } 75 result.Body.Close() // nolint: errcheck 76 result.Body = nil 77 if d := testy.DiffInterface(test.expected, result); d != nil { 78 t.Error(d) 79 } 80 }) 81 }