Clean up testing code

Ugh on the error comparison.
This commit is contained in:
kortschak 2014-07-04 10:29:14 +09:30
parent eb3fd17e1d
commit 3f6cfc98d5
2 changed files with 49 additions and 29 deletions

View file

@ -119,6 +119,7 @@ func (t *Triple) Equals(o *Triple) bool {
// Pretty-prints a triple. // Pretty-prints a triple.
func (t *Triple) String() string { func (t *Triple) String() string {
// TODO(kortschak) String methods should generally not terminate in '\n'.
return fmt.Sprintf("%s -- %s -> %s\n", t.Subject, t.Predicate, t.Object) return fmt.Sprintf("%s -- %s -> %s\n", t.Subject, t.Predicate, t.Object)
} }

View file

@ -15,40 +15,59 @@
package http package http
import ( import (
"fmt"
"reflect"
"testing" "testing"
. "github.com/smartystreets/goconvey/convey" "github.com/google/cayley/graph"
) )
func TestParseJSONOkay(t *testing.T) { var parseTests = []struct {
Convey("Parse JSON", t, func() { message string
bytelist := []byte(`[ input string
expect []*graph.Triple
err error
}{
{
message: "parse correct JSON",
input: `[
{"subject": "foo", "predicate": "bar", "object": "baz"}, {"subject": "foo", "predicate": "bar", "object": "baz"},
{"subject": "foo", "predicate": "bar", "object": "baz", "provenance": "graph"} {"subject": "foo", "predicate": "bar", "object": "baz", "provenance": "graph"}
]`) ]`,
x, err := ParseJsonToTripleList(bytelist) expect: []*graph.Triple{
So(err, ShouldBeNil) {"foo", "bar", "baz", ""},
So(len(x), ShouldEqual, 2) {"foo", "bar", "baz", "graph"},
So(x[0].Subject, ShouldEqual, "foo") },
So(x[0].Provenance, ShouldEqual, "") err: nil,
So(x[1].Provenance, ShouldEqual, "graph") },
}) {
message: "parse correct JSON with extra field",
Convey("Parse JSON extra field", t, func() { input: `[
bytelist := []byte(`[ {"subject": "foo", "predicate": "bar", "object": "foo", "something_else": "extra data"}
{"subject": "foo", "predicate": "bar", "object": "foo", "something_else": "extra data"} ]`,
]`) expect: []*graph.Triple{
_, err := ParseJsonToTripleList(bytelist) {"foo", "bar", "foo", ""},
So(err, ShouldBeNil) },
}) err: nil,
} },
{
func TestParseJSONFail(t *testing.T) { message: "reject incorrect JSON",
Convey("Parse JSON Fail", t, func() { input: `[
bytelist := []byte(`[
{"subject": "foo", "predicate": "bar"} {"subject": "foo", "predicate": "bar"}
]`) ]`,
_, err := ParseJsonToTripleList(bytelist) expect: nil,
So(err, ShouldNotBeNil) err: fmt.Errorf("Invalid triple at index %d. %v", 0, &graph.Triple{"foo", "bar", "", ""}),
}) },
}
func TestParseJSON(t *testing.T) {
for _, test := range parseTests {
got, err := ParseJsonToTripleList([]byte(test.input))
if fmt.Sprint(err) != fmt.Sprint(test.err) {
t.Errorf("Failed to %v with unexpected error, got:%v expected %v", test.message, err, test.err)
}
if !reflect.DeepEqual(got, test.expect) {
t.Errorf("Failed to %v, got:%v expect:%v", test.message, got, test.expect)
}
}
} }