Skip to content
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

Improve integration tests #85

Merged
merged 2 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ require (
github.com/apstndb/lox v0.0.0-20241102092239-40172f618f5c
github.com/apstndb/memebridge v0.0.0-20241123172322-a745771fe5be
github.com/apstndb/spannerplanviz v0.3.2
github.com/apstndb/spantype v0.3.0
github.com/apstndb/spantype v0.3.2
github.com/apstndb/spanvalue v0.0.0-20241103175520-dc3408b8d84e
github.com/cloudspannerecosystem/memefish v0.0.0-20241106111047-2b2b4b23a1e7
github.com/cloudspannerecosystem/memefish v0.0.0-20241203074241-66dfc61aa2dd
github.com/go-json-experiment/json v0.0.0-20241127185351-9802db03f36a
github.com/google/go-cmp v0.6.0
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.1.0
github.com/hymkor/go-multiline-ny v0.17.0
github.com/jessevdk/go-flags v1.6.1
github.com/k0kubun/pp v3.0.1+incompatible
github.com/k0kubun/pp/v3 v3.4.1
github.com/mattn/go-runewidth v0.0.16
github.com/ngicks/go-iterator-helper v0.0.15
github.com/nyaosorg/go-readline-ny v1.6.2
Expand Down Expand Up @@ -69,7 +70,6 @@ require (
github.com/envoyproxy/go-control-plane v0.13.0 // indirect
github.com/envoyproxy/protoc-gen-validate v1.1.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-json-experiment/json v0.0.0-20241127185351-9802db03f36a // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
Expand All @@ -79,7 +79,7 @@ require (
github.com/google/uuid v1.6.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.4 // indirect
github.com/googleapis/gax-go/v2 v2.13.0 // indirect
github.com/k0kubun/pp/v3 v3.4.1 // indirect
github.com/k0kubun/pp v3.0.1+incompatible // indirect
github.com/klauspost/compress v1.17.4 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
Expand Down
17 changes: 4 additions & 13 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -670,10 +670,8 @@ github.com/apstndb/memebridge v0.0.0-20241123172322-a745771fe5be h1:nkxHsdoFzh9m
github.com/apstndb/memebridge v0.0.0-20241123172322-a745771fe5be/go.mod h1:FaufbBYU/dQaQChu/b62igLoLJmT2Qj8lPEdbxRls2c=
github.com/apstndb/spannerplanviz v0.3.2 h1:AeTNTdE05+7T4HWPV1yjLSYCbr51ZugtR3KCkw5SAD0=
github.com/apstndb/spannerplanviz v0.3.2/go.mod h1:/tY1Y1tLTp3Czj9BI2/M02W3P/wJgmKwuDS5zARLhvc=
github.com/apstndb/spantype v0.2.0 h1:yoV0riMXhLeT9z8D1zhvwIraOvIU0fDKwWrWgtmoNns=
github.com/apstndb/spantype v0.2.0/go.mod h1:zn3qR4ebyy3HumRKRG0COMRSf4tmaR1HptQwe7/Xu7g=
github.com/apstndb/spantype v0.3.0 h1:MX9ODH338O7KW7IHxiv09uaZZRQS6PZm3t9s/wdiPSQ=
github.com/apstndb/spantype v0.3.0/go.mod h1:zn3qR4ebyy3HumRKRG0COMRSf4tmaR1HptQwe7/Xu7g=
github.com/apstndb/spantype v0.3.2 h1:QScfFNjW0KoPcqnn3tSZ15p+cRDVF0u6g/KJIqQNWuM=
github.com/apstndb/spantype v0.3.2/go.mod h1:zn3qR4ebyy3HumRKRG0COMRSf4tmaR1HptQwe7/Xu7g=
github.com/apstndb/spanvalue v0.0.0-20241103175520-dc3408b8d84e h1:uWViNQM3/1RPtJcw6+4l3XHAm4uCWCWe0xVQvpXj8mM=
github.com/apstndb/spanvalue v0.0.0-20241103175520-dc3408b8d84e/go.mod h1:NV6r4opNnDtM+cU6IjbMyAtNbV/oIX8X9xknIGJ1B8k=
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
Expand All @@ -697,8 +695,8 @@ github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWR
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cloudspannerecosystem/memefish v0.0.0-20241106111047-2b2b4b23a1e7 h1:vmS9Nvh7ij5EVnMwvkWsL84xzx5cGM3j/SJlM1zfVLE=
github.com/cloudspannerecosystem/memefish v0.0.0-20241106111047-2b2b4b23a1e7/go.mod h1:iYAaNZfVIn4QYfUmXt+3EeHAok/kqpN/fp/8kgDHjx8=
github.com/cloudspannerecosystem/memefish v0.0.0-20241203074241-66dfc61aa2dd h1:CYg8aAFxk8pOKA7bJFQ5HffpKLkQmLPaMpfMkjTWNys=
github.com/cloudspannerecosystem/memefish v0.0.0-20241203074241-66dfc61aa2dd/go.mod h1:iYAaNZfVIn4QYfUmXt+3EeHAok/kqpN/fp/8kgDHjx8=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
Expand Down Expand Up @@ -921,10 +919,6 @@ github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/jung-kurt/gofpdf v1.0.0/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes=
github.com/jung-kurt/gofpdf v1.0.3-0.20190309125859-24315acbbda5/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes=
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 h1:uC1QfSlInpQF+M0ao65imhwqKnz3Q2z/d8PWZRMQvDM=
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88/go.mod h1:3w7q1U84EfirKl04SVQ/s7nPm1ZPhiXd34z40TNz36k=
github.com/k0kubun/pp v1.3.1-0.20200204103551-99835366d1cc h1:XLjmW07gT7cG/wb6mavIrvAIWBYaTacPo8UOnxGSspA=
github.com/k0kubun/pp v1.3.1-0.20200204103551-99835366d1cc/go.mod h1:qK2ivXw91omfE1uXcpR5kWbAMZRdDOnGbqWlZ7reRFk=
github.com/k0kubun/pp v3.0.1+incompatible h1:3tqvf7QgUnZ5tXO6pNAZlrvHgl6DvifjDrd9g2S9Z40=
github.com/k0kubun/pp v3.0.1+incompatible/go.mod h1:GWse8YhT0p8pT4ir3ZgBbfZild3tgzSScAn6HmfYukg=
github.com/k0kubun/pp/v3 v3.4.1 h1:1WdFZDRRqe8UsR61N/2RoOZ3ziTEqgTPVqKrHeb779Y=
Expand Down Expand Up @@ -956,10 +950,8 @@ github.com/lyft/protoc-gen-star v0.6.1/go.mod h1:TGAoBVkt8w7MPG72TrKIu85MIdXwDuz
github.com/lyft/protoc-gen-star/v2 v2.0.1/go.mod h1:RcCdONR2ScXaYnQC5tUzxzlpA3WVYF7/opLeUgcQs/o=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
Expand Down Expand Up @@ -1308,7 +1300,6 @@ golang.org/x/sync v0.9.0 h1:fEo0HyrW1GIgZdpbhCRO0PkJajUS5H9IFUztCgEo2jQ=
golang.org/x/sync v0.9.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
244 changes: 233 additions & 11 deletions integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ import (
"cloud.google.com/go/spanner/admin/database/apiv1/databasepb"
instance "cloud.google.com/go/spanner/admin/instance/apiv1"
"cloud.google.com/go/spanner/admin/instance/apiv1/instancepb"

// Use dot import to simplify tests
. "github.com/apstndb/spantype/testutil"
"github.com/testcontainers/testcontainers-go/modules/gcloud"
"spheric.cloud/xiter"
)
Expand Down Expand Up @@ -203,6 +206,39 @@ func generateUniqueTableId() string {
return fmt.Sprintf("spanner_cli_test_%d_%d", time.Now().UnixNano(), count)
}

func setupSession(t *testing.T, ctx context.Context, spannerContainer *gcloud.GCloudContainer, teardownDDLs []string) (*Session, func()) {
options := defaultClientOptions(spannerContainer)
session, err := NewSession(ctx, &systemVariables{
Project: spannerContainer.Settings.ProjectID,
Instance: testInstanceId,
Database: testDatabaseId,
RPCPriority: sppb.RequestOptions_PRIORITY_UNSPECIFIED}, options...)
if err != nil {
t.Fatalf("failed to create test session: err=%s", err)
}

dbPath := databasePath(spannerContainer.Settings.ProjectID, testInstanceId, testDatabaseId)

tearDown := func() {
op, err := session.adminClient.UpdateDatabaseDdl(ctx, &adminpb.UpdateDatabaseDdlRequest{
Database: dbPath,
Statements: teardownDDLs,
})
if err != nil {
t.Fatalf("failed to drop table: err=%s", err)
}
if err := op.Wait(ctx); err != nil {
t.Fatalf("failed to drop table: err=%s", err)
}
}
return session, tearDown
}

var testTableRowType = sliceOf(
NameCodeToStructTypeField("id", sppb.TypeCode_INT64),
NameCodeToStructTypeField("active", sppb.TypeCode_BOOL),
)

func setup(t *testing.T, ctx context.Context, spannerContainer *gcloud.GCloudContainer, dmls []string) (*Session, string, func()) {
options := defaultClientOptions(spannerContainer)
session, err := NewSession(ctx, &systemVariables{
Expand Down Expand Up @@ -265,17 +301,18 @@ func setup(t *testing.T, ctx context.Context, spannerContainer *gcloud.GCloudCon
return session, tableId, tearDown
}

func compareResult(t *testing.T, got *Result, expected *Result) {
func compareResult[T any](t *testing.T, got T, expected T) {
t.Helper()
opts := []cmp.Option{
cmpopts.IgnoreFields(Result{}, "Stats"),
cmpopts.IgnoreFields(Result{}, "Timestamp"),
// Commit Stats is only provided by real instances
cmpopts.IgnoreFields(Result{}, "CommitStats"),
cmpopts.EquateEmpty(),
protocmp.Transform(),
}
if !cmp.Equal(got, expected, opts...) {
t.Errorf("diff: %s", cmp.Diff(got, expected, opts...))
t.Errorf("diff(-got, +expected): %s", cmp.Diff(got, expected, opts...))
}
}

Expand Down Expand Up @@ -308,11 +345,8 @@ func TestSelect(t *testing.T) {
Row{[]string{"2", "false"}},
},
AffectedRows: 2,
ColumnTypes: []*sppb.StructType_Field{
{Name: "id", Type: &sppb.Type{Code: sppb.TypeCode_INT64}},
{Name: "active", Type: &sppb.Type{Code: sppb.TypeCode_BOOL}},
},
IsMutation: false,
ColumnTypes: testTableRowType,
IsMutation: false,
})
}

Expand Down Expand Up @@ -418,6 +452,197 @@ func TestSystemVariables(t *testing.T) {
})
}

func TestStatements(t *testing.T) {
spannerContainer, teardown := initialize(t)
defer teardown()

tests := []struct {
desc string
stmt []string
wantResults []*Result
teardownDDLs []string
}{
{
desc: "begin, insert THEN RETURN, rollback, select",
stmt: sliceOf(
"CREATE TABLE TestTable1(id INT64, active BOOL) PRIMARY KEY(id)",
"BEGIN",
"INSERT INTO TestTable1 (id, active) VALUES (1, true), (2, false) THEN RETURN *",
"ROLLBACK",
"SELECT id, active FROM TestTable1 ORDER BY id ASC",
),
teardownDDLs: sliceOf("DROP TABLE TestTable1"),
wantResults: []*Result{
{IsMutation: true},
{IsMutation: true},
{
IsMutation: true, AffectedRows: 2,
Rows: sliceOf(
toRow("1", "true"),
toRow("2", "false"),
),
ColumnNames: sliceOf("id", "active"),
ColumnTypes: testTableRowType,
},
{IsMutation: true},
{
Rows: []Row{},
ColumnNames: sliceOf("id", "active"),
ColumnTypes: testTableRowType,
},
},
},
{
desc: "begin, insert, commit, select",
stmt: sliceOf(
"CREATE TABLE TestTable2(id INT64, active BOOL) PRIMARY KEY(id)",
"BEGIN",
"INSERT INTO TestTable2 (id, active) VALUES (1, true), (2, false)",
"COMMIT",
"SELECT id, active FROM TestTable2 ORDER BY id ASC",
),
teardownDDLs: sliceOf("DROP TABLE TestTable2"),
wantResults: []*Result{
{IsMutation: true},
{IsMutation: true},
{IsMutation: true, AffectedRows: 2},
{IsMutation: true},
{
AffectedRows: 2,
Rows: sliceOf(toRow("1", "true"), toRow("2", "false")),
ColumnNames: sliceOf("id", "active"),
ColumnTypes: testTableRowType,
},
},
},
{
desc: "read-only transactions",
stmt: sliceOf(
"CREATE TABLE TestTable3(id INT64, active BOOL) PRIMARY KEY(id)",
"INSERT INTO TestTable3 (id, active) VALUES (1, true), (2, false)",
"BEGIN RO",
"SELECT id, active FROM TestTable3 ORDER BY id ASC",
"ROLLBACK",
"BEGIN",
"SET TRANSACTION READ ONLY",
"SELECT id, active FROM TestTable3 ORDER BY id ASC",
"COMMIT",
"SET READONLY = TRUE",
"BEGIN",
"SELECT id, active FROM TestTable3 ORDER BY id ASC",
"COMMIT",
),
teardownDDLs: sliceOf("DROP TABLE TestTable3"),
wantResults: []*Result{
{IsMutation: true},
{IsMutation: true, AffectedRows: 2},
{IsMutation: true},
{
AffectedRows: 2,
Rows: sliceOf(toRow("1", "true"), toRow("2", "false")),
ColumnNames: sliceOf("id", "active"),
ColumnTypes: testTableRowType,
},
{IsMutation: true},
{IsMutation: true},
{IsMutation: true},
{
AffectedRows: 2,
Rows: sliceOf(toRow("1", "true"), toRow("2", "false")),
ColumnNames: sliceOf("id", "active"),
ColumnTypes: testTableRowType,
},
{IsMutation: true},
{KeepVariables: true},
{IsMutation: true},
{
AffectedRows: 2,
Rows: sliceOf(toRow("1", "true"), toRow("2", "false")),
ColumnNames: sliceOf("id", "active"),
ColumnTypes: testTableRowType,
},
{IsMutation: true},
},
},
{
desc: "read-write transactions",
stmt: sliceOf(
"CREATE TABLE TestTable4(id INT64, active BOOL) PRIMARY KEY(id)",
"INSERT INTO TestTable4 (id, active) VALUES (1, true), (2, false)",
"BEGIN",
"DELETE TestTable4 WHERE TRUE THEN RETURN *",
"ROLLBACK",
"BEGIN",
"SET TRANSACTION READ WRITE",
"DELETE TestTable4 WHERE TRUE THEN RETURN *",
"ROLLBACK",
"BEGIN RW",
"DELETE TestTable4 WHERE TRUE THEN RETURN *",
"COMMIT",
),
teardownDDLs: sliceOf("DROP TABLE TestTable4"),
wantResults: []*Result{
{IsMutation: true},
{IsMutation: true, AffectedRows: 2},
{IsMutation: true},
{
IsMutation: true,
AffectedRows: 2,
Rows: sliceOf(toRow("1", "true"), toRow("2", "false")),
ColumnNames: sliceOf("id", "active"),
ColumnTypes: testTableRowType,
},
{IsMutation: true},
{IsMutation: true},
{IsMutation: true},
{
IsMutation: true,
AffectedRows: 2,
Rows: sliceOf(toRow("1", "true"), toRow("2", "false")),
ColumnNames: sliceOf("id", "active"),
ColumnTypes: testTableRowType,
},
{IsMutation: true},
{IsMutation: true},
{
IsMutation: true,
AffectedRows: 2,
Rows: sliceOf(toRow("1", "true"), toRow("2", "false")),
ColumnNames: sliceOf("id", "active"),
ColumnTypes: testTableRowType,
},
{IsMutation: true},
},
},
}

for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 180*time.Second)
defer cancel()

session, tearDown := setupSession(t, ctx, spannerContainer, tt.teardownDDLs)
defer tearDown()

var gots []*Result
for i, s := range tt.stmt {
// begin
stmt, err := BuildStatement(s)
if err != nil {
t.Fatalf("invalid statement[%d]: error=%s", i, err)
}

result, err := stmt.Execute(ctx, session)
if err != nil {
t.Fatalf("unexpected error happened[%d]: %s", i, err)
}
gots = append(gots, result)
}
compareResult(t, gots, tt.wantResults)
})
}
}

func TestReadWriteTransaction(t *testing.T) {
spannerContainer, teardown := initialize(t)
defer teardown()
Expand Down Expand Up @@ -726,10 +951,7 @@ func TestReadOnlyTransaction(t *testing.T) {
Row{[]string{"1", "true"}},
Row{[]string{"2", "false"}},
},
ColumnTypes: []*sppb.StructType_Field{
{Name: "id", Type: &sppb.Type{Code: sppb.TypeCode_INT64}},
{Name: "active", Type: &sppb.Type{Code: sppb.TypeCode_BOOL}},
},
ColumnTypes: testTableRowType,
AffectedRows: 2,
IsMutation: false,
})
Expand Down
Loading
Loading