Skip to content

Commit

Permalink
Improving some unit tests and centrailizing some comparison functions…
Browse files Browse the repository at this point in the history
… that were duplicated in a few different packages
  • Loading branch information
kellrott committed Dec 19, 2019
1 parent 332edf6 commit 8017cd8
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 54 deletions.
59 changes: 42 additions & 17 deletions engine/inspect/test/inspect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,14 @@ import (
"github.com/bmeg/grip/engine/core"
"github.com/bmeg/grip/engine/inspect"
"github.com/bmeg/grip/gripql"
"github.com/bmeg/grip/util/setcmp"
)

func arrayEq(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}

func TestStepNumber(t *testing.T) {
q := gripql.NewQuery()
q = q.V().Out().In().Has(gripql.Eq("$.test", "value"))
o := inspect.PipelineSteps(q.Statements)
if !arrayEq(o, []string{"1", "2", "3", "3"}) {
if !setcmp.ArrayEq(o, []string{"1", "2", "3", "3"}) {
t.Error("Step mapping error")
}
}
Expand All @@ -34,6 +23,13 @@ func TestAsMapping(t *testing.T) {
q := gripql.NewQuery()
q = q.V().As("a").Out().As("b").In()
out := inspect.PipelineAsSteps(q.Statements)
if x, ok := out["a"]; !ok {
t.Errorf("a step not found")
} else {
if x != "1" {
t.Errorf("a step numbered incorrectly")
}
}
fmt.Printf("vars: %s\n", out)
}

Expand All @@ -45,7 +41,7 @@ func TestOutputMasking(t *testing.T) {
if len(out) != 1 {
t.Errorf("Wrong number of step outputs %d", len(out))
}
if !arrayEq(out["3"], []string{"*"}) {
if !setcmp.ArrayEq(out["3"], []string{"*"}) {
t.Errorf("Incorrect output")
}

Expand All @@ -56,10 +52,10 @@ func TestOutputMasking(t *testing.T) {
if len(out) != 2 {
t.Errorf("Wrong number of step outputs %d", len(out))
}
if !arrayEq(out["3"], []string{"*"}) {
if !setcmp.ArrayEq(out["3"], []string{"*"}) {
t.Errorf("Incorrect output")
}
if !arrayEq(out["4"], []string{"*"}) {
if !setcmp.ArrayEq(out["4"], []string{"*"}) {
t.Errorf("Incorrect output")
}

Expand All @@ -75,11 +71,17 @@ func TestOutputMasking(t *testing.T) {
q = q.V().Out().In().Count()
out = inspect.PipelineStepOutputs(q.Statements)
fmt.Printf("vars: %s\n", out)
if len(out) != 0 {
t.Errorf("Incorrect output count")
}

q = gripql.NewQuery()
q = q.V().Out().In().Has(gripql.Eq("$.test", "value")).Count()
out = inspect.PipelineStepOutputs(q.Statements)
fmt.Printf("vars: %s\n", out)
if len(out) != 1 {
t.Errorf("Incorrect output count")
}

q = gripql.NewQuery()
q = q.V().HasLabel("test").Out().In().Has(gripql.Eq("$.test", "value")).Count()
Expand All @@ -103,7 +105,16 @@ func TestOutputMasking(t *testing.T) {
q = q.V().HasLabel("Person").As("person").Out().Distinct("$person.name")
out = inspect.PipelineStepOutputs(q.Statements)
fmt.Printf("vars: %s -> %s\n", inspect.PipelineSteps(q.Statements), out)

if len(out) != 2 {
t.Errorf("Incorrect output count")
}
if x, ok := out["1"]; ok {
if !setcmp.ContainsString(x, "_label") {
t.Errorf("_label not found")
}
} else {
t.Errorf("step 1 missing from outputs")
}
}

func TestOutputIndexMasking(t *testing.T) {
Expand All @@ -125,9 +136,23 @@ func TestPathFind(t *testing.T) {
o := q.V().HasLabel("test").Out().As("a").Out().Out().Select("a")
r := inspect.PipelineNoLoadPath(o.Statements, 2)
fmt.Printf("%#v\n", r)
if len(r) != 1 {
t.Errorf("wrong number of paths found")
} else {
if !setcmp.ArrayIntEq(r[0], []int{4, 5}) {
t.Errorf("incorrect path found")
}
}

q = gripql.NewQuery()
o = q.V().HasLabel("test").Out().Out().Out().In("test")
r = inspect.PipelineNoLoadPath(o.Statements, 2)
fmt.Printf("%#v\n", r)
if len(r) != 1 {
t.Errorf("wrong number of paths found")
} else {
if !setcmp.ArrayIntEq(r[0], []int{1, 2, 3, 4}) {
t.Errorf("incorrect path found")
}
}
}
3 changes: 2 additions & 1 deletion grids/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/bmeg/grip/engine/pipeline"
"github.com/bmeg/grip/gdbi"
"github.com/bmeg/grip/gripql"
"github.com/bmeg/grip/util/setcmp"
log "github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -46,7 +47,7 @@ func (comp Compiler) Compile(stmts []*gripql.GraphStatement) (gdbi.Pipeline, err
for i := 0; i < len(stmts); i++ {
foundPath := -1
for p := range noLoadPaths {
if containsInt(noLoadPaths[p], i) {
if setcmp.ContainsInt(noLoadPaths[p], i) {
foundPath = p
}
}
Expand Down
36 changes: 5 additions & 31 deletions grids/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,11 @@ import (
"github.com/bmeg/grip/kvi"
"github.com/bmeg/grip/kvindex"
"github.com/bmeg/grip/protoutil"
"github.com/bmeg/grip/util/setcmp"
proto "github.com/golang/protobuf/proto"
log "github.com/sirupsen/logrus"
)

func contains(a []string, v string) bool {
for _, i := range a {
if i == v {
return true
}
}
return false
}

func containsInt(a []int, v int) bool {
for _, i := range a {
if i == v {
return true
}
}
return false
}

func containsUint(a []uint64, v uint64) bool {
for _, i := range a {
if i == v {
return true
}
}
return false
}

// GetTimestamp returns the update timestamp
func (ggraph *Graph) GetTimestamp() string {
return ggraph.kdb.ts.Get(ggraph.graphID)
Expand Down Expand Up @@ -458,7 +432,7 @@ func (ggraph *Graph) GetOutChannel(reqChan chan gdbi.ElementLookup, load bool, e
for it.Seek(skeyPrefix); it.Valid() && bytes.HasPrefix(it.Key(), skeyPrefix); it.Next() {
keyValue := it.Key()
_, _, _, dst, label := SrcEdgeKeyParse(keyValue)
if len(edgeLabelKeys) == 0 || containsUint(edgeLabelKeys, label) {
if len(edgeLabelKeys) == 0 || setcmp.ContainsUint(edgeLabelKeys, label) {
vkey := VertexKey(ggraph.graphKey, dst)
vertexChan <- elementData{
data: vkey,
Expand Down Expand Up @@ -522,7 +496,7 @@ func (ggraph *Graph) GetInChannel(reqChan chan gdbi.ElementLookup, load bool, ed
for it.Seek(dkeyPrefix); it.Valid() && bytes.HasPrefix(it.Key(), dkeyPrefix); it.Next() {
keyValue := it.Key()
_, _, src, _, label := DstEdgeKeyParse(keyValue)
if len(edgeLabelKeys) == 0 || containsUint(edgeLabelKeys, label) {
if len(edgeLabelKeys) == 0 || setcmp.ContainsUint(edgeLabelKeys, label) {
vkey := VertexKey(ggraph.graphKey, src)
srcID := ggraph.kdb.keyMap.GetVertexID(ggraph.graphKey, src)
lID := ggraph.kdb.keyMap.GetVertexLabel(ggraph.graphKey, src)
Expand Down Expand Up @@ -571,7 +545,7 @@ func (ggraph *Graph) GetOutEdgeChannel(reqChan chan gdbi.ElementLookup, load boo
for it.Seek(skeyPrefix); it.Valid() && bytes.HasPrefix(it.Key(), skeyPrefix); it.Next() {
keyValue := it.Key()
_, eid, src, dst, label := SrcEdgeKeyParse(keyValue)
if len(edgeLabelKeys) == 0 || containsUint(edgeLabelKeys, label) {
if len(edgeLabelKeys) == 0 || setcmp.ContainsUint(edgeLabelKeys, label) {
e := gripql.Edge{}
e.Gid = ggraph.kdb.keyMap.GetEdgeID(ggraph.graphKey, eid)
e.From = ggraph.kdb.keyMap.GetVertexID(ggraph.graphKey, src)
Expand Down Expand Up @@ -618,7 +592,7 @@ func (ggraph *Graph) GetInEdgeChannel(reqChan chan gdbi.ElementLookup, load bool
for it.Seek(dkeyPrefix); it.Valid() && bytes.HasPrefix(it.Key(), dkeyPrefix); it.Next() {
keyValue := it.Key()
_, eid, src, dst, label := DstEdgeKeyParse(keyValue)
if len(edgeLabelKeys) == 0 || containsUint(edgeLabelKeys, label) {
if len(edgeLabelKeys) == 0 || setcmp.ContainsUint(edgeLabelKeys, label) {
e := gripql.Edge{}
e.Gid = ggraph.kdb.keyMap.GetEdgeID(ggraph.graphKey, eid)
e.From = ggraph.kdb.keyMap.GetVertexID(ggraph.graphKey, src)
Expand Down
11 changes: 6 additions & 5 deletions grids/graph_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/bmeg/grip/gripql"
"github.com/bmeg/grip/kvi"
"github.com/bmeg/grip/protoutil"
"github.com/bmeg/grip/util/setcmp"
)

type RawPathProcessor struct {
Expand Down Expand Up @@ -373,7 +374,7 @@ func (r *PathLabelProc) Process(ctx context.Context, in chan *PathTraveler, out
go func() {
defer close(out)
for i := range in {
if containsUint(labels, i.current.Label) {
if setcmp.ContainsUint(labels, i.current.Label) {
out <- i
}
}
Expand Down Expand Up @@ -485,7 +486,7 @@ func (ggraph *Graph) RawGetOutChannel(reqChan chan *RawElementLookup, edgeLabels
for it.Seek(ePrefix); it.Valid() && bytes.HasPrefix(it.Key(), ePrefix); it.Next() {
keyValue := it.Key()
_, _, _, dstvkey, lkey := SrcEdgeKeyParse(keyValue)
if len(edgeLabels) == 0 || containsUint(edgeLabelKeys, lkey) {
if len(edgeLabels) == 0 || setcmp.ContainsUint(edgeLabelKeys, lkey) {
dstlkey := ggraph.kdb.keyMap.GetVertexLabel(ggraph.graphKey, dstvkey)
o <- &RawElementLookup{
Element: &RawDataElement{
Expand Down Expand Up @@ -521,7 +522,7 @@ func (ggraph *Graph) RawGetInChannel(reqChan chan *RawElementLookup, edgeLabels
for it.Seek(ePrefix); it.Valid() && bytes.HasPrefix(it.Key(), ePrefix); it.Next() {
keyValue := it.Key()
_, _, srcvkey, _, lkey := DstEdgeKeyParse(keyValue)
if len(edgeLabels) == 0 || containsUint(edgeLabelKeys, lkey) {
if len(edgeLabels) == 0 || setcmp.ContainsUint(edgeLabelKeys, lkey) {
srclkey := ggraph.kdb.keyMap.GetVertexLabel(ggraph.graphKey, srcvkey)
o <- &RawElementLookup{
Element: &RawDataElement{
Expand Down Expand Up @@ -557,7 +558,7 @@ func (ggraph *Graph) RawGetOutEdgeChannel(reqChan chan *RawElementLookup, edgeLa
for it.Seek(ePrefix); it.Valid() && bytes.HasPrefix(it.Key(), ePrefix); it.Next() {
keyValue := it.Key()
_, ekey, srcvkey, dstvkey, lkey := SrcEdgeKeyParse(keyValue)
if len(edgeLabels) == 0 || containsUint(edgeLabelKeys, lkey) {
if len(edgeLabels) == 0 || setcmp.ContainsUint(edgeLabelKeys, lkey) {
o <- &RawElementLookup{
Element: &RawDataElement{
Gid: ekey,
Expand Down Expand Up @@ -594,7 +595,7 @@ func (ggraph *Graph) RawGetInEdgeChannel(reqChan chan *RawElementLookup, edgeLab
for it.Seek(ePrefix); it.Valid() && bytes.HasPrefix(it.Key(), ePrefix); it.Next() {
keyValue := it.Key()
_, ekey, srcvkey, dstvkey, lkey := DstEdgeKeyParse(keyValue)
if len(edgeLabels) == 0 || containsUint(edgeLabelKeys, lkey) {
if len(edgeLabels) == 0 || setcmp.ContainsUint(edgeLabelKeys, lkey) {
o <- &RawElementLookup{
Element: &RawDataElement{
Gid: ekey,
Expand Down
52 changes: 52 additions & 0 deletions util/setcmp/setcmp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package setcmp

func ArrayEq(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}

func ArrayIntEq(a, b []int) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}

func ContainsString(a []string, v string) bool {
for _, i := range a {
if i == v {
return true
}
}
return false
}

func ContainsInt(a []int, v int) bool {
for _, i := range a {
if i == v {
return true
}
}
return false
}

func ContainsUint(a []uint64, v uint64) bool {
for _, i := range a {
if i == v {
return true
}
}
return false
}

0 comments on commit 8017cd8

Please sign in to comment.