Browse code

gograph: fix a bug which caused the unicity of (parent, name) to not be enforced

Solomon Hykes authored on 2013/10/10 11:44:31
Showing 2 changed files
... ...
@@ -22,7 +22,9 @@ const (
22 22
         CONSTRAINT "parent_fk" FOREIGN KEY ("parent_id") REFERENCES "entity" ("id"),
23 23
         CONSTRAINT "entity_fk" FOREIGN KEY ("entity_id") REFERENCES "entity" ("id")
24 24
         );
25
+    `
25 26
 
27
+	createEdgeIndices = `
26 28
     CREATE UNIQUE INDEX "name_parent_ix" ON "edge" (parent_id, name);
27 29
     `
28 30
 )
... ...
@@ -67,6 +69,9 @@ func NewDatabase(dbPath string) (*Database, error) {
67 67
 	if _, err := conn.Exec(createEdgeTable); err != nil {
68 68
 		return nil, err
69 69
 	}
70
+	if _, err := conn.Exec(createEdgeIndices); err != nil {
71
+		return nil, err
72
+	}
70 73
 
71 74
 	rollback := func() {
72 75
 		conn.Exec("ROLLBACK")
... ...
@@ -59,6 +59,18 @@ func TestSetEntityWithDifferentName(t *testing.T) {
59 59
 	}
60 60
 }
61 61
 
62
+func TestSetDuplicateEntity(t *testing.T) {
63
+	db := newTestDb(t)
64
+	defer destroyTestDb(db)
65
+
66
+	if _, err := db.Set("/foo", "42"); err != nil {
67
+		t.Fatal(err)
68
+	}
69
+	if _, err := db.Set("/foo", "43"); err == nil {
70
+		t.Fatalf("Creating an entry with a duplciate path did not cause an error")
71
+	}
72
+}
73
+
62 74
 func TestCreateChild(t *testing.T) {
63 75
 	db := newTestDb(t)
64 76
 	defer destroyTestDb(db)