2016-09-15 05:45:09 +02:00
|
|
|
package queries
|
2016-03-16 08:04:26 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
2018-02-22 19:18:41 +01:00
|
|
|
null "github.com/lbryio/lbry.go/null"
|
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 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 {
|
2016-09-03 20:54:23 +02:00
|
|
|
z := NonZeroDefaultSet(test.Defaults, test.Obj)
|
2016-05-02 08:34:25 +02:00
|
|
|
if !reflect.DeepEqual(test.Ret, z) {
|
|
|
|
t.Errorf("[%d] mismatch:\nWant: %#v\nGot: %#v", i, test.Ret, z)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|