2016-03-16 08:04:26 +01:00
|
|
|
package boil
|
|
|
|
|
|
|
|
import (
|
2016-05-02 08:34:25 +02:00
|
|
|
"reflect"
|
2016-03-16 08:04:26 +01:00
|
|
|
"testing"
|
|
|
|
"time"
|
2016-05-02 08:34:25 +02:00
|
|
|
|
2016-06-08 07:45:34 +02:00
|
|
|
"gopkg.in/nullbio/null.v4"
|
2016-03-16 08:04:26 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type testObj struct {
|
|
|
|
ID int
|
|
|
|
Name string `db:"TestHello"`
|
|
|
|
HeadSize int
|
|
|
|
}
|
|
|
|
|
2016-05-02 08:34:25 +02:00
|
|
|
func TestSetComplement(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
A []string
|
|
|
|
B []string
|
|
|
|
C []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
[]string{"thing1", "thing2", "thing3"},
|
|
|
|
[]string{"thing2", "otherthing", "stuff"},
|
|
|
|
[]string{"thing1", "thing3"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{},
|
|
|
|
[]string{"thing1", "thing2"},
|
|
|
|
[]string{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{"thing1", "thing2"},
|
|
|
|
[]string{},
|
|
|
|
[]string{"thing1", "thing2"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{"thing1", "thing2"},
|
|
|
|
[]string{"thing1", "thing2"},
|
|
|
|
[]string{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
|
|
|
c := SetComplement(test.A, test.B)
|
|
|
|
if !reflect.DeepEqual(test.C, c) {
|
|
|
|
t.Errorf("[%d] mismatch:\nWant: %#v\nGot: %#v", i, test.C, c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetIntersect(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
A []string
|
|
|
|
B []string
|
|
|
|
C []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
[]string{"thing1", "thing2", "thing3"},
|
|
|
|
[]string{"thing2", "otherthing", "stuff"},
|
|
|
|
[]string{"thing2"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{},
|
|
|
|
[]string{"thing1", "thing2"},
|
|
|
|
[]string{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{"thing1", "thing2"},
|
|
|
|
[]string{},
|
|
|
|
[]string{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{"thing1", "thing2"},
|
|
|
|
[]string{"thing1", "thing2"},
|
|
|
|
[]string{"thing1", "thing2"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
|
|
|
c := SetIntersect(test.A, test.B)
|
|
|
|
if !reflect.DeepEqual(test.C, c) {
|
|
|
|
t.Errorf("[%d] mismatch:\nWant: %#v\nGot: %#v", i, test.C, c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-15 12:14:47 +02:00
|
|
|
func TestSetMerge(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
A []string
|
|
|
|
B []string
|
|
|
|
C []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
[]string{"thing1", "thing2", "thing3"},
|
|
|
|
[]string{"thing1", "thing3", "thing4"},
|
|
|
|
[]string{"thing1", "thing2", "thing3", "thing4"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{},
|
|
|
|
[]string{"thing1", "thing2"},
|
|
|
|
[]string{"thing1", "thing2"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{"thing1", "thing2"},
|
|
|
|
[]string{},
|
|
|
|
[]string{"thing1", "thing2"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{"thing1", "thing2", "thing3"},
|
|
|
|
[]string{"thing1", "thing2", "thing3"},
|
|
|
|
[]string{"thing1", "thing2", "thing3"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{"thing1", "thing2"},
|
|
|
|
[]string{"thing3", "thing4"},
|
|
|
|
[]string{"thing1", "thing2", "thing3", "thing4"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
|
|
|
m := SetMerge(test.A, test.B)
|
|
|
|
if !reflect.DeepEqual(test.C, m) {
|
|
|
|
t.Errorf("[%d] mismatch:\nWant: %#v\nGot: %#v", i, test.C, m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-02 08:34:25 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-07 08:21:00 +02:00
|
|
|
func TestSortByKeys(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
Keys []string
|
|
|
|
Strs []string
|
|
|
|
Ret []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
[]string{"id", "name", "thing", "stuff"},
|
|
|
|
[]string{"thing", "stuff", "name", "id"},
|
|
|
|
[]string{"id", "name", "thing", "stuff"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{"id", "name", "thing", "stuff"},
|
|
|
|
[]string{"id", "name", "thing", "stuff"},
|
|
|
|
[]string{"id", "name", "thing", "stuff"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{"id", "name", "thing", "stuff"},
|
|
|
|
[]string{"stuff", "thing"},
|
|
|
|
[]string{"thing", "stuff"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
|
|
|
z := SortByKeys(test.Keys, test.Strs)
|
|
|
|
if !reflect.DeepEqual(test.Ret, z) {
|
|
|
|
t.Errorf("[%d] mismatch:\nWant: %#v\nGot: %#v", i, test.Ret, z)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|