|
| 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 | +} |
0 commit comments