Skip to content

Commit

Permalink
Test null values
Browse files Browse the repository at this point in the history
  • Loading branch information
BrandonRomano committed Jun 23, 2017
1 parent 174aee1 commit e3cf84f
Show file tree
Hide file tree
Showing 3 changed files with 746 additions and 638 deletions.
101 changes: 101 additions & 0 deletions null_handlers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package validator_test

import (
v "github.com/go-carrot/validator"
"github.com/stretchr/testify/assert"
"gopkg.in/guregu/null.v3"
"testing"
"time"
)

// TestNullInt tests handling of a null.Int as the result
func TestNullInt(t *testing.T) {
// Test success case
var id null.Int
err := v.Validate([]*v.Value{
{Result: &id, Name: "id", Input: "12", TypeHandler: v.NullIntHandler},
})
assert.Nil(t, err)
assert.Equal(t, int64(12), id.Int64)
assert.True(t, id.Valid)

// Test failure case
var failureId null.Int
err = v.Validate([]*v.Value{
{Result: &failureId, Name: "id", Input: "12a", TypeHandler: v.NullIntHandler},
})
assert.NotNil(t, err)
assert.False(t, failureId.Valid)
}

// TestNullString tests handling of a null.String as the result
func TestNullString(t *testing.T) {
// Test success case
var slug null.String
err := v.Validate([]*v.Value{
{Result: &slug, Name: "slug", Input: "hello", TypeHandler: v.NullStringHandler},
})
assert.Nil(t, err)
assert.Equal(t, "hello", slug.String)
assert.True(t, slug.Valid)
}

// TestNullFloat tests handling of a null.Float as the result
func TestNullFloat(t *testing.T) {
// Test success case
var id null.Float
err := v.Validate([]*v.Value{
{Result: &id, Name: "id", Input: "12.8", TypeHandler: v.NullFloatHandler},
})
assert.Nil(t, err)
assert.Equal(t, float64(12.8), id.Float64)
assert.True(t, id.Valid)

// Test failure case
var failureId null.Float
err = v.Validate([]*v.Value{
{Result: &failureId, Name: "id", Input: "12.8a", TypeHandler: v.NullFloatHandler},
})
assert.NotNil(t, err)
assert.False(t, failureId.Valid)
}

// TestNullBool tests handling of a null.Bool as the result
func TestNullBool(t *testing.T) {
// Test success case
var someBool null.Bool
err := v.Validate([]*v.Value{
{Result: &someBool, Name: "some_bool", Input: "true", TypeHandler: v.NullBoolHandler},
})
assert.Nil(t, err)
assert.True(t, someBool.Bool)
assert.True(t, someBool.Valid)

// Test failure case
var someOtherBool null.Bool
err = v.Validate([]*v.Value{
{Result: &someOtherBool, Name: "some_other_bool", Input: "12.8a", TypeHandler: v.NullBoolHandler},
})
assert.NotNil(t, err)
assert.False(t, someOtherBool.Valid)
}

// TestNullTime tests handling of a null.Time as the result
func TestNullTime(t *testing.T) {
// Test success case
var successTime null.Time
err := v.Validate([]*v.Value{
{Result: &successTime, Name: "time", Input: "2012-11-01T22:08:41+00:00", TypeHandler: v.NullTimeHandler},
})
assert.Nil(t, err)
assert.Equal(t, successTime.Time.Year(), 2012)
assert.Equal(t, successTime.Time.Month(), time.November)
assert.Equal(t, successTime.Time.Day(), 1)

// Test failure case
var errorTime time.Time
err = v.Validate([]*v.Value{
{Result: &errorTime, Name: "time", Input: "abcd", TypeHandler: v.NullTimeHandler},
})
assert.NotNil(t, err)
}
Loading

0 comments on commit e3cf84f

Please sign in to comment.