sqlboiler/queries/helpers_test.go

68 lines
1.4 KiB
Go
Raw Normal View History

package queries
import (
"reflect"
"testing"
"time"
2020-02-10 14:49:11 +01:00
null "github.com/lbryio/lbry.go/v2/extras/null"
)
type testObj struct {
ID int
Name string `db:"TestHello"`
HeadSize int
}
func TestNonZeroDefaultSet(t *testing.T) {
t.Parallel()
type Anything struct {
ID int
Name string
CreatedAt *time.Time
UpdatedAt null.Time
}
now := time.Now()
tests := []struct {
Defaults []string
Obj interface{}
Ret []string
}{
{
[]string{"id"},
Anything{Name: "hi", CreatedAt: nil, UpdatedAt: null.Time{Valid: false}},
[]string{},
},
{
[]string{"id"},
Anything{ID: 5, Name: "hi", CreatedAt: nil, UpdatedAt: null.Time{Valid: false}},
[]string{"id"},
},
{
[]string{},
Anything{ID: 5, Name: "hi", CreatedAt: nil, UpdatedAt: null.Time{Valid: false}},
[]string{},
},
{
[]string{"id", "created_at", "updated_at"},
Anything{ID: 5, Name: "hi", CreatedAt: nil, UpdatedAt: null.Time{Valid: false}},
[]string{"id"},
},
{
[]string{"id", "created_at", "updated_at"},
Anything{ID: 5, Name: "hi", CreatedAt: &now, UpdatedAt: null.Time{Valid: true, Time: time.Now()}},
[]string{"id", "created_at", "updated_at"},
},
}
for i, test := range tests {
z := NonZeroDefaultSet(test.Defaults, test.Obj)
if !reflect.DeepEqual(test.Ret, z) {
t.Errorf("[%d] mismatch:\nWant: %#v\nGot: %#v", i, test.Ret, z)
}
}
}