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 { diff --git a/repository_test.go b/repository_test.go index 2c8d5afd..75c4354e 100644 --- a/repository_test.go +++ b/repository_test.go @@ -1255,7 +1255,6 @@ func TestRepository_Update_saveBelongsTo(t *testing.T) { ID: 1, Name: "name", UpdatedAt: now(), - CreatedAt: now(), }, }, profile) @@ -1345,7 +1344,6 @@ func TestRepository_Update_saveHasOne(t *testing.T) { assert.Equal(t, User{ ID: userID, UpdatedAt: now(), - CreatedAt: now(), Address: Address{ ID: 1, Street: "street", @@ -1377,7 +1375,6 @@ func TestRepository_Update_saveHasOneCascadeDisabled(t *testing.T) { assert.Equal(t, User{ ID: userID, UpdatedAt: now(), - CreatedAt: now(), Address: Address{ ID: 1, Street: "street", @@ -1434,7 +1431,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(), UserRoles: []UserRole{ {UserID: 10, RoleID: 2}, @@ -1461,7 +1457,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(), UserRoles: []UserRole{ {RoleID: 2}, @@ -2241,7 +2236,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(), Emails: []Email{ {ID: 3, UserID: 1, Email: "email3@gmail.com"}, 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()), }, }