Skip to content

Commit b7d4dbe

Browse files
committed
tests added
1 parent f2f76d2 commit b7d4dbe

27 files changed

Lines changed: 1599 additions & 0 deletions

File tree

connector/filter_test.go

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package connector
2+
3+
import (
4+
"encoding/json"
5+
"os"
6+
"path/filepath"
7+
"testing"
8+
9+
"github.com/hasura/ndc-sdk-go/schema"
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
const filterTestsPath = "./testdata/filter_tests/"
14+
15+
func TestGetPredicate(t *testing.T) {
16+
tests := []struct {
17+
name string
18+
gotExpression string
19+
expectedColumnPath string
20+
wantPredicate string
21+
}{
22+
{
23+
name: "nested_001",
24+
expectedColumnPath: "route.departure_airport.location.state",
25+
},
26+
{
27+
name: "nested_and_002",
28+
expectedColumnPath: "",
29+
},
30+
{
31+
name: "003",
32+
expectedColumnPath: "route.arrival_airport.location.coordinates.elevation",
33+
},
34+
{
35+
name: "004",
36+
expectedColumnPath: "route.arrival_airport.terminals",
37+
},
38+
{
39+
name: "aggregations_005",
40+
expectedColumnPath: "metric_value",
41+
},
42+
{
43+
name: "006",
44+
expectedColumnPath: "name",
45+
},
46+
}
47+
48+
for _, tt := range tests {
49+
t.Run(tt.name, func(t *testing.T) {
50+
// setup test data
51+
gotB, err := os.ReadFile(filepath.Join(filterTestsPath, "get_predicate", tt.name, "got.json"))
52+
assert.NoError(t, err, "Error reading got.json file")
53+
tt.gotExpression = string(gotB)
54+
55+
wantB, err := os.ReadFile(filepath.Join(filterTestsPath, "get_predicate", tt.name, "want.json"))
56+
assert.NoError(t, err, "Error reading want.json file")
57+
tt.wantPredicate = string(wantB)
58+
59+
// Convert tt.expression from JSON string to schema.Expression
60+
var expression schema.Expression
61+
err = json.Unmarshal([]byte(tt.gotExpression), &expression)
62+
assert.NoError(t, err, "Error unmarshalling expression JSON")
63+
64+
// Convert tt.expectedPredicate from JSON string to schema.Expression
65+
var expectedPredicate schema.Expression
66+
err = json.Unmarshal([]byte(tt.wantPredicate), &expectedPredicate)
67+
assert.NoError(t, err, "Error unmarshalling expectedPredicate JSON")
68+
69+
// Call getPredicate and validate results
70+
path, result := getPredicate(expression)
71+
assert.Equal(t, tt.expectedColumnPath, path)
72+
assert.Equal(t, expectedPredicate, result)
73+
74+
// uncomment to update want file
75+
// err = os.WriteFile(filepath.Join(filterTestsPath, "get_predicate", tt.name, "want.json"), []byte(tt.wantPredicate), 0644)
76+
// assert.NoError(t, err, "Error writing want file")
77+
})
78+
}
79+
}
80+
81+
func TestRequiresNestedFiltering(t *testing.T) {
82+
tests := []struct {
83+
name string
84+
predicate schema.Expression
85+
expectedNested bool
86+
expectedNestedField string
87+
}{
88+
{
89+
name: "Valid nested collection",
90+
predicate: schema.Expression{
91+
"in_collection": schema.ExistsInCollection{
92+
"column_name": "my_nested_field",
93+
"type": "nested_collection",
94+
},
95+
},
96+
expectedNested: true,
97+
expectedNestedField: "my_nested_field",
98+
},
99+
{
100+
name: "Missing in_collection key",
101+
predicate: schema.Expression{
102+
"some_other_key": "some_value",
103+
},
104+
expectedNested: false,
105+
expectedNestedField: "",
106+
},
107+
{
108+
name: "Invalid in_collection type",
109+
predicate: schema.Expression{
110+
"in_collection": "invalid_type",
111+
},
112+
expectedNested: false,
113+
expectedNestedField: "",
114+
},
115+
}
116+
117+
for _, tt := range tests {
118+
t.Run(tt.name, func(t *testing.T) {
119+
nested, fieldName := requiresNestedFiltering(tt.predicate)
120+
assert.Equal(t, tt.expectedNested, nested)
121+
assert.Equal(t, tt.expectedNestedField, fieldName)
122+
})
123+
}
124+
}

connector/query_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,22 @@ var tests = []test{
141141
group: "payments",
142142
name: "search_after_multiple_sorts",
143143
},
144+
{
145+
group: "flights_nested_flattened",
146+
name: "nested_filtering",
147+
},
148+
{
149+
group: "flights_nested_flattened",
150+
name: "nested_filtering_and",
151+
},
152+
{
153+
group: "flights_nested_flattened",
154+
name: "nested_filtering_range",
155+
},
156+
{
157+
group: "flights_nested_flattened",
158+
name: "nested_filtering_or",
159+
},
144160
}
145161

