SetValid() func and bring coverage back up to 100%
This commit is contained in:
parent
d3f0388e17
commit
5cd2a757d8
16 changed files with 143 additions and 0 deletions
6
bool.go
6
bool.go
|
@ -93,6 +93,12 @@ func (b Bool) MarshalText() ([]byte, error) {
|
|||
return []byte("true"), nil
|
||||
}
|
||||
|
||||
// SetValid changes this Bool's value and also sets it to be non-null.
|
||||
func (b *Bool) SetValid(v bool) {
|
||||
b.Bool = v
|
||||
b.Valid = true
|
||||
}
|
||||
|
||||
// Ptr returns a poBooler to this Bool's value, or a nil poBooler if this Bool is null.
|
||||
func (b Bool) Ptr() *bool {
|
||||
if !b.Valid {
|
||||
|
|
15
bool_test.go
15
bool_test.go
|
@ -9,6 +9,7 @@ var (
|
|||
boolJSON = []byte(`true`)
|
||||
falseJSON = []byte(`false`)
|
||||
nullBoolJSON = []byte(`{"Bool":true,"Valid":true}`)
|
||||
invalidJSON = []byte(`:)`)
|
||||
)
|
||||
|
||||
func TestBoolFrom(t *testing.T) {
|
||||
|
@ -51,6 +52,13 @@ func TestUnmarshalBool(t *testing.T) {
|
|||
err = json.Unmarshal(nullJSON, &null)
|
||||
maybePanic(err)
|
||||
assertNullBool(t, null, "null json")
|
||||
|
||||
var invalid Bool
|
||||
err = invalid.UnmarshalText(invalidJSON)
|
||||
if err == nil {
|
||||
panic("err should not be nil")
|
||||
}
|
||||
assertNullBool(t, invalid, "invalid json")
|
||||
}
|
||||
|
||||
func TestTextUnmarshalBool(t *testing.T) {
|
||||
|
@ -132,6 +140,13 @@ func TestBoolIsZero(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestBoolSetValid(t *testing.T) {
|
||||
change := NewBool(false, false)
|
||||
assertNullBool(t, change, "SetValid()")
|
||||
change.SetValid(true)
|
||||
assertBool(t, change, "SetValid()")
|
||||
}
|
||||
|
||||
func TestBoolScan(t *testing.T) {
|
||||
var b Bool
|
||||
err := b.Scan(true)
|
||||
|
|
6
float.go
6
float.go
|
@ -90,6 +90,12 @@ func (f Float) MarshalText() ([]byte, error) {
|
|||
return []byte(strconv.FormatFloat(n, 'f', -1, 64)), nil
|
||||
}
|
||||
|
||||
// SetValid changes this Float's value and also sets it to be non-null.
|
||||
func (f *Float) SetValid(v float64) {
|
||||
f.Float64 = v
|
||||
f.Valid = true
|
||||
}
|
||||
|
||||
// Ptr returns a poFloater to this Float's value, or a nil poFloater if this Float is null.
|
||||
func (f Float) Ptr() *float64 {
|
||||
if !f.Valid {
|
||||
|
|
|
@ -131,6 +131,13 @@ func TestFloatIsZero(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestFloatSetValid(t *testing.T) {
|
||||
change := NewFloat(0, false)
|
||||
assertNullFloat(t, change, "SetValid()")
|
||||
change.SetValid(1.2345)
|
||||
assertFloat(t, change, "SetValid()")
|
||||
}
|
||||
|
||||
func TestFloatScan(t *testing.T) {
|
||||
var f Float
|
||||
err := f.Scan(1.2345)
|
||||
|
|
6
int.go
6
int.go
|
@ -91,6 +91,12 @@ func (i Int) MarshalText() ([]byte, error) {
|
|||
return []byte(strconv.FormatInt(n, 10)), nil
|
||||
}
|
||||
|
||||
// SetValid changes this Int's value and also sets it to be non-null.
|
||||
func (i *Int) SetValid(n int64) {
|
||||
i.Int64 = n
|
||||
i.Valid = true
|
||||
}
|
||||
|
||||
// Ptr returns a pointer to this Int's value, or a nil pointer if this Int is null.
|
||||
func (i Int) Ptr() *int64 {
|
||||
if !i.Valid {
|
||||
|
|
|
@ -144,6 +144,13 @@ func TestIntScan(t *testing.T) {
|
|||
assertNullInt(t, null, "scanned null")
|
||||
}
|
||||
|
||||
func TestIntSetValid(t *testing.T) {
|
||||
change := NewInt(0, false)
|
||||
assertNullInt(t, change, "SetValid()")
|
||||
change.SetValid(12345)
|
||||
assertInt(t, change, "SetValid()")
|
||||
}
|
||||
|
||||
func assertInt(t *testing.T, i Int, from string) {
|
||||
if i.Int64 != 12345 {
|
||||
t.Errorf("bad %s int: %d ≠ %d\n", from, i.Int64, 12345)
|
||||
|
|
|
@ -102,6 +102,12 @@ func (b Bool) MarshalText() ([]byte, error) {
|
|||
return []byte("true"), nil
|
||||
}
|
||||
|
||||
// SetValid changes this Bool's value and also sets it to be non-null.
|
||||
func (b *Bool) SetValid(v bool) {
|
||||
b.Bool = v
|
||||
b.Valid = true
|
||||
}
|
||||
|
||||
// Ptr returns a pointer to this Bool's value, or a nil pointer if this Bool is null.
|
||||
func (b Bool) Ptr() *bool {
|
||||
if !b.Valid {
|
||||
|
|
|
@ -54,6 +54,11 @@ func TestTextUnmarshalBool(t *testing.T) {
|
|||
maybePanic(err)
|
||||
assertBool(t, b, "UnmarshalText() bool")
|
||||
|
||||
var zero Bool
|
||||
err = zero.UnmarshalText([]byte("false"))
|
||||
maybePanic(err)
|
||||
assertFalseBool(t, zero, "UnmarshalText() false")
|
||||
|
||||
var blank Bool
|
||||
err = blank.UnmarshalText([]byte(""))
|
||||
maybePanic(err)
|
||||
|
@ -63,6 +68,13 @@ func TestTextUnmarshalBool(t *testing.T) {
|
|||
err = null.UnmarshalText([]byte("null"))
|
||||
maybePanic(err)
|
||||
assertNullBool(t, null, `UnmarshalText() "null"`)
|
||||
|
||||
var invalid Bool
|
||||
err = invalid.UnmarshalText([]byte(":D"))
|
||||
if err == nil {
|
||||
panic("err should not be nil")
|
||||
}
|
||||
assertNullBool(t, invalid, "invalid json")
|
||||
}
|
||||
|
||||
func TestMarshalBool(t *testing.T) {
|
||||
|
@ -71,6 +83,11 @@ func TestMarshalBool(t *testing.T) {
|
|||
maybePanic(err)
|
||||
assertJSONEquals(t, data, "true", "non-empty json marshal")
|
||||
|
||||
zero := NewBool(false, true)
|
||||
data, err = json.Marshal(zero)
|
||||
maybePanic(err)
|
||||
assertJSONEquals(t, data, "false", "zero json marshal")
|
||||
|
||||
// invalid values should be encoded as null
|
||||
null := NewBool(false, false)
|
||||
data, err = json.Marshal(null)
|
||||
|
@ -84,6 +101,11 @@ func TestMarshalBoolText(t *testing.T) {
|
|||
maybePanic(err)
|
||||
assertJSONEquals(t, data, "true", "non-empty text marshal")
|
||||
|
||||
zero := NewBool(false, true)
|
||||
data, err = zero.MarshalText()
|
||||
maybePanic(err)
|
||||
assertJSONEquals(t, data, "false", "zero text marshal")
|
||||
|
||||
// invalid values should be encoded as null
|
||||
null := NewBool(false, false)
|
||||
data, err = null.MarshalText()
|
||||
|
@ -122,6 +144,13 @@ func TestBoolIsZero(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestBoolSetValid(t *testing.T) {
|
||||
change := NewBool(false, false)
|
||||
assertNullBool(t, change, "SetValid()")
|
||||
change.SetValid(true)
|
||||
assertBool(t, change, "SetValid()")
|
||||
}
|
||||
|
||||
func TestBoolScan(t *testing.T) {
|
||||
var b Bool
|
||||
err := b.Scan(true)
|
||||
|
@ -143,6 +172,15 @@ func assertBool(t *testing.T, b Bool, from string) {
|
|||
}
|
||||
}
|
||||
|
||||
func assertFalseBool(t *testing.T, b Bool, from string) {
|
||||
if b.Bool != false {
|
||||
t.Errorf("bad %s bool: %v ≠ %v\n", from, b.Bool, false)
|
||||
}
|
||||
if !b.Valid {
|
||||
t.Error(from, "is invalid, but should be valid")
|
||||
}
|
||||
}
|
||||
|
||||
func assertNullBool(t *testing.T, b Bool, from string) {
|
||||
if b.Valid {
|
||||
t.Error(from, "is valid, but should be invalid")
|
||||
|
|
|
@ -90,6 +90,12 @@ func (f Float) MarshalText() ([]byte, error) {
|
|||
return []byte(strconv.FormatFloat(f.Float64, 'f', -1, 64)), nil
|
||||
}
|
||||
|
||||
// SetValid changes this Float's value and also sets it to be non-null.
|
||||
func (f *Float) SetValid(n float64) {
|
||||
f.Float64 = n
|
||||
f.Valid = true
|
||||
}
|
||||
|
||||
// Ptr returns a pointer to this Float's value, or a nil pointer if this Float is null.
|
||||
func (f Float) Ptr() *float64 {
|
||||
if !f.Valid {
|
||||
|
|
|
@ -121,6 +121,13 @@ func TestFloatIsZero(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestFloatSetValid(t *testing.T) {
|
||||
change := NewFloat(0, false)
|
||||
assertNullFloat(t, change, "SetValid()")
|
||||
change.SetValid(1.2345)
|
||||
assertFloat(t, change, "SetValid()")
|
||||
}
|
||||
|
||||
func TestFloatScan(t *testing.T) {
|
||||
var f Float
|
||||
err := f.Scan(1.2345)
|
||||
|
|
|
@ -90,6 +90,12 @@ func (i Int) MarshalText() ([]byte, error) {
|
|||
return []byte(strconv.FormatInt(i.Int64, 10)), nil
|
||||
}
|
||||
|
||||
// SetValid changes this Int's value and also sets it to be non-null.
|
||||
func (i *Int) SetValid(n int64) {
|
||||
i.Int64 = n
|
||||
i.Valid = true
|
||||
}
|
||||
|
||||
// Ptr returns a pointer to this Int's value, or a nil pointer if this Int is null.
|
||||
func (i Int) Ptr() *int64 {
|
||||
if !i.Valid {
|
||||
|
|
|
@ -121,6 +121,13 @@ func TestIntIsZero(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestIntSetValid(t *testing.T) {
|
||||
change := NewInt(0, false)
|
||||
assertNullInt(t, change, "SetValid()")
|
||||
change.SetValid(12345)
|
||||
assertInt(t, change, "SetValid()")
|
||||
}
|
||||
|
||||
func TestIntScan(t *testing.T) {
|
||||
var i Int
|
||||
err := i.Scan(12345)
|
||||
|
|
|
@ -72,6 +72,12 @@ func (s *String) UnmarshalText(text []byte) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// SetValid changes this String's value and also sets it to be non-null.
|
||||
func (s *String) SetValid(v string) {
|
||||
s.String = v
|
||||
s.Valid = true
|
||||
}
|
||||
|
||||
// Ptr returns a pointer to this String's value, or a nil pointer if this String is null.
|
||||
func (s String) Ptr() *string {
|
||||
if !s.Valid {
|
||||
|
|
|
@ -132,6 +132,13 @@ func TestStringIsZero(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestStringSetValid(t *testing.T) {
|
||||
change := NewString("", false)
|
||||
assertNullStr(t, change, "SetValid()")
|
||||
change.SetValid("test")
|
||||
assertStr(t, change, "SetValid()")
|
||||
}
|
||||
|
||||
func TestStringScan(t *testing.T) {
|
||||
var str String
|
||||
err := str.Scan("test")
|
||||
|
|
|
@ -75,6 +75,12 @@ func (s *String) UnmarshalText(text []byte) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// SetValid changes this String's value and also sets it to be non-null.
|
||||
func (s *String) SetValid(v string) {
|
||||
s.String = v
|
||||
s.Valid = true
|
||||
}
|
||||
|
||||
// Ptr returns a pointer to this String's value, or a nil pointer if this String is null.
|
||||
func (s String) Ptr() *string {
|
||||
if !s.Valid {
|
||||
|
|
|
@ -138,6 +138,13 @@ func TestStringScan(t *testing.T) {
|
|||
assertNullStr(t, null, "scanned null")
|
||||
}
|
||||
|
||||
func TestStringSetValid(t *testing.T) {
|
||||
change := NewString("", false)
|
||||
assertNullStr(t, change, "SetValid()")
|
||||
change.SetValid("test")
|
||||
assertStr(t, change, "SetValid()")
|
||||
}
|
||||
|
||||
func maybePanic(err error) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
|
|
Loading…
Add table
Reference in a new issue