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

WIP: omitempty bug in InsertRecords #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
27 changes: 27 additions & 0 deletions tests/pgkit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,33 @@ func TestRowsWithBigInt(t *testing.T) {
}
}

func TestSugarInsertRecordsMixedOmit(t *testing.T) {
truncateTable(t, "accounts")

records := []*Account{}
created := time.Now().Add(-time.Hour * 24 * 30)
records = append(records, &Account{Name: "michael", CreatedAt: created})
records = append(records, &Account{Name: "mary"})

// Insert
q1 := DB.SQL.InsertRecords(records) //, "accounts")
qs := make(pgkit.Queries, 0)
qs = append(qs, q1)
_, err := DB.Query.BatchExec(context.Background(), qs)
assert.NoError(t, err)

// Select all
var accounts []*Account
q2 := DB.SQL.Select("*").From("accounts").OrderBy("name")
err = DB.Query.GetAll(context.Background(), q2, &accounts)
assert.NoError(t, err)
assert.Len(t, accounts, 2)
assert.Equal(t, "mary", accounts[0].Name)
assert.Nil(t, accounts[0].CreatedAt)
assert.Equal(t, "michael", accounts[1].Name)
assert.Equal(t, created.Unix(), accounts[1].CreatedAt.Unix())
}

func TestSugarInsertAndSelectMultipleRecords(t *testing.T) {
truncateTable(t, "accounts")

Expand Down
2 changes: 1 addition & 1 deletion tests/testdata/pgkit_test_db.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CREATE TABLE accounts (
id SERIAL PRIMARY KEY,
name VARCHAR(255),
name VARCHAR(255) NOT NULL,
disabled BOOLEAN,
new_column_not_in_code BOOLEAN, -- test for backward-compatible migrations, see https://github.com/goware/pgkit/issues/13
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL
Expand Down