Ejemplo n.º 1
0
func TestUnmarshalNullDate(t *testing.T) {
	var jsonNull string = `null`
	var jsonEmpty string = `""`
	var stringDate string = `"2010-07-03"`
	var stringTime string = `"2010-07-03T13:24:33"`
	var stringBogus string = `"bogus"`

	var n Date
	var err error
	err = json.Unmarshal([]byte(jsonNull), &n)
	expect.Nil(t, err)
	expect.False(t, n.Valid)

	err = json.Unmarshal([]byte(jsonEmpty), &n)
	expect.Nil(t, err)
	expect.False(t, n.Valid)

	err = json.Unmarshal([]byte(stringDate), &n)
	expect.Nil(t, err)
	expect.True(t, n.Valid)
	expect.Equal(t, n.Date.Year, 2010)
	expect.Equal(t, n.Date.Month, time.July)
	expect.Equal(t, n.Date.Day, 3)

	err = json.Unmarshal([]byte(stringTime), &n)
	expect.NotNil(t, err)
	expect.False(t, n.Valid)

	err = json.Unmarshal([]byte(stringBogus), &n)
	expect.NotNil(t, err)
	expect.False(t, n.Valid)
}
Ejemplo n.º 2
0
func TestUnmarshalNullTime(t *testing.T) {
	var jsonNull string = `null`
	var jsonEmpty string = `""`
	var stringTime string = `"2010-07-03T13:24:33Z"`
	var stringBogus string = `"bogus"`

	var n Time
	var err error
	err = json.Unmarshal([]byte(jsonNull), &n)
	expect.Nil(t, err)
	expect.False(t, n.Valid)

	err = json.Unmarshal([]byte(jsonEmpty), &n)
	expect.Nil(t, err)
	expect.False(t, n.Valid)

	err = json.Unmarshal([]byte(stringTime), &n)
	expect.Nil(t, err)
	expect.True(t, n.Valid)
	expect.Equal(t, n.Time.Format("2006-01-02 15:04:05"), "2010-07-03 13:24:33")

	err = json.Unmarshal([]byte(stringBogus), &n)
	expect.NotNil(t, err)
	expect.False(t, n.Valid)
}
Ejemplo n.º 3
0
func TestUnmarshalNullString(t *testing.T) {
	var jsonNull string = `null`
	var jsonNumber string = `3`
	var jsonEmpty string = `""`
	var validString string = `"foo"`

	var n String
	var err error
	err = json.Unmarshal([]byte(jsonNull), &n)
	expect.Nil(t, err)
	expect.False(t, n.Valid)

	err = json.Unmarshal([]byte(jsonNumber), &n)
	expect.NotNil(t, err)
	expect.False(t, n.Valid)

	err = json.Unmarshal([]byte(jsonEmpty), &n)
	expect.Nil(t, err)
	expect.True(t, n.Valid)
	expect.Equal(t, n.String, "")

	err = json.Unmarshal([]byte(validString), &n)
	expect.Nil(t, err)
	expect.True(t, n.Valid)
	expect.Equal(t, n.String, "foo")

}
Ejemplo n.º 4
0
func TestUnmarshalNullBool(t *testing.T) {
	var jsonNull string = `null`
	var jsonEmpty string = `""`
	var bogusString string = `"bogus"`
	var validTrue string = `true`
	var validFalse string = `false`

	var n Bool
	var err error
	err = json.Unmarshal([]byte(jsonNull), &n)
	expect.Nil(t, err)
	expect.False(t, n.Valid)

	err = json.Unmarshal([]byte(jsonEmpty), &n)
	expect.Nil(t, err)
	expect.False(t, n.Valid)

	err = json.Unmarshal([]byte(bogusString), &n)
	expect.NotNil(t, err)
	expect.False(t, n.Valid)

	err = json.Unmarshal([]byte(validTrue), &n)
	expect.Nil(t, err)
	expect.True(t, n.Valid)

	err = json.Unmarshal([]byte(validFalse), &n)
	expect.Nil(t, err)
	expect.True(t, n.Valid)
}
Ejemplo n.º 5
0
func TestScanNullTime(t *testing.T) {
	var rawTime = time.Now()
	var mysqlTime = "2010-07-03 13:24:33"
	var byteTime = []byte(mysqlTime)
	var notTime = 3

	var n Time
	var err error
	err = n.Scan(rawTime)
	expect.Nil(t, err)
	expect.True(t, n.Valid)
	expect.NotEmpty(t, n.Time.Format("2006-01-02 15:04:05"))

	err = n.Scan(mysqlTime)
	expect.Nil(t, err)
	expect.True(t, n.Valid)
	expect.Equal(t, n.Time.Format("2006-01-02 15:04:05"), mysqlTime)

	err = n.Scan(byteTime)
	expect.Nil(t, err)
	expect.True(t, n.Valid)
	expect.Equal(t, n.Time.Format("2006-01-02 15:04:05"), mysqlTime)

	err = n.Scan(notTime)
	expect.NotNil(t, err)
	expect.False(t, n.Valid)
}
Ejemplo n.º 6
0
func TestPositionIsValid(t *testing.T) {
	var pos Position

	pos = Position{"", 0, 0, 0}
	expect.False(t, pos.IsValid())

	pos = Position{"", 0, 1, 0}
	expect.True(t, pos.IsValid())
}
Ejemplo n.º 7
0
func TestScanNullDate(t *testing.T) {
	var rawTime = time.Date(2010, time.July, 3, 13, 24, 33, 999, time.UTC)
	var mysqlTime = "2010-07-03 13:24:33"
	var mysqlDate = "2010-07-03"
	var byteTime = []byte(mysqlTime)
	var byteDate = []byte(mysqlDate)
	var notTime = 3

	var n Date
	var err error
	err = n.Scan(rawTime)
	expect.Nil(t, err)
	expect.True(t, n.Valid)
	expect.Equal(t, n.Date.Year, 2010)
	expect.Equal(t, n.Date.Month, time.July)
	expect.Equal(t, n.Date.Day, 3)

	err = n.Scan(mysqlTime)
	expect.NotNil(t, err)
	expect.False(t, n.Valid)

	err = n.Scan(mysqlDate)
	expect.Nil(t, err)
	expect.True(t, n.Valid)
	expect.Equal(t, n.Date.Year, 2010)
	expect.Equal(t, n.Date.Month, time.July)
	expect.Equal(t, n.Date.Day, 3)

	err = n.Scan(byteTime)
	expect.NotNil(t, err)
	expect.False(t, n.Valid)

	err = n.Scan(byteDate)
	expect.Nil(t, err)
	expect.True(t, n.Valid)
	expect.Equal(t, n.Date.Year, 2010)
	expect.Equal(t, n.Date.Month, time.July)
	expect.Equal(t, n.Date.Day, 3)

	err = n.Scan(notTime)
	expect.NotNil(t, err)
	expect.False(t, n.Valid)
}
Ejemplo n.º 8
0
func TestSetNullable(t *testing.T) {
	var ns String
	expect.False(t, ns.Valid)
	ns.Set("hello world")
	expect.True(t, ns.Valid)

	var ni Int
	expect.False(t, ni.Valid)
	ni.Set(1)
	expect.True(t, ni.Valid)

	var nt Time
	expect.False(t, nt.Valid)
	nt.Set(time.Now())
	expect.True(t, nt.Valid)

	var nd Date
	expect.False(t, nd.Valid)
	nd.Set(date.From(time.Now()))
	expect.True(t, nd.Valid)

	var nb Bool
	expect.False(t, nb.Valid)
	nb.Set(true)
	expect.True(t, nb.Valid)

	var nu UUID
	expect.False(t, nu.Valid)
	nu.Set(uuid.UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8})
	expect.True(t, nu.Valid)
}
Ejemplo n.º 9
0
func TestUnmarshalNullInt(t *testing.T) {
	var jsonNull string = `null`
	var rationalFloat string = `12.22`
	var roundedFloat string = `16.0`
	var validZero string = `0`
	var validNegative string = `-300`
	var validPositive string = `1602525`

	var n Int
	var err error
	err = json.Unmarshal([]byte(jsonNull), &n)
	expect.Nil(t, err)
	expect.False(t, n.Valid)

	err = json.Unmarshal([]byte(rationalFloat), &n)
	expect.NotNil(t, err)
	expect.False(t, n.Valid)

	err = json.Unmarshal([]byte(roundedFloat), &n)
	expect.NotNil(t, err)
	expect.False(t, n.Valid)

	err = json.Unmarshal([]byte(validZero), &n)
	expect.Nil(t, err)
	expect.True(t, n.Valid)
	expect.Equal(t, n.Int, 0)

	err = json.Unmarshal([]byte(validNegative), &n)
	expect.Nil(t, err)
	expect.True(t, n.Valid)
	expect.Equal(t, n.Int, -300)

	err = json.Unmarshal([]byte(validPositive), &n)
	expect.Nil(t, err)
	expect.True(t, n.Valid)
	expect.Equal(t, n.Int, 1602525)
}
Ejemplo n.º 10
0
func TestScanNullUUID(t *testing.T) {
	// start with a null UUID and scan a typical UUID
	{
		expectedUUID := uuid.UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
		stringUUID := "6ba7b810-9dad-11d1-80b4-00c04fd430c8"

		n := UUID{}
		err := n.Scan(stringUUID)
		expect.Nil(t, err, "error unmarshaling null.UUID")
		expect.True(t, n.Valid, "null.UUID should be valid")
		expect.Equal(t, n.UUID, expectedUUID, "UUIDs should be equal")
	}

	// start with some UUID, and scan nil
	{
		n := SomeUUID(uuid.UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8})
		err := n.Scan(nil)
		expect.Nil(t, err, "error unmarshaling null.UUID")
		expect.False(t, n.Valid, "null.UUID should not be valid")
		expect.Equal(t, n.UUID, uuid.Nil, "null.UUID value should be equal to uuid.Nil")
	}
}