Skip to content

Commit c493bc1

Browse files
committed
add additional geo.distance tests
1 parent 799664a commit c493bc1

File tree

1 file changed

+75
-40
lines changed

1 file changed

+75
-40
lines changed

filter_parser_test.go

Lines changed: 75 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func TestFilterAnyArrayOfPrimitiveTypes(t *testing.T) {
134134
}
135135
}
136136

137-
func TestFilterGeoPolygon(t *testing.T) {
137+
func TestFilterGeoIntersects(t *testing.T) {
138138

139139
input := "geo.intersects(location, geography'Polygon((-122.031577 47.578581, -122.031577 47.678581, -122.131577 47.678581, -122.031577 47.578581))')"
140140
q, err := ParseFilterString(context.Background(), input)
@@ -1156,27 +1156,6 @@ func TestFilterNestedFunction(t *testing.T) {
11561156
}
11571157
}
11581158

1159-
func TestGeo(t *testing.T) {
1160-
1161-
//q, err := ParseFilterString(context.Background(), "geo.distance(Foo,Bar) lt 5")
1162-
//if err != nil {
1163-
// t.Fatal(err)
1164-
//}
1165-
//t.Log(q)
1166-
1167-
q, err := ParseFilterString(context.Background(), "geo.distance(Foo,geography'POINT(-122.131577 47.678581)') lt 5")
1168-
if err != nil {
1169-
t.Fatal(err)
1170-
}
1171-
t.Log(q)
1172-
1173-
q, err = ParseFilterString(context.Background(), `geo.intersects(Foo,geography'POLYGON(( -122.34 47.65, -122.34 47.60, -122.30 47.60, -122.30 47.65, -122.34 47.65 ))')`)
1174-
if err != nil {
1175-
t.Fatal(err)
1176-
}
1177-
t.Log(q)
1178-
}
1179-
11801159
func TestValidFilterSyntax(t *testing.T) {
11811160
queries := []string{
11821161
"substring(CompanyName,1,2) eq 'lf'", // substring with 3 arguments.
@@ -1866,34 +1845,90 @@ func TestFilterSubstringNestedFunction(t *testing.T) {
18661845
t.Errorf("Tree representation does not match expected value. error: %v. Tree:\n%v", err, tree)
18671846
}
18681847
}
1869-
func TestFilterGeoFunctions(t *testing.T) {
1870-
ctx := context.Background()
1871-
// Previously, the parser was incorrectly interpreting the 'geo.xxx' functions as the 'ge' operator.
1872-
input := "geo.distance(CurrentPosition,TargetPosition)"
1873-
tokens, err := GlobalExpressionTokenizer.Tokenize(ctx, input)
1848+
func TestGeoDistance(t *testing.T) {
1849+
1850+
input := "geo.distance(location,geography'POINT(-122.13 47.67)') lt 5"
1851+
q, err := ParseFilterString(context.Background(), input)
18741852
if err != nil {
1875-
t.Error(err)
1853+
t.Errorf("Error parsing query %s. Error: %s", input, err.Error())
18761854
return
18771855
}
1878-
output, err := GlobalFilterParser.InfixToPostfix(ctx, tokens)
1856+
var expect = []expectedParseNode{
1857+
{Value: "lt", Depth: 0, Type: ExpressionTokenLogical},
1858+
{Value: "geo.distance", Depth: 1, Type: ExpressionTokenFunc},
1859+
{Value: "location", Depth: 2, Type: ExpressionTokenLiteral},
1860+
{Value: "-122.13 47.67", Depth: 2, Type: ExpressionTokenGeographyPoint},
1861+
{Value: "5", Depth: 1, Type: ExpressionTokenInteger},
1862+
}
1863+
pos := 0
1864+
err = CompareTree(q.Tree, expect, &pos, 0)
18791865
if err != nil {
1880-
t.Error(err)
1881-
return
1866+
fmt.Printf("Got tree:\n%v\n", q.Tree.String())
1867+
t.Errorf("Tree representation does not match expected value. error: %s", err.Error())
18821868
}
1883-
tree, err := GlobalFilterParser.PostfixToTree(ctx, output)
1869+
1870+
input = "geo.distance(location,geography'POINT(-122.13 47)') lt 5"
1871+
q, err = ParseFilterString(context.Background(), input)
18841872
if err != nil {
1885-
t.Error(err)
1873+
t.Errorf("Error parsing query %s. Error: %s", input, err.Error())
18861874
return
18871875
}
1888-
var expect []expectedParseNode = []expectedParseNode{
1889-
{Value: "geo.distance", Depth: 0, Type: ExpressionTokenFunc},
1890-
{Value: "CurrentPosition", Depth: 1, Type: ExpressionTokenLiteral},
1891-
{Value: "TargetPosition", Depth: 1, Type: ExpressionTokenLiteral},
1876+
expect = []expectedParseNode{
1877+
{Value: "lt", Depth: 0, Type: ExpressionTokenLogical},
1878+
{Value: "geo.distance", Depth: 1, Type: ExpressionTokenFunc},
1879+
{Value: "location", Depth: 2, Type: ExpressionTokenLiteral},
1880+
{Value: "-122.13 47", Depth: 2, Type: ExpressionTokenGeographyPoint},
1881+
{Value: "5", Depth: 1, Type: ExpressionTokenInteger},
18921882
}
1893-
pos := 0
1894-
err = CompareTree(tree, expect, &pos, 0)
1883+
pos = 0
1884+
err = CompareTree(q.Tree, expect, &pos, 0)
18951885
if err != nil {
1896-
t.Errorf("Tree representation does not match expected value. error: %v. Tree:\n%v", err, tree)
1886+
fmt.Printf("Got tree:\n%v\n", q.Tree.String())
1887+
t.Errorf("Tree representation does not match expected value. error: %s", err.Error())
1888+
}
1889+
1890+
// negative tests
1891+
1892+
// geo.distance function with too few arguments, needs 2
1893+
input = "geo.distance(location) lt 5"
1894+
_, err = ParseFilterString(context.Background(), input)
1895+
if err == nil || !strings.Contains(err.Error(), "invalid number of arguments for function") {
1896+
t.Errorf("parsing should fail for bad query, got ==> %v", err)
1897+
}
1898+
1899+
// geo.distance function with too many arguments, needs 2
1900+
input = "geo.distance(location,geography'POINT(1,2)', extra) lt 5"
1901+
_, err = ParseFilterString(context.Background(), input)
1902+
if err == nil || !strings.Contains(err.Error(), "invalid token sequence") {
1903+
t.Errorf("parsing should fail for bad query, got ==> %v", err)
1904+
}
1905+
1906+
// POINT with too few coordinates, needs 2
1907+
input = "geo.distance(location,geography'POINT()') lt 5"
1908+
_, err = ParseFilterString(context.Background(), input)
1909+
if err == nil || !strings.Contains(err.Error(), "invalid token sequence") {
1910+
t.Errorf("parsing should fail for bad query, got ==> %v", err)
1911+
}
1912+
1913+
// POINT with too few coordinates, needs 2
1914+
input = "geo.distance(location,geography'POINT(1)') lt 5"
1915+
_, err = ParseFilterString(context.Background(), input)
1916+
if err == nil || !strings.Contains(err.Error(), "invalid token sequence") {
1917+
t.Errorf("parsing should fail for bad query, got ==> %v", err)
1918+
}
1919+
1920+
// POINT with too many coordinates, needs 2
1921+
input = "geo.distance(location,geography'POINT(1,2,3)') lt 5"
1922+
_, err = ParseFilterString(context.Background(), input)
1923+
if err == nil || !strings.Contains(err.Error(), "invalid token sequence") {
1924+
t.Errorf("parsing should fail for bad query, got ==> %v", err)
1925+
}
1926+
1927+
// geo.distance must be followed by a comparison operator
1928+
input = "geo.distance(location,geography'POINT(-122.13 47.67)')"
1929+
_, err = ParseFilterString(context.Background(), input)
1930+
if err == nil || !strings.Contains(err.Error(), "Value must be a boolean expression") {
1931+
t.Errorf("parsing should fail for bad query, got ==> %v", err)
18971932
}
18981933
}
18991934

0 commit comments

Comments
 (0)