Skip to content

Commit

Permalink
yaml: add a test case for all data types
Browse files Browse the repository at this point in the history
  • Loading branch information
apstndb committed Jul 26, 2022
1 parent 225da12 commit ed811a3
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 0 deletions.
78 changes: 78 additions & 0 deletions internal/spanner/spanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package spanner
import (
"context"
"fmt"
"math/big"
"os"
"testing"
"time"

"cloud.google.com/go/civil"
"cloud.google.com/go/spanner"
"github.com/google/go-cmp/cmp"

Expand All @@ -30,6 +33,20 @@ type baz struct {
Name string `spanner:"Name"`
}

type allTypes struct {
ID string `spanner:"ID"`
StringValue string `spanner:"StringValue"`
BoolValue bool `spanner:"BoolValue"`
Int64Value int64 `spanner:"Int64Value"`
Float64Value float64 `spanner:"Float64Value"`
JSONValue spanner.NullJSON `spanner:"JSONValue"`
BytesValue []byte `spanner:"BytesValue"`
TimestampValue time.Time `spanner:"TimestampValue"`
NumericValue *big.Rat `spanner:"NumericValue"`
DateValue civil.Date `spanner:"DateValue"`
StringArray []string `spanner:"StringArray"`
}

func TestSave(t *testing.T) {
t.Parallel()
ctx := context.Background()
Expand Down Expand Up @@ -113,6 +130,27 @@ func TestSave(t *testing.T) {
},
},
},
{
Name: "AllTypes",
Records: []*model.Record{
{
Values: map[string]interface{}{
"DateValue": "2022-04-01",
"Float64Value": float64(3.14159),
"ID": "All_Type_Values",
"Int64Value": int64(42),
"JSONValue": `{"test": 1}`,
"NumericValue": "-12345678901234567890123456789.123456789",
"StringValue": "FooBar",
"TimestampValue": "2022-04-01T00:00:00Z",
// Commented out because it is not yet supported.
// "StringArray": []interface{}{"Foo", "Bar"},
"BoolValue": true,
"BytesValue": "aG9nZQ==",
},
},
},
},
})
if err != nil {
t.Errorf("failed to save: %s", err)
Expand Down Expand Up @@ -217,6 +255,46 @@ func TestSave(t *testing.T) {
if diff := cmp.Diff(actualBazs, expectedBazs); diff != "" {
t.Errorf("\n(-actual, +expected)\n%s", diff)
}

var actualAllTypes []*allTypes
err = db.client.ReadOnlyTransaction().Query(ctx, spanner.Statement{
SQL: "SELECT * FROM AllTypes ORDER BY ID",
}).Do(func(row *spanner.Row) error {
b := new(allTypes)
if err = row.ToStruct(b); err != nil {
return fmt.Errorf("failed to populate allTypes by rows: %w", err)
}
actualAllTypes = append(actualAllTypes, b)

return nil
})
if err != nil {
t.Errorf("failed to select AllTypes: %s", err)
return
}
expectedNumeric, _ := new(big.Rat).SetString("-12345678901234567890123456789.123456789")
expectedAllTypes := []*allTypes{
{
ID: "All_Type_Values",
StringValue: "FooBar",
BoolValue: true,
Int64Value: 42,
Float64Value: 3.14159,
JSONValue: spanner.NullJSON{Value: map[string]interface{}{"test": float64(1)}, Valid: true},
BytesValue: []byte{'h', 'o', 'g', 'e'},
TimestampValue: time.Date(2022, time.April, 1, 0, 0, 0, 0, time.UTC),
NumericValue: expectedNumeric,
DateValue: civil.Date{2022, time.April, 1},
},
}
if diff := cmp.Diff(actualAllTypes, expectedAllTypes, cmp.Comparer(func(x, y *big.Rat) bool {
if x == nil || y == nil {
return false
}
return x.Cmp(y) == 0
})); diff != "" {
t.Errorf("\n(-actual, +expected)\n%s", diff)
}
}

func TestSortTablesByDependencies(t *testing.T) {
Expand Down
14 changes: 14 additions & 0 deletions internal/spanner/testdata/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,17 @@ CREATE TABLE Boo (
Name STRING(MAX),
CONSTRAINT FK_BooBaz FOREIGN KEY (BazID) REFERENCES Baz (BazID)
) PRIMARY KEY(BooID);

CREATE TABLE AllTypes (
ID STRING(MAX) NOT NULL,
BoolValue BOOL,
Int64Value INT64,
Float64Value FLOAT64,
TimestampValue TIMESTAMP,
DateValue DATE,
StringValue STRING(MAX),
BytesValue BYTES(MAX),
NumericValue NUMERIC,
JSONValue JSON,
StringArray ARRAY<STRING(MAX)>,
) PRIMARY KEY(ID);
16 changes: 16 additions & 0 deletions internal/yaml/testdata/seeds/AllTypes.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
# Values are need to be encoded as documented.
# https://cloud.google.com/spanner/docs/reference/rpc/google.spanner.v1#google.spanner.v1.TypeCode
- ID: "All_Type_Values"
BoolValue: true
BytesValue: "aG9nZQ==" # base64("hoge")
DateValue: "2022-04-01"
Float64Value: 3.14159
Int64Value: 42
JSONValue: '{"test": 1}'
NumericValue: "-12345678901234567890123456789.123456789"
StringValue: "FooBar"
TimestampValue: "2022-04-01T00:00:00Z"
# StringArray:
# - "Foo"
# - "Bar"
20 changes: 20 additions & 0 deletions internal/yaml/yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,26 @@ func TestLoad(t *testing.T) {
}

expected := []*model.Table{
{
Name: "AllTypes",
Records: []*model.Record{
{
Values: map[string]interface{}{
"DateValue": "2022-04-01",
"Float64Value": float64(3.14159),
"ID": "All_Type_Values",
"Int64Value": int64(42),
"JSONValue": `{"test": 1}`,
"NumericValue": string("-12345678901234567890123456789.123456789"),
"StringValue": "FooBar",
"TimestampValue": "2022-04-01T00:00:00Z",
// "StringArray": []interface{}{"Foo", "Bar"},
"BoolValue": true,
"BytesValue": "aG9nZQ==",
},
},
},
},
{
Name: "Bar",
Records: []*model.Record{
Expand Down

0 comments on commit ed811a3

Please sign in to comment.