Skip to content

Commit

Permalink
Merge branch 'interleave-fkactions' of https://github.com/aasthabhari…
Browse files Browse the repository at this point in the history
…ll/spanner-migration-tool into interleave-fkactions
  • Loading branch information
aasthabharill-google committed Jul 18, 2024
2 parents 64e692d + 97f1929 commit 7daf613
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 100 deletions.
17 changes: 11 additions & 6 deletions internal/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,34 @@ import (
"fmt"
"strconv"
"strings"
"sync"

"github.com/GoogleCloudPlatform/spanner-migration-tool/schema"
"github.com/GoogleCloudPlatform/spanner-migration-tool/spanner/ddl"
)

type Counter struct {
counterMutex sync.Mutex
ObjectId string
}

var Cntr Counter

func GenerateIdSuffix() string {

counter, _ := strconv.Atoi(Cntr.ObjectId)
// Thread safe Counter to generate ids in session file.
func (c *Counter) GenerateIdSuffix() string {
c.counterMutex.Lock()
counter, _ := strconv.Atoi(c.ObjectId)

counter = counter + 1

Cntr.ObjectId = strconv.Itoa(counter)
return Cntr.ObjectId
c.ObjectId = strconv.Itoa(counter)
returnVal := c.ObjectId
c.counterMutex.Unlock()
return returnVal
}

func GenerateId(idPrefix string) string {
idSuffix := GenerateIdSuffix()
idSuffix := Cntr.GenerateIdSuffix()
id := idPrefix + idSuffix
return id
}
Expand Down
51 changes: 51 additions & 0 deletions internal/helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// 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 internal

import (
"sync"
"testing"

"github.com/stretchr/testify/assert"
)

func TestGenerateIdSuffix(t *testing.T) {
var wg sync.WaitGroup
tests := []struct {
number int
expected string
}{
{10000, "10000"},
{20000, "30000"},
{10000, "40000"},
}
counter := Counter{}
for _, tc := range tests {
// Call GenerateIdSuffix n number of times parallely.
for i := 0; i < tc.number; i++ {
// Increment the WaitGroup counter.
wg.Add(1)
go func() {
counter.GenerateIdSuffix()
// Decrement the counter when the goroutine completes.
defer wg.Done()
}()
}
// Wait for all Go routines in the tc to complete
wg.Wait()
// Assert that the counter is actually incremented to n.
assert.Equal(t, tc.expected, counter.ObjectId)
}
}
10 changes: 6 additions & 4 deletions test_data/goldens/postgres/create_table.test
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
-- invalid_table_name
CREATE TABLE "table-with-invalid-chars$%^&" (
id bigint
);
ALTER TABLE "table-with-invalid-chars$%^&" ADD PRIMARY KEY (id)
--
-- GoogleSQL
CREATE TABLE `table_with_invalid_chars____` (
`id` INT64 NOT NULL ,
) PRIMARY KEY (`id`)
--
-- PostgreSQL
CREATE TABLE table_with_invalid_chars____ (
id INT8 NOT NULL ,
PRIMARY KEY (id)
)
==
-- create_table_happy_path
CREATE TABLE test (
id bigint PRIMARY KEY,
col text
)
--
-- GoogleSQL
CREATE TABLE `test` (
`id` INT64 NOT NULL ,
`col` STRING(MAX),
) PRIMARY KEY (`id`)
--
-- PostgreSQL
CREATE TABLE test (
id INT8 NOT NULL ,
col VARCHAR(2621440),
Expand Down
159 changes: 106 additions & 53 deletions testing/common/goldens.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,101 +16,154 @@ package common

import (
"bufio"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
)

type GoldenTestCase struct {
InputSchema string
ExpectedGSQLSchema string
ExpectedPSQLSchema string
Name string
Input string
GSQLWant string
PSQLWant string
}

type GoldenParseStatus int
type parseStatus int

const (
InputSchema GoldenParseStatus = iota
GSQLSchema
PSQLSchema
parsingInput parseStatus = iota
parsingGSQL
parsingPSQL
)

const (
GoldenTestPartSeparator = "--"
GoldenTestCaseSeparator = "=="
goldenTestCaseNamePrefix = "--"
goldenGoogleSQLExpectation = "-- GoogleSQL"
goldenPostgreSQLExpectation = "-- PostgreSQL"
goldenTestCaseEndOfTest = "=="
)

func GoldenTestCasesFrom(path string) ([]GoldenTestCase, error) {
file, err := os.Open(path)
func GoldenTestCasesFrom(t testing.TB, dir string) []GoldenTestCase {
t.Helper()
var tests []GoldenTestCase
entries, err := os.ReadDir(dir)
if err != nil {
return nil, err
t.Fatalf("error when reading golden tests from dir %s: %s", dir, err)
return nil
}
for _, entry := range entries {
if !entry.IsDir() {
path := filepath.Join(dir, entry.Name())
ts := goldenTestCasesFromFile(t, path)
if err != nil {
return nil
}
tests = append(tests, ts...)
}
}
return tests
}

func goldenTestCasesFromFile(t testing.TB, filePath string) []GoldenTestCase {
t.Helper()
file, err := os.Open(filePath)
dirName := filepath.Base(filepath.Dir(filePath))
fileName := filepath.Base(filePath)
if err != nil {
t.Fatalf("error when reading golden tests from path %s: %s", filePath, err)
return nil
}
defer file.Close()

var testCases []GoldenTestCase
var inputSchema, expectedGSQLSchema, expectedPSQLSchema strings.Builder
parsingStatus := InputSchema
var testName, inputSchema, GSQLWant, PSQLWant strings.Builder
parsingStatus := parsingInput
lineNum := 0
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lineNum++
line := scanner.Text()

// First line should be the test case name
if testName.Len() == 0 {
testName.WriteString(dirName)
testName.WriteString("/")
testName.WriteString(fileName)
testName.WriteString("/")
testName.WriteString(strings.Trim(strings.ReplaceAll(line, goldenTestCaseNamePrefix, ""), ""))
continue
}
// Beginning of GoogleSQL expectation
if line == goldenGoogleSQLExpectation {
if GSQLWant.Len() != 0 {
t.Fatal("bad format: Duplicated GoogleSQL definition in test case")
return nil
}
parsingStatus = parsingGSQL
continue
}
// Beginning of PostgreSQL expectation
if line == goldenPostgreSQLExpectation {
if PSQLWant.Len() != 0 {
t.Fatal("bad format: Duplicated PostgreSQL definition in test case")
return nil
}
parsingStatus = parsingPSQL
continue
}

// End of a test case
if line == GoldenTestCaseSeparator {
if line == goldenTestCaseEndOfTest {
if inputSchema.Len() == 0 {
return nil, fmt.Errorf("bad format: Invalid test case at line %d, missing source schema", lineNum)
t.Fatalf("bad format: Invalid test case at line %d, missing source schema", lineNum)
return nil
}
if expectedGSQLSchema.Len() == 0 {
return nil, fmt.Errorf("bad format: Invalid test case at line %d, missing expected GoogleSQL schema", lineNum)
if GSQLWant.Len() == 0 {
t.Fatalf("bad format: Invalid test case at line %d, missing expected GoogleSQL schema", lineNum)
return nil
}
if expectedPSQLSchema.Len() == 0 {
return nil, fmt.Errorf("bad format: Invalid test case at line %d, missing expected PostgreSQL schema", lineNum)
if PSQLWant.Len() == 0 {
t.Fatalf("bad format: Invalid test case at line %d, missing expected PostgreSQL schema", lineNum)
return nil
}
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
Name: testName.String(),
Input: inputSchema.String(),
GSQLWant: strings.TrimRight(GSQLWant.String(), "\n"),
PSQLWant: strings.TrimRight(PSQLWant.String(), "\n")})
parsingStatus = parsingInput
testName.Reset()
inputSchema.Reset()
expectedGSQLSchema.Reset()
expectedPSQLSchema.Reset()
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)
}
GSQLWant.Reset()
PSQLWant.Reset()
continue
}

// Test body
switch parsingStatus {
case InputSchema:
inputSchema.WriteString(line + "\n")
case GSQLSchema:
expectedGSQLSchema.WriteString(line + "\n")
case PSQLSchema:
expectedPSQLSchema.WriteString(line + "\n")
case parsingInput:
inputSchema.WriteString(line)
inputSchema.WriteString("\n")
case parsingGSQL:
GSQLWant.WriteString(line)
GSQLWant.WriteString("\n")
case parsingPSQL:
PSQLWant.WriteString(line)
PSQLWant.WriteString("\n")
}
}

// Test case is invalid
if parsingStatus != InputSchema {
return nil, fmt.Errorf("bad format: Invalid test case at line %d", lineNum)
// Test case not finished
if parsingStatus != parsingInput {
t.Fatalf("bad format: Invalid test case at line %d", lineNum)
return nil
}

if err := scanner.Err(); err != nil {
return nil, err
t.Fatalf("bad format: Error when scanning golden test file %s: %s", filePath, err)
return nil
}

return testCases, nil
return testCases
}
Loading

0 comments on commit 7daf613

Please sign in to comment.