Skip to content
Snippets Groups Projects
convertQuery_test.go 48.7 KiB
Newer Older
  • Learn to ignore specific revisions
  • 				"depth": {
    					"min": 1,
    					"max": 1
    				},
    				"fromType": "entity",
    
    	// 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 := []byte(`LET result = (
            FOR e_0 IN parliament
            FILTER e_0.name LIKE "%Geert%"
            LET e1 = (
                    FOR e_1 IN commissions
                    FOR r0 IN part_of
                    FILTER r0._from == e_0._id AND r0._to == e_1._id
                    LET e2 = (
                            FOR e_2 IN parliament
                            FOR r1 IN part_of
                            FILTER r1._from == e_2._id AND r1._to == e_1._id
                            LET e3 = (
                                    FOR e_3 IN parties
                                    FOR r2 IN member_of
                                    FILTER e_3.seats < 10
                                    FILTER r2._from == e_2._id AND r2._to == e_3._id
                                    FILTER length(e_3) != 0 AND length(r2) != 0
    
    Kieran van Gaalen's avatar
    Kieran van Gaalen committed
                                    RETURN {"nodes":union_distinct([e_3], []), "rel": union_distinct([r2], []), "mod": union_distinct([], [])}
    
                            )
                            LET e4 = (
                                    FOR e_4 IN resolutions
                                    FOR r3 IN submits
                                    FILTER e_4.date LIKE "%mei%"
                                    FILTER r3._from == e_2._id AND r3._to == e_4._id
                                    FILTER length(e_4) != 0 AND length(r3) != 0
    
    Kieran van Gaalen's avatar
    Kieran van Gaalen committed
                                    RETURN {"nodes":union_distinct([e_4], []), "rel": union_distinct([r3], []), "mod": union_distinct([], [])}
    
                            )
                            FILTER length(e3) != 0 AND length(e4) != 0 AND length(e_2) != 0 AND length(r1) != 0
    
    Kieran van Gaalen's avatar
    Kieran van Gaalen committed
                            RETURN {"nodes":union_distinct(flatten(e3[**].nodes), flatten(e4[**].nodes), [e_2]), "rel": union_distinct(flatten(e3[**].rel), flatten(e4[**].rel), [r1]), "mod": union_distinct(flatten(e3[**].mod), flatten(e4[**].mod), [])}
    
    Kieran van Gaalen's avatar
    Kieran van Gaalen committed
                    RETURN {"nodes":union_distinct(flatten(e2[**].nodes), [e_1]), "rel": union_distinct(flatten(e2[**].rel), [r0]), "mod": union_distinct(flatten(e2[**].mod), [])}
    
    Kieran van Gaalen's avatar
    Kieran van Gaalen committed
            RETURN {"nodes":union_distinct(flatten(e1[**].nodes), [e_0]), "rel": union_distinct(flatten(e1[**].rel), []), "mod": union_distinct(flatten(e1[**].mod), [])}
    
    Kieran van Gaalen's avatar
    Kieran van Gaalen committed
    LET nodes = union_distinct(flatten(result[**].nodes),[])
    LET edges = union_distinct(flatten(result[**].rel),[])
    RETURN {"vertices":nodes,"edges":edges}`)
    
    	regExCleaner := regexp.MustCompile(`\s+`)
    	correctCleanedResult := regExCleaner.ReplaceAllString(string(correctConvertedResult), " ")
    	convertedCleanedResult := regExCleaner.ReplaceAllString(*convertedResult, " ")
    	assert.Equal(t, correctCleanedResult, convertedCleanedResult)
    
    LoLo5689's avatar
    LoLo5689 committed
    /*
    
    Tests nine entities (four types) with three entity filters and two junctions
    Query description: Give me all parties, with less than 10 seats, connected to their respective parliament members, who are then connected to the resolutions they submitted, but only those submitted in May, and connected to the comissions they're in, which are then connected to all of their members, but only those with "Geert" in their name (resulting in only "Geert Wilders"), who is then connected to their submited resolutions and their party, which is connected to all of its members
    
    LoLo5689's avatar
    LoLo5689 committed
    	t: *testing.T, makes go recognise this as a test
    */
    
    func TestDoubleJunctionNineEntitiesThreeEntityFilters(t *testing.T) {
    
    	// Setup for test
    	// Create query conversion service
    	service := NewService()
    
    	query := []byte(`{
    
    				"constraints": [
    					{
    						"attribute": "name",
    						"value": "Geert",
    						"dataType": "string",
    						"matchType": "contains"
    					}
    				]
    			},
    
    				"name": "parties",
    				"ID": 3,
    				"constraints": [
    					{
    						"attribute": "seats",
    						"value": "10",
    						"dataType": "int",
    						"matchType": "LT"
    					}
    				]
    			},
    
    				"name": "resolutions",
    				"ID": 4,
    				"constraints": [
    					{
    						"attribute": "date",
    						"value": "mei",
    						"dataType": "string",
    						"matchType": "contains"
    					}
    				]
    			},
    
    				"name": "resolutions",
    				"ID": 5,
    				"constraints": []
    			},
    			{
    				"name": "parties",
    				"ID": 6,
    				"constraints": []
    
    				"depth": {
    					"min": 1,
    					"max": 1
    				},
    				"fromType": "entity",
    
    				"name": "part_of",
    				"depth": {
    					"min": 1,
    					"max": 1
    				},
    				"fromType": "entity",
    				"fromId": 2,
    				"toType": "entity",
    				"toID": 1,
    				"constraints": []
    
    				"name": "member_of",
    				"depth": {
    					"min": 1,
    					"max": 1
    				},
    				"fromType": "entity",
    				"fromId": 2,
    				"toType": "entity",
    				"toID": 3,
    				"constraints": []
    
    				"name": "submits",
    				"depth": {
    					"min": 1,
    					"max": 1
    				},
    				"fromType": "entity",
    				"fromId": 2,
    				"toType": "entity",
    				"toID": 4,
    				"constraints": []
    			},
    
    				"depth": {
    					"min": 1,
    					"max": 1
    				},
    				"fromType": "entity",
    
    				"depth": {
    					"min": 1,
    					"max": 1
    				},
    				"fromType": "entity",
    
    				"toID": 6,
    				"constraints": []
    			}
    			,
    			{
    				"ID": 6,
    				"name": "member_of",
    				"depth": {
    					"min": 1,
    					"max": 1
    				},
    				"fromType": "entity",
    				"fromId": 7,
    				"toType": "entity",
    				"toID": 6,
    				"constraints": []
    
    
    	// 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 := []byte(`LET result = (
            FOR e_3 IN parties
            FILTER e_3.seats < 10
            LET e2 = (
                    FOR e_2 IN parliament
                    FOR r2 IN member_of
                    FILTER r2._from == e_2._id AND r2._to == e_3._id
                    LET e1 = (
                            FOR e_1 IN commissions
                            FOR r1 IN part_of
                            FILTER r1._from == e_2._id AND r1._to == e_1._id
                            LET e0 = (
                                    FOR e_0 IN parliament
                                    FOR r0 IN part_of
                                    FILTER e_0.name LIKE "%Geert%"
                                    FILTER r0._from == e_0._id AND r0._to == e_1._id
                                    LET e5 = (
                                            FOR e_5 IN resolutions
                                            FOR r4 IN submits
                                            FILTER r4._from == e_0._id AND r4._to == e_5._id
                                            FILTER length(e_5) != 0 AND length(r4) != 0
    
    Kieran van Gaalen's avatar
    Kieran van Gaalen committed
                                            RETURN {"nodes":union_distinct([e_5], []), "rel": union_distinct([r4], []), "mod": union_distinct([], [])}
    
                                    )
                                    LET e6 = (
                                            FOR e_6 IN parties
                                            FOR r5 IN member_of
                                            FILTER r5._from == e_0._id AND r5._to == e_6._id
                                            LET e7 = (
                                                    FOR e_7 IN parliament
                                                    FOR r6 IN member_of
                                                    FILTER r6._from == e_7._id AND r6._to == e_6._id
                                                    FILTER length(e_7) != 0 AND length(r6) != 0
    
    Kieran van Gaalen's avatar
    Kieran van Gaalen committed
                                                    RETURN {"nodes":union_distinct([e_7], []), "rel": union_distinct([r6], []), "mod": union_distinct([], [])}
    
    Kieran van Gaalen's avatar
    Kieran van Gaalen committed
                                            RETURN {"nodes":union_distinct(flatten(e7[**].nodes), [e_6]), "rel": union_distinct(flatten(e7[**].rel), [r5]), "mod": union_distinct(flatten(e7[**].mod), [])}
    
                                    )
                                    FILTER length(e5) != 0 AND length(e6) != 0 AND length(e_0) != 0 AND length(r0) != 0
    
    Kieran van Gaalen's avatar
    Kieran van Gaalen committed
                                    RETURN {"nodes":union_distinct(flatten(e5[**].nodes), flatten(e6[**].nodes), [e_0]), "rel": union_distinct(flatten(e5[**].rel), flatten(e6[**].rel), [r0]), "mod": union_distinct(flatten(e5[**].mod), flatten(e6[**].mod), [])}
    
    Kieran van Gaalen's avatar
    Kieran van Gaalen committed
                            RETURN {"nodes":union_distinct(flatten(e0[**].nodes), [e_1]), "rel": union_distinct(flatten(e0[**].rel), [r1]), "mod": union_distinct(flatten(e0[**].mod), [])}
    
                    )
                    LET e4 = (
                            FOR e_4 IN resolutions
                            FOR r3 IN submits
                            FILTER e_4.date LIKE "%mei%"
                            FILTER r3._from == e_2._id AND r3._to == e_4._id
                            FILTER length(e_4) != 0 AND length(r3) != 0
    
    Kieran van Gaalen's avatar
    Kieran van Gaalen committed
                            RETURN {"nodes":union_distinct([e_4], []), "rel": union_distinct([r3], []), "mod": union_distinct([], [])}
    
                    )
                    FILTER length(e1) != 0 AND length(e4) != 0 AND length(e_2) != 0 AND length(r2) != 0
    
    Kieran van Gaalen's avatar
    Kieran van Gaalen committed
                    RETURN {"nodes":union_distinct(flatten(e1[**].nodes), flatten(e4[**].nodes), [e_2]), "rel": union_distinct(flatten(e1[**].rel), flatten(e4[**].rel), [r2]), "mod": union_distinct(flatten(e1[**].mod), flatten(e4[**].mod), [])}
    
    Kieran van Gaalen's avatar
    Kieran van Gaalen committed
            RETURN {"nodes":union_distinct(flatten(e2[**].nodes), [e_3]), "rel": union_distinct(flatten(e2[**].rel), []), "mod": union_distinct(flatten(e2[**].mod), [])}
    
    Kieran van Gaalen's avatar
    Kieran van Gaalen committed
    LET nodes = union_distinct(flatten(result[**].nodes),[])
    LET edges = union_distinct(flatten(result[**].rel),[])
    RETURN {"vertices":nodes,"edges":edges}`)
    
    	regExCleaner := regexp.MustCompile(`\s+`)
    	correctCleanedResult := regExCleaner.ReplaceAllString(string(correctConvertedResult), " ")
    	convertedCleanedResult := regExCleaner.ReplaceAllString(*convertedResult, " ")
    	assert.Equal(t, correctCleanedResult, convertedCleanedResult)
    
    LoLo5689's avatar
    LoLo5689 committed
    /*
    
    Tests two entities (one type) with one entity filter and one relation filter
    Query description: Give me all airports, in the state "HI", connected to any other airport by flight, but only the flights on "day" 15
    
    LoLo5689's avatar
    LoLo5689 committed
    	t: *testing.T, makes go recognise this as a test
    */
    
    func TestTwoEntitiesOneEntityFilterOneRelationFilter(t *testing.T) {
    
    	// Setup for test
    	// Create query conversion service
    	service := NewService()
    
    	query := []byte(`{
    
    				"name": "airports",
    				"constraints": [
    					{
    						"attribute": "state",
    						"value": "HI",
    						"dataType": "string",
    						"matchType": "exact"
    					}
    				]
    			},
    			{
    				"ID": 1,
    				"name": "airports",
    				"constraints":[]
    
    				"ToType": "entity",
    				"toID": 1,
    				"constraints": [
    					{	
    						"attribute": "Day",
    						"value": "15",
    						"dataType": "int",
    						"matchType": "EQ",
    						"inType": "",
    						"inID": -1
    					}
    				]
    
    	// Unmarshall the incoming message into an IncomingJSONQuery object
    	var JSONQuery entity.IncomingQueryJSON
    	json.Unmarshal(query, &JSONQuery)
    
    
    
    	// Assert that there is no error
    	assert.NoError(t, err)
    
    
    	// Assert that the result and the expected result are the same
    	correctConvertedResult := []byte(`LET result = (
            FOR e_0 IN airports
            FILTER e_0.state == "HI"
            LET e1 = (
                    FOR e_1 IN airports
                    FOR r0 IN flights
                    FILTER r0.Day == 15
                    FILTER r0._from == e_0._id AND r0._to == e_1._id
                    FILTER length(e_1) != 0 AND length(r0) != 0
    
    Kieran van Gaalen's avatar
    Kieran van Gaalen committed
                    RETURN {"nodes":union_distinct([e_1], []), "rel": union_distinct([r0], []), "mod": union_distinct([], [])}
    
    Kieran van Gaalen's avatar
    Kieran van Gaalen committed
            RETURN {"nodes":union_distinct(flatten(e1[**].nodes), [e_0]), "rel": union_distinct(flatten(e1[**].rel), []), "mod": union_distinct(flatten(e1[**].mod), [])}
    
    Kieran van Gaalen's avatar
    Kieran van Gaalen committed
    LET nodes = union_distinct(flatten(result[**].nodes),[])
    LET edges = union_distinct(flatten(result[**].rel),[])
    RETURN {"vertices":nodes,"edges":edges}`)
    
    	regExCleaner := regexp.MustCompile(`\s+`)
    	correctCleanedResult := regExCleaner.ReplaceAllString(string(correctConvertedResult), " ")
    	convertedCleanedResult := regExCleaner.ReplaceAllString(*convertedResult, " ")
    	assert.Equal(t, correctCleanedResult, convertedCleanedResult)
    
    
    //TODO
    //FIX THESE TESTS, THEY'RE NOT THAT INTERESTING BUT SHOULD BE FIXED ANYWAY
    
    
    /*
    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(`{
    		"databaseName": "TweedeKamer",
    		"return": {
    			"entities": [
    				0,
    				1
    			]
    		},
    		"entities": [
    			{
    				"name": "parliament",
    				"ID": 0,
    				"constraints": []
    			},
    			{
    				"name": "parties",
    				"ID": 1,
    				"constraints": []
    			}
    		],		
    		"groupBys": [],		
    		"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 := `LET nodes = first(RETURN UNION_DISTINCT([],[]))
    	LET edges = first(RETURN UNION_DISTINCT([],[]))
    	RETURN {"vertices":nodes, "edges":edges }`
    	regExCleaner := regexp.MustCompile(`\s+`)
    	correctCleanedResult := regExCleaner.ReplaceAllString(string(correctConvertedResult), " ")
    	convertedCleanedResult := regExCleaner.ReplaceAllString(*convertedResult, " ")
    	assert.Equal(t, correctCleanedResult, convertedCleanedResult)
    }
    
    /*
    Tests an entity with a lower than -1 in a relation
    	t: *testing.T, makes go recognise this as a test
    */
    func TestIncorrectRelationFrom(t *testing.T) {
    	// Setup for test
    	// Create query conversion service
    	service := NewService()
    
    	query := []byte(`{
    		"databaseName": "TweedeKamer",
    		"return": {
    			"entities": [
    				0,
    				1
    			],
    			"relations": [
    				0
    			]
    		},
    		"entities": [
    			{
    				"name": "parliament",
    				"ID": 0,
    				"constraints": []
    			},
    			{
    				"name": "parties",
    				"ID": 1,
    				"constraints": []
    			}
    		],
    		"relations": [
    			{
    				"ID": 0,
    				"name": "member_of",
    				"depth": {
    					"min": 1,
    					"max": 1
    				},
    				"fromType": "entity",
    				"fromID": 0,
    				"toType": "entity",
    				"toID": -4,
    				"constraints":[]
    			}
    		],
    		"groupBys": [],		
    		"limit": 5000,
    		"modifiers": []
    	}`)
    
    	// Unmarshall the incoming message into an IncomingJSONQuery object
    	var JSONQuery entity.IncomingQueryJSON
    	json.Unmarshal(query, &JSONQuery)
    
    	_, err := service.ConvertQuery(&JSONQuery)
    
    	// Assert that there is an error
    	assert.Equal(t, errors.New("JSONQuery invalid"), err)
    }