Skip to content
Snippets Groups Projects
Commit be8abfcd authored by Lelieveld,J.R.J. (Joris)'s avatar Lelieveld,J.R.J. (Joris)
Browse files

More fixes on query modifiers

parent c70762bd
No related branches found
No related tags found
No related merge requests found
......@@ -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)
}
}
......
......@@ -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
......
......@@ -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
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment