Skip to content

Commit

Permalink
filed for upsert
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangz222 committed Sep 19, 2020
1 parent 1295f88 commit b5cff5e
Showing 1 changed file with 75 additions and 1 deletion.
76 changes: 75 additions & 1 deletion field/field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,20 @@ func TestBeforeUpdate(t *testing.T) {

}

type UserField struct {
DefaultField `bson:",inline"`

Name string `bson:"name"`
Age int `bson:"age"`
CreateTimeAt int64 `bson:"createTimeAt"`
UpdateTimeAt time.Time `bson:"updateTimeAt"`
MyId primitive.ObjectID `bson:"myId"`
}

func (u *UserField) CustomFields() CustomFieldsBuilder {
return NewCustom().SetCreateAt("CreateTimeAt").SetUpdateAt("UpdateTimeAt").SetId("MyId")
}

func TestBeforeUpsert(t *testing.T) {
ast := require.New(t)

Expand All @@ -125,7 +139,7 @@ func TestBeforeUpsert(t *testing.T) {
ast.NotEqual(primitive.NilObjectID, u.Id)
// custom fields
ast.NotEqual(time.Time{}, u.CreateTimeAt)
ast.NotEqual(time.Time{}, u.UpdateTimeAt)
ast.NotEqual(0, u.UpdateTimeAt)
ast.NotEqual(primitive.NilObjectID, u.MyId)

u1, u2 := &User{Name: "Lucas", Age: 7}, &User{Name: "Alice", Age: 8}
Expand All @@ -136,6 +150,8 @@ func TestBeforeUpsert(t *testing.T) {
for _, v := range us {
ast.NotEqual(time.Time{}, v.CreateAt)
ast.NotEqual(time.Time{}, v.UpdateAt)
ast.NotEqual(time.Time{}, u.CreateTimeAt)
ast.NotEqual(0, u.UpdateTimeAt)
ast.NotEqual(primitive.NilObjectID, v.Id)
}

Expand Down Expand Up @@ -167,6 +183,64 @@ func TestBeforeUpsert(t *testing.T) {

}

// same as TestBeforeUpsert, just switch type of CreateTimeAt and UpdateTimeAt
func TestBeforeUpsertUserFiled(t *testing.T) {
ast := require.New(t)

// with empty fileds
u := &UserField{Name: "Lucas", Age: 7}
err := Do(u, BeforeUpsert)
ast.NoError(err)
// default fields
ast.NotEqual(time.Time{}, u.CreateAt)
ast.NotEqual(time.Time{}, u.UpdateAt)
ast.NotEqual(primitive.NilObjectID, u.Id)
// custom fields
ast.NotEqual(0, u.CreateTimeAt)
ast.NotEqual(time.Time{}, u.UpdateTimeAt)
ast.NotEqual(primitive.NilObjectID, u.MyId)

u1, u2 := &UserField{Name: "Lucas", Age: 7}, &UserField{Name: "Alice", Age: 8}
us := []*UserField{u1, u2}
err = Do(us, BeforeUpsert)
ast.NoError(err)

for _, v := range us {
ast.NotEqual(time.Time{}, v.CreateAt)
ast.NotEqual(time.Time{}, v.UpdateAt)
ast.NotEqual(0, u.CreateTimeAt)
ast.NotEqual(time.Time{}, u.UpdateTimeAt)
ast.NotEqual(primitive.NilObjectID, v.Id)
}

u3 := User{Name: "Lucas", Age: 7}
err = Do(u3, BeforeUpsert)
ast.NoError(err)

// upsert with valid value
tBefore3s := time.Now().Add(-3 * time.Second)
id := primitive.NewObjectID()
u = &UserField{Name: "Lucas", Age: 7}
u.CreateAt = tBefore3s
u.UpdateAt = tBefore3s
u.Id = id
u.MyId = id
u.CreateTimeAt = tBefore3s.Unix()
u.UpdateTimeAt = tBefore3s

err = Do(u, BeforeUpsert)
ast.NoError(err)

ast.Equal(tBefore3s, u.CreateAt)
ast.Equal(id, u.Id)
ast.NotEqual(tBefore3s, u.UpdateAt)

ast.NotEqual(tBefore3s, u.UpdateTimeAt)
ast.Equal(id, u.MyId)
ast.Equal(tBefore3s.Unix(), u.CreateTimeAt)

}

func TestNilError(t *testing.T) {
ast := require.New(t)

Expand Down

0 comments on commit b5cff5e

Please sign in to comment.