Skip to content
Snippets Groups Projects
hierarchy_test.go 6.11 KiB
package aql

import (
	"encoding/json"
	"fmt"
	"strings"
	"testing"

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

func TestHierarchyBasic(t *testing.T) {
	// Setup for test
	// Create query conversion service
	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)
	// Get the hierarchy and turn it into JSON so we can turn it into strings later
	entityMap, relationMap, _ := entity.FixIndices(&JSONQuery)
	treeList, topNode := createHierarchy(&JSONQuery, entityMap, relationMap)
	jsonTopNode, err := json.Marshal(topNode)
	if err != nil {
		fmt.Println("Marshalling went wrong")
	}
	jsonTreeList, err := json.Marshal(treeList)
	if err != nil {
		fmt.Println("Marshalling went wrong")
	}
	// These are the expected (correct) outputs
	correctTopNode := []byte(`{"ID":0,"Name":"parliament","Constraints":[{"Attribute":"name","Value":"Geert","DataType":"string","MatchType":"CONTAINS","InID":0,"InType":""}]}`)
	correctTreeList := []byte(`[
		{
			"Self": {
				"FromNode": {
					"ID": 0,
					"Name": "parliament",
					"Constraints": [
						{
							"Attribute": "name",
							"Value": "Geert",
							"DataType": "string",
							"MatchType": "CONTAINS",
							"InID": 0,
							"InType": ""
						}
					]
				},
				"Rel": {
					"ID": 0,
					"Name": "part_of",
					"FromType": "entity",
					"FromID": 0,
					"ToType": "entity",
					"ToID": 1,
					"Depth": {
						"Min": 1,
						"Max": 1
					},
					"Constraints": []
				},
				"ToNode": {
					"ID": 1,
					"Name": "commissions",
					"Constraints": []
				}
			},
			"Parent": -1,
			"Children": [
				1
			]
		},
		{
			"Self": {
				"FromNode": {
					"ID": 2,
					"Name": "parliament",
					"Constraints": []
				},
				"Rel": {
					"ID": 1,
					"Name": "part_of",
					"FromType": "entity",
					"FromID": 2,
					"ToType": "entity",
					"ToID": 1,
					"Depth": {
						"Min": 1,
						"Max": 1
					},
					"Constraints": []
				},
				"ToNode": {
					"ID": 1,
					"Name": "commissions",
					"Constraints": []
				}
			},
			"Parent": 0,
			"Children": [
				2,
				3
			]
		},
		{
			"Self": {
				"FromNode": {
					"ID": 2,
					"Name": "parliament",
					"Constraints": []
				},
				"Rel": {
					"ID": 2,
					"Name": "member_of",
					"FromType": "entity",
					"FromID": 2,
					"ToType": "entity",
					"ToID": 3,
					"Depth": {
						"Min": 1,
						"Max": 1
					},
					"Constraints": []
				},
				"ToNode": {
					"ID": 3,
					"Name": "parties",
					"Constraints": [
						{
							"Attribute": "seats",
							"Value": "10",
							"DataType": "int",
							"MatchType": "LT",
							"InID": 0,
							"InType": ""
						}
					]
				}
			},
			"Parent": 1,
			"Children": []
		},
		{
			"Self": {
				"FromNode": {
					"ID": 2,
					"Name": "parliament",
					"Constraints": []
				},
				"Rel": {
					"ID": 3,
					"Name": "submits",
					"FromType": "entity",
					"FromID": 2,
					"ToType": "entity",
					"ToID": 4,
					"Depth": {
						"Min": 1,
						"Max": 1
					},
					"Constraints": []
				},
				"ToNode": {
					"ID": 4,
					"Name": "resolutions",
					"Constraints": [
						{
							"Attribute": "date",
							"Value": "mei",
							"DataType": "string",
							"MatchType": "CONTAINS",
							"InID": 0,
							"InType": ""
						}
					]
				}
			},
			"Parent": 1,
			"Children": []
		}
	]`)
	// Clean up the input and expected results
	cleanedTopNode := strings.ReplaceAll(string(jsonTopNode), "\n", "")
	cleanedTopNode = strings.ReplaceAll(cleanedTopNode, "\t", "")
	cleanedTopNode = strings.ReplaceAll(cleanedTopNode, " ", "")

	cleanedTreeList := strings.ReplaceAll(string(jsonTreeList), "\n", "")
	cleanedTreeList = strings.ReplaceAll(cleanedTreeList, "\t", "")
	cleanedTreeList = strings.ReplaceAll(cleanedTreeList, " ", "")

	cleanedCorrectTopNode := strings.ReplaceAll(string(correctTopNode), "\n", "")
	cleanedCorrectTopNode = strings.ReplaceAll(cleanedCorrectTopNode, "\t", "")
	cleanedCorrectTopNode = strings.ReplaceAll(cleanedCorrectTopNode, " ", "")

	cleanedCorrectTreeList := strings.ReplaceAll(string(correctTreeList), "\n", "")
	cleanedCorrectTreeList = strings.ReplaceAll(cleanedCorrectTreeList, "\t", "")
	cleanedCorrectTreeList = strings.ReplaceAll(cleanedCorrectTreeList, " ", "")

	assert.Equal(t, cleanedCorrectTopNode, cleanedTopNode)
	assert.Equal(t, cleanedCorrectTreeList, cleanedTreeList)
}