mirror of
https://github.com/shopspring/decimal.git
synced 2024-11-22 20:40:48 +01:00
Adding tests from the Scan method
This commit is contained in:
parent
b873aa2b43
commit
faca378c8a
1 changed files with 61 additions and 0 deletions
|
@ -813,6 +813,67 @@ func TestDecimal_Max(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDecimal_Scan(t *testing.T) {
|
||||||
|
// test the Scan method the implements the
|
||||||
|
// sql.Scanner interface
|
||||||
|
// check for the for different type of values
|
||||||
|
// that are possible to be received from the database
|
||||||
|
// drivers
|
||||||
|
|
||||||
|
// in normal operations the db driver (sqlite at least)
|
||||||
|
// will return an int64 if you specified a numeric format
|
||||||
|
a := Decimal{}
|
||||||
|
dbvalue := float64(54.33)
|
||||||
|
expected := NewFromFloat(dbvalue)
|
||||||
|
|
||||||
|
err := a.Scan(dbvalue)
|
||||||
|
if err != nil {
|
||||||
|
// Scan failed... no need to test result value
|
||||||
|
t.Errorf("a.Scan(54.33) failed with message: %s", err)
|
||||||
|
|
||||||
|
} else {
|
||||||
|
// Scan suceeded... test resulting values
|
||||||
|
if !a.Equals(expected) {
|
||||||
|
t.Errorf("%s does not equal to %s", a, expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// at least SQLite returns an int64 when 0 is stored in the db
|
||||||
|
// and you specified a numeric format on the schema
|
||||||
|
dbvalue_int := int64(0)
|
||||||
|
expected = New(dbvalue_int, 0)
|
||||||
|
|
||||||
|
err = a.Scan(dbvalue_int)
|
||||||
|
if err != nil {
|
||||||
|
// Scan failed... no need to test result value
|
||||||
|
t.Errorf("a.Scan(0) failed with message: %s", err)
|
||||||
|
|
||||||
|
} else {
|
||||||
|
// Scan suceeded... test resulting values
|
||||||
|
if !a.Equals(expected) {
|
||||||
|
t.Errorf("%s does not equal to %s", a, expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// in case you specified a varchar in your SQL schema,
|
||||||
|
// the database driver will return byte slice []byte
|
||||||
|
value_str := "535.666"
|
||||||
|
dbvalue_str := []byte(value_str)
|
||||||
|
expected, err = NewFromString(value_str)
|
||||||
|
|
||||||
|
err = a.Scan(dbvalue_str)
|
||||||
|
if err != nil {
|
||||||
|
// Scan failed... no need to test result value
|
||||||
|
t.Errorf("a.Scan('535.666') failed with message: %s", err)
|
||||||
|
|
||||||
|
} else {
|
||||||
|
// Scan suceeded... test resulting values
|
||||||
|
if !a.Equals(expected) {
|
||||||
|
t.Errorf("%s does not equal to %s", a, expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// old tests after this line
|
// old tests after this line
|
||||||
|
|
||||||
func TestDecimal_Scale(t *testing.T) {
|
func TestDecimal_Scale(t *testing.T) {
|
||||||
|
|
Loading…
Reference in a new issue