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

add test for AllowCommitTimestamp #158

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ jobs:
name: regenerate testdata and check diff
command: |
make testdata YOBIN=./yo
git diff --quiet test/
git diff test/

v2test:
docker:
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ testdata-from-ddl/default:
$(YOBIN) generate ./test/testdata/schema.sql --from-ddl --package models --out test/testmodels/default/

testdata-from-ddl/underscore:
rm -rf test/testmodels/underscores && mkdir -p test/testmodels/underscores
$(YOBIN) generate ./test/testdata/schema.sql --from-ddl --package models --underscore --out test/testmodels/underscores/
rm -rf test/testmodels/underscore && mkdir -p test/testmodels/underscore
$(YOBIN) generate ./test/testdata/schema.sql --from-ddl --package models --underscore --out test/testmodels/underscore/

testdata-from-ddl/single:
rm -rf test/testmodels/single && mkdir -p test/testmodels/single
Expand Down
14 changes: 7 additions & 7 deletions loaders/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,13 @@ func (s *SpannerLoaderFromDDL) ColumnList(name string) ([]*models.Column, error)
}

cols = append(cols, &models.Column{
FieldOrdinal: i + 1,
ColumnName: c.Name.Name,
DataType: c.Type.SQL(),
NotNull: c.NotNull,
IsPrimaryKey: pk,
IsGenerated: isGenerated,
AllowCommitTimestamp: allowCommitTimestamp,
FieldOrdinal: i + 1,
ColumnName: c.Name.Name,
DataType: c.Type.SQL(),
NotNull: c.NotNull,
IsPrimaryKey: pk,
IsGenerated: isGenerated,
IsAllowCommitTimestamp: allowCommitTimestamp,
})
}

Expand Down
14 changes: 7 additions & 7 deletions models/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ type Table struct {

// Column represents column info.
type Column struct {
FieldOrdinal int // field_ordinal
ColumnName string // column_name
DataType string // data_type
NotNull bool // not_null
IsPrimaryKey bool // is_primary_key
IsGenerated bool // is_generated
AllowCommitTimestamp bool // allow_commit_timestamp
FieldOrdinal int // field_ordinal
ColumnName string // column_name
DataType string // data_type
NotNull bool // not_null
IsPrimaryKey bool // is_primary_key
IsGenerated bool // is_generated
IsAllowCommitTimestamp bool // is_allow_commit_timestamp
}

// Index represents an index.
Expand Down
4 changes: 3 additions & 1 deletion templates/type.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ func ({{ $short }} *{{ .Name }}) columnsToValues(cols []string) ([]interface{},
switch col {
{{- range .Fields }}
case "{{ colname .Col }}":
{{- if .CustomType }}
{{- if .Col.IsAllowCommitTimestamp }}
ret = append(ret, spanner.CommitTimestamp)
{{- else if .CustomType }}
ret = append(ret, {{ .Type }}({{ $short }}.{{ .Name }}))
{{- else }}
ret = append(ret, {{ $short }}.{{ .Name }})
Expand Down
66 changes: 66 additions & 0 deletions test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,72 @@ func TestCustomCompositePrimaryKey(t *testing.T) {
})
}

func TestAllowCommitTimestamp(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

v := &models.AllowCommitTimestamp{
ID: 300,
UpdatedAt: spanner.CommitTimestamp,
}

if _, err := client.Apply(ctx, []*spanner.Mutation{v.Insert(ctx)}); err != nil {
t.Fatalf("Apply failed: %v", err)
}

var insertTime time.Time
t.Run("Insert", func(t *testing.T) {
got, err := models.FindAllowCommitTimestamp(ctx, client.Single(), 300)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got.UpdatedAt.IsZero() {
t.Errorf("got UpdatedAt.IsZero")
}
insertTime = got.UpdatedAt
})

t.Run("Update", func(t *testing.T) {
gc := &models.AllowCommitTimestamp{
ID: 300,
UpdatedAt: spanner.CommitTimestamp,
}

if _, err := client.Apply(ctx, []*spanner.Mutation{gc.Update(ctx)}); err != nil {
t.Fatalf("Apply failed: %v", err)
}

got, err := models.FindAllowCommitTimestamp(ctx, client.Single(), 300)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

if !got.UpdatedAt.After(insertTime) {
t.Errorf("expected UpdatedAt (%v) to be after insertTime (%v)", got.UpdatedAt, insertTime)
}
})

t.Run("InsertOrUpdate", func(t *testing.T) {
gc := &models.AllowCommitTimestamp{
ID: 300,
UpdatedAt: spanner.CommitTimestamp,
}

if _, err := client.Apply(ctx, []*spanner.Mutation{gc.InsertOrUpdate(ctx)}); err != nil {
t.Fatalf("Apply failed: %v", err)
}

got, err := models.FindAllowCommitTimestamp(ctx, client.Single(), 300)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

if got.UpdatedAt.IsZero() {
t.Errorf("got UpdatedAt.IsZero")
}
})
}

func TestGeneratedColumn(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
Expand Down
5 changes: 5 additions & 0 deletions test/testdata/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,8 @@ CREATE TABLE GeneratedColumns (
LastName STRING(50) NOT NULL,
FullName STRING(100) NOT NULL AS (ARRAY_TO_STRING([FirstName, LastName], " ")) STORED,
) PRIMARY KEY (ID);

CREATE TABLE AllowCommitTimestamp (
ID INT64 NOT NULL,
UpdatedAt TIMESTAMP NOT NULL OPTIONS (allow_commit_timestamp=true)
) PRIMARY KEY(ID);
175 changes: 175 additions & 0 deletions test/testmodels/customtypes/allowcommittimestamp.yo.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading