Newer
Older
/*
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)
*/
"errors"
"strings"
"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": [],
// 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 }`
assert.Equal(t, correctConvertedResult, *convertedResult)
}
/*
Tests multiple entity types
t: *testing.T, makes go recognise this as a test
*/
func TestMultipleEntityTypes(t *testing.T) {
// Setup for test
// Create query conversion service
service := NewService()
query := []byte(`{
"databaseName": "test",
"return": {
"entities": [
0,
1
],
"relations": [
0
]
},
"entities": [
{
"type": "kamerleden",
"ID": 0
},
{
"type": "partijen",
"ID": 1
}
],
"relations": [
{
"ID": 0
"type": "lid_van",
"depth": {
"min": 1,
"max": 1
},
"fromType": "entity",
"fromID": 0,
"toType": "entity",
"toID": 1
}
Geurtjens,D. (Douwe Geurtjens)
committed
"filters": [
{
"ID": 0,
"fromType": "entity",
"fromID": 0,
"toType": "relation",
"toID": 0,
"attribute": "partij",
"value": "GL",
"dataType": "string",
"matchType": "exact",
"inType": "",
"inID": -1
Geurtjens,D. (Douwe Geurtjens)
committed
},
{
"ID": 1,
"fromType": "entity",
"fromID": 1,
"toType": "relation",
"toID": 0,
Geurtjens,D. (Douwe Geurtjens)
committed
"attribute": "zetels",
"value": "6",
"dataType": "int",
"matchType": "GT",
"inType": "",
"inID": -1
}
"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 partijenLET n0 = (FOR x IN kamerleden FILTER x.partij == "GL" RETURN x)LET r0 = (FOR x IN n0 FOR v, e, p IN 1..1 OUTBOUND x lid_van OPTIONS { uniqueEdges: "path" }FILTER v.zetels > 6 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(correctConvertedResult, "\n", "")
//cleanedResult = strings.ReplaceAll(cleanedResult, "\t", "")
convertedCleanedResult := strings.ReplaceAll(*convertedResult, "\n", "")
convertedCleanedResult = strings.ReplaceAll(convertedCleanedResult, "\t", "")
assert.Equal(t, correctConvertedResult, convertedCleanedResult)
/*
Tests a query with one attribute
t: *testing.T, makes go recognise this as a test
*/
func TestEntityOneAttributeQuery(t *testing.T) {
// Setup for test
// Create query conversion service
service := NewService()
query := []byte(`{
"return": {
"entities": [
0
],
"relations": []
},
"entities": [
{
"type": "airports",
"filters": [
{
"ID": 0,
"fromType": "entity",
"fromID": 0,
"toType": "",
"toID": -1,
"attribute": "state",
"value": "HI",
"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)
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.state == "HI" 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)
}
/*
Test a relation with a constraint
t: *testing.T, makes go recognise this as a test
*/
func TestRelationWithConstraint(t *testing.T) {
// Setup for test
// Create query conversion service
service := NewService()
query := []byte(`{
"return": {
"entities": [
0
],
"relations": [
0
]
},
"entities": [
{
"type": "airports",
"constraints": [
{
"attribute": "state",
"value": "HI",
"matchType": "exact"
}
]
}
],
"relations": [
{
"type": "flights",
"depth": {
"min": 1,
"max": 1
},
"FromType": "entity",
"fromID": 0,
"ToType": "",
"toID": -1,
"groupBys": [],
"filters": [
{
"ID": 0,
"fromType": "entity",
"fromID": 0,
"toType": "relation",
"toID": 0,
"attribute": "Day",
"value": "15",
"dataType": "int",
"matchType": "EQ",
"inType": "",
"inID": -1
}
],
"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 n0 = (FOR x IN airports FILTER x.state == "HI" RETURN x)LET r0 = (FOR x IN n0 FOR v, e, p IN 1..1 OUTBOUND x flights OPTIONS { uniqueEdges: "path" }FILTER p.edges[*].Day ALL == 15 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)
}
/*
Tests the count modifier
t: *testing.T, makes go recognise this as a test
*/
func TestModifierCountEntity(t *testing.T) {
// Setup for test
// Create query conversion service
service := NewService()
query := []byte(`{
"return": {
"entities": [
0
],
"relations": []
},
"entities": [
{
"type": "airports",
"constraints": [
]
}
],
"relations": [],
"groupBys": [],
"filters": [
{
"ID": 0,
"fromType": "entity",
"fromID": 0,
"toType": "relation",
"toID": 0,
"attribute": "state",
"value": "HI",
"dataType": "string",
"matchType": "exact",
"inType": "",
"inID": -1
}
],
"limit": 5000,
"modifiers": [
{
"type": "COUNT",
"selectedType": "entity",
"id": 0,
"attributeIndex": -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.state == "HI" RETURN x)RETURN LENGTH (n0)`
cleanedResult := strings.ReplaceAll(*convertedResult, "\n", "")
cleanedResult = strings.ReplaceAll(cleanedResult, "\t", "")
assert.Equal(t, correctConvertedResult, cleanedResult)
}
/*
Tests the count modifer with an attribute
t: *testing.T, makes go recognise this as a test
*/
func TestModifierCountEntityAttribute(t *testing.T) {
// Setup for test
// Create query conversion service
service := NewService()
query := []byte(`{
"return": {
"entities": [
0
],
"relations": []
},
"entities": [
{
"ID": 0,
"type": "airports"
}
],
"relations": [],
"groupBys": [],
"filters": [
{
"ID": 0,
"fromType": "entity",
"fromID": 0,
"toType": "",
"toID": -1,
"attribute": "state",
"value": "HI",
"dataType": "string",
"matchType": "exact",
"inType": "",
"inID": -1
}
],
"limit": 5000,
"modifiers": [
{
"type": "SUM",
"selectedType": "entity",
"id": 0,
"attributeIndex": 0
}
]
}`)
// 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.state == "HI" RETURN x)RETURN SUM (n0[*].state)`
cleanedResult := strings.ReplaceAll(*convertedResult, "\n", "")
cleanedResult = strings.ReplaceAll(cleanedResult, "\t", "")
assert.Equal(t, correctConvertedResult, cleanedResult)
}
/*
Tests the count modifier on a relation
t: *testing.T, makes go recognise this as a test
*/
func TestModifierCountRelation(t *testing.T) {
// Setup for test
// Create query conversion service
service := NewService()
query := []byte(`{
"return": {
"entities": [
0
],
"relations": [
0
]
},
"entities": [
{
"ID": 0,
"type": "airports"
}
],
"relations": [
{
"type": "flights",
"depth": {
"min": 1,
"max": 1
},
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
"fromType": "entity",
"fromID": 0,
"toType": "",
"toID": -1
}
],
"groupBys": [],
"filters": [
{
"ID": 0,
"fromType": "entity",
"fromID": 0,
"toType": "relation",
"toID": 0,
"attribute": "state",
"value": "HI",
"dataType": "string",
"matchType": "exact",
"inType": "",
"inID": -1
},
{
"ID": 1,
"fromType": "relation",
"fromID": 0,
"toType": "entity",
"toID": 0,
"attribute": "Day",
"value": "15",
"dataType": "int",
"matchType": "EQ",
"inType": "",
"inID": -1
}
],
"limit": 5000,
"modifiers": [
{
"type": "COUNT",
"selectedType": "relation",
"id": 0,
"attributeIndex": -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.state == "HI" RETURN x)LET r0 = (FOR x IN n0 FOR v, e, p IN 1..1 OUTBOUND x flights OPTIONS { uniqueEdges: "path" }FILTER p.edges[*].Day ALL == 15 RETURN DISTINCT p )RETURN LENGTH (unique(r0[*].edges[**]))`
cleanedResult := strings.ReplaceAll(*convertedResult, "\n", "")
cleanedResult = strings.ReplaceAll(cleanedResult, "\t", "")
assert.Equal(t, correctConvertedResult, cleanedResult)
}
/*
Tests the count modifier with an entity swap
t: *testing.T, makes go recognise this as a test
*/
func TestModifierCountEntitySwap(t *testing.T) {
// Setup for test
// Create query conversion service
service := NewService()
query := []byte(`{
"databaseName": "TweedeKamer",
"return": {
"entities": [
0,
1
],
"relations": [
0
]
{
"ID": 0,
"type": "partijen"
},
{
"ID": 1,
"type": "kamerleden"
}
{
"ID": 0,
"type": "lid_van",
"depth": {
"min": 1,
"max": 1
},
"fromType": "entity",
"fromID": 1,
"toType": "entity",
"toID": 0
}
"groupBys": [],
"filters": [],
"limit": 5000,
"modifiers": [
{
"type": "COUNT",
"selectedType": "entity",
"selectedTypeId": 1,
"attributeIndex": -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 := `WITH partijenLET n1 = (FOR x IN kamerleden RETURN x)LET r0 = (FOR x IN n1 FOR v, e, p IN 1..1 OUTBOUND x lid_van OPTIONS { uniqueEdges: "path" }RETURN DISTINCT p )RETURN LENGTH (unique(r0[*].vertices[0]))`
cleanedResult := strings.ReplaceAll(*convertedResult, "\n", "")
cleanedResult = strings.ReplaceAll(cleanedResult, "\t", "")
assert.Equal(t, correctConvertedResult, cleanedResult)
}
/*
Tests the count modifier on a relation and attribute
t: *testing.T, makes go recognise this as a test
*/
func TestModifierCountRelationAttribute(t *testing.T) {
// Setup for test
// Create query conversion service
service := NewService()
query := []byte(`{
"return": {
"entities": [
0
],
"relations": [
0
]
},
"entities": [
{
"ID": 0,
"type": "airports"
}
],
"relations": [
{
"type": "flights",
"depth": {
"min": 1,
"max": 1
},
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
"fromType": "entity",
"fromID": 0,
"toType": "",
"toID": -1
}
],
"groupBys": [
],
"filters": [
{
"ID": 0,
"fromType": "entity",
"fromID": 0,
"toType": "relation",
"toID": 0,
"attribute": "state",
"value": "HI",
"dataType": "string",
"matchType": "exact",
"inType": "",
"inID": -1
},
{
"ID": 1,
"fromType": "relation",
"fromID": 0,
"toType": "",
"toID": -1,
"attribute": "Day",
"value": "15",
"dataType": "int",
"matchType": "EQ",
"inType": "",
"inID": -1
}
],
"limit": 5000,
"modifiers": [
{
"type": "AVG",
"selectedType": "relation",
"id": 0,
"attributeIndex": 0
}
]
}`)
// 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.state == "HI" RETURN x)LET r0 = (FOR x IN n0 FOR v, e, p IN 1..1 OUTBOUND x flights OPTIONS { uniqueEdges: "path" }FILTER p.edges[*].Day ALL == 15 RETURN DISTINCT p )RETURN AVG (r0[*].edges[**].Day)`
cleanedResult := strings.ReplaceAll(*convertedResult, "\n", "")
cleanedResult = strings.ReplaceAll(cleanedResult, "\t", "")
assert.Equal(t, correctConvertedResult, cleanedResult)
}
/*
Tests a relation with an in out constraint
t: *testing.T, makes go recognise this as a test
*/
func TestRelationWithInOutConstraint(t *testing.T) {
// Setup for test
// Create query conversion service
service := NewService()
query := []byte(`{
"return": {
"entities": [
0,
1
],
"relations": [
0
]
},
"entities": [
{
"type": "airports",
"constraints": [
{
"attribute": "city",
"value": "San Francisco",
"matchType": "exact"
}
]
},
{
"type": "airports",
"constraints": [
{
"attribute": "state",
"value": "HI",
"matchType": "exact"
}
]
}
],
"relations": [
{
"type": "flights",
"depth": {
"min": 1,
"max": 3
},
"fromType": "entity",
"fromID": 1,
"toType": "entity",
"toID": 0
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
],
"groupBys": [],
"filters": [
{
"ID": 0,
"fromType": "entity",
"fromID": 0,
"toType": "relation",
"toID": 0,
"attribute": "city",
"value": "San Francisco",
"dataType": "string",
"matchType": "exact",
"inType": "",
"inID": -1
},
{
"ID": 1,
"fromType": "entity",
"fromID": 1,
"toType": "relation",
"toID": 0,
"attribute": "state",
"value": "HI",
"dataType": "string",
"matchType": "exact",
"inType": "",
"inID": -1
}, {
"ID": 2,
"fromType": "relation",
"fromID": 0,
"toType": "",
"toID": -1,
"attribute": "Day",
"value": "15",
"dataType": "int",
"matchType": "EQ",
"inType": "",
"inID": -1
}
],
"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 n1 = (FOR x IN airports FILTER x.state == "HI" RETURN x)LET r0 = (FOR x IN n1 FOR v, e, p IN 1..3 OUTBOUND x flights OPTIONS { uniqueEdges: "path" }FILTER v.city == "San Francisco" FILTER p.edges[*].Day ALL == 15 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)
}
/*
Tests two relations
t: *testing.T, makes go recognise this as a test
*/
func TestTwoRelations(t *testing.T) {
// Setup for test
// Create query conversion service
service := NewService()
query := []byte(`{
"return": {
"entities": [
0,
1,
2
],
"relations": [
0,
1
]
},
"entities": [
{
"ID": 0,
"type": "airports"
"ID": 1,
"type": "airports"
"ID": 2,
"type": "airports"
}
],
"relations": [
{
"type": "flights",
"depth": {
"min": 1,
"max": 3
},
"fromType": "entity",
"fromID": 2,
"toType": "entity",
"toID": 1
"type": "flights",
"depth": {
"min": 1,
"max": 1
},
"fromType": "entity",
"fromID": 0,
"toType": "",
"toID": -1
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
],
"groupBys": [],
"filters": [
{
"ID": 0,
"fromType": "entity",
"fromID": 0,
"toType": "relation",
"toID": 1,
"attribute": "city",
"value": "New York",
"dataType": "string",
"matchType": "exact",
"inType": "",
"inID": -1
},
{
"ID": 1,
"fromType": "entity",
"fromID": 1,
"toType": "relation",
"toID": 0,
"attribute": "city",
"value": "San Francisco",
"dataType": "string",
"matchType": "exact",
"inType": "",
"inID": -1
},
{
"ID": 2,
"fromType": "entity",
"fromID": 2,
"toType": "relation",
"toID": 0,
"attribute": "state",
"value": "HI",
"dataType": "string",
"matchType": "exact",
"inType": "",
"inID": -1
},
{
"ID": 3,
"fromType": "relation",
"fromID": 0,
"toType": "",
"toID": -1,
"attribute": "Day",
"value": "15",
"dataType": "int",
"matchType": "EQ",
"inType": "",
"inID": -1
}
],
"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 n2 = (FOR x IN airports FILTER x.state == "HI" RETURN x)LET r0 = (FOR x IN n2 FOR v, e, p IN 1..3 OUTBOUND x flights OPTIONS { uniqueEdges: "path" }FILTER v.city == "San Francisco" FILTER p.edges[*].Day ALL == 15 LIMIT 5000 RETURN DISTINCT p )LET n0 = (FOR x IN airports FILTER x.city == "New York" RETURN x)LET r1 = (FOR x IN n0 FOR v, e, p IN 1..1 OUTBOUND x flights 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)
}
/*
Tests a relation with only a to node
t: *testing.T, makes go recognise this as a test
*/
func TestRelationWithOnlyToNode(t *testing.T) {
// Setup for test
// Create query conversion service
service := NewService()
query := []byte(`{
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
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",