Add test for float64 precision

This commit is contained in:
Jinzhu 2013-12-03 08:23:26 +08:00
parent 2b36ccddd0
commit 1ff630f3d5
3 changed files with 21 additions and 1 deletions

View File

@ -28,7 +28,7 @@ func (d *postgres) SqlTag(column interface{}, size int) string {
case int64, uint64, sql.NullInt64:
return "bigint"
case float32, float64, sql.NullFloat64:
return "double precision"
return "numeric"
case []byte:
return "bytea"
case string, sql.NullString:

View File

@ -29,6 +29,7 @@ type User struct {
ShippingAddressId int64 // Embedded struct's foreign key
When time.Time
CreditCard CreditCard
Latitude float64
PasswordHash []byte
IgnoreMe int64 `sql:"-"`
}
@ -159,6 +160,22 @@ func TestFirstAndLast(t *testing.T) {
}
}
func TestPrecision(t *testing.T) {
f := 35.03554004971999
user := User{Name: "Precision", Latitude: f}
db.Save(&user)
if user.Latitude != f {
t.Errorf("Float64 should not be changed after save")
}
var u User
db.First(&u, "name = ?", "Precision")
fmt.Println(u.Latitude)
if u.Latitude != f {
t.Errorf("Float64 should not be changed after query")
}
}
func TestCreateAndUpdate(t *testing.T) {
name, name2, new_name := "update", "update2", "new_update"
user := User{Name: name, Age: 1, PasswordHash: []byte{'f', 'a', 'k', '4'}}
@ -1111,6 +1128,7 @@ func TestNot(t *testing.T) {
}
db.Not(User{Name: "3"}).Find(&users5)
if len(users1)-len(users5) != int(name_3_count) {
t.Errorf("Should find all users's name not equal 3")
}

View File

@ -90,6 +90,8 @@ func isBlank(value reflect.Value) bool {
switch value.Kind() {
case reflect.Int, reflect.Int64, reflect.Int32:
return value.Int() == 0
case reflect.Float32, reflect.Float64:
return value.Float() == 0
case reflect.String:
return value.String() == ""
case reflect.Slice: