From be8abfcdd06cd9692ba5767c2248e418ced1ad90 Mon Sep 17 00:00:00 2001
From: Joris <j.r.j.lelieveld@students.uu.nl>
Date: Tue, 1 Jun 2021 16:15:27 +0200
Subject: [PATCH] More fixes on query modifiers

---
 aql/convertQuery.go      | 16 +++++-----
 aql/convertQuery_test.go | 63 ++++++++++++++++++++++++++++++++++++++++
 entity/queryStruct.go    |  2 +-
 3 files changed, 72 insertions(+), 9 deletions(-)

diff --git a/aql/convertQuery.go b/aql/convertQuery.go
index e1e1fe4..af21018 100644
--- a/aql/convertQuery.go
+++ b/aql/convertQuery.go
@@ -158,7 +158,7 @@ func createQuery(JSONQuery *entity.IncomingQueryJSON) *string {
 			// Select the correct addition to the return of r0[**]
 			if modifier.SelectedType == "entity" {
 				// ASSUMING THERE IS ONLY 1 RELATION
-				if JSONQuery.Relations[0].EntityFrom == modifier.ID {
+				if JSONQuery.Relations[0].EntityFrom == modifier.SelectedTypeID {
 					// This should always be 0, because that is the start of the path
 					pathDistinction = ".vertices[0]"
 
@@ -174,10 +174,10 @@ func createQuery(JSONQuery *entity.IncomingQueryJSON) *string {
 			// Getting the attribute if there is one
 			if modifier.AttributeIndex != -1 {
 				if modifier.SelectedType == "entity" {
-					pathDistinction += fmt.Sprintf(".%v", JSONQuery.Entities[modifier.ID].Constraints[modifier.AttributeIndex].Attribute)
+					pathDistinction += fmt.Sprintf(".%v", JSONQuery.Entities[modifier.SelectedTypeID].Constraints[modifier.AttributeIndex].Attribute)
 
 				} else {
-					pathDistinction += fmt.Sprintf(".%v", JSONQuery.Relations[modifier.ID].Constraints[modifier.AttributeIndex].Attribute)
+					pathDistinction += fmt.Sprintf(".%v", JSONQuery.Relations[modifier.SelectedTypeID].Constraints[modifier.AttributeIndex].Attribute)
 
 				}
 			}
@@ -194,25 +194,25 @@ func createQuery(JSONQuery *entity.IncomingQueryJSON) *string {
 		} else {
 			// Check if the modifier is on an attribute
 			if modifier.AttributeIndex == -1 {
-				ret += fmt.Sprintf("RETURN LENGTH (n%v)", modifier.ID)
+				ret += fmt.Sprintf("RETURN LENGTH (n%v)", modifier.SelectedTypeID)
 			} else {
 				var attribute string
 
 				// Selecting the right attribute from either the entity constraint or relation constraint
 				if modifier.SelectedType == "entity" {
-					attribute = JSONQuery.Entities[modifier.ID].Constraints[modifier.AttributeIndex].Attribute
+					attribute = JSONQuery.Entities[modifier.SelectedTypeID].Constraints[modifier.AttributeIndex].Attribute
 
 				} else {
-					attribute = JSONQuery.Relations[modifier.ID].Constraints[modifier.AttributeIndex].Attribute
+					attribute = JSONQuery.Relations[modifier.SelectedTypeID].Constraints[modifier.AttributeIndex].Attribute
 
 				}
 
 				// If count is used it has to be replaced with Length + unique else use the modifier type
 				if modifier.Type == "COUNT" {
-					ret += fmt.Sprintf("RETURN LENGTH (unique(n%v[*].%v))", modifier.ID, attribute)
+					ret += fmt.Sprintf("RETURN LENGTH (unique(n%v[*].%v))", modifier.SelectedTypeID, attribute)
 
 				} else {
-					ret += fmt.Sprintf("RETURN %v (n%v[*].%v)", modifier.Type, modifier.ID, attribute)
+					ret += fmt.Sprintf("RETURN %v (n%v[*].%v)", modifier.Type, modifier.SelectedTypeID, attribute)
 
 				}
 			}
diff --git a/aql/convertQuery_test.go b/aql/convertQuery_test.go
index 35ad9ff..44917c8 100644
--- a/aql/convertQuery_test.go
+++ b/aql/convertQuery_test.go
@@ -402,6 +402,69 @@ func TestModifierCountRelation(t *testing.T) {
 	cleanedResult = strings.ReplaceAll(cleanedResult, "\t", "")
 	assert.Equal(t, correctConvertedResult, cleanedResult)
 }
+func TestModifierCountEntitySwap(t *testing.T) {
+	// Setup for test
+	// Create query conversion service
+	service := NewService()
+
+	query := []byte(`{
+		"databaseName": "TweedeKamer",
+		"return": {
+		  "entities": [
+			0,
+			1
+		  ],
+		  "relations": [
+			0
+		  ]
+		},
+		"entities": [
+		  {
+			"type": "partijen",
+			"constraints": []
+		  },
+		  {
+			"type": "kamerleden",
+			"constraints": []
+		  }
+		],
+		"relations": [
+		  {
+			"type": "lid_van",
+			"depth": {
+			  "min": 1,
+			  "max": 1
+			},
+			"entityFrom": 1,
+			"entityTo": 0,
+			"constraints": []
+		  }
+		],
+		"limit": 5000,
+		"modifiers": [
+		  {
+			"type": "COUNT",
+			"selectedType": "entity",
+			"selectedTypeId": 1,
+			"attributeIndex": -1
+		  }
+		]
+	  }`)
+
+	// Unmarshall the incoming message into an IncomingJSONQuery object
+	var JSONQuery entity.IncomingQueryJSON
+	json.Unmarshal(query, &JSONQuery)
+	convertedResult, err := service.ConvertQuery(&JSONQuery)
+
+	// Assert that there is no error
+	assert.NoError(t, err)
+
+	// Assert that the result and the expected result are the same
+	correctConvertedResult := `WITH partijenLET n1 = (FOR x IN kamerleden RETURN x)LET r0 = (FOR x IN n1 FOR v, e, p IN 1..1 OUTBOUND x lid_van OPTIONS { uniqueEdges: "path" }RETURN DISTINCT p )RETURN LENGTH (unique(r0[*].vertices[0]))`
+	cleanedResult := strings.ReplaceAll(*convertedResult, "\n", "")
+	cleanedResult = strings.ReplaceAll(cleanedResult, "\t", "")
+	assert.Equal(t, correctConvertedResult, cleanedResult)
+}
 func TestModifierCountRelationAttribute(t *testing.T) {
 	// Setup for test
 	// Create query conversion service
diff --git a/entity/queryStruct.go b/entity/queryStruct.go
index ec04a6e..9dd51dc 100644
--- a/entity/queryStruct.go
+++ b/entity/queryStruct.go
@@ -37,7 +37,7 @@ type QueryRelationStruct struct {
 type QueryModifierStruct struct {
 	Type           string // SUM COUNT AVG
 	SelectedType   string // node relation
-	ID             int    // ID of the enitity or relation
+	SelectedTypeID int    // ID of the enitity or relation
 	AttributeIndex int    // = -1 if its the node or relation, = > -1 if an attribute is selected
 }
 
-- 
GitLab