github.com/go-kivik/kivik/v4@v4.3.2/driver/changes_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 driver 14 15 import ( 16 "encoding/json" 17 "testing" 18 19 "gitlab.com/flimzy/testy" 20 ) 21 22 func TestChangesUnmarshal(t *testing.T) { 23 tests := []struct { 24 name string 25 input string 26 expected ChangedRevs 27 err string 28 }{ 29 { 30 name: "invalid JSON", 31 input: `{"foo":"bar"}`, 32 err: `json: cannot unmarshal object into Go value of type []struct { Rev string "json:\"rev\"" }`, 33 }, 34 { 35 name: "success", 36 input: `[ 37 {"rev": "6-460637e73a6288cb24d532bf91f32969"}, 38 {"rev": "5-eeaa298781f60b7bcae0c91bdedd1b87"} 39 ]`, 40 expected: ChangedRevs{"6-460637e73a6288cb24d532bf91f32969", "5-eeaa298781f60b7bcae0c91bdedd1b87"}, 41 }, 42 } 43 for _, test := range tests { 44 t.Run(test.name, func(t *testing.T) { 45 var result ChangedRevs 46 err := json.Unmarshal([]byte(test.input), &result) 47 if !testy.ErrorMatches(test.err, err) { 48 t.Errorf("Unexpected error: %s", err) 49 } 50 if d := testy.DiffInterface(test.expected, result); d != nil { 51 t.Error(d) 52 } 53 }) 54 } 55 }