2014-08-30 16:53:25 +02:00
|
|
|
package null
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-11-10 16:00:05 +01:00
|
|
|
intJSON = []byte(`12345`)
|
2014-08-30 16:53:25 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestIntFrom(t *testing.T) {
|
|
|
|
i := IntFrom(12345)
|
|
|
|
assertInt(t, i, "IntFrom()")
|
|
|
|
|
|
|
|
zero := IntFrom(0)
|
2014-09-01 20:22:17 +02:00
|
|
|
if !zero.Valid {
|
|
|
|
t.Error("IntFrom(0)", "is invalid, but should be valid")
|
2014-08-30 16:53:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-31 10:28:34 +02:00
|
|
|
func TestIntFromPtr(t *testing.T) {
|
2016-05-17 14:02:36 +02:00
|
|
|
n := int(12345)
|
2014-08-31 10:28:34 +02:00
|
|
|
iptr := &n
|
|
|
|
i := IntFromPtr(iptr)
|
|
|
|
assertInt(t, i, "IntFromPtr()")
|
|
|
|
|
|
|
|
null := IntFromPtr(nil)
|
|
|
|
assertNullInt(t, null, "IntFromPtr(nil)")
|
|
|
|
}
|
|
|
|
|
2014-08-30 16:53:25 +02:00
|
|
|
func TestUnmarshalInt(t *testing.T) {
|
|
|
|
var i Int
|
|
|
|
err := json.Unmarshal(intJSON, &i)
|
|
|
|
maybePanic(err)
|
|
|
|
assertInt(t, i, "int json")
|
|
|
|
|
|
|
|
var null Int
|
|
|
|
err = json.Unmarshal(nullJSON, &null)
|
|
|
|
maybePanic(err)
|
|
|
|
assertNullInt(t, null, "null json")
|
2014-09-09 05:31:58 +02:00
|
|
|
|
|
|
|
var badType Int
|
|
|
|
err = json.Unmarshal(boolJSON, &badType)
|
|
|
|
if err == nil {
|
|
|
|
panic("err should not be nil")
|
|
|
|
}
|
|
|
|
assertNullInt(t, badType, "wrong type json")
|
2015-09-10 13:05:24 +02:00
|
|
|
|
|
|
|
var invalid Int
|
|
|
|
err = invalid.UnmarshalJSON(invalidJSON)
|
|
|
|
if _, ok := err.(*json.SyntaxError); !ok {
|
|
|
|
t.Errorf("expected json.SyntaxError, not %T", err)
|
|
|
|
}
|
|
|
|
assertNullInt(t, invalid, "invalid json")
|
2014-08-30 16:53:25 +02:00
|
|
|
}
|
|
|
|
|
2014-09-17 03:05:23 +02:00
|
|
|
func TestUnmarshalNonIntegerNumber(t *testing.T) {
|
|
|
|
var i Int
|
2016-05-17 14:02:36 +02:00
|
|
|
err := json.Unmarshal(float64JSON, &i)
|
2014-09-17 03:05:23 +02:00
|
|
|
if err == nil {
|
|
|
|
panic("err should be present; non-integer number coerced to int")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-30 16:53:25 +02:00
|
|
|
func TestTextUnmarshalInt(t *testing.T) {
|
|
|
|
var i Int
|
|
|
|
err := i.UnmarshalText([]byte("12345"))
|
|
|
|
maybePanic(err)
|
|
|
|
assertInt(t, i, "UnmarshalText() int")
|
|
|
|
|
|
|
|
var blank Int
|
|
|
|
err = blank.UnmarshalText([]byte(""))
|
|
|
|
maybePanic(err)
|
|
|
|
assertNullInt(t, blank, "UnmarshalText() empty int")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarshalInt(t *testing.T) {
|
|
|
|
i := IntFrom(12345)
|
|
|
|
data, err := json.Marshal(i)
|
|
|
|
maybePanic(err)
|
|
|
|
assertJSONEquals(t, data, "12345", "non-empty json marshal")
|
|
|
|
|
2014-09-01 20:22:17 +02:00
|
|
|
// invalid values should be encoded as null
|
2014-08-30 16:53:25 +02:00
|
|
|
null := NewInt(0, false)
|
|
|
|
data, err = json.Marshal(null)
|
|
|
|
maybePanic(err)
|
2014-09-01 20:22:17 +02:00
|
|
|
assertJSONEquals(t, data, "null", "null json marshal")
|
2014-08-30 16:53:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarshalIntText(t *testing.T) {
|
|
|
|
i := IntFrom(12345)
|
|
|
|
data, err := i.MarshalText()
|
|
|
|
maybePanic(err)
|
|
|
|
assertJSONEquals(t, data, "12345", "non-empty text marshal")
|
|
|
|
|
2014-09-01 20:22:17 +02:00
|
|
|
// invalid values should be encoded as null
|
2014-08-30 16:53:25 +02:00
|
|
|
null := NewInt(0, false)
|
|
|
|
data, err = null.MarshalText()
|
|
|
|
maybePanic(err)
|
2014-09-01 20:22:17 +02:00
|
|
|
assertJSONEquals(t, data, "", "null text marshal")
|
2014-08-30 16:53:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestIntPointer(t *testing.T) {
|
|
|
|
i := IntFrom(12345)
|
2014-08-31 10:28:34 +02:00
|
|
|
ptr := i.Ptr()
|
2014-08-30 16:53:25 +02:00
|
|
|
if *ptr != 12345 {
|
2015-01-28 07:15:30 +01:00
|
|
|
t.Errorf("bad %s int: %#v ≠ %d\n", "pointer", ptr, 12345)
|
2014-08-30 16:53:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
null := NewInt(0, false)
|
2014-08-31 10:28:34 +02:00
|
|
|
ptr = null.Ptr()
|
2014-08-30 16:53:25 +02:00
|
|
|
if ptr != nil {
|
|
|
|
t.Errorf("bad %s int: %#v ≠ %s\n", "nil pointer", ptr, "nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-06 20:14:43 +02:00
|
|
|
func TestIntIsNull(t *testing.T) {
|
2014-08-30 16:53:25 +02:00
|
|
|
i := IntFrom(12345)
|
2017-07-06 20:14:43 +02:00
|
|
|
if i.IsNull() {
|
|
|
|
t.Errorf("IsNull() should be false")
|
2014-08-30 16:53:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
null := NewInt(0, false)
|
2017-07-06 20:14:43 +02:00
|
|
|
if !null.IsNull() {
|
|
|
|
t.Errorf("IsNull() should be true")
|
2014-08-30 16:53:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
zero := NewInt(0, true)
|
2017-07-06 20:14:43 +02:00
|
|
|
if zero.IsNull() {
|
|
|
|
t.Errorf("IsNull() should be false")
|
|
|
|
}
|
|
|
|
|
|
|
|
var testInt interface{}
|
|
|
|
testInt = zero
|
|
|
|
if _, ok := testInt.(Nullable); !ok {
|
|
|
|
t.Errorf("Nullable interface should be implemented")
|
2014-08-30 16:53:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-01 20:22:17 +02:00
|
|
|
func TestIntSetValid(t *testing.T) {
|
|
|
|
change := NewInt(0, false)
|
|
|
|
assertNullInt(t, change, "SetValid()")
|
|
|
|
change.SetValid(12345)
|
|
|
|
assertInt(t, change, "SetValid()")
|
|
|
|
}
|
|
|
|
|
2014-08-30 16:53:25 +02:00
|
|
|
func TestIntScan(t *testing.T) {
|
|
|
|
var i Int
|
|
|
|
err := i.Scan(12345)
|
|
|
|
maybePanic(err)
|
|
|
|
assertInt(t, i, "scanned int")
|
|
|
|
|
|
|
|
var null Int
|
|
|
|
err = null.Scan(nil)
|
|
|
|
maybePanic(err)
|
|
|
|
assertNullInt(t, null, "scanned null")
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertInt(t *testing.T, i Int, from string) {
|
2016-05-17 14:02:36 +02:00
|
|
|
if i.Int != 12345 {
|
|
|
|
t.Errorf("bad %s int: %d ≠ %d\n", from, i.Int, 12345)
|
2014-08-30 16:53:25 +02:00
|
|
|
}
|
|
|
|
if !i.Valid {
|
|
|
|
t.Error(from, "is invalid, but should be valid")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertNullInt(t *testing.T, i Int, from string) {
|
|
|
|
if i.Valid {
|
|
|
|
t.Error(from, "is valid, but should be invalid")
|
|
|
|
}
|
|
|
|
}
|