From 86a911b4a8c5f30fabf87fd5b17431785f1ea9b8 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Wed, 4 Apr 2018 19:16:32 +0100 Subject: [PATCH] Improve tests for DB indexes --- db/define_test.go | 62 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 52 insertions(+), 10 deletions(-) diff --git a/db/define_test.go b/db/define_test.go index d9c5e315..654951a4 100644 --- a/db/define_test.go +++ b/db/define_test.go @@ -518,6 +518,42 @@ func TestDefine(t *testing.T) { }) + Convey("Define an index on a table, and ensure it allows duplicate record values", t, func() { + + setupDB() + + txt := ` + USE NS test DB test; + DEFINE INDEX test ON person COLUMNS account, email; + UPDATE person:one SET account="demo", email="info@demo.com"; + UPDATE person:one SET account="demo", email="info@demo.com"; + UPDATE person:one SET account="demo", email="info@demo.com"; + UPDATE person:two SET account="demo", email="info@demo.com"; + UPDATE person:tre SET account="demo", email="info@demo.com"; + SELECT * FROM person ORDER BY meta.id; + ` + + res, err := Execute(setupKV(), txt, nil) + So(err, ShouldBeNil) + So(res, ShouldHaveLength, 8) + So(res[1].Status, ShouldEqual, "OK") + So(res[2].Result, ShouldHaveLength, 1) + So(res[2].Status, ShouldEqual, "OK") + So(res[3].Result, ShouldHaveLength, 1) + So(res[3].Status, ShouldEqual, "OK") + So(res[4].Result, ShouldHaveLength, 1) + So(res[4].Status, ShouldEqual, "OK") + So(res[5].Result, ShouldHaveLength, 1) + So(res[5].Status, ShouldEqual, "OK") + So(res[6].Result, ShouldHaveLength, 1) + So(res[6].Status, ShouldEqual, "OK") + So(res[7].Result, ShouldHaveLength, 3) + So(data.Consume(res[7].Result[0]).Get("id").Data(), ShouldResemble, &sql.Thing{"person", "one"}) + So(data.Consume(res[7].Result[1]).Get("id").Data(), ShouldResemble, &sql.Thing{"person", "tre"}) + So(data.Consume(res[7].Result[2]).Get("id").Data(), ShouldResemble, &sql.Thing{"person", "two"}) + + }) + Convey("Define a unique index on a table, and ensure it prevents duplicate record values", t, func() { setupDB() @@ -526,23 +562,29 @@ func TestDefine(t *testing.T) { USE NS test DB test; DEFINE INDEX test ON person COLUMNS account, email UNIQUE; UPDATE person:one SET account="demo", email="info@demo.com"; + UPDATE person:one SET account="demo", email="info@demo.com"; + UPDATE person:one SET account="demo", email="info@demo.com"; UPDATE person:two SET account="demo", email="info@demo.com"; UPDATE person:tre SET account="demo", email="info@demo.com"; - SELECT * FROM person; + SELECT * FROM person ORDER BY meta.id; ` res, err := Execute(setupKV(), txt, nil) So(err, ShouldBeNil) - So(res, ShouldHaveLength, 6) + So(res, ShouldHaveLength, 8) So(res[1].Status, ShouldEqual, "OK") So(res[2].Result, ShouldHaveLength, 1) So(res[2].Status, ShouldEqual, "OK") - So(res[3].Result, ShouldHaveLength, 0) - So(res[3].Status, ShouldEqual, "ERR_IX") - So(res[4].Result, ShouldHaveLength, 0) - So(res[4].Status, ShouldEqual, "ERR_IX") - So(res[5].Result, ShouldHaveLength, 1) - So(data.Consume(res[5].Result[0]).Get("id").Data(), ShouldResemble, &sql.Thing{"person", "one"}) + So(res[3].Result, ShouldHaveLength, 1) + So(res[3].Status, ShouldEqual, "OK") + So(res[4].Result, ShouldHaveLength, 1) + So(res[4].Status, ShouldEqual, "OK") + So(res[5].Result, ShouldHaveLength, 0) + So(res[5].Status, ShouldEqual, "ERR_IX") + So(res[6].Result, ShouldHaveLength, 0) + So(res[6].Status, ShouldEqual, "ERR_IX") + So(res[7].Result, ShouldHaveLength, 1) + So(data.Consume(res[7].Result[0]).Get("id").Data(), ShouldResemble, &sql.Thing{"person", "one"}) }) @@ -556,12 +598,12 @@ func TestDefine(t *testing.T) { UPDATE person:one SET account="demo", email="info@demo.com"; UPDATE person:two SET account="demo", email="info@demo.com"; UPDATE person:tre SET account="demo", email="info@demo.com"; - SELECT * FROM person; + SELECT * FROM person ORDER BY meta.id; DEFINE INDEX test ON person COLUMNS account, email UNIQUE; UPDATE person:one SET account="demo", email="info@demo.com"; UPDATE person:two SET account="demo", email="info@demo.com"; UPDATE person:tre SET account="demo", email="info@demo.com"; - SELECT * FROM person; + SELECT * FROM person ORDER BY meta.id; ` res, err := Execute(setupKV(), txt, nil)