From 6e8fdaa2dbd546eae852ac981c7ece3901310d61 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Tue, 5 Dec 2017 10:13:56 +0000 Subject: [PATCH] Ensure nil fields are removed when SCHEMAFULL --- db/define_test.go | 75 +++++++++++++++++++++++++++++++++++++++--- util/data/data.go | 4 --- util/data/data_test.go | 2 +- 3 files changed, 72 insertions(+), 9 deletions(-) diff --git a/db/define_test.go b/db/define_test.go index 18282912..87fddf95 100644 --- a/db/define_test.go +++ b/db/define_test.go @@ -17,6 +17,7 @@ package db import ( "testing" + "github.com/abcum/surreal/sql" "github.com/abcum/surreal/util/data" . "github.com/smartystreets/goconvey/convey" ) @@ -113,8 +114,64 @@ func TestDefine(t *testing.T) { res, err := Execute(setupKV(), txt, nil) So(err, ShouldBeNil) So(res, ShouldHaveLength, 4) - So(data.Consume(res[3].Result[0]).Get("test").Data(), ShouldEqual, true) - So(data.Consume(res[3].Result[0]).Get("other").Data(), ShouldEqual, nil) + So(data.Consume(res[3].Result[0]).Data(), ShouldResemble, map[string]interface{}{ + "id": &sql.Thing{"person", "test"}, + "meta": map[string]interface{}{ + "tb": "person", + "id": "test", + }, + "test": true, + }) + + }) + + Convey("Define a schemafull table with nil values", t, func() { + + setupDB() + + txt := ` + USE NS test DB test; + DEFINE TABLE person SCHEMAFULL; + DEFINE FIELD test ON person TYPE boolean; + UPDATE @person:test SET test=true, other=NULL; + ` + + res, err := Execute(setupKV(), txt, nil) + So(err, ShouldBeNil) + So(res, ShouldHaveLength, 4) + So(data.Consume(res[3].Result[0]).Data(), ShouldResemble, map[string]interface{}{ + "id": &sql.Thing{"person", "test"}, + "meta": map[string]interface{}{ + "tb": "person", + "id": "test", + }, + "test": true, + }) + + }) + + Convey("Define a schemafull table with nested records", t, func() { + + setupDB() + + txt := ` + USE NS test DB test; + DEFINE TABLE person SCHEMAFULL; + DEFINE FIELD test ON person TYPE record (person); + UPDATE @person:test SET test=person:other; + ` + + res, err := Execute(setupKV(), txt, nil) + So(err, ShouldBeNil) + So(res, ShouldHaveLength, 4) + So(data.Consume(res[3].Result[0]).Data(), ShouldResemble, map[string]interface{}{ + "id": &sql.Thing{"person", "test"}, + "meta": map[string]interface{}{ + "tb": "person", + "id": "test", + }, + "test": &sql.Thing{"person", "other"}, + }) }) @@ -127,13 +184,23 @@ func TestDefine(t *testing.T) { DEFINE TABLE person SCHEMAFULL; DEFINE FIELD test ON person TYPE array; DEFINE FIELD test.* ON person TYPE record (person); - UPDATE @person:test SET test=[], test+=person:1, test+=person:2; + UPDATE @person:test SET test=[], test+=person:one, test+=person:two; ` res, err := Execute(setupKV(), txt, nil) So(err, ShouldBeNil) So(res, ShouldHaveLength, 5) - So(data.Consume(res[4].Result[0]).Get("test").Data(), ShouldHaveLength, 2) + So(data.Consume(res[4].Result[0]).Data(), ShouldResemble, map[string]interface{}{ + "id": &sql.Thing{"person", "test"}, + "meta": map[string]interface{}{ + "tb": "person", + "id": "test", + }, + "test": []interface{}{ + &sql.Thing{"person", "one"}, + &sql.Thing{"person", "two"}, + }, + }) }) diff --git a/util/data/data.go b/util/data/data.go index 34b060ad..1652c657 100644 --- a/util/data/data.go +++ b/util/data/data.go @@ -959,10 +959,6 @@ func (d *Doc) Each(exec Iterator) error { func (d *Doc) each(exec Iterator, prev []string) error { - if d.data == nil { - return nil - } - // Define the temporary object so // that we can loop over and traverse // down the path parts of the data diff --git a/util/data/data_test.go b/util/data/data_test.go index 02989655..b639ee04 100644 --- a/util/data/data_test.go +++ b/util/data/data_test.go @@ -71,7 +71,7 @@ func TestOperations(t *testing.T) { i++ return nil }) - So(i, ShouldEqual, 0) + So(i, ShouldEqual, 1) }) // ----------------------------------------------------------------------