Skip to content
Snippets Groups Projects
convertQuery_test.go 48.70 KiB
/*
This program has been developed by students from the bachelor Computer Science at Utrecht University within the Software Project course.
© Copyright Utrecht University (Department of Information and Computing Sciences)
*/

package aql

import (
	"encoding/json"
	"errors"
	"regexp"
	"testing"

	"git.science.uu.nl/graphpolaris/query-conversion/entity"
	"github.com/stretchr/testify/assert"
)

/*
Tests an empty query
	t: *testing.T, makes go recognise this as a test
*/
func TestEmptyQueryConversion(t *testing.T) {
	// Setup for test
	// Create query conversion service
	service := NewService()

	query := []byte(`{
		"return": {
			"entities": [],
			"relations": []
		},
		"entities": [],
		"relations": [],
		"groupBys": [],
		"filters": [],
		"limit": 5000
	}`)

	// 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 two entities (two types) without a filter
Query description: Give me all parties connected to their respective parliament members
	t: *testing.T, makes go recognise this as a test
*/
func TestTwoEntitiesNoFilter(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": 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 := []byte(`LET result = (
        FOR e_0 IN parliament
        LET e1 = (
                FOR e_1 IN parties
                FOR r0 IN member_of
                FILTER r0._from == e_0._id AND r0._to == e_1._id
                FILTER length(e_1) != 0 AND length(r0) != 0
                RETURN {"nodes":union_distinct([e_1], []), "rel": union_distinct([r0], [])}
        )
        FILTER length(e1) != 0 AND length(e_0) != 0
        RETURN {"nodes":union_distinct(flatten(e1[**].nodes), [e_0]), "rel": union_distinct(flatten(e1[**].rel), [])}
)
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)
}

