package aql import ( "encoding/json" "fmt" "sort" "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 ], "relations": [ 0 ], "groupBys": [] }, "entities": [ { "name": "parliament", "ID": 0 }, { "name": "parties", "ID": 1 } ], "relations": [ { "ID": 0, "name": "member_of", "depth": { "min": 1, "max": 1 }, "fromType": "entity", "fromID": 0, "toType": "entity", "toID": 1 } ], "groupBys": [], "filters": [ { "ID": 0, "filteredType": "entity", "filteredID": 0, "attribute": "age", "dataType": "int", "matchType": "NEQ", "value": "40", "inType": "", "inID": 0 }, { "ID": 1, "filteredType": "relation", "filteredID": 0, "attribute": "isChairman", "dataType": "bool", "matchType": "exact", "value": "true", "inType": "", "inID": 0 } ], "limit": 5000 } `) // Unmarshall the incoming message into an IncomingJSONQuery object var JSONQuery entity.IncomingQueryJSON json.Unmarshal(query, &JSONQuery) search(&JSONQuery, 0) // Assert that the result and the expected result are the same correctResult := `[[{entity 0} {entity 1}] [{filter 0}] [{relation 0}] [{filter 1}]]` assert.Equal(t, correctResult, fmt.Sprint(listoflists)) } func TestHierarchyRandomStart(t *testing.T) { // Setup for test // Create query conversion service query := []byte(`{ "return": { "entities": [ 0, 1 ], "relations": [ 0 ], "groupBys": [] }, "entities": [ { "name": "parties", "ID": 1 }, { "name": "parliament", "ID": 0 } ], "relations": [ { "ID": 0, "name": "member_of", "depth": { "min": 1, "max": 1 }, "fromType": "entity", "fromID": 0, "toType": "entity", "toID": 1 } ], "groupBys": [], "filters": [ { "ID": 0, "filteredType": "entity", "filteredID": 0, "attribute": "age", "dataType": "int", "matchType": "NEQ", "value": "40", "inType": "", "inID": 0 }, { "ID": 1, "filteredType": "relation", "filteredID": 0, "attribute": "isChairman", "dataType": "bool", "matchType": "exact", "value": "true", "inType": "", "inID": 0 } ], "limit": 5000 } `) // Unmarshall the incoming message into an IncomingJSONQuery object var JSONQuery entity.IncomingQueryJSON json.Unmarshal(query, &JSONQuery) correctResult := make([]pdictList, 4) correctResult[0] = pdictList{{typename: "entity", pointer: 0}, {typename: "entity", pointer: 1}} correctResult[1] = pdictList{{typename: "filter", pointer: 0}} correctResult[2] = pdictList{{typename: "relation", pointer: 0}} correctResult[3] = pdictList{{typename: "filter", pointer: 1}} for i := range JSONQuery.Entities { search(&JSONQuery, i) sortedListOfLists := make([]pdictList, len(listoflists)) for i, list := range listoflists { k := make(pdictList, list.Len()) copy(k, list) sort.Sort(k) sortedListOfLists[i] = k } assert.Equal(t, fmt.Sprint(correctResult), fmt.Sprint(sortedListOfLists)) } } func TestHierarchyWithGroupby(t *testing.T) { // Setup for test // Create query conversion service query := []byte(`{ "return": { "entities": [ 0, 1, 2, 3 ], "relations": [ 0, 1, 2 ] }, "entities": [ { "ID": 0, "name": "parliament" }, { "ID": 1, "name": "commissions" }, { "ID": 2, "name": "parliament" }, { "ID": 3, "name": "resolutions" } ], "relations": [ { "type": "part_of", "depth": { "min": 1, "max": 1 }, "fromType": "entity", "fromId": 0, "toType": "entity", "toID": 1 }, { "type": "part_of", "depth": { "min": 1, "max": 1 }, "fromType": "groupBy", "fromID": 0, "toType": "entity", "toID": 2 }, { "type": "submits", "depth": { "min": 1, "max": 1 }, "fromType": "entity", "fromID": 2, "toType": "entity", "toID": 3 } ], "groupBys": [ { "ID": 0, "groupType": "entity", "groupID": 0, "groupAttribute": "age", "byType": "entity", "byID": 1, "byAttribute": "name", "appliedModifier": "AVG", "relationID": 0, "constraints": [ { "attribute": "age", "value": "45", "dataType": "number", "matchType": "GT", "functionPointer": { "from": -1, "to": -1 } } ] } ], "filters": [ { "ID": 0, "fromType": "groupBy", "fromID": 0, "toType": "relation", "toID": 1, "attribute": "age", "value": "45", "dataType": "number", "matchType": "GT", "inType": "", "inID": -1 } ], "limit": 5000, "modifiers": [], "databaseName": "TweedeKamer" } `) // Unmarshall the incoming message into an IncomingJSONQuery object var JSONQuery entity.IncomingQueryJSON json.Unmarshal(query, &JSONQuery) correctResult := make([]pdictList, 5) correctResult[0] = pdictList{{typename: "entity", pointer: 0}, {typename: "entity", pointer: 1}} correctResult[1] = pdictList{{typename: "relation", pointer: 0}} correctResult[2] = pdictList{{typename: "entity", pointer: 2}, {typename: "entity", pointer: 3}, {typename: "groupBy", pointer: 0}} correctResult[3] = pdictList{{typename: "filter", pointer: 0}} correctResult[4] = pdictList{{typename: "relation", pointer: 1}, {typename: "relation", pointer: 2}} for i := range JSONQuery.Entities { search(&JSONQuery, i) sortedListOfLists := make([]pdictList, len(listoflists)) for i, list := range listoflists { k := make(pdictList, list.Len()) copy(k, list) sort.Sort(k) sortedListOfLists[i] = k } assert.Equal(t, fmt.Sprint(correctResult), fmt.Sprint(sortedListOfLists)) } }