github.com/cayleygraph/cayley@v0.7.7/internal/http/http_test.go (about) 1 // Copyright 2014 The Cayley Authors. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package http 16 17 import ( 18 "fmt" 19 "reflect" 20 "testing" 21 22 "github.com/cayleygraph/quad" 23 ) 24 25 var parseTests = []struct { 26 message string 27 input string 28 expect []quad.Quad 29 err error 30 }{ 31 { 32 message: "parse correct JSON", 33 input: `[ 34 {"subject": "foo", "predicate": "bar", "object": "baz"}, 35 {"subject": "foo", "predicate": "bar", "object": "baz", "label": "graph"} 36 ]`, 37 expect: []quad.Quad{ 38 quad.Make("foo", "bar", "baz", nil), 39 quad.Make("foo", "bar", "baz", "graph"), 40 }, 41 err: nil, 42 }, 43 { 44 message: "parse correct JSON with extra field", 45 input: `[ 46 {"subject": "foo", "predicate": "bar", "object": "foo", "something_else": "extra data"} 47 ]`, 48 expect: []quad.Quad{ 49 quad.Make("foo", "bar", "foo", nil), 50 }, 51 err: nil, 52 }, 53 { 54 message: "reject incorrect JSON", 55 input: `[ 56 {"subject": "foo", "predicate": "bar"} 57 ]`, 58 expect: nil, 59 err: fmt.Errorf("invalid quad at index %d. %v", 0, quad.Make("foo", "bar", nil, nil)), 60 }, 61 } 62 63 func TestParseJSON(t *testing.T) { 64 for _, test := range parseTests { 65 got, err := ParseJSONToQuadList([]byte(test.input)) 66 if fmt.Sprint(err) != fmt.Sprint(test.err) { 67 t.Errorf("Failed to %v with unexpected error, got:%v expected %v", test.message, err, test.err) 68 } 69 if !reflect.DeepEqual(got, test.expect) { 70 t.Errorf("Failed to %v, got:%v expect:%v", test.message, got, test.expect) 71 } 72 } 73 }