diff --git a/decimal_test.go b/decimal_test.go index 5202234..e560ffb 100644 --- a/decimal_test.go +++ b/decimal_test.go @@ -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 func TestDecimal_Scale(t *testing.T) {