From 1ff630f3d5a290e54c53f2749c75a28d3f7cfc12 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Tue, 3 Dec 2013 08:23:26 +0800 Subject: [PATCH] Add test for float64 precision --- dialect/postgres.go | 2 +- gorm_test.go | 18 ++++++++++++++++++ utils.go | 2 ++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/dialect/postgres.go b/dialect/postgres.go index a225ae02..fece0ff5 100644 --- a/dialect/postgres.go +++ b/dialect/postgres.go @@ -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: diff --git a/gorm_test.go b/gorm_test.go index 5a1edb2e..a231475a 100644 --- a/gorm_test.go +++ b/gorm_test.go @@ -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") } diff --git a/utils.go b/utils.go index 2233c686..005af2d1 100644 --- a/utils.go +++ b/utils.go @@ -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: