-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Add golden tests to pg_dump (#861)
Adds golden tests for PostgreSQL schema conversion (using pg_dump).
- Loading branch information
1 parent
b3a66d4
commit 913f1bc
Showing
3 changed files
with
218 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,16 @@ | ||
CREATE TABLE test ( | ||
id bigint PRIMARY KEY, | ||
col text | ||
) | ||
-- | ||
CREATE TABLE `test` ( | ||
`id` INT64 NOT NULL , | ||
`col` STRING(MAX), | ||
) PRIMARY KEY (`id`) | ||
-- | ||
CREATE TABLE test ( | ||
id INT8 NOT NULL , | ||
col VARCHAR(2621440), | ||
PRIMARY KEY (id) | ||
) | ||
== |
This file contains 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,113 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package common | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
"strings" | ||
) | ||
|
||
type GoldenTestCase struct { | ||
InputSchema string | ||
ExpectedGSQLSchema string | ||
ExpectedPSQLSchema string | ||
} | ||
|
||
type GoldenParseStatus int | ||
|
||
const ( | ||
InputSchema GoldenParseStatus = iota | ||
GSQLSchema | ||
PSQLSchema | ||
) | ||
|
||
const ( | ||
GoldenTestPartSeparator = "--" | ||
GoldenTestCaseSeparator = "==" | ||
) | ||
|
||
func GoldenTestCasesFrom(path string) ([]GoldenTestCase, error) { | ||
file, err := os.Open(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer file.Close() | ||
|
||
var testCases []GoldenTestCase | ||
var inputSchema, expectedGSQLSchema, expectedPSQLSchema strings.Builder | ||
parsingStatus := InputSchema | ||
lineNum := 0 | ||
scanner := bufio.NewScanner(file) | ||
for scanner.Scan() { | ||
lineNum++ | ||
line := scanner.Text() | ||
|
||
// End of a test case | ||
if line == GoldenTestCaseSeparator { | ||
if inputSchema.Len() == 0 { | ||
return nil, fmt.Errorf("bad format: Invalid test case at line %d, missing source schema", lineNum) | ||
} | ||
if expectedGSQLSchema.Len() == 0 { | ||
return nil, fmt.Errorf("bad format: Invalid test case at line %d, missing expected GoogleSQL schema", lineNum) | ||
} | ||
if expectedPSQLSchema.Len() == 0 { | ||
return nil, fmt.Errorf("bad format: Invalid test case at line %d, missing expected PostgreSQL schema", lineNum) | ||
} | ||
sanitizedGSQLSchema := strings.TrimRight(expectedGSQLSchema.String(), "\n") | ||
sanitizedPSQLSchema := strings.TrimRight(expectedPSQLSchema.String(), "\n") | ||
testCases = append(testCases, GoldenTestCase{ | ||
InputSchema: inputSchema.String(), | ||
ExpectedGSQLSchema: sanitizedGSQLSchema, | ||
ExpectedPSQLSchema: sanitizedPSQLSchema}) | ||
parsingStatus = InputSchema | ||
continue | ||
} | ||
|
||
// End of part of a test case | ||
if line == GoldenTestPartSeparator { | ||
switch parsingStatus { | ||
case InputSchema: | ||
parsingStatus = GSQLSchema | ||
case GSQLSchema: | ||
parsingStatus = PSQLSchema | ||
default: | ||
return nil, fmt.Errorf("bad format: expected end of test case at line %d", lineNum) | ||
} | ||
continue | ||
} | ||
|
||
switch parsingStatus { | ||
case InputSchema: | ||
inputSchema.WriteString(line + "\n") | ||
case GSQLSchema: | ||
expectedGSQLSchema.WriteString(line + "\n") | ||
case PSQLSchema: | ||
expectedPSQLSchema.WriteString(line + "\n") | ||
} | ||
} | ||
|
||
// Test case is invalid | ||
if parsingStatus != InputSchema { | ||
return nil, fmt.Errorf("bad format: Invalid test case at line %d", lineNum) | ||
} | ||
|
||
if err := scanner.Err(); err != nil { | ||
return nil, err | ||
} | ||
|
||
return testCases, nil | ||
} |
This file contains 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,89 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package postgres_test | ||
|
||
import ( | ||
"bufio" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/GoogleCloudPlatform/spanner-migration-tool/common/constants" | ||
"github.com/GoogleCloudPlatform/spanner-migration-tool/internal" | ||
"github.com/GoogleCloudPlatform/spanner-migration-tool/logger" | ||
"github.com/GoogleCloudPlatform/spanner-migration-tool/sources/common" | ||
"github.com/GoogleCloudPlatform/spanner-migration-tool/sources/postgres" | ||
"github.com/GoogleCloudPlatform/spanner-migration-tool/spanner/ddl" | ||
commonTesting "github.com/GoogleCloudPlatform/spanner-migration-tool/testing/common" | ||
"github.com/stretchr/testify/assert" | ||
"go.uber.org/zap" | ||
) | ||
|
||
const GoldenTestsDir = "../../test_data/goldens/postgres" | ||
|
||
func formatDdl(ddl []string) string { | ||
return strings.ReplaceAll(strings.Join(ddl, "\n"), "\t", strings.Repeat(" ", 4)) | ||
} | ||
|
||
func TestGoldens(t *testing.T) { | ||
logger.Log = zap.NewNop() | ||
entries, err := os.ReadDir(GoldenTestsDir) | ||
if err != nil { | ||
t.Fatalf("error when reading entries of golden tests dir %s: %s", GoldenTestsDir, err) | ||
} | ||
|
||
schemaToSpanner := common.SchemaToSpannerImpl{} | ||
|
||
for _, entry := range entries { | ||
if !entry.IsDir() { | ||
path := filepath.Join(GoldenTestsDir, entry.Name()) | ||
testCases, err := commonTesting.GoldenTestCasesFrom(path) | ||
if err != nil { | ||
t.Fatalf("error when reading golden tests from path %s: %s", path, err) | ||
} | ||
|
||
t.Logf("executing %d test cases from %s", len(testCases), path) | ||
for _, testCase := range testCases { | ||
conv := internal.MakeConv() | ||
conv.SetLocation(time.UTC) | ||
conv.SetSchemaMode() | ||
|
||
err := common.ProcessDbDump( | ||
conv, | ||
internal.NewReader(bufio.NewReader(strings.NewReader(testCase.InputSchema)), nil), | ||
postgres.DbDumpImpl{}) | ||
if err != nil { | ||
t.Fatalf("error when processing dump %s: %s", testCase.InputSchema, err) | ||
} | ||
|
||
err = schemaToSpanner.SchemaToSpannerDDL(conv, postgres.ToDdlImpl{}) | ||
if err != nil { | ||
t.Fatalf("error when converting schema to spanner ddl %s: %s", testCase.InputSchema, err) | ||
} | ||
config := ddl.Config{Comments: false, ProtectIds: true, Tables: true, ForeignKeys: true} | ||
|
||
config.SpDialect = constants.DIALECT_GOOGLESQL | ||
actual := ddl.GetDDL(config, conv.SpSchema, conv.SpSequences) | ||
assert.Equal(t, testCase.ExpectedGSQLSchema, formatDdl(actual)) | ||
|
||
config.SpDialect = constants.DIALECT_POSTGRESQL | ||
actual = ddl.GetDDL(config, conv.SpSchema, conv.SpSequences) | ||
assert.Equal(t, testCase.ExpectedPSQLSchema, formatDdl(actual)) | ||
} | ||
} | ||
} | ||
} |