From 14c748a6d7f30ba000d38d6129ac99885130273e Mon Sep 17 00:00:00 2001 From: Michael Standen Date: Tue, 5 Nov 2024 12:15:28 +1300 Subject: [PATCH] omitempty bug in InsertRecords --- tests/pgkit_test.go | 27 +++++++++++++++++++++++++++ tests/testdata/pgkit_test_db.sql | 2 +- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/tests/pgkit_test.go b/tests/pgkit_test.go index 1b2cb47..38a29dc 100644 --- a/tests/pgkit_test.go +++ b/tests/pgkit_test.go @@ -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") diff --git a/tests/testdata/pgkit_test_db.sql b/tests/testdata/pgkit_test_db.sql index a55dbf8..cf32f8e 100644 --- a/tests/testdata/pgkit_test_db.sql +++ b/tests/testdata/pgkit_test_db.sql @@ -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