1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 14:00:54 +00:00

supplement datetime precision UT

This commit is contained in:
AllenX2018 2020-08-26 11:51:05 +08:00
parent cceecad8c2
commit b83094ac1e
3 changed files with 39 additions and 2 deletions

View File

@ -66,12 +66,14 @@ checkColumn:
case TypeDateField: case TypeDateField:
col = T["time.Time-date"] col = T["time.Time-date"]
case TypeDateTimeField: case TypeDateTimeField:
if fi.timePrecision == nil { // the precision of sqlite is not implemented
if al.Driver == 2 || fi.timePrecision == nil {
col = T["time.Time"] col = T["time.Time"]
} else { }else {
s := T["time.Time-precision"] s := T["time.Time-precision"]
col = fmt.Sprintf(s, *fi.timePrecision) col = fmt.Sprintf(s, *fi.timePrecision)
} }
case TypeBitField: case TypeBitField:
col = T["int8"] col = T["int8"]
case TypeSmallIntegerField: case TypeSmallIntegerField:

View File

@ -241,6 +241,21 @@ type UserBig struct {
Name string Name string
} }
type TM struct {
ID int `orm:"column(id)"`
TMPrecision1 time.Time `orm:"type(datetime);precision(3)"`
TMPrecision2 time.Time `orm:"auto_now_add;type(datetime);precision(4)"`
}
func (t *TM) TableName() string {
return "tm"
}
func NewTM() *TM {
obj := new(TM)
return obj
}
type User struct { type User struct {
ID int `orm:"column(id)"` ID int `orm:"column(id)"`
UserName string `orm:"size(30);unique"` UserName string `orm:"size(30);unique"`

View File

@ -204,6 +204,7 @@ func TestSyncDb(t *testing.T) {
RegisterModel(new(PtrPk)) RegisterModel(new(PtrPk))
RegisterModel(new(Index)) RegisterModel(new(Index))
RegisterModel(new(StrPk)) RegisterModel(new(StrPk))
RegisterModel(new(TM))
err := RunSyncdb("default", true, Debug) err := RunSyncdb("default", true, Debug)
throwFail(t, err) throwFail(t, err)
@ -230,6 +231,7 @@ func TestRegisterModels(t *testing.T) {
RegisterModel(new(PtrPk)) RegisterModel(new(PtrPk))
RegisterModel(new(Index)) RegisterModel(new(Index))
RegisterModel(new(StrPk)) RegisterModel(new(StrPk))
RegisterModel(new(TM))
BootStrap() BootStrap()
@ -313,6 +315,24 @@ func TestDataTypes(t *testing.T) {
} }
} }
func TestTM(t *testing.T) {
// The precision of sqlite is not implemented
if dORM.Driver().Type() == 2 {
return
}
var recTM TM
tm := NewTM()
tm.TMPrecision1 = time.Unix(1596766024, 123456789)
tm.TMPrecision2 = time.Unix(1596766024, 123456789)
_, err := dORM.Insert(tm)
throwFail(t, err)
err = dORM.QueryTable("tm").One(&recTM)
throwFail(t, err)
throwFail(t, AssertIs(recTM.TMPrecision1.String(), "2020-08-07 02:07:04.123 +0000 UTC"))
throwFail(t, AssertIs(recTM.TMPrecision2.String(), "2020-08-07 02:07:04.1235 +0000 UTC"))
}
func TestNullDataTypes(t *testing.T) { func TestNullDataTypes(t *testing.T) {
d := DataNull{} d := DataNull{}