/*
Tests two entities (two types) with one entity filter
Query description: Give me all parties, with less than 10 seats, connected to their respective parliament members
	t: *testing.T, makes go recognise this as a test
*/
func TestTwoEntitiesOneEntityFilter(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": [
					{
						"attribute": "seats",
						"value": "10",
						"dataType": "int",
						"matchType": "LT"
					}
				]
			}
		],
		"relations": [
			{
				"ID": 0,
				"name": "member_of",
				"depth": {
					"min": 1,
					"max": 1
				},
				"fromType": "entity",
				"fromID": 0,
				"toType": "entity",
				"toID": 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 := []byte(`LET result = (
        FOR e_0 IN parliament
        LET e1 = (
                FOR e_1 IN parties
                FOR r0 IN member_of
                FILTER e_1.seats < 10
                FILTER r0._from == e_0._id AND r0._to == e_1._id
                FILTER length(e_1) != 0 AND length(r0) != 0
                RETURN {"nodes":union_distinct([e_1], []), "rel": union_distinct([r0], [])}
        )
        FILTER length(e1) != 0 AND length(e_0) != 0
        RETURN {"nodes":union_distinct(flatten(e1[**].nodes), [e_0]), "rel": union_distinct(flatten(e1[**].rel), [])}
)
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)
}

/*
Tests two entities (two types) with two entity filters
Query description: Give me all parties, with less than 10 seats, connected to their respective parliament members, who are more than 45 years old
	t: *testing.T, makes go recognise this as a test
*/
func TestTwoEntitiesTwoEntityFilters(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": [
					{
						"attribute": "age",
						"value": "45",
						"dataType": "int",
						"matchType": "GT"
					}
				]
			},
			{
				"name": "parties",
				"ID": 1,
				"constraints": [
					{
						"attribute": "seats",
						"value": "10",
						"dataType": "int",
						"matchType": "LT"
					}
				]
			}
		],
		"relations": [
			{
				"ID": 0,
				"name": "member_of",
				"depth": {
					"min": 1,
					"max": 1
				},
				"fromType": "entity",
				"fromID": 0,
				"toType": "entity",
				"toID": 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 := []byte(`LET result = (
        FOR e_0 IN parliament
        FILTER e_0.age > 45
        LET e1 = (
                FOR e_1 IN parties
                FOR r0 IN member_of
                FILTER e_1.seats < 10
                FILTER r0._from == e_0._id AND r0._to == e_1._id
                FILTER length(e_1) != 0 AND length(r0) != 0
                RETURN {"nodes":union_distinct([e_1], []), "rel": union_distinct([r0], [])}
        )
        FILTER length(e1) != 0 AND length(e_0) != 0
        RETURN {"nodes":union_distinct(flatten(e1[**].nodes), [e_0]), "rel": union_distinct(flatten(e1[**].rel), [])}
)
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)
}

/*
Tests three entities (three types) without a filter
Query description: Give me all parties, connected to their respective parliament members, who are then connected to the resolutions they submitted
	t: *testing.T, makes go recognise this as a test
*/
func TestThreeEntitiesNoFilter(t *testing.T) {
	// Setup for test
	// Create query conversion service
	service := NewService()

	query := []byte(`{
		"databaseName": "TweedeKamer",
		"return": {
			"entities": [
				0,
				1,
				2
			],
			"relations": [
				0,
				1
			]
		},
		"entities": [
			{
				"name": "parliament",
				"ID": 0,
				"constraints": []
			},
			{
				"name": "parties",
				"ID": 1,
				"constraints": []
			},
			{
				"name": "resolutions",
				"ID": 2,
				"constraints": []
			}
		],
		"relations": [
			{
				"ID": 0,
				"name": "member_of",
				"depth": {
					"min": 1,
					"max": 1
				},
				"fromType": "entity",
				"fromID": 0,
				"toType": "entity",
				"toID": 1,
				"constraints":[]
			},
			{
				"ID": 1,
				"name": "submits",
				"depth": {
					"min": 1,
					"max": 1
				},
				"fromType": "entity",
				"fromID": 0,
				"toType": "entity",
				"toID": 2,
				"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 := []byte(`LET result = (
        FOR e_1 IN parties
        LET e0 = (
                FOR e_0 IN parliament
                FOR r0 IN member_of
                FILTER r0._from == e_0._id AND r0._to == e_1._id
                LET e2 = (
                        FOR e_2 IN resolutions
                        FOR r1 IN submits
                        FILTER r1._from == e_0._id AND r1._to == e_2._id
                        FILTER length(e_2) != 0 AND length(r1) != 0
                        RETURN {"nodes":union_distinct([e_2], []), "rel": union_distinct([r1], [])}
                )
                FILTER length(e2) != 0 AND length(e_0) != 0 AND length(r0) != 0
                RETURN {"nodes":union_distinct(flatten(e2[**].nodes), [e_0]), "rel": union_distinct(flatten(e2[**].rel), [r0])}
        )
        FILTER length(e0) != 0 AND length(e_1) != 0
        RETURN {"nodes":union_distinct(flatten(e0[**].nodes), [e_1]), "rel": union_distinct(flatten(e0[**].rel), [])}
)
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)
}

/*
Tests three entities (three types) with one entity filter
Query description: Give me all parties, connected to their respective parliament members, whose name has "Geert" in it (this results in only "Geert Wilders"), who are/is then connected to the resolutions they submitted
	t: *testing.T, makes go recognise this as a test
*/
func TestThreeEntitiesOneEntityFilter(t *testing.T) {
	// Setup for test
	// Create query conversion service
	service := NewService()

	query := []byte(`{
		"databaseName": "TweedeKamer",
		"return": {
			"entities": [
				0,
				1,
				2
			],
			"relations": [
				0,
				1
			]
		},
		"entities": [
			{
				"name": "parliament",
				"ID": 0,				
				"constraints": [
					{
						"attribute": "name",
						"value": "Geert",
						"dataType": "string",
						"matchType": "contains"
					}
				]
			},
			{
				"name": "parties",
				"ID": 1,
				"constraints": []
			},
			{
				"name": "resolutions",
				"ID": 2,
				"constraints": []
			}
		],
		"relations": [
			{
				"ID": 0,
				"name": "member_of",
				"depth": {
					"min": 1,
					"max": 1
				},
				"fromType": "entity",
				"fromID": 0,
				"toType": "entity",
				"toID": 1,
				"constraints":[]
			},
			{
				"ID": 1,
				"name": "submits",
				"depth": {
					"min": 1,
					"max": 1
				},
				"fromType": "entity",
				"fromID": 0,
				"toType": "entity",
				"toID": 2,
				"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 := []byte(`LET result = (
        FOR e_1 IN parties
        LET e0 = (
                FOR e_0 IN parliament
                FOR r0 IN member_of
                FILTER e_0.name LIKE "%Geert%"
                FILTER r0._from == e_0._id AND r0._to == e_1._id
                LET e2 = (
                        FOR e_2 IN resolutions
                        FOR r1 IN submits
                        FILTER r1._from == e_0._id AND r1._to == e_2._id
                        FILTER length(e_2) != 0 AND length(r1) != 0
                        RETURN {"nodes":union_distinct([e_2], []), "rel": union_distinct([r1], [])}
                )
                FILTER length(e2) != 0 AND length(e_0) != 0 AND length(r0) != 0
                RETURN {"nodes":union_distinct(flatten(e2[**].nodes), [e_0]), "rel": union_distinct(flatten(e2[**].rel), [r0])}
        )
        FILTER length(e0) != 0 AND length(e_1) != 0
        RETURN {"nodes":union_distinct(flatten(e0[**].nodes), [e_1]), "rel": union_distinct(flatten(e0[**].rel), [])}
)
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)
}

/*
Tests three entities (three types) with two entity filters
Query description: Give me all parties, connected to their respective parliament members, whose name has "Geert" in it (this results in only "Geert Wilders"), who are/is then connected to the resolutions they submitted, but only those submitted in May
	t: *testing.T, makes go recognise this as a test
*/
func TestThreeEntitiesTwoEntityFilters(t *testing.T) {
	// Setup for test
	// Create query conversion service
	service := NewService()

	query := []byte(`{
		"databaseName": "TweedeKamer",
		"return": {
			"entities": [
				0,
				1,
				2
			],
			"relations": [
				0,
				1
			]
		},
		"entities": [
			{
				"name": "parliament",
				"ID": 0,				
				"constraints": [
					{
						"attribute": "name",
						"value": "Geert",
						"dataType": "string",
						"matchType": "contains"
					}
				]
			},
			{
				"name": "parties",
				"ID": 1,
				"constraints": []
			},
			{
				"name": "resolutions",
				"ID": 2,
				"constraints": [
					{
						"attribute": "date",
						"value": "mei",
						"dataType": "string",
						"matchType": "contains"
					}
				]
			}
		],
		"relations": [
			{
				"ID": 0,
				"name": "member_of",
				"depth": {
					"min": 1,
					"max": 1
				},
				"fromType": "entity",
				"fromID": 0,
				"toType": "entity",
				"toID": 1,
				"constraints":[]
			},
			{
				"ID": 1,
				"name": "submits",
				"depth": {
					"min": 1,
					"max": 1
				},
				"fromType": "entity",
				"fromID": 0,
				"toType": "entity",
				"toID": 2,
				"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 := []byte(`LET result = (
        FOR e_1 IN parties
        LET e0 = (
                FOR e_0 IN parliament
                FOR r0 IN member_of
                FILTER e_0.name LIKE "%Geert%"
                FILTER r0._from == e_0._id AND r0._to == e_1._id
                LET e2 = (
                        FOR e_2 IN resolutions
                        FOR r1 IN submits
                        FILTER e_2.date LIKE "%mei%"
                        FILTER r1._from == e_0._id AND r1._to == e_2._id
                        FILTER length(e_2) != 0 AND length(r1) != 0
                        RETURN {"nodes":union_distinct([e_2], []), "rel": union_distinct([r1], [])}
                )
                FILTER length(e2) != 0 AND length(e_0) != 0 AND length(r0) != 0
                RETURN {"nodes":union_distinct(flatten(e2[**].nodes), [e_0]), "rel": union_distinct(flatten(e2[**].rel), [r0])}
        )
        FILTER length(e0) != 0 AND length(e_1) != 0
        RETURN {"nodes":union_distinct(flatten(e0[**].nodes), [e_1]), "rel": union_distinct(flatten(e0[**].rel), [])}
)
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)
}

/*
Tests five entities (three types) with four entity filters
Query description: Give me all parties, which have less than 10 seats, connected to their respective parliament members, whose name has "A" in it, who are/is then connected to the resolutions they submitted, but only those submitted in May, which are then also connected to all other persons who were part of that submission, who are part of the "VVD"
Translator's note: This returns a member of the PvdA who submitted a motion alongside a member of the VVD
hmmmmmmmmmmmmmmmmmmmmmmmmmmyo+/oooooooooooooooo+/+oymmmmmmmmmhso++/+++oo+++/++ohmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
hmmmmmmmmmmmmmmmmmmmmmmmho/+ooooooooooooooooooooooo+/oymdyo+/+ooooooooooooooooo++ymmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
hmmmmmmmmmmmmmmmmmmmmmh++ooooooooooooooooooooooooooooo+/:+ooooooooooooooooooooooo++dmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
hmmmmmmmmmmmmmmmmmmmdo/ooooooooooooooooooooooooooooooooo//oooooooooooooooooooooooo+/dmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
hmmmmmmmmmmmmmmmmmmy/+ooooooooooooo++////////////++oooooo//ooooooooooooooooooooooooo/dmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
hmmmmmmmmmmmmmmmmmo/ooooooooooo+///++++oooooooo++++///++oo:+oooooooooooooooooooooooo++mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
hmmmmmmmmmmmmmmmd++ooooooooo+//++oooooooooooooooooooo++////:+++////////////////////++:ydmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
hmmmmmmmmmmmmmmd/+oooooooooo++ooooooooooooooooooooooooooo+/:/++ooooooooooooooooo+o+++++++oshmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
hmmmmmmmmmmmmmd/+oooooooooooooooooooooooooooooo+++++++++++++//+ooooooooooooooooooooooooooo+++ohmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
hmmmmmmmmmmmmm++oooooooooooooooooooooooo+++////++++++++++++++/://+ooooo+++++////////////////++//mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
hmmmmmmmmmmmms/oooooooooooooooooooooo++//+++++//////////////++++++:++///++/++++///////////////+/+ohmmmmNmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
hmmmmmmmdhs++:ooooooooooooooooooo+++//+++////+++++sosysssso+++++//://////++++++++//////+o+++++///+/smmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
hmmmmmms+++o+/ooooooooooooooo+///+++++//+osyy+:+:...-:yNMMNmddyso++/++ooooooyho--o-````-+dmddyo+++//ymmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
hmmmmd++oooo:+oooooooooooo+++++++////+oydNMy. `s: `:-` :mMMMMMMMMNmy/oooydNMMo` .o- .+:` -mMMMNdhhso/ommmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
hmmmh/oooooo:ooooooooooooo//+++++oooymMMMMN.   .. :dh.  +MMMMMMMMMMMoodNMMMMN`   -. :hs`  oMMMMMMMMNyommmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
hmdy/ooooooo/ooooooooooooo+//////:yNMMMMMMm`   :-  `    :MMMMMMMMMMNsMMMMMMMd    -.       /MMMMMMMMMN+mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
hmy/ooooooooooooooooooooooooooooo++smNMMMMN.           `sMMMMMMMMMNsmMMMMMMMm.           .hMMMMMMNmhshmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
hh/ooooooooooooooooooooooooooooooo+o++oymNNd:`       `:hMMMMMNNmyo+:sdmNNNNMMd:``      `+dNmmhysoo++mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
y/oooooooooooooooooooooooooooooooo///+o+++osys+/://+shdddysoo+++oo+:++++++ooosso/-----/++/++++o++oydmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
-+ooooooooooooooooooooooooooooooooooo+////+++++++////++++oooo+/////ooooooooooooooooooooooooooo+ommmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
/ooooooooooooooooooooooooooooooooooooooooo++/////////////////+oooooooooooooooooooooooooooooo++ymmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
/ooooooooooooooooooooooooooooooooooooooooooooooooooooooo+/+//ooooooooo+/+oooooooooooooo+/ossdmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
/oooooooooooooooooooooooooooooooooooooooooooooooooo++///+oooooooooooooo+////:////////////ymmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
/oooooooooooooooooooooooooooooooooooooooooooooo+///+oooooooooooooooooooooooo+/+oooooooooo++ymmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
/oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo++dmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
/ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo+/dmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
/oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo+/mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
/ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo/ymmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
/ooooooooooooooooooooooooooooooo++++++++++ooooooooooooooooooooooooooooooooooooooooooooooooooooo/mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
/oooooooooooooooooooooooooooo+//++++++++///////++++++ooooooooooooooooooooooooooooooooooooo+++///+smmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
/oooooooooooooooooooooooooo+/++oooooooooooooo++++++////////+++++++oooooooooooooooo+++++////+++oo+:mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
/ooooooooooooooooooooooooo+:+oooo+///////++++++ooooooooo++++++++///////////////////++++++ooooo+++ymmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
/ooooooooooooooooooooooooo:+ooooo++++++++++++//////++++++++ooooooooooooooooooooooooooooo++++++oydmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
/ooooooooooooooooooooooooo+/++++oooooooooooooooo++++++++///////////+/++++++++++////////////+:smmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
/ooooooooooooooooooooo/+ooo+///////////////+++++++oooooooooooo+++++++++++++++++++++++ooooooo+/mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
/+ooooooooooooooooooooo//ooooooooooooooooo++++++/////++++++++++++++oooooooooooooooooooooo++++hmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
y++ooooooooooooooooooooo+//+++ooooooooooooooooooooooo++++////////////////////////++//++++osymmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
yd//++ooooooooooooooooooooo+++oooooooooooooooooooooooooooooooooooooooooooo++++++++//sddmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
::::////++oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo+++ohmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
:/:::://+/////+oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo+++oymmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
://////::://+++///////+++ooooooooooooooooooooooooooooooooooooooooooooo++++sydmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
://////////::::/++ooo+++////////////++++++ooooooooooooooooo+++++/////shdmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
-///////////////::::://+++oooooooooo+++++////////////////////+++/::::odmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm

	t: *testing.T, makes go recognise this as a test
*/
func TestFiveEntitiesFourEntityFilters(t *testing.T) {
	// Setup for test
	// Create query conversion service
	service := NewService()

	query := []byte(`{
		"databaseName": "TweedeKamer",
		"return": {
			"entities": [
				0,
				1,
				2,
				3,
				4
			],
			"relations": [
				0,
				1,
				2,
				3
			]
		},
		"entities": [
			{
				"name": "parliament",
				"ID": 0,
				"constraints": [
					{
						"attribute": "name",
						"value": "A",
						"dataType": "string",
						"matchType": "contains"
					}
				]
			},
			{
				"name": "parties",
				"ID": 1,
				"constraints": [
					{
						"attribute": "seats",
						"value": "10",
						"dataType": "int",
						"matchType": "LT"
					}
				]
			},
			{
				"name": "resolutions",
				"ID": 2,
				"constraints": [
					{
						"attribute": "date",
						"value": "mei",
						"dataType": "string",
						"matchType": "contains"
					}
				]
			},
			{
				"name": "parliament",
				"ID": 3,
				"constraints":[]
			},
			{
				"name": "parties",
				"ID": 4,
				"constraints": [
					{
						"attribute": "name",
						"value": "Volkspartij voor Vrijheid en Democratie",
						"dataType": "string",
						"matchType": "=="
					}
				]
			}
		],
		"relations": [
			{
				"ID": 0,
				"name": "member_of",
				"depth": {
					"min": 1,
					"max": 1
				},
				"fromType": "entity",
				"fromID": 0,
				"toType": "entity",
				"toID": 1,
				"constraints": []
			},
			{
				"ID": 1,
				"name": "submits",
				"depth": {
					"min": 1,
					"max": 1
				},
				"fromType": "entity",
				"fromID": 0,
				"toType": "entity",
				"toID": 2,
				"constraints": []
			},
			{
				"ID": 2,
				"name": "submits",
				"depth": {
					"min": 1,
					"max": 1
				},
				"fromType": "entity",
				"fromID": 3,
				"toType": "entity",
				"toID": 2,
				"constraints": []
			},
			{
				"ID": 3,
				"name": "member_of",
				"depth": {
					"min": 1,
					"max": 1
				},
				"fromType": "entity",
				"fromID": 3,
				"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)

	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_1 IN parties
        FILTER e_1.seats < 10
        LET e0 = (
                FOR e_0 IN parliament
                FOR r0 IN member_of
                FILTER e_0.name LIKE "%A%"
                FILTER r0._from == e_0._id AND r0._to == e_1._id
                LET e2 = (
                        FOR e_2 IN resolutions
                        FOR r1 IN submits
                        FILTER e_2.date LIKE "%mei%"
                        FILTER r1._from == e_0._id AND r1._to == e_2._id
                        LET e3 = (
                                FOR e_3 IN parliament
                                FOR r2 IN submits
                                FILTER r2._from == e_3._id AND r2._to == e_2._id
                                LET e4 = (
                                        FOR e_4 IN parties
                                        FOR r3 IN member_of
                                        FILTER e_4.name == "Volkspartij voor Vrijheid en Democratie"
                                        FILTER r3._from == e_3._id AND r3._to == e_4._id
                                        FILTER length(e_4) != 0 AND length(r3) != 0
                                        RETURN {"nodes":union_distinct([e_4], []), "rel": union_distinct([r3], [])}
                                )
                                FILTER length(e4) != 0 AND length(e_3) != 0 AND length(r2) != 0
                                RETURN {"nodes":union_distinct(flatten(e4[**].nodes), [e_3]), "rel": union_distinct(flatten(e4[**].rel), [r2])}
                        )
                        FILTER length(e3) != 0 AND length(e_2) != 0 AND length(r1) != 0
                        RETURN {"nodes":union_distinct(flatten(e3[**].nodes), [e_2]), "rel": union_distinct(flatten(e3[**].rel), [r1])}
                )
                FILTER length(e2) != 0 AND length(e_0) != 0 AND length(r0) != 0
                RETURN {"nodes":union_distinct(flatten(e2[**].nodes), [e_0]), "rel": union_distinct(flatten(e2[**].rel), [r0])}
        )
        FILTER length(e0) != 0 AND length(e_1) != 0
        RETURN {"nodes":union_distinct(flatten(e0[**].nodes), [e_1]), "rel": union_distinct(flatten(e0[**].rel), [])}
)
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)
}

/*
Tests five entities (four types) with three entity filters and one junction
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")
	t: *testing.T, makes go recognise this as a test
*/
func TestSingleJunctionFiveEntitiesThreeEntityFilters(t *testing.T) {
	// Setup for test
	// Create query conversion service
	service := NewService()

	query := []byte(`{
		"return": {
			"entities": [
				0,
				1,
				2,
				3,
				4
			],
			"relations": [
				0,
				1,
				2,
				3
			]
		},
		"entities": [
			{
				"name": "parliament",
				"ID": 0,
				"constraints": [
					{
						"attribute": "name",
						"value": "Geert",
						"dataType": "string",
						"matchType": "contains"
					}
				]
			},
			{
				"name": "commissions",
				"ID": 1,
				"constraints": []
			},
			{
				"name": "parliament",
				"ID": 2,
				"constraints": []
			},
			{
				"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"
					}
				]
			}
			
		],
		"groupBys": [],
		"relations": [
			{
				"ID": 0,
				"name": "part_of",
				"depth": {
					"min": 1,
					"max": 1
				},
				"fromType": "entity",
				"fromId": 0,
				"toType": "entity",
				"toID": 1,
				"constraints": []
			},
			{
				"ID": 1,
				"name": "part_of",
				"depth": {
					"min": 1,
					"max": 1
				},
				"fromType": "entity",
				"fromId": 2,
				"toType": "entity",
				"toID": 1,
				"constraints": []
			},
			{
				"ID": 2,
				"name": "member_of",
				"depth": {
					"min": 1,
					"max": 1
				},
				"fromType": "entity",
				"fromId": 2,
				"toType": "entity",
				"toID": 3,
				"constraints": []
			},
			{
				"ID": 3,
				"name": "submits",
				"depth": {
					"min": 1,
					"max": 1
				},
				"fromType": "entity",
				"fromId": 2,
				"toType": "entity",
				"toID": 4,
				"constraints": []
			}
		],
		"limit": 5000
	}`)

	// 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
                                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
                                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
                        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), [])}
                )
                FILTER length(e2) != 0 AND length(e_1) != 0 AND length(r0) != 0
                RETURN {"nodes":union_distinct(flatten(e2[**].nodes), [e_1]), "rel": union_distinct(flatten(e2[**].rel), [r0]), "mod": union_distinct(flatten(e2[**].mod), [])}
        )
        FILTER length(e1) != 0 AND length(e_0) != 0
        RETURN {"nodes":union_distinct(flatten(e1[**].nodes), [e_0]), "rel": union_distinct(flatten(e1[**].rel), []), "mod": union_distinct(flatten(e1[**].mod), [])}
)
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)
}

/*
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
	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(`{
		"return": {
			"entities": [
				0,
				1,
				2,
				3,
				4,
				5,
				6,
				7
			],
			"relations": [
				0,
				1,
				2,
				3,
				4,
				5,
				6
			]
		},
		"entities": [
			{
				"name": "parliament",
				"ID": 0,
				"constraints": [
					{
						"attribute": "name",
						"value": "Geert",
						"dataType": "string",
						"matchType": "contains"
					}
				]
			},
			{
				"name": "commissions",
				"ID": 1,
				"constraints": []
			},
			{
				"name": "parliament",
				"ID": 2,
				"constraints": []
			},
			{
				"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": []
			}
			,
			{
				"name": "parliament",
				"ID": 7,
				"constraints": []
			}
			
		],
		"groupBys": [],
		"relations": [
			{
				"ID": 0,
				"name": "part_of",
				"depth": {
					"min": 1,
					"max": 1
				},
				"fromType": "entity",
				"fromId": 0,
				"toType": "entity",
				"toID": 1,
				"constraints": []
			},
			{
				"ID": 1,
				"name": "part_of",
				"depth": {
					"min": 1,
					"max": 1
				},
				"fromType": "entity",
				"fromId": 2,
				"toType": "entity",
				"toID": 1,
				"constraints": []
			},
			{
				"ID": 2,
				"name": "member_of",
				"depth": {
					"min": 1,
					"max": 1
				},
				"fromType": "entity",
				"fromId": 2,
				"toType": "entity",
				"toID": 3,
				"constraints": []
			},
			{
				"ID": 3,
				"name": "submits",
				"depth": {
					"min": 1,
					"max": 1
				},
				"fromType": "entity",
				"fromId": 2,
				"toType": "entity",
				"toID": 4,
				"constraints": []
			},
			{
				"ID": 4,
				"name": "submits",
				"depth": {
					"min": 1,
					"max": 1
				},
				"fromType": "entity",
				"fromId": 0,
				"toType": "entity",
				"toID": 5,
				"constraints": []
			},
			{
				"ID": 5,
				"name": "member_of",
				"depth": {
					"min": 1,
					"max": 1
				},
				"fromType": "entity",
				"fromId": 0,
				"toType": "entity",
				"toID": 6,
				"constraints": []
			}
			,
			{
				"ID": 6,
				"name": "member_of",
				"depth": {
					"min": 1,
					"max": 1
				},
				"fromType": "entity",
				"fromId": 7,
				"toType": "entity",
				"toID": 6,
				"constraints": []
			}
		],
		"limit": 5000
	}`)

	// 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
                                        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
                                                RETURN {"nodes":union_distinct([e_7], []), "rel": union_distinct([r6], []), "mod": union_distinct([], [])}
                                        )
                                        FILTER length(e7) != 0 AND length(e_6) != 0 AND length(r5) != 0
                                        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
                                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), [])}
                        )
                        FILTER length(e0) != 0 AND length(e_1) != 0 AND length(r1) != 0
                        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
                        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
                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), [])}
        )
        FILTER length(e2) != 0 AND length(e_3) != 0
        RETURN {"nodes":union_distinct(flatten(e2[**].nodes), [e_3]), "rel": union_distinct(flatten(e2[**].rel), []), "mod": union_distinct(flatten(e2[**].mod), [])}
)
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)
}

/*
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
	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(`{
		"return": {
			"entities": [
				0,
				1
			],
			"relations": [
				0
			]
		},
		"entities": [
			{
				"ID": 0,
				"name": "airports",
				"constraints": [
					{
						"attribute": "state",
						"value": "HI",
						"dataType": "string",
						"matchType": "exact"
					}
				]
			},
			{
				"ID": 1,
				"name": "airports",
				"constraints":[]
			}
		],
		"relations": [
			{
				"ID": 0,
				"name": "flights",
				"depth": {
					"min": 1,
					"max": 1
				},
				"FromType": "entity",
				"fromID": 0,
				"ToType": "entity",
				"toID": 1,
				"constraints": [
					{	
						"attribute": "Day",
						"value": "15",
						"dataType": "int",
						"matchType": "EQ",
						"inType": "",
						"inID": -1
					}
				]
			}
		],
		"groupBys": [],
		"limit": 5000
	}`)

	// 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 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
                RETURN {"nodes":union_distinct([e_1], []), "rel": union_distinct([r0], []), "mod": union_distinct([], [])}
        )
        FILTER length(e1) != 0 AND length(e_0) != 0
        RETURN {"nodes":union_distinct(flatten(e1[**].nodes), [e_0]), "rel": union_distinct(flatten(e1[**].rel), []), "mod": union_distinct(flatten(e1[**].mod), [])}
)
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)
}