Skip to content

Commit

Permalink
Merge pull request #215 from gobuffalo/task/support-unix-timestamp
Browse files Browse the repository at this point in the history
Add support for UNIX timestamps (CreatedAt & UpdatedAt)
  • Loading branch information
mclark4386 committed Aug 22, 2018
2 parents 2540a72 + 0d2af90 commit 85d15df
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 36 deletions.
2 changes: 1 addition & 1 deletion benchmarks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func translateOne(sql string) string {
for _, char := range str {
out = append(out, byte(char))
}
curr += 1
curr++
} else {
out = append(out, sql[i])
}
Expand Down
66 changes: 33 additions & 33 deletions finders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -615,87 +615,87 @@ func Test_Count_Disregards_Pagination(t *testing.T) {
r.NoError(err)
}

first_users := Users{}
second_users := Users{}
firstUsers := Users{}
secondUsers := Users{}

q := tx.Paginate(1, 3)
q.All(&first_users)
q.All(&firstUsers)
r.Equal(len(names), q.Paginator.TotalEntriesSize) //ensure paginator populates count
r.Equal(3, len(first_users))
r.Equal(3, len(firstUsers))

first_users = Users{}
firstUsers = Users{}
q = tx.RawQuery("select * from users").Paginate(1, 3)
q.All(&first_users)
q.All(&firstUsers)
r.Equal(1, q.Paginator.Page)
r.Equal(3, q.Paginator.PerPage)
r.Equal(len(names), q.Paginator.TotalEntriesSize) //ensure paginator populates count

r.Equal(3, len(first_users))
r.Equal(3, len(firstUsers))
totalFirstPage := q.Paginator.TotalPages

q = tx.Paginate(2, 3)
q.All(&second_users)
q.All(&secondUsers)

r.Equal(3, len(second_users))
r.Equal(3, len(secondUsers))
totalSecondPage := q.Paginator.TotalPages

r.NotEqual(0, totalFirstPage)
r.NotEqual(0, totalSecondPage)
r.Equal(totalFirstPage, totalSecondPage)

first_users = Users{}
firstUsers = Users{}
q = tx.RawQuery("select * from users limit 2").Paginate(1, 5)
err := q.All(&first_users)
err := q.All(&firstUsers)
r.NoError(err)
r.Equal(2, len(first_users)) //raw query limit applies
r.Equal(2, len(firstUsers)) //raw query limit applies

first_users = Users{}
firstUsers = Users{}
q = tx.RawQuery("select * from users limit 2 offset 1").Paginate(1, 5)
err = q.All(&first_users)
err = q.All(&firstUsers)
r.NoError(err)
r.Equal(2, len(first_users))
r.Equal(2, len(firstUsers))

first_users = Users{}
firstUsers = Users{}
q = tx.RawQuery("select * from users limit 2 offset\t1").Paginate(1, 5)
err = q.All(&first_users)
err = q.All(&firstUsers)
r.NoError(err)
r.Equal(2, len(first_users))
r.Equal(2, len(firstUsers))

first_users = Users{}
firstUsers = Users{}
q = tx.RawQuery(`select * from users limit 2 offset
1`).Paginate(1, 5)
err = q.All(&first_users)
err = q.All(&firstUsers)
r.NoError(err)
r.Equal(2, len(first_users))
r.Equal(2, len(firstUsers))

first_users = Users{}
firstUsers = Users{}
q = tx.RawQuery(`select * from users limit 2 offset
1
`).Paginate(1, 5) //ending space and tab
err = q.All(&first_users)
err = q.All(&firstUsers)
r.NoError(err)
r.Equal(2, len(first_users))
r.Equal(2, len(firstUsers))

if tx.Dialect.Name() == "sqlite" {
first_users = Users{}
firstUsers = Users{}
q = tx.RawQuery("select * from users limit 2,1").Paginate(1, 5)
err = q.All(&first_users)
err = q.All(&firstUsers)
r.NoError(err)
r.Equal(2, len(first_users))
r.Equal(2, len(firstUsers))

first_users = Users{}
firstUsers = Users{}
q = tx.RawQuery("select * from users limit 2 , 1").Paginate(1, 5)
err = q.All(&first_users)
err = q.All(&firstUsers)
r.NoError(err)
r.Equal(2, len(first_users))
r.Equal(2, len(firstUsers))
}

if tx.Dialect.Name() == "postgresql" {
first_users = Users{}
firstUsers = Users{}
q = tx.RawQuery("select * from users FETCH FIRST 3 rows only").Paginate(1, 5)
err = q.All(&first_users)
err = q.All(&firstUsers)
r.NoError(err)
r.Equal(3, len(first_users)) //should fetch only 3
r.Equal(3, len(firstUsers)) //should fetch only 3
}
})
}
Expand Down
16 changes: 14 additions & 2 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,26 @@ func (m *Model) setID(i interface{}) {
func (m *Model) touchCreatedAt() {
fbn, err := m.fieldByName("CreatedAt")
if err == nil {
fbn.Set(reflect.ValueOf(time.Now()))
now := time.Now()
switch fbn.Kind() {
case reflect.Int, reflect.Int64:
fbn.SetInt(now.Unix())
default:
fbn.Set(reflect.ValueOf(now))
}
}
}

func (m *Model) touchUpdatedAt() {
fbn, err := m.fieldByName("UpdatedAt")
if err == nil {
fbn.Set(reflect.ValueOf(time.Now()))
now := time.Now()
switch fbn.Kind() {
case reflect.Int, reflect.Int64:
fbn.SetInt(now.Unix())
default:
fbn.Set(reflect.ValueOf(now))
}
}
}

Expand Down
35 changes: 35 additions & 0 deletions model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pop

import (
"testing"
"time"

"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -48,3 +49,37 @@ func Test_TableName_With_Array(t *testing.T) {
m := Model{Value: []tn{}}
r.Equal("this is my table name", m.TableName())
}

type TimeTimestamp struct {
ID int `db:"id"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
}

type UnixTimestamp struct {
ID int `db:"id"`
CreatedAt int `db:"created_at"`
UpdatedAt int `db:"updated_at"`
}

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

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

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

m := Model{Value: &UnixTimestamp{}}
m.touchCreatedAt()
m.touchUpdatedAt()
v := m.Value.(*UnixTimestamp)
r.NotZero(v.CreatedAt)
r.NotZero(v.UpdatedAt)
}

0 comments on commit 85d15df

Please sign in to comment.