146162
func TestPrepareElasticsearchQuery(t *testing.T) {
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"in_collection": {
3+
"column_name": "route",
4+
"type": "nested_collection"
5+
},
6+
"predicate": {
7+
"in_collection": {
8+
"column_name": "arrival_airport",
9+
"type": "nested_collection"
10+
},
11+
"predicate": {
12+
"in_collection": {
13+
"column_name": "location",
14+
"type": "nested_collection"
15+
},
16+
"predicate": {
17+
"in_collection": {
18+
"column_name": "coordinates",
19+
"type": "nested_collection"
20+
},
21+
"predicate": {
22+
"column": {
23+
"type": "column",
24+
"name": "elevation"
25+
},
26+
"operator": "range",
27+
"type": "binary_comparison_operator",
28+
"value": {
29+
"type": "scalar",
30+
"value": {
31+
"boost": "",
32+
"gt": "",
33+
"gte": "200",
34+
"lt": "",
35+
"lte": ""
36+
}
37+
}
38+
},
39+
"type": "exists"
40+
},
41+
"type": "exists"
42+
},
43+
"type": "exists"
44+
},
45+
"type": "exists"
46+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"column": {
3+
"type": "column",
4+
"name": "elevation"
5+
},
6+
"operator": "range",
7+
"type": "binary_comparison_operator",
8+
"value": {
9+
"type": "scalar",
10+
"value": {
11+
"boost": "",
12+
"gt": "",
13+
"gte": "200",
14+
"lt": "",
15+
"lte": ""
16+
}
17+
}
18+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"in_collection": {
3+
"column_name": "route",
4+
"type": "nested_collection"
5+
},
6+
"predicate": {
7+
"in_collection": {
8+
"column_name": "arrival_airport",
9+
"type": "nested_collection"
10+
},
11+
"predicate": {
12+
"column": {
13+
"type": "column",
14+
"name": "terminals"
15+
},
16+
"operator": "match",
17+
"type": "binary_comparison_operator",
18+
"value": {
19+
"type": "scalar",
20+
"value": "2"
21+
}
22+
},
23+
"type": "exists"
24+
},
25+
"type": "exists"
26+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"column": {
3+
"type": "column",
4+
"name": "terminals"
5+
},
6+
"operator": "match",
7+
"type": "binary_comparison_operator",
8+
"value": {
9+
"type": "scalar",
10+
"value": "2"
11+
}
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"column": {
3+
"type": "column",
4+
"name": "name"
5+
},
6+
"operator": "prefix",
7+
"type": "binary_comparison_operator",
8+
"value": {
9+
"type": "scalar",
10+
"value": "J"
11+
}
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"column": {
3+
"type": "column",
4+
"name": "name"
5+
},
6+
"operator": "prefix",
7+
"type": "binary_comparison_operator",
8+
"value": {
9+
"type": "scalar",
10+
"value": "J"
11+
}
12+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"column": {
3+
"type": "column",
4+
"name": "metric_value"
5+
},
6+
"operator": "range",
7+
"type": "binary_comparison_operator",
8+
"value": {
9+
"type": "scalar",
10+
"value": {
11+
"boost": "",
12+
"gt": "",
13+
"gte": "",
14+
"lt": "75",
15+
"lte": ""
16+
}
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"column": {
3+
"type": "column",
4+
"name": "metric_value"
5+
},
6+
"operator": "range",
7+
"type": "binary_comparison_operator",
8+
"value": {
9+
"type": "scalar",
10+
"value": {
11+
"boost": "",
12+
"gt": "",
13+
"gte": "",
14+
"lt": "75",
15+
"lte": ""
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)