-
Notifications
You must be signed in to change notification settings - Fork 2
Add YAML and param-file golden tests #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| package params | ||
|
|
||
| import ( | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/google/go-cmp/cmp" | ||
| ) | ||
|
|
||
| func TestLoadParamFileFixtures(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| tests := []struct { | ||
| file string | ||
| want map[string]string | ||
| }{ | ||
| { | ||
| file: "testdata/readme_example.yaml", | ||
| want: map[string]string{ | ||
| "arr": "ARRAY<STRING>", | ||
| "names": `[STRUCT<FirstName STRING, LastName STRING>("John", "Doe"), ("Mary", "Sue")]`, | ||
| }, | ||
| }, | ||
| { | ||
| file: "testdata/spanner_literals.yml", | ||
| want: map[string]string{ | ||
| "b": "TRUE", | ||
| "bs": `b"foo"`, | ||
| "i64": "1", | ||
| "f64": "1.0", | ||
| "f32": "CAST(1.0 AS FLOAT32)", | ||
| "n": `NUMERIC "1"`, | ||
| "s": "'foo'", | ||
| "js": `JSON "{}"`, | ||
| "ts": `TIMESTAMP "2000-01-01T00:00:00Z"`, | ||
| "ival_single": "INTERVAL 3 DAY", | ||
| "n_b": "CAST(NULL AS BOOL)", | ||
| }, | ||
| }, | ||
| { | ||
| file: "testdata/multiline_literal.yaml", | ||
| want: map[string]string{ | ||
| "query": "SELECT SingerId, FirstName\nFROM Singers\nWHERE SingerId = @id\n", | ||
| "id": "42", | ||
| }, | ||
| }, | ||
| { | ||
| file: "testdata/large_int.yaml", | ||
| want: map[string]string{ | ||
| "id": "1234567890123456789", | ||
| }, | ||
| }, | ||
| { | ||
| file: "testdata/scalars_extra.yaml", | ||
| want: map[string]string{ | ||
| "id": "123", | ||
| "enabled": "FALSE", | ||
| "price": "1.0", | ||
| "ratio": "0.25", | ||
| "tag": "yes", | ||
| "label": "ARRAY<INT64>", | ||
| "created_at": `TIMESTAMP "2023-01-01T00:00:00Z"`, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range tests { | ||
| tc := tc | ||
| t.Run(filepath.Base(tc.file), func(t *testing.T) { | ||
| t.Parallel() | ||
| got, err := LoadParamFile(tc.file) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if diff := cmp.Diff(tc.want, got); diff != "" { | ||
| t.Fatalf("(-want +got)\n%s", diff) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestLoadParamFileFixtureErrors(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| if _, err := LoadParamFile("testdata/nested_map.yaml"); err == nil { | ||
| t.Fatal("expected error for nested map") | ||
| } | ||
| if _, err := LoadParamFile("testdata/nested_array.yaml"); err == nil { | ||
| t.Fatal("expected error for nested array") | ||
| } | ||
| } | ||
|
|
||
| func TestMergeParamsWithFixture(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| file, err := LoadParamFile("testdata/readme_example.yaml") | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| got := MergeParams(file, map[string]string{"arr": "ARRAY<INT64>", "extra": "1"}) | ||
| want := map[string]string{ | ||
| "arr": "ARRAY<INT64>", | ||
| "names": `[STRUCT<FirstName STRING, LastName STRING>("John", "Doe"), ("Mary", "Sue")]`, | ||
| "extra": "1", | ||
| } | ||
| if diff := cmp.Diff(want, got); diff != "" { | ||
| t.Fatalf("(-want +got)\n%s", diff) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| id: 1234567890123456789 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| query: | | ||
| SELECT SingerId, FirstName | ||
| FROM Singers | ||
| WHERE SingerId = @id | ||
| id: "42" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| arr: [1, 2] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| opts: | ||
| key: value |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # params.yaml from README (--param-file example) | ||
| arr: ARRAY<STRING> | ||
| names: '[STRUCT<FirstName STRING, LastName STRING>("John", "Doe"), ("Mary", "Sue")]' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| id: 123 | ||
| enabled: false | ||
| price: 1.0 | ||
| ratio: 0.25 | ||
| tag: yes | ||
| label: "ARRAY<INT64>" | ||
| created_at: 2023-01-01T00:00:00Z |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| # Common Spanner parameter literal strings for --param-file | ||
| b: "TRUE" | ||
| bs: 'b"foo"' | ||
| i64: "1" | ||
| f64: "1.0" | ||
| f32: "CAST(1.0 AS FLOAT32)" | ||
| n: 'NUMERIC "1"' | ||
| s: "'foo'" | ||
| js: 'JSON "{}"' | ||
| ts: 'TIMESTAMP "2000-01-01T00:00:00Z"' | ||
| ival_single: "INTERVAL 3 DAY" | ||
| n_b: "CAST(NULL AS BOOL)" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| sppb "cloud.google.com/go/spanner/apiv1/spannerpb" | ||
| "google.golang.org/protobuf/encoding/protojson" | ||
| ) | ||
|
|
||
| func loadProfileJSONFixture(path string) (*sppb.ResultSet, error) { | ||
| b, err := os.ReadFile(path) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| var rs sppb.ResultSet | ||
| if err := protojson.Unmarshal(b, &rs); err != nil { | ||
| return nil, err | ||
| } | ||
| return &rs, nil | ||
| } | ||
|
|
||
| type profileYAMLGoldenCase struct { | ||
| jsonFile string | ||
| filter string | ||
| golden string | ||
| } | ||
|
|
||
| func profileYAMLGoldenCases() []profileYAMLGoldenCase { | ||
| return []profileYAMLGoldenCase{ | ||
| { | ||
| jsonFile: "testdata/profile/param_scalar.json", | ||
| filter: ".", | ||
| golden: "profile_param_scalar", | ||
| }, | ||
| { | ||
| jsonFile: "testdata/profile/albums_by_singer.json", | ||
| filter: ".", | ||
| golden: "profile_albums_by_singer", | ||
| }, | ||
| { | ||
| jsonFile: "testdata/profile/singers_limit3.json", | ||
| filter: ".", | ||
| golden: "profile_singers_limit3", | ||
| }, | ||
| { | ||
| jsonFile: "testdata/profile/singers_limit3.json", | ||
| filter: `{rowType: .metadata.rowType, rows: .rows}`, | ||
| golden: "profile_singers_limit3_rowtype_rows", | ||
| }, | ||
| { | ||
| jsonFile: "testdata/profile/singers_limit3.json", | ||
| filter: `.rows[]`, | ||
| golden: "profile_singers_limit3_rows_stream", | ||
| }, | ||
| { | ||
| jsonFile: "testdata/profile/singers_limit3.json", | ||
| filter: `.metadata.rowType.fields | map(.name)`, | ||
| golden: "profile_singers_limit3_field_names", | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func TestProfileJSONToYamlGolden(t *testing.T) { | ||
| for _, tc := range profileYAMLGoldenCases() { | ||
| tc := tc | ||
| t.Run(tc.golden, func(t *testing.T) { | ||
|
apstndb marked this conversation as resolved.
|
||
| rs, err := loadProfileJSONFixture(tc.jsonFile) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if rs.GetMetadata() == nil || rs.GetMetadata().GetRowType() == nil { | ||
| t.Fatal("missing metadata.rowType") | ||
| } | ||
| if rs.GetStats() == nil { | ||
| t.Fatal("missing stats") | ||
| } | ||
| if rs.GetStats().GetQueryPlan() == nil && rs.GetStats().GetQueryStats() == nil { | ||
| t.Fatal("missing query plan or query stats") | ||
| } | ||
|
|
||
| got, err := encodeResultSetYAML(tc.filter, rs) | ||
| if err != nil { | ||
| t.Fatalf("encodeResultSetYAML() error = %v", err) | ||
| } | ||
|
|
||
| goldenPath := filepath.Join("testdata", "yaml_output", tc.golden+".golden") | ||
| if *updateGolden { | ||
| if err := os.MkdirAll(filepath.Dir(goldenPath), 0o755); err != nil { | ||
| t.Fatalf("MkdirAll() error = %v", err) | ||
| } | ||
| if err := os.WriteFile(goldenPath, got, 0o644); err != nil { | ||
| t.Fatalf("WriteFile() error = %v", err) | ||
| } | ||
| t.Logf("updated %s", goldenPath) | ||
| return | ||
| } | ||
|
|
||
| want, err := os.ReadFile(goldenPath) | ||
| if err != nil { | ||
| t.Fatalf("ReadFile(%q) error = %v (run: go test -update-golden -run TestProfileJSONToYamlGolden)", goldenPath, err) | ||
| } | ||
| if string(got) != string(want) { | ||
| t.Fatalf("PROFILE JSON → YAML mismatch for %s\n\ngot:\n%s\n\nwant:\n%s", tc.golden, got, want) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.