Improve Transaction: deduplicate quads, allow adding/removing a quad in the same tx
This commit is contained in:
parent
b74f8f1340
commit
45d96e14ec
3 changed files with 105 additions and 19 deletions
62
graph/transaction_test.go
Normal file
62
graph/transaction_test.go
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
package graph
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/google/cayley/quad"
|
||||
)
|
||||
|
||||
func TestTransaction(t *testing.T) {
|
||||
var tx *Transaction
|
||||
|
||||
// simples adds / removes
|
||||
tx = NewTransaction()
|
||||
|
||||
tx.AddQuad(quad.Quad{Subject: "E", Predicate: "follows", Object: "F", Label: ""})
|
||||
tx.AddQuad(quad.Quad{Subject: "F", Predicate: "follows", Object: "G", Label: ""})
|
||||
tx.RemoveQuad(quad.Quad{Subject: "A", Predicate: "follows", Object: "Z", Label: ""})
|
||||
if len(tx.Deltas) != 3 {
|
||||
t.Errorf("Expected 3 Deltas, have %d delta(s)", len(tx.Deltas))
|
||||
}
|
||||
|
||||
// add, remove -> nothing
|
||||
tx = NewTransaction()
|
||||
tx.AddQuad(quad.Quad{Subject: "E", Predicate: "follows", Object: "G", Label: ""})
|
||||
tx.RemoveQuad(quad.Quad{Subject: "E", Predicate: "follows", Object: "G", Label: ""})
|
||||
if len(tx.Deltas) != 0 {
|
||||
t.Errorf("Expected [add, remove]->[], have %d Deltas", len(tx.Deltas))
|
||||
}
|
||||
|
||||
// remove, add -> nothing
|
||||
tx = NewTransaction()
|
||||
tx.AddQuad(quad.Quad{Subject: "E", Predicate: "follows", Object: "G", Label: ""})
|
||||
tx.RemoveQuad(quad.Quad{Subject: "E", Predicate: "follows", Object: "G", Label: ""})
|
||||
if len(tx.Deltas) != 0 {
|
||||
t.Errorf("Expected [add, remove]->[], have %d delta(s)", len(tx.Deltas))
|
||||
}
|
||||
|
||||
// add x2 -> add x1
|
||||
tx = NewTransaction()
|
||||
tx.AddQuad(quad.Quad{Subject: "E", Predicate: "follows", Object: "G", Label: ""})
|
||||
tx.AddQuad(quad.Quad{Subject: "E", Predicate: "follows", Object: "G", Label: ""})
|
||||
if len(tx.Deltas) != 1 {
|
||||
t.Errorf("Expected [add, add]->[add], have %d delta(s)", len(tx.Deltas))
|
||||
}
|
||||
|
||||
// remove x2 -> remove x1
|
||||
tx = NewTransaction()
|
||||
tx.RemoveQuad(quad.Quad{Subject: "E", Predicate: "follows", Object: "G", Label: ""})
|
||||
tx.RemoveQuad(quad.Quad{Subject: "E", Predicate: "follows", Object: "G", Label: ""})
|
||||
if len(tx.Deltas) != 1 {
|
||||
t.Errorf("Expected [remove, remove]->[remove], have %d delta(s)", len(tx.Deltas))
|
||||
}
|
||||
|
||||
// add, remove x2 -> remove x1
|
||||
tx = NewTransaction()
|
||||
tx.AddQuad(quad.Quad{Subject: "E", Predicate: "follows", Object: "G", Label: ""})
|
||||
tx.RemoveQuad(quad.Quad{Subject: "E", Predicate: "follows", Object: "G", Label: ""})
|
||||
tx.RemoveQuad(quad.Quad{Subject: "E", Predicate: "follows", Object: "G", Label: ""})
|
||||
if len(tx.Deltas) != 1 {
|
||||
t.Errorf("Expected [add, remove, remove]->[remove], have %d delta(s)", len(tx.Deltas))
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue