Skip to content

Commit

Permalink
Improve model tests (#411)
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislas-m authored Jul 14, 2019
1 parent 42586cb commit 6251207
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 28 deletions.
28 changes: 16 additions & 12 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/gofrs/uuid"
)

var nowFunc = time.Now

var tableMap = map[string]string{}
var tableMapMu = sync.RWMutex{}

Expand Down Expand Up @@ -142,26 +144,28 @@ func (m *Model) setID(i interface{}) {
func (m *Model) touchCreatedAt() {
fbn, err := m.fieldByName("CreatedAt")
if err == nil {
now := time.Now()
switch fbn.Kind() {
case reflect.Int, reflect.Int64:
if fbn.Int() == 0 {
fbn.SetInt(now.Unix())
}
now := nowFunc()
v := fbn.Interface()
if !IsZeroOfUnderlyingType(v) {
// Do not override already set CreatedAt
return
}
switch v.(type) {
case int, int64:
fbn.SetInt(now.Unix())
default:
if fbn.Interface().(time.Time).IsZero() {
fbn.Set(reflect.ValueOf(now))
}
fbn.Set(reflect.ValueOf(now))
}
}
}

func (m *Model) touchUpdatedAt() {
fbn, err := m.fieldByName("UpdatedAt")
if err == nil {
now := time.Now()
switch fbn.Kind() {
case reflect.Int, reflect.Int64:
now := nowFunc()
v := fbn.Interface()
switch v.(type) {
case int, int64:
fbn.SetInt(now.Unix())
default:
fbn.Set(reflect.ValueOf(now))
Expand Down
53 changes: 37 additions & 16 deletions model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,14 @@ func (tn) TableName() string {
func Test_TableName(t *testing.T) {
r := require.New(t)

m := Model{Value: tn{}}
r.Equal("this is my table name", m.TableName())
}

func Test_TableName_With_Array(t *testing.T) {
r := require.New(t)

m := Model{Value: []tn{}}
r.Equal("this is my table name", m.TableName())
cases := []interface{}{
tn{},
[]tn{},
}
for _, tc := range cases {
m := Model{Value: tc}
r.Equal("this is my table name", m.TableName())
}
}

type TimeTimestamp struct {
Expand All @@ -66,46 +65,68 @@ func Test_Touch_Time_Timestamp(t *testing.T) {
r := require.New(t)

m := Model{Value: &TimeTimestamp{}}

// Override time.Now()
t0, _ := time.Parse(time.RFC3339, "2019-07-14T00:00:00Z")
nowFunc = func() time.Time { return t0 }
defer func() { nowFunc = time.Now }()

m.touchCreatedAt()
m.touchUpdatedAt()
v := m.Value.(*TimeTimestamp)
r.NotZero(v.CreatedAt)
r.NotZero(v.UpdatedAt)
r.Equal(t0, v.CreatedAt)
r.Equal(t0, v.UpdatedAt)
}

func Test_Touch_Time_Timestamp_With_Existing_Value(t *testing.T) {
r := require.New(t)

createdAt := time.Now().Add(-36 * time.Hour)
// Override time.Now()
t0, _ := time.Parse(time.RFC3339, "2019-07-14T00:00:00Z")
nowFunc = func() time.Time { return t0 }
defer func() { nowFunc = time.Now }()

createdAt := nowFunc().Add(-36 * time.Hour)

m := Model{Value: &TimeTimestamp{CreatedAt: createdAt}}
m.touchCreatedAt()
m.touchUpdatedAt()
v := m.Value.(*TimeTimestamp)
r.Equal(createdAt, v.CreatedAt)
r.NotZero(v.UpdatedAt)
r.Equal(t0, v.UpdatedAt)
}

func Test_Touch_Unix_Timestamp(t *testing.T) {
r := require.New(t)

m := Model{Value: &UnixTimestamp{}}

// Override time.Now()
t0, _ := time.Parse(time.RFC3339, "2019-07-14T00:00:00Z")
nowFunc = func() time.Time { return t0 }
defer func() { nowFunc = time.Now }()

m.touchCreatedAt()
m.touchUpdatedAt()
v := m.Value.(*UnixTimestamp)
r.NotZero(v.CreatedAt)
r.NotZero(v.UpdatedAt)
r.Equal(int(t0.Unix()), v.CreatedAt)
r.Equal(int(t0.Unix()), v.UpdatedAt)
}

func Test_Touch_Unix_Timestamp_With_Existing_Value(t *testing.T) {
r := require.New(t)

// Override time.Now()
t0, _ := time.Parse(time.RFC3339, "2019-07-14T00:00:00Z")
nowFunc = func() time.Time { return t0 }
defer func() { nowFunc = time.Now }()

createdAt := int(time.Now().Add(-36 * time.Hour).Unix())

m := Model{Value: &UnixTimestamp{CreatedAt: createdAt}}
m.touchCreatedAt()
m.touchUpdatedAt()
v := m.Value.(*UnixTimestamp)
r.Equal(createdAt, v.CreatedAt)
r.NotZero(v.UpdatedAt)
r.Equal(int(t0.Unix()), v.UpdatedAt)
}

0 comments on commit 6251207

Please sign in to comment.