diff --git a/aql/convertQuery.go b/aql/convertQuery.go index e1e1fe4312676095599a83615f4766383928a190..af2101807517bf27155d34b7ec8cd749c0acfedc 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 35ad9ff4b5a3102500f1ad48dbb4df4eb724045a..44917c8ff658f9c43ec182cd9653c26fc99ba8f5 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 ec04a6e7e28e15a806060d4ab5cabf54def159f2..9dd51dcc1e3a1c5a71a890483ca37175bb16502d 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 }