2016-09-06 18:56:31 +02:00
|
|
|
package null
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
bytesJSON = []byte(`"hello"`)
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBytesFrom(t *testing.T) {
|
2016-11-10 08:47:49 +01:00
|
|
|
i := BytesFrom([]byte(`hello`))
|
2016-09-06 18:56:31 +02:00
|
|
|
assertBytes(t, i, "BytesFrom()")
|
|
|
|
|
|
|
|
zero := BytesFrom(nil)
|
|
|
|
if zero.Valid {
|
|
|
|
t.Error("BytesFrom(nil)", "is valid, but should be invalid")
|
|
|
|
}
|
2016-09-07 14:24:10 +02:00
|
|
|
|
|
|
|
zero = BytesFrom([]byte{})
|
|
|
|
if !zero.Valid {
|
|
|
|
t.Error("BytesFrom([]byte{})", "is invalid, but should be valid")
|
|
|
|
}
|
2016-09-06 18:56:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBytesFromPtr(t *testing.T) {
|
2016-11-10 08:47:49 +01:00
|
|
|
n := []byte(`hello`)
|
2016-09-06 18:56:31 +02:00
|
|
|
iptr := &n
|
|
|
|
i := BytesFromPtr(iptr)
|
|
|
|
assertBytes(t, i, "BytesFromPtr()")
|
|
|
|
|
|
|
|
null := BytesFromPtr(nil)
|
|
|
|
assertNullBytes(t, null, "BytesFromPtr(nil)")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnmarshalBytes(t *testing.T) {
|
|
|
|
var i Bytes
|
|
|
|
err := json.Unmarshal(bytesJSON, &i)
|
|
|
|
maybePanic(err)
|
|
|
|
assertBytes(t, i, "[]byte json")
|
|
|
|
|
|
|
|
var ni Bytes
|
|
|
|
err = ni.UnmarshalJSON([]byte{})
|
2016-11-10 08:47:49 +01:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Expected error")
|
2016-09-06 18:56:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var null Bytes
|
2016-11-10 08:47:49 +01:00
|
|
|
err = null.UnmarshalJSON([]byte("null"))
|
|
|
|
if null.Valid == true {
|
|
|
|
t.Errorf("expected Valid to be false, got true")
|
2016-09-06 18:56:31 +02:00
|
|
|
}
|
2016-11-10 08:47:49 +01:00
|
|
|
if null.Bytes != nil {
|
|
|
|
t.Errorf("Expected Bytes to be nil, but was not: %#v %#v", null.Bytes, []byte(`null`))
|
2016-09-06 18:56:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTextUnmarshalBytes(t *testing.T) {
|
|
|
|
var i Bytes
|
2016-11-10 08:47:49 +01:00
|
|
|
err := i.UnmarshalText([]byte(`hello`))
|
2016-09-06 18:56:31 +02:00
|
|
|
maybePanic(err)
|
|
|
|
assertBytes(t, i, "UnmarshalText() []byte")
|
|
|
|
|
|
|
|
var blank Bytes
|
|
|
|
err = blank.UnmarshalText([]byte(""))
|
|
|
|
maybePanic(err)
|
|
|
|
assertNullBytes(t, blank, "UnmarshalText() empty []byte")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarshalBytes(t *testing.T) {
|
|
|
|
i := BytesFrom([]byte(`"hello"`))
|
|
|
|
data, err := json.Marshal(i)
|
|
|
|
maybePanic(err)
|
|
|
|
assertJSONEquals(t, data, `"hello"`, "non-empty json marshal")
|
|
|
|
|
|
|
|
// invalid values should be encoded as null
|
|
|
|
null := NewBytes(nil, false)
|
|
|
|
data, err = json.Marshal(null)
|
|
|
|
maybePanic(err)
|
|
|
|
assertJSONEquals(t, data, "null", "null json marshal")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarshalBytesText(t *testing.T) {
|
|
|
|
i := BytesFrom([]byte(`"hello"`))
|
|
|
|
data, err := i.MarshalText()
|
|
|
|
maybePanic(err)
|
|
|
|
assertJSONEquals(t, data, `"hello"`, "non-empty text marshal")
|
|
|
|
|
|
|
|
// invalid values should be encoded as null
|
|
|
|
null := NewBytes(nil, false)
|
|
|
|
data, err = null.MarshalText()
|
|
|
|
maybePanic(err)
|
|
|
|
assertJSONEquals(t, data, "", "null text marshal")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBytesPointer(t *testing.T) {
|
|
|
|
i := BytesFrom([]byte(`"hello"`))
|
|
|
|
ptr := i.Ptr()
|
|
|
|
if !bytes.Equal(*ptr, []byte(`"hello"`)) {
|
|
|
|
t.Errorf("bad %s []byte: %#v ≠ %s\n", "pointer", ptr, `"hello"`)
|
|
|
|
}
|
|
|
|
|
|
|
|
null := NewBytes(nil, false)
|
|
|
|
ptr = null.Ptr()
|
|
|
|
if ptr != nil {
|
|
|
|
t.Errorf("bad %s []byte: %#v ≠ %s\n", "nil pointer", ptr, "nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-06 20:14:43 +02:00
|
|
|
func TestBytesIsNull(t *testing.T) {
|
2016-09-06 18:56:31 +02:00
|
|
|
i := BytesFrom([]byte(`"hello"`))
|
2017-07-06 20:14:43 +02:00
|
|
|
if i.IsNull() {
|
|
|
|
t.Errorf("IsNull() should be false")
|
2016-09-06 18:56:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
null := NewBytes(nil, false)
|
2017-07-06 20:14:43 +02:00
|
|
|
if !null.IsNull() {
|
|
|
|
t.Errorf("IsNull() should be true")
|
2016-09-06 18:56:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
zero := NewBytes(nil, 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")
|
2016-09-06 18:56:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBytesSetValid(t *testing.T) {
|
|
|
|
change := NewBytes(nil, false)
|
|
|
|
assertNullBytes(t, change, "SetValid()")
|
2016-11-10 08:47:49 +01:00
|
|
|
change.SetValid([]byte(`hello`))
|
2016-09-06 18:56:31 +02:00
|
|
|
assertBytes(t, change, "SetValid()")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBytesScan(t *testing.T) {
|
|
|
|
var i Bytes
|
2016-11-10 08:47:49 +01:00
|
|
|
err := i.Scan(`hello`)
|
2016-09-06 18:56:31 +02:00
|
|
|
maybePanic(err)
|
2016-11-10 08:47:49 +01:00
|
|
|
assertBytes(t, i, "Scan() []byte")
|
2016-09-06 18:56:31 +02:00
|
|
|
|
|
|
|
var null Bytes
|
|
|
|
err = null.Scan(nil)
|
|
|
|
maybePanic(err)
|
|
|
|
assertNullBytes(t, null, "scanned null")
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertBytes(t *testing.T, i Bytes, from string) {
|
2016-11-10 08:47:49 +01:00
|
|
|
if !bytes.Equal(i.Bytes, []byte("hello")) {
|
|
|
|
t.Errorf("bad %s []byte: %v ≠ %v\n", from, string(i.Bytes), string([]byte(`hello`)))
|
2016-09-06 18:56:31 +02:00
|
|
|
}
|
|
|
|
if !i.Valid {
|
|
|
|
t.Error(from, "is invalid, but should be valid")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertNullBytes(t *testing.T, i Bytes, from string) {
|
|
|
|
if i.Valid {
|
|
|
|
t.Error(from, "is valid, but should be invalid")
|
|
|
|
}
|
|
|
|
}
|