Skip to content
Snippets Groups Projects
convertQuery_test.go 31 KiB
Newer Older
  • Learn to ignore specific revisions
  • 	// 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 := `LET n0 = (FOR x IN airports FILTER x.city == "San Francisco" RETURN x)LET r0 = (FOR x IN n0 FOR v, e, p IN 1..1 INBOUND x flights OPTIONS { uniqueEdges: "path" }LIMIT 5000 RETURN DISTINCT p )LET nodes = first(RETURN UNION_DISTINCT(flatten(r0[**].vertices), [],[]))LET edges = first(RETURN UNION_DISTINCT(flatten(r0[**].edges), [],[]))RETURN {"vertices":nodes, "edges":edges }`
    	cleanedResult := strings.ReplaceAll(*convertedResult, "\n", "")
    	cleanedResult = strings.ReplaceAll(cleanedResult, "\t", "")
    	assert.Equal(t, correctConvertedResult, cleanedResult)
    }
    
    
    LoLo5689's avatar
    LoLo5689 committed
    /*
    Tests too manu return entities
    	t: *testing.T, makes go recognise this as a test
    */
    
    func TestTooManyReturnEntities(t *testing.T) {
    	// Setup for test
    	// Create query conversion service
    	service := NewService()
    
    	query := []byte(`{
    
    				0
    			]
    		},
    		"entities": [
    			{
    				"ID": 0,
    				"type": "airports"
    			}
    		],
    		"relations": [
    			{
    				"ID": 0,
    				"type": "flights",
    				"depth": {
    					"min": 1,
    					"max": 1
    				},
    				"fromType": "",
    				"fromID": -1,
    				"toType": "entity",
    				"toID": 0
    			}
    		],
    		"groupBys": [],
    		"filters": [
    			{
    				"ID": 0,
    				"fromType": "entity",
    				"fromID": 0,
    				"toType": "relation",
    				"toID": 0,
    				"attribute": "city",
    				"value": "San Francisco",
    				"dataType": "string",
    				"matchType": "exact",
    				"inType": "",
    				"inID": -1
    			}
    		],
    		"limit": 5000
    	}`)
    
    	// Unmarshall the incoming message into an IncomingJSONQuery object
    	var JSONQuery entity.IncomingQueryJSON
    	json.Unmarshal(query, &JSONQuery)
    
    	_, err := service.ConvertQuery(&JSONQuery)
    
    
    	// Assert that there is no error
    	assert.Equal(t, errors.New("non-existing entity referenced in return"), err)
    }
    
    
    LoLo5689's avatar
    LoLo5689 committed
    /*
    Tests too manu return relations
    	t: *testing.T, makes go recognise this as a test
    */
    
    func TestTooManyReturnRelations(t *testing.T) {
    	// Setup for test
    	// Create query conversion service
    	service := NewService()
    
    	query := []byte(`{
    
    				0,
    				1,
    				2
    			]
    		},
    		"entities": [
    			{
    				"ID": 0,
    				"type": "airports"
    			}
    		],
    		"relations": [
    			{
    				"ID": 0,
    				"type": "flights",
    				"depth": {
    					"min": 1,
    					"max": 1
    				},
    				"fromType": "",
    				"fromID": -1,
    				"toType": "entity",
    				"toID": 0
    			}
    		],
    		"groupBys": [],
    		"filters": [
    			{
    				"ID": 0,
    				"fromType": "entity",
    				"fromID": 0,
    				"toType": "relation",
    				"toID": 0,
    				"attribute": "city",
    				"value": "San Francisco",
    				"dataType": "string",
    				"matchType": "exact",
    				"inType": "",
    				"inID": -1
    			}
    		],
    		"limit": 5000
    	}`)
    
    	// Unmarshall the incoming message into an IncomingJSONQuery object
    	var JSONQuery entity.IncomingQueryJSON
    	json.Unmarshal(query, &JSONQuery)
    
    	_, err := service.ConvertQuery(&JSONQuery)
    
    
    	// Assert that there is no error
    	assert.Equal(t, errors.New("non-existing relation referenced in return"), err)
    }
    
    
    LoLo5689's avatar
    LoLo5689 committed
    /*
    Tests negative return entities
    	t: *testing.T, makes go recognise this as a test
    */
    
    func TestNegativeReturnEntities(t *testing.T) {
    	// Setup for test
    	// Create query conversion service
    	service := NewService()
    
    	query := []byte(`{
    
    				0,
    				1,
    				2
    			]
    		},
    		"entities": [
    			{
    				"ID": 0,
    				"type": "airports"
    			}
    		],
    		"relations": [
    			{
    				"ID": 0,
    				"type": "flights",
    				"depth": {
    					"min": 1,
    					"max": 1
    				},
    				"fromType": "",
    				"fromID": -1,
    				"toType": "entity",
    				"toID": 0
    			}
    		],
    		"groupBys": [],
    		"filters": [
    			{
    				"ID": 0,
    				"fromType": "entity",
    				"fromID": 0,
    				"toType": "relation",
    				"toID": 0,
    				"attribute": "city",
    				"value": "San Francisco",
    				"dataType": "string",
    				"matchType": "exact",
    				"inType": "",
    				"inID": -1
    			}
    		],
    		"limit": 5000
    	}`)
    
    	// Unmarshall the incoming message into an IncomingJSONQuery object
    	var JSONQuery entity.IncomingQueryJSON
    	json.Unmarshal(query, &JSONQuery)
    
    	_, err := service.ConvertQuery(&JSONQuery)
    
    
    	// Assert that there is no error
    	assert.Equal(t, errors.New("non-existing entity referenced in return"), err)
    }
    
    
    LoLo5689's avatar
    LoLo5689 committed
    /*
    Tests a query with no relation field
    	t: *testing.T, makes go recognise this as a test
    */
    
    func TestNoRelationsField(t *testing.T) {
    	// Setup for test
    	// Create query conversion service
    	service := NewService()
    
    	query := []byte(`{
    			"return": {
    				"entities": [
    					0
    				]
    			},
    			"entities": [
    				{
    
    					"ID": 0,
    					"type": "airports"
    				}
    			],
    			"groupBys": [],
    			"filters": [
    				{
    					"ID": 0,
    					"fromType": "entity",
    					"fromID": 0,
    					"toType": "relation",
    					"toID": 0,
    					"attribute": "city",
    					"value": "San Francisco",
    					"dataType": "string",
    					"matchType": "exact",
    					"inType": "",
    					"inID": -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 := `LET n0 = (FOR x IN airports FILTER x.city == "San Francisco" RETURN x)LET nodes = first(RETURN UNION_DISTINCT(n0,[],[]))LET edges = first(RETURN UNION_DISTINCT([],[]))RETURN {"vertices":nodes, "edges":edges }`
    	cleanedResult := strings.ReplaceAll(*convertedResult, "\n", "")
    	cleanedResult = strings.ReplaceAll(cleanedResult, "\t", "")
    	assert.Equal(t, correctConvertedResult, cleanedResult)
    }
    
    
    /*
    Tests a query with double WITH
    	t: *testing.T, makes go recognise this as a test
    */
    func TestDoubleWITH(t *testing.T) {
    	// Setup for test
    	// Create query conversion service
    	service := NewService()
    
    	query := []byte(`{
    
    		"databaseName": "test",
    		"return": {
    			"entities": [
    				0,
    				1,
    				2,
    				3
    			],
    			"relations": [
    				0,
    				1
    			]
    		},
    		"entities": [
    			{
    				"ID": 0,
    				"type": "kamerleden"
    			},
    			{
    				"ID": 1,
    				"type": "partijen"
    			},
    			{
    				"ID": 2,
    				"type": "kamerleden"
    			},
    			{
    				"ID": 3,
    				"type": "commissies"
    			}
    		],
    		"relations": [
    			{
    				"ID": 0,
    				"type": "lid_van",
    				"depth": {
    					"min": 1,
    					"max": 1
    				},
    				"fromType": "entity",
    				"fromID": 0,
    				"toType": "entity",
    				"toID": 1
    			},
    			{
    				"ID": 1,
    				"type": "onderdeel_van",
    				"depth": {
    					"min": 1,
    					"max": 1
    				},
    				"fromType": "entity",
    				"fromID": 2,
    				"toType": "entity",
    				"toID": 3
    			}
    		],
    		"groupBys": [],
    		"filters": [],
    		"limit": 5000,
    		"modifiers": []
    	}`)
    
    
    	// 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 partijen, commissiesLET n0 = (FOR x IN kamerleden RETURN x)LET r0 = (FOR x IN n0 FOR v, e, p IN 1..1 OUTBOUND x lid_van OPTIONS { uniqueEdges: \"path\" }LIMIT 5000 RETURN DISTINCT p )LET n2 = (FOR x IN kamerleden RETURN x)LET r1 = (FOR x IN n2 FOR v, e, p IN 1..1 OUTBOUND x onderdeel_van OPTIONS { uniqueEdges: \"path\" }LIMIT 5000 RETURN DISTINCT p )LET nodes = first(RETURN UNION_DISTINCT(flatten(r0[**].vertices), flatten(r1[**].vertices), [],[]))LET edges = first(RETURN UNION_DISTINCT(flatten(r0[**].edges), flatten(r1[**].edges), [],[]))RETURN {\"vertices\":nodes, \"edges\":edges }"
    	cleanedResult := strings.ReplaceAll(*convertedResult, "\n", "")
    	cleanedResult = strings.ReplaceAll(cleanedResult, "\t", "")
    	assert.Equal(t, correctConvertedResult, cleanedResult)
    }
    
    
    LoLo5689's avatar
    LoLo5689 committed
    /*
    Tests an entity with a lower than -1 in a relation
    	t: *testing.T, makes go recognise this as a test
    */
    
    func TestEntityFromLowerThanNegativeOneInRelation(t *testing.T) {
    	// Setup for test
    	// Create query conversion service
    	service := NewService()
    
    	query := []byte(`{
    
    				0
    			]
    		},
    		"entities": [
    			{
    				"ID": 0,
    				"type": "airports"
    			}
    		],
    		"relations": [
    			{
    				"ID": 0,
    				"type": "flights",
    				"depth": {
    					"min": 1,
    					"max": 1
    				},
    				"fromType": "",
    				"fromID": -4,
    				"toType": "entity",
    				"toID": 0
    			}
    		],
    		"groupBys": [],
    		"filters": [
    			{
    				"ID": 0,
    				"fromType": "entity",
    				"fromID": 0,
    				"toType": "relation",
    				"toID": 0,
    				"attribute": "city",
    				"value": "San Francisco",
    				"dataType": "string",
    				"matchType": "exact",
    				"inType": "",
    				"inID": -1
    			}
    		],
    		"limit": 5000
    	}`)
    
    	// Unmarshall the incoming message into an IncomingJSONQuery object
    	var JSONQuery entity.IncomingQueryJSON
    	json.Unmarshal(query, &JSONQuery)
    
    	_, err := service.ConvertQuery(&JSONQuery)
    
    
    	// Assert that there is no error
    	assert.NoError(t, err)
    }