From 1ae74d9509f009bae33c1912cb7d6e5e088b7fe5 Mon Sep 17 00:00:00 2001 From: Evan Marcey Date: Mon, 13 May 2019 11:40:18 -0400 Subject: [PATCH 1/2] Compare vertexes --- types.go | 38 +++ types_test.go | 904 ++++++++++++++++++++++++++++++++++++++++++++++++++ utils.go | 57 ++++ 3 files changed, 999 insertions(+) create mode 100644 types_test.go diff --git a/types.go b/types.go index 17a5645..6058366 100644 --- a/types.go +++ b/types.go @@ -19,17 +19,47 @@ type Vertex struct { Value VertexValue `json:"@value"` } +func (v1 Vertex) Equals(v2 Vertex) bool { + return v1.Type == v2.Type && v1.Value.Equals(v2.Value) +} + type VertexValue struct { ID string `json:"id"` Label string `json:"label"` Properties map[string][]VertexProperty `json:"properties"` } +func (v1 VertexValue) Equals(v2 VertexValue) bool { + if v1.ID != v2.ID || v1.Label != v2.Label { + return false + } + if len(v1.Properties) != len(v2.Properties) { + return false + } + for k, val1Slice := range v1.Properties { + val2Slice, ok := v2.Properties[k] + if !ok || len(val1Slice) != len(val2Slice) { + return false + } + for i, val1 := range val1Slice { + val2 := val2Slice[i] + if !val1.Equals(val2) { + return false + } + } + } + return true +} + type VertexProperty struct { Type string `json:"@type"` Value VertexPropertyValue `json:"@value"` } +func (v1 VertexProperty) Equals(v2 VertexProperty) bool { + return v1.Type == v2.Type && v1.Value.Equals(v2.Value) +} + type EdgeProperty struct { Type string `json:"@type"` Value EdgePropertyValue `json:"@value"` @@ -41,6 +71,10 @@ type VertexPropertyValue struct { Value interface{} `json:"value"` } +func (v1 VertexPropertyValue) Equals(v2 VertexPropertyValue) bool { + return v1.ID.Equals(v2.ID) && v1.Label == v2.Label && InterfacesMatch(v1.Value, v2.Value) +} + type EdgePropertyValue struct { Label string `json:"key"` Value interface{} `json:"value"` @@ -53,6 +87,10 @@ type GenericValue struct { Value interface{} `json:"@value"` } +func (g1 GenericValue) Equals(g2 GenericValue) bool { + return g1.Type == g2.Type && InterfacesMatch(g1.Value, g2.Value) +} + type Edges []Edge type Edge struct { diff --git a/types_test.go b/types_test.go new file mode 100644 index 0000000..f8e3f38 --- /dev/null +++ b/types_test.go @@ -0,0 +1,904 @@ +package gremlin + +import ( + "testing" +) + +func TestGenericValuesEquals(t *testing.T) { + var testCases = []struct { + given1 GenericValue + given2 GenericValue + expected bool + }{ + { + given1: GenericValue{}, + given2: GenericValue{}, + expected: true, + }, + { + given1: GenericValue{"test", "test"}, + given2: GenericValue{"test", "test"}, + expected: true, + }, + { + given1: GenericValue{"test", 1}, + given2: GenericValue{"test", 1}, + expected: true, + }, + { + given1: GenericValue{"test", map[string]interface{}{"test": "test"}}, + given2: GenericValue{"test", map[string]interface{}{"test": "test"}}, + expected: true, + }, + { + given1: GenericValue{"test", "test"}, + given2: GenericValue{"test", "test1"}, + expected: false, + }, + { + given1: GenericValue{"test", map[string]interface{}{"test": "test"}}, + given2: GenericValue{"test", map[string]interface{}{"test": "test1"}}, + expected: false, + }, + { + given1: GenericValue{"test", "1"}, + given2: GenericValue{"test", 1}, + expected: true, + }, + } + + for i, testCase := range testCases { + result := testCase.given1.Equals(testCase.given2) + if result != testCase.expected { + t.Error("test", i, "given", testCase.given1, "and", testCase.given2, "expected", testCase.expected, "result", result) + } + } +} + +func TestVertexPropertyValueEquals(t *testing.T) { + var testCases = []struct { + given1 VertexPropertyValue + given2 VertexPropertyValue + expected bool + }{ + { + given1: VertexPropertyValue{}, + given2: VertexPropertyValue{}, + expected: true, + }, + { + given1: VertexPropertyValue{GenericValue{"test", "test"}, "test", "test"}, + given2: VertexPropertyValue{GenericValue{"test", "test"}, "test", "test"}, + expected: true, + }, + { + given1: VertexPropertyValue{GenericValue{"test", 1}, "test", 1}, + given2: VertexPropertyValue{GenericValue{"test", 1}, "test", 1}, + expected: true, + }, + { + given1: VertexPropertyValue{GenericValue{"test", 1}, "test", 1}, + given2: VertexPropertyValue{GenericValue{"test", 2}, "test", 1}, + expected: false, + }, + { + given1: VertexPropertyValue{GenericValue{"test", 1}, "test", 1}, + given2: VertexPropertyValue{GenericValue{"test", 1}, "test", 2}, + expected: false, + }, + { + given1: VertexPropertyValue{GenericValue{"test", false}, "test", false}, + given2: VertexPropertyValue{GenericValue{"test", false}, "test", false}, + expected: true, + }, + { + given1: VertexPropertyValue{GenericValue{"test", false}, "test", false}, + given2: VertexPropertyValue{GenericValue{"test", true}, "test", false}, + expected: false, + }, + { + given1: VertexPropertyValue{GenericValue{"test", false}, "test", false}, + given2: VertexPropertyValue{GenericValue{"test", false}, "test", true}, + expected: false, + }, + { + given1: VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test", map[string]interface{}{"test": "test"}}, + given2: VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test", map[string]interface{}{"test": "test"}}, + expected: true, + }, + { + given1: VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test", map[string]interface{}{"test": "test"}}, + given2: VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test1"}}, "test", map[string]interface{}{"test": "test"}}, + expected: false, + }, + { + given1: VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test", map[string]interface{}{"test": "test"}}, + given2: VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test", map[string]interface{}{"test": "test1"}}, + expected: false, + }, + } + + for i, testCase := range testCases { + result := testCase.given1.Equals(testCase.given2) + if result != testCase.expected { + t.Error("test", i, "given", testCase.given1, "and", testCase.given2, "expected", testCase.expected, "result", result) + } + } +} + +func TestVertexPropertyEquals(t *testing.T) { + var testCases = []struct { + given1 VertexProperty + given2 VertexProperty + expected bool + }{ + { + given1: VertexProperty{}, + given2: VertexProperty{}, + expected: true, + }, + { + given1: VertexProperty{"", VertexPropertyValue{GenericValue{"test", "test"}, "test", "test"}}, + given2: VertexProperty{"", VertexPropertyValue{GenericValue{"test", "test"}, "test", "test"}}, + expected: true, + }, + { + given1: VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test", 1}}, + given2: VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test", 1}}, + expected: true, + }, + { + given1: VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test", 1}}, + given2: VertexProperty{"test1", VertexPropertyValue{GenericValue{"test", 1}, "test", 1}}, + expected: false, + }, + { + given1: VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test", 1}}, + given2: VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 2}, "test", 1}}, + expected: false, + }, + { + given1: VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test", 1}}, + given2: VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test", 2}}, + expected: false, + }, + { + given1: VertexProperty{"test", VertexPropertyValue{GenericValue{"test", false}, "test", false}}, + given2: VertexProperty{"test", VertexPropertyValue{GenericValue{"test", false}, "test", false}}, + expected: true, + }, + { + given1: VertexProperty{"test", VertexPropertyValue{GenericValue{"test", false}, "test", false}}, + given2: VertexProperty{"test1", VertexPropertyValue{GenericValue{"test", false}, "test", false}}, + expected: false, + }, + { + given1: VertexProperty{"test", VertexPropertyValue{GenericValue{"test", false}, "test", false}}, + given2: VertexProperty{"test", VertexPropertyValue{GenericValue{"test", true}, "test", false}}, + expected: false, + }, + { + given1: VertexProperty{"test", VertexPropertyValue{GenericValue{"test", false}, "test", false}}, + given2: VertexProperty{"test", VertexPropertyValue{GenericValue{"test", false}, "test", true}}, + expected: false, + }, + { + given1: VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test", map[string]interface{}{"test": "test"}}}, + given2: VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test", map[string]interface{}{"test": "test"}}}, + expected: true, + }, + { + given1: VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test", map[string]interface{}{"test": "test"}}}, + given2: VertexProperty{"test1", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test", map[string]interface{}{"test": "test"}}}, + expected: false, + }, + { + given1: VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test", map[string]interface{}{"test": "test"}}}, + given2: VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test1"}}, "test", map[string]interface{}{"test": "test"}}}, + expected: false, + }, + { + given1: VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test", map[string]interface{}{"test": "test"}}}, + given2: VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test", map[string]interface{}{"test": "test1"}}}, + expected: false, + }, + } + + for i, testCase := range testCases { + result := testCase.given1.Equals(testCase.given2) + if result != testCase.expected { + t.Error("test", i, "given", testCase.given1, "and", testCase.given2, "expected", testCase.expected, "result", result) + } + } +} + +func TestVertexValueEquals(t *testing.T) { + var testCases = []struct { + given1 VertexValue + given2 VertexValue + expected bool + }{ + { + given1: VertexValue{}, + given2: VertexValue{}, + expected: true, + }, + { + given1: VertexValue{ID: "test"}, + given2: VertexValue{ID: "test"}, + expected: true, + }, + { + given1: VertexValue{ID: "test"}, + given2: VertexValue{ID: "test1"}, + expected: false, + }, + { + given1: VertexValue{Label: "test"}, + given2: VertexValue{Label: "test"}, + expected: true, + }, + { + given1: VertexValue{Label: "test"}, + given2: VertexValue{Label: "test1"}, + expected: false, + }, + { + given1: VertexValue{ + ID: "test_id", + Label: "test_label", + Properties: map[string][]VertexProperty{ + "test": []VertexProperty{ + VertexProperty{"", VertexPropertyValue{GenericValue{"test", "test"}, "test", "test"}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test", 1}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", true}, "test", true}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test", map[string]interface{}{"test": "test"}}}, + }, + }, + }, + given2: VertexValue{ + ID: "test_id", + Label: "test_label", + Properties: map[string][]VertexProperty{ + "test": []VertexProperty{ + VertexProperty{"", VertexPropertyValue{GenericValue{"test", "test"}, "test", "test"}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test", 1}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", true}, "test", true}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test", map[string]interface{}{"test": "test"}}}, + }, + }, + }, + expected: true, + }, + { + given1: VertexValue{ + ID: "test_id", + Label: "test_label", + Properties: map[string][]VertexProperty{ + "test": []VertexProperty{ + VertexProperty{"", VertexPropertyValue{GenericValue{"test", "test"}, "test", "test"}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test", 1}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", true}, "test", true}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test", map[string]interface{}{"test": "test"}}}, + }, + }, + }, + given2: VertexValue{ + ID: "test_id", + Label: "test_label", + Properties: map[string][]VertexProperty{ + "test1": []VertexProperty{ + VertexProperty{"", VertexPropertyValue{GenericValue{"test", "test"}, "test", "test"}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test", 1}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", true}, "test", true}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test", map[string]interface{}{"test": "test"}}}, + }, + }, + }, + expected: false, + }, + { + given1: VertexValue{ + ID: "test_id", + Label: "test_label", + Properties: map[string][]VertexProperty{ + "test": []VertexProperty{ + VertexProperty{"", VertexPropertyValue{GenericValue{"test", "test"}, "test", "test"}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test", 1}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", true}, "test", true}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test", map[string]interface{}{"test": "test"}}}, + }, + }, + }, + given2: VertexValue{ + ID: "test_id", + Label: "test_label", + Properties: map[string][]VertexProperty{ + "test": []VertexProperty{ + VertexProperty{"", VertexPropertyValue{GenericValue{"test", "test"}, "test", "test1"}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test", 1}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", true}, "test", true}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test", map[string]interface{}{"test": "test"}}}, + }, + }, + }, + expected: false, + }, + { + given1: VertexValue{ + ID: "test_id", + Label: "test_label", + Properties: map[string][]VertexProperty{ + "test": []VertexProperty{ + VertexProperty{"", VertexPropertyValue{GenericValue{"test", "test"}, "test", "test"}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test", 1}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", true}, "test", true}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test", map[string]interface{}{"test": "test"}}}, + }, + }, + }, + given2: VertexValue{ + ID: "test_id", + Label: "test_label", + Properties: map[string][]VertexProperty{ + "test": []VertexProperty{ + VertexProperty{"", VertexPropertyValue{GenericValue{"test", "test"}, "test", "test"}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test", 2}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", true}, "test", true}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test", map[string]interface{}{"test": "test"}}}, + }, + }, + }, + expected: false, + }, + { + given1: VertexValue{ + ID: "test_id", + Label: "test_label", + Properties: map[string][]VertexProperty{ + "test": []VertexProperty{ + VertexProperty{"", VertexPropertyValue{GenericValue{"test", "test"}, "test", "test"}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test", 1}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", true}, "test", true}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test", map[string]interface{}{"test": "test"}}}, + }, + }, + }, + given2: VertexValue{ + ID: "test_id", + Label: "test_label", + Properties: map[string][]VertexProperty{ + "test": []VertexProperty{ + VertexProperty{"", VertexPropertyValue{GenericValue{"test", "test"}, "test", "test"}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test", 1}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", true}, "test", false}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test", map[string]interface{}{"test": "test"}}}, + }, + }, + }, + expected: false, + }, + { + given1: VertexValue{ + ID: "test_id", + Label: "test_label", + Properties: map[string][]VertexProperty{ + "test": []VertexProperty{ + VertexProperty{"", VertexPropertyValue{GenericValue{"test", "test"}, "test", "test"}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test", 1}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", true}, "test", true}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test", map[string]interface{}{"test": "test"}}}, + }, + }, + }, + given2: VertexValue{ + ID: "test_id", + Label: "test_label", + Properties: map[string][]VertexProperty{ + "test": []VertexProperty{ + VertexProperty{"", VertexPropertyValue{GenericValue{"test", "test"}, "test", "test"}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test", 1}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", true}, "test", true}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test", map[string]interface{}{"test": "test1"}}}, + }, + }, + }, + expected: false, + }, + { + given1: VertexValue{ + ID: "test_id", + Label: "test_label", + Properties: map[string][]VertexProperty{ + "test": []VertexProperty{ + VertexProperty{"", VertexPropertyValue{GenericValue{"test", "test"}, "test", "test"}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test", 1}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", true}, "test", true}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test", map[string]interface{}{"test": "test"}}}, + }, + "test2": []VertexProperty{ + VertexProperty{"", VertexPropertyValue{GenericValue{"test", "test"}, "test2", "test"}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test2", 1}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", true}, "test2", true}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test2", map[string]interface{}{"test": "test"}}}, + }, + }, + }, + given2: VertexValue{ + ID: "test_id", + Label: "test_label", + Properties: map[string][]VertexProperty{ + "test": []VertexProperty{ + VertexProperty{"", VertexPropertyValue{GenericValue{"test", "test"}, "test", "test"}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test", 1}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", true}, "test", true}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test", map[string]interface{}{"test": "test"}}}, + }, + "test2": []VertexProperty{ + VertexProperty{"", VertexPropertyValue{GenericValue{"test", "test"}, "test2", "test"}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test2", 1}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", true}, "test2", true}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test2", map[string]interface{}{"test": "test"}}}, + }, + }, + }, + expected: true, + }, + { + given1: VertexValue{ + ID: "test_id", + Label: "test_label", + Properties: map[string][]VertexProperty{ + "test": []VertexProperty{ + VertexProperty{"", VertexPropertyValue{GenericValue{"test", "test"}, "test", "test"}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test", 1}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", true}, "test", true}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test", map[string]interface{}{"test": "test"}}}, + }, + "test2": []VertexProperty{ + VertexProperty{"", VertexPropertyValue{GenericValue{"test", "test"}, "test2", "test"}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test2", 1}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", true}, "test2", true}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test2", map[string]interface{}{"test": "test"}}}, + }, + }, + }, + given2: VertexValue{ + ID: "test_id", + Label: "test_label", + Properties: map[string][]VertexProperty{ + "test": []VertexProperty{ + VertexProperty{"", VertexPropertyValue{GenericValue{"test", "test"}, "test", "test"}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test", 1}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", true}, "test", true}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test", map[string]interface{}{"test": "test"}}}, + }, + "test3": []VertexProperty{ + VertexProperty{"", VertexPropertyValue{GenericValue{"test", "test"}, "test2", "test"}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test2", 1}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", true}, "test2", true}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test2", map[string]interface{}{"test": "test"}}}, + }, + }, + }, + expected: false, + }, + { + given1: VertexValue{ + ID: "test_id", + Label: "test_label", + Properties: map[string][]VertexProperty{ + "test": []VertexProperty{ + VertexProperty{"", VertexPropertyValue{GenericValue{"test", "test"}, "test", "test"}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test", 1}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", true}, "test", true}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test", map[string]interface{}{"test": "test"}}}, + }, + "test2": []VertexProperty{ + VertexProperty{"", VertexPropertyValue{GenericValue{"test", "test"}, "test2", "test"}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test2", 1}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", true}, "test2", true}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test2", map[string]interface{}{"test": "test"}}}, + }, + }, + }, + given2: VertexValue{ + ID: "test_id", + Label: "test_label", + Properties: map[string][]VertexProperty{ + "test": []VertexProperty{ + VertexProperty{"", VertexPropertyValue{GenericValue{"test", "test"}, "test", "test"}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test", 1}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", true}, "test", true}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test", map[string]interface{}{"test": "test"}}}, + }, + "test2": []VertexProperty{ + VertexProperty{"", VertexPropertyValue{GenericValue{"test", "test"}, "test5adfad", "test"}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test2", 1}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", true}, "test2", true}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test2", map[string]interface{}{"test": "test"}}}, + }, + }, + }, + expected: false, + }, + } + + for i, testCase := range testCases { + result := testCase.given1.Equals(testCase.given2) + if result != testCase.expected { + t.Error("test", i, "given", testCase.given1, "and", testCase.given2, "expected", testCase.expected, "result", result) + } + } +} + +func TestVertexEquals(t *testing.T) { + var testCases = []struct { + given1 Vertex + given2 Vertex + expected bool + }{ + { + given1: Vertex{}, + given2: Vertex{}, + expected: true, + }, + { + given1: Vertex{"type", VertexValue{}}, + given2: Vertex{"type", VertexValue{}}, + expected: true, + }, + { + given1: Vertex{"type", VertexValue{}}, + given2: Vertex{"type2", VertexValue{}}, + expected: false, + }, + { + given1: Vertex{"type", VertexValue{ID: "test"}}, + given2: Vertex{"type", VertexValue{ID: "test"}}, + expected: true, + }, + { + given1: Vertex{"type", VertexValue{ID: "test"}}, + given2: Vertex{"type2", VertexValue{ID: "test"}}, + expected: false, + }, + { + given1: Vertex{"type", VertexValue{ID: "test"}}, + given2: Vertex{"type", VertexValue{ID: "test1"}}, + expected: false, + }, + { + given1: Vertex{"type", VertexValue{Label: "test"}}, + given2: Vertex{"type", VertexValue{Label: "test"}}, + expected: true, + }, + { + given1: Vertex{"type", VertexValue{Label: "test"}}, + given2: Vertex{"type", VertexValue{Label: "test1"}}, + expected: false, + }, + { + given1: Vertex{"type", + VertexValue{ + ID: "test_id", + Label: "test_label", + Properties: map[string][]VertexProperty{ + "test": []VertexProperty{ + VertexProperty{"", VertexPropertyValue{GenericValue{"test", "test"}, "test", "test"}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test", 1}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", true}, "test", true}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test", map[string]interface{}{"test": "test"}}}, + }, + }, + }, + }, + given2: Vertex{"type", + VertexValue{ + ID: "test_id", + Label: "test_label", + Properties: map[string][]VertexProperty{ + "test": []VertexProperty{ + VertexProperty{"", VertexPropertyValue{GenericValue{"test", "test"}, "test", "test"}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test", 1}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", true}, "test", true}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test", map[string]interface{}{"test": "test"}}}, + }, + }, + }, + }, + expected: true, + }, + { + given1: Vertex{"type", + VertexValue{ + ID: "test_id", + Label: "test_label", + Properties: map[string][]VertexProperty{ + "test": []VertexProperty{ + VertexProperty{"", VertexPropertyValue{GenericValue{"test", "test"}, "test", "test"}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test", 1}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", true}, "test", true}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test", map[string]interface{}{"test": "test"}}}, + }, + }, + }, + }, + given2: Vertex{"type", + VertexValue{ + ID: "test_id", + Label: "test_label", + Properties: map[string][]VertexProperty{ + "test1": []VertexProperty{ + VertexProperty{"", VertexPropertyValue{GenericValue{"test", "test"}, "test", "test"}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test", 1}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", true}, "test", true}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test", map[string]interface{}{"test": "test"}}}, + }, + }, + }, + }, + expected: false, + }, + { + given1: Vertex{"type", + VertexValue{ + ID: "test_id", + Label: "test_label", + Properties: map[string][]VertexProperty{ + "test": []VertexProperty{ + VertexProperty{"", VertexPropertyValue{GenericValue{"test", "test"}, "test", "test"}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test", 1}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", true}, "test", true}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test", map[string]interface{}{"test": "test"}}}, + }, + }, + }, + }, + given2: Vertex{"type", + VertexValue{ + ID: "test_id", + Label: "test_label", + Properties: map[string][]VertexProperty{ + "test": []VertexProperty{ + VertexProperty{"", VertexPropertyValue{GenericValue{"test", "test"}, "test", "test1"}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test", 1}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", true}, "test", true}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test", map[string]interface{}{"test": "test"}}}, + }, + }, + }, + }, + expected: false, + }, + { + given1: Vertex{"type", + VertexValue{ + ID: "test_id", + Label: "test_label", + Properties: map[string][]VertexProperty{ + "test": []VertexProperty{ + VertexProperty{"", VertexPropertyValue{GenericValue{"test", "test"}, "test", "test"}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test", 1}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", true}, "test", true}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test", map[string]interface{}{"test": "test"}}}, + }, + }, + }, + }, + given2: Vertex{"type", + VertexValue{ + ID: "test_id", + Label: "test_label", + Properties: map[string][]VertexProperty{ + "test": []VertexProperty{ + VertexProperty{"", VertexPropertyValue{GenericValue{"test", "test"}, "test", "test"}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test", 2}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", true}, "test", true}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test", map[string]interface{}{"test": "test"}}}, + }, + }, + }, + }, + expected: false, + }, + { + given1: Vertex{"type", + VertexValue{ + ID: "test_id", + Label: "test_label", + Properties: map[string][]VertexProperty{ + "test": []VertexProperty{ + VertexProperty{"", VertexPropertyValue{GenericValue{"test", "test"}, "test", "test"}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test", 1}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", true}, "test", true}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test", map[string]interface{}{"test": "test"}}}, + }, + }, + }, + }, + given2: Vertex{"type", + VertexValue{ + ID: "test_id", + Label: "test_label", + Properties: map[string][]VertexProperty{ + "test": []VertexProperty{ + VertexProperty{"", VertexPropertyValue{GenericValue{"test", "test"}, "test", "test"}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test", 1}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", true}, "test", false}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test", map[string]interface{}{"test": "test"}}}, + }, + }, + }, + }, + expected: false, + }, + { + given1: Vertex{"type", + VertexValue{ + ID: "test_id", + Label: "test_label", + Properties: map[string][]VertexProperty{ + "test": []VertexProperty{ + VertexProperty{"", VertexPropertyValue{GenericValue{"test", "test"}, "test", "test"}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test", 1}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", true}, "test", true}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test", map[string]interface{}{"test": "test"}}}, + }, + }, + }, + }, + given2: Vertex{"type", + VertexValue{ + ID: "test_id", + Label: "test_label", + Properties: map[string][]VertexProperty{ + "test": []VertexProperty{ + VertexProperty{"", VertexPropertyValue{GenericValue{"test", "test"}, "test", "test"}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test", 1}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", true}, "test", true}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test", map[string]interface{}{"test": "test1"}}}, + }, + }, + }, + }, + expected: false, + }, + { + given1: Vertex{"type", + VertexValue{ + ID: "test_id", + Label: "test_label", + Properties: map[string][]VertexProperty{ + "test": []VertexProperty{ + VertexProperty{"", VertexPropertyValue{GenericValue{"test", "test"}, "test", "test"}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test", 1}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", true}, "test", true}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test", map[string]interface{}{"test": "test"}}}, + }, + "test2": []VertexProperty{ + VertexProperty{"", VertexPropertyValue{GenericValue{"test", "test"}, "test2", "test"}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test2", 1}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", true}, "test2", true}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test2", map[string]interface{}{"test": "test"}}}, + }, + }, + }, + }, + given2: Vertex{"type", + VertexValue{ + ID: "test_id", + Label: "test_label", + Properties: map[string][]VertexProperty{ + "test": []VertexProperty{ + VertexProperty{"", VertexPropertyValue{GenericValue{"test", "test"}, "test", "test"}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test", 1}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", true}, "test", true}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test", map[string]interface{}{"test": "test"}}}, + }, + "test2": []VertexProperty{ + VertexProperty{"", VertexPropertyValue{GenericValue{"test", "test"}, "test2", "test"}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test2", 1}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", true}, "test2", true}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test2", map[string]interface{}{"test": "test"}}}, + }, + }, + }, + }, + expected: true, + }, + { + given1: Vertex{"type", + VertexValue{ + ID: "test_id", + Label: "test_label", + Properties: map[string][]VertexProperty{ + "test": []VertexProperty{ + VertexProperty{"", VertexPropertyValue{GenericValue{"test", "test"}, "test", "test"}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test", 1}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", true}, "test", true}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test", map[string]interface{}{"test": "test"}}}, + }, + "test2": []VertexProperty{ + VertexProperty{"", VertexPropertyValue{GenericValue{"test", "test"}, "test2", "test"}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test2", 1}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", true}, "test2", true}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test2", map[string]interface{}{"test": "test"}}}, + }, + }, + }, + }, + given2: Vertex{"type", + VertexValue{ + ID: "test_id", + Label: "test_label", + Properties: map[string][]VertexProperty{ + "test": []VertexProperty{ + VertexProperty{"", VertexPropertyValue{GenericValue{"test", "test"}, "test", "test"}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test", 1}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", true}, "test", true}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test", map[string]interface{}{"test": "test"}}}, + }, + "test3": []VertexProperty{ + VertexProperty{"", VertexPropertyValue{GenericValue{"test", "test"}, "test2", "test"}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test2", 1}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", true}, "test2", true}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test2", map[string]interface{}{"test": "test"}}}, + }, + }, + }, + }, + expected: false, + }, + { + given1: Vertex{"type", + VertexValue{ + ID: "test_id", + Label: "test_label", + Properties: map[string][]VertexProperty{ + "test": []VertexProperty{ + VertexProperty{"", VertexPropertyValue{GenericValue{"test", "test"}, "test", "test"}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test", 1}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", true}, "test", true}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test", map[string]interface{}{"test": "test"}}}, + }, + "test2": []VertexProperty{ + VertexProperty{"", VertexPropertyValue{GenericValue{"test", "test"}, "test2", "test"}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test2", 1}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", true}, "test2", true}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test2", map[string]interface{}{"test": "test"}}}, + }, + }, + }, + }, + given2: Vertex{"type", + VertexValue{ + ID: "test_id", + Label: "test_label", + Properties: map[string][]VertexProperty{ + "test": []VertexProperty{ + VertexProperty{"", VertexPropertyValue{GenericValue{"test", "test"}, "test", "test"}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test", 1}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", true}, "test", true}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test", map[string]interface{}{"test": "test"}}}, + }, + "test2": []VertexProperty{ + VertexProperty{"", VertexPropertyValue{GenericValue{"test", "test"}, "test5adfad", "test"}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", 1}, "test2", 1}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", true}, "test2", true}}, + VertexProperty{"test", VertexPropertyValue{GenericValue{"test", map[string]interface{}{"test": "test"}}, "test2", map[string]interface{}{"test": "test"}}}, + }, + }, + }, + }, + expected: false, + }, + } + + for i, testCase := range testCases { + result := testCase.given1.Equals(testCase.given2) + if result != testCase.expected { + t.Error("test", i, "given", testCase.given1, "and", testCase.given2, "expected", testCase.expected, "result", result) + } + } +} diff --git a/utils.go b/utils.go index 56441de..48fd6da 100644 --- a/utils.go +++ b/utils.go @@ -3,6 +3,7 @@ package gremlin import ( "bytes" "fmt" + "reflect" "regexp" "strings" ) @@ -69,3 +70,59 @@ func MakeGremlinQuery(gremlinQuery GremlinQuery, argRegexP *regexp.Regexp) (stri } return fmt.Sprintf(gremlinQuery.Query, args...), nil } + +func InterfacesMatch(interface1, interface2 interface{}) bool { + switch interface1.(type) { + case map[string]interface{}: + val1Map := interface1.(map[string]interface{}) + val2Map := interface2.(map[string]interface{}) + if len(val1Map) != len(val2Map) { + return false + } + for k, val1 := range val1Map { + val2, ok := val2Map[k] + if !ok { + return false + } + if !InterfacesMatch(val1, val2) { + return false + } + } + + case []interface{}: + val1List := interface1.([]interface{}) + val2List := interface2.([]interface{}) + if len(val1List) != len(val2List) { + return false + } + for i, val1Map := range val1List { + if !InterfacesMatch(val1Map, val2List[i]) { + return false + } + } + + case []string: + val1List := interface1.([]string) + val2List := interface2.([]string) + if len(val1List) != len(val2List) { + return false + } + for i, val1Map := range val1List { + if !InterfacesMatch(val1Map, val2List[i]) { + return false + } + } + + default: + if interface1 == nil && interface2 != nil { + return false + } else if interface1 != nil && interface2 == nil { + return false + } else if interface1 != nil && interface2 != nil { + if !reflect.DeepEqual(fmt.Sprintf("%v", interface1), fmt.Sprintf("%v", interface2)) { + return false + } + } + } + return true +} From f47776e5c91445126a77f827f131f8d3ddf9d565 Mon Sep 17 00:00:00 2001 From: Evan Marcey Date: Mon, 13 May 2019 11:49:46 -0400 Subject: [PATCH 2/2] addressing comments --- types.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/types.go b/types.go index 6058366..2c88ca9 100644 --- a/types.go +++ b/types.go @@ -30,10 +30,7 @@ type VertexValue struct { } func (v1 VertexValue) Equals(v2 VertexValue) bool { - if v1.ID != v2.ID || v1.Label != v2.Label { - return false - } - if len(v1.Properties) != len(v2.Properties) { + if v1.ID != v2.ID || v1.Label != v2.Label || len(v1.Properties) != len(v2.Properties) { return false } for k, val1Slice := range v1.Properties {