Skip to content

Commit

Permalink
omitempty bug in InsertRecords
Browse files Browse the repository at this point in the history
  • Loading branch information
ScreamingHawk committed Nov 5, 2024
1 parent 8166aa3 commit 14c748a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
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

0 comments on commit 14c748a

Please sign in to comment.