From af768792fe97cabcadeb582e939029e1c75d58ea Mon Sep 17 00:00:00 2001 From: Kamil Polakowski Date: Fri, 2 Oct 2020 12:55:22 +0200 Subject: [PATCH 1/2] don't set created_at to now() by default for updates --- repository_test.go | 6 ------ structset.go | 2 +- structset_test.go | 3 +-- 3 files changed, 2 insertions(+), 9 deletions(-) diff --git a/repository_test.go b/repository_test.go index a9768697..acb4608d 100644 --- a/repository_test.go +++ b/repository_test.go @@ -1253,7 +1253,6 @@ func TestRepository_Update_saveBelongsTo(t *testing.T) { ID: 1, Name: "name", UpdatedAt: now(), - CreatedAt: now(), }, }, address) @@ -1340,7 +1339,6 @@ func TestRepository_Update_saveHasOne(t *testing.T) { assert.Equal(t, User{ ID: userID, UpdatedAt: now(), - CreatedAt: now(), Address: Address{ ID: 1, Street: "street", @@ -1372,7 +1370,6 @@ func TestRepository_Update_saveHasOneCascadeDisabled(t *testing.T) { assert.Equal(t, User{ ID: userID, UpdatedAt: now(), - CreatedAt: now(), Address: Address{ ID: 1, Street: "street", @@ -1432,7 +1429,6 @@ func TestRepository_Update_saveHasMany(t *testing.T) { assert.Nil(t, repo.Update(context.TODO(), &user)) assert.Equal(t, User{ ID: 10, - CreatedAt: now(), UpdatedAt: now(), Transactions: []Transaction{ { @@ -1466,7 +1462,6 @@ func TestRepository_Update_saveHasManyCascadeDisabled(t *testing.T) { assert.Nil(t, repo.Update(context.TODO(), &user, Cascade(false))) assert.Equal(t, User{ ID: 10, - CreatedAt: now(), UpdatedAt: now(), Transactions: []Transaction{ { @@ -2249,7 +2244,6 @@ func TestRepository_saveHasMany_replace(t *testing.T) { assert.Nil(t, repo.(*repository).saveHasMany(cw, doc, &mutation, false)) assert.Equal(t, User{ ID: 1, - CreatedAt: now(), UpdatedAt: now(), Transactions: []Transaction{ {ID: 3, BuyerID: 1, Item: "item3"}, diff --git a/structset.go b/structset.go index 9531b832..86593c69 100644 --- a/structset.go +++ b/structset.go @@ -28,7 +28,7 @@ func (s Structset) Apply(doc *Document, mut *Mutation) { switch field { case "created_at", "inserted_at": if doc.Flag(HasCreatedAt) { - if value, ok := doc.Value(field); ok && value.(time.Time).IsZero() { + if value, ok := doc.Value(field); ok && value.(time.Time).IsZero() && isZero(doc.PrimaryValue()) { s.set(doc, mut, field, t, true) continue } diff --git a/structset_test.go b/structset_test.go index 374a8057..41ddce95 100644 --- a/structset_test.go +++ b/structset_test.go @@ -44,7 +44,7 @@ func TestStructset(t *testing.T) { "id": Set("id", 1), "name": Set("name", "Luffy"), "age": Set("age", 0), - "created_at": Set("created_at", now()), + "created_at": Set("created_at", time.Time{}), "updated_at": Set("updated_at", now()), }, } @@ -85,7 +85,6 @@ func TestStructset_skipZero(t *testing.T) { Mutates: map[string]Mutate{ "id": Set("id", 1), "name": Set("name", "Luffy"), - "created_at": Set("created_at", now()), "updated_at": Set("updated_at", now()), }, } From 63568c9f74ea429edc225f794c736d81998b4127 Mon Sep 17 00:00:00 2001 From: Kamil Polakowski Date: Thu, 15 Oct 2020 17:42:38 +0200 Subject: [PATCH 2/2] fix tests --- adapter/specs/insert.go | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/adapter/specs/insert.go b/adapter/specs/insert.go index 1026ab2f..fe899979 100644 --- a/adapter/specs/insert.go +++ b/adapter/specs/insert.go @@ -2,6 +2,7 @@ package specs import ( "testing" + "time" "github.com/go-rel/rel" "github.com/go-rel/rel/where" @@ -144,17 +145,19 @@ func Inserts(t *testing.T, repo rel.Repository) { repo.MustInsert(ctx, &user) + now := time.Now() + tests := []interface{}{ &User{}, &User{Name: "insert", Age: 100}, &User{Name: "insert", Age: 100, Note: ¬e}, &User{Note: ¬e}, - &User{ID: 123, Name: "insert", Age: 100, Note: ¬e}, + &User{ID: 123, Name: "insert", Age: 100, Note: ¬e, CreatedAt: now}, &Address{}, &Address{Name: "work"}, &Address{UserID: &user.ID}, &Address{Name: "work", UserID: &user.ID}, - &Address{ID: 123, Name: "work", UserID: &user.ID}, + &Address{ID: 123, Name: "work", UserID: &user.ID, CreatedAt: now}, &Composite{Primary1: 1, Primary2: 2, Data: "data-1-2"}, } @@ -192,19 +195,21 @@ func InsertAll(t *testing.T, repo rel.Repository) { repo.MustInsert(ctx, &user) + now := time.Now() + tests := []interface{}{ &[]User{{}}, &[]User{{Name: "insert", Age: 100}}, &[]User{{Name: "insert", Age: 100, Note: ¬e}}, &[]User{{Note: ¬e}}, &[]User{{Name: "insert", Age: 100}, {Name: "insert too"}}, - &[]User{{ID: 224, Name: "insert", Age: 100}, {ID: 234, Name: "insert too"}}, + &[]User{{ID: 224, Name: "insert", Age: 100, CreatedAt: now}, {ID: 234, Name: "insert too", CreatedAt: now}}, &[]Address{{}}, &[]Address{{Name: "work"}}, &[]Address{{UserID: &user.ID}}, &[]Address{{Name: "work", UserID: &user.ID}}, &[]Address{{Name: "work"}, {Name: "home"}}, - &[]Address{{ID: 233, Name: "work"}, {ID: 235, Name: "home"}}, + &[]Address{{ID: 233, Name: "work", CreatedAt: now}, {ID: 235, Name: "home", CreatedAt: now}}, } for _, record := range tests { @@ -217,10 +222,13 @@ func InsertAll(t *testing.T, repo rel.Repository) { // InsertAllPartialCustomPrimary tests insert multiple specifications. func InsertAllPartialCustomPrimary(t *testing.T, repo rel.Repository) { + + now := time.Now() + tests := []interface{}{ - &[]User{{ID: 300, Name: "insert 300", Age: 100}, {Name: "insert 300+?"}}, - &[]User{{Name: "insert 305-?", Age: 100}, {ID: 305, Name: "insert 305+?"}}, - &[]User{{Name: "insert 310-?"}, {ID: 310, Name: "insert 310", Age: 100}, {Name: "insert 300+?"}}, + &[]User{{ID: 300, Name: "insert 300", Age: 100, CreatedAt: now}, {Name: "insert 300+?"}}, + &[]User{{Name: "insert 305-?", Age: 100}, {ID: 305, Name: "insert 305+?", CreatedAt: now}}, + &[]User{{Name: "insert 310-?"}, {ID: 310, Name: "insert 310", Age: 100, CreatedAt: now}, {Name: "insert 300+?"}}, } for _, record := range tests {