-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package genmai | ||
|
||
import "time" | ||
|
||
// TimeStamp is fields for timestamps that commonly used. | ||
type TimeStamp struct { | ||
// Time of creation. This field will be set automatically by BeforeInsert. | ||
CreatedAt time.Time | ||
|
||
// Time of update. This field will be set by BeforeInsert or BeforeUpdate. | ||
UpdatedAt time.Time | ||
} | ||
|
||
// BeforeInsert sets current time to CreatedAt and UpdatedAt field. | ||
// It always returns nil. | ||
func (ts *TimeStamp) BeforeInsert() error { | ||
n := now() | ||
ts.CreatedAt = n | ||
ts.UpdatedAt = n | ||
return nil | ||
} | ||
|
||
// BeforeUpdate sets current time to UpdatedAt field. | ||
// It always returns nil. | ||
func (ts *TimeStamp) BeforeUpdate() error { | ||
ts.UpdatedAt = now() | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package genmai | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestTimeStamp_BeforeInsert(t *testing.T) { | ||
createdAt, err := time.Parse("2006-01-02 15:04:05", "2014-02-24 22:36:56") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
updatedAt, err := time.Parse("2006-01-02 15:04:05", "2014-02-24 23:51:26") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
n, err := time.Parse("2006-01-02 15:04:05", "2000-02-02 16:57:38") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
baknow := now | ||
now = func() time.Time { | ||
return n | ||
} | ||
defer func() { | ||
now = baknow | ||
}() | ||
tm := &TimeStamp{ | ||
CreatedAt: createdAt, | ||
UpdatedAt: updatedAt, | ||
} | ||
if err := tm.BeforeInsert(); err != nil { | ||
t.Fatal(err) | ||
} | ||
actual := tm.CreatedAt | ||
expected := n | ||
if !reflect.DeepEqual(actual, expected) { | ||
t.Errorf("Expect %q, but %q", expected, actual) | ||
} | ||
actual = tm.UpdatedAt | ||
expected = n | ||
if !reflect.DeepEqual(actual, expected) { | ||
t.Errorf("Expect %q, but %q", expected, actual) | ||
} | ||
} | ||
|
||
func TestTimeStamp_BeforeUpdate(t *testing.T) { | ||
createdAt, err := time.Parse("2006-01-02 15:04:05", "2014-02-24 22:36:56") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
updatedAt, err := time.Parse("2006-01-02 15:04:05", "2014-02-24 23:51:26") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
n, err := time.Parse("2006-01-02 15:04:05", "2000-02-02 16:57:38") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
baknow := now | ||
now = func() time.Time { | ||
return n | ||
} | ||
defer func() { | ||
now = baknow | ||
}() | ||
tm := &TimeStamp{ | ||
CreatedAt: createdAt, | ||
UpdatedAt: updatedAt, | ||
} | ||
if err := tm.BeforeUpdate(); err != nil { | ||
t.Fatal(err) | ||
} | ||
actual := tm.CreatedAt | ||
expected := createdAt | ||
if !reflect.DeepEqual(actual, expected) { | ||
t.Errorf("Expect %q, but %q", expected, actual) | ||
} | ||
actual = tm.UpdatedAt | ||
expected = n | ||
if !reflect.DeepEqual(actual, expected) { | ||
t.Errorf("Expect %q, but %q", expected, actual) | ||
} | ||
} |