2015-01-03 22:03:57 +02:00
|
|
|
package authboss
|
|
|
|
|
|
|
|
import (
|
2015-03-04 08:34:37 +02:00
|
|
|
"database/sql"
|
|
|
|
"database/sql/driver"
|
2015-01-03 22:03:57 +02:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2015-03-04 08:34:37 +02:00
|
|
|
type NullTime struct {
|
|
|
|
Time time.Time
|
|
|
|
Valid bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nt *NullTime) Scan(value interface{}) error {
|
|
|
|
nt.Time, nt.Valid = value.(time.Time)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nt NullTime) Value() (driver.Value, error) {
|
|
|
|
if !nt.Valid {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return nt.Time, nil
|
|
|
|
}
|
|
|
|
|
2015-01-15 05:18:45 +02:00
|
|
|
func TestAttributes_Names(t *testing.T) {
|
2015-02-16 06:07:36 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2015-01-15 05:18:45 +02:00
|
|
|
attr := Attributes{
|
2015-01-19 00:35:44 +02:00
|
|
|
"integer": 5,
|
|
|
|
"string": "string",
|
2015-02-07 14:27:12 +02:00
|
|
|
"bool": true,
|
2015-01-19 00:35:44 +02:00
|
|
|
"date_time": time.Now(),
|
2015-01-15 05:18:45 +02:00
|
|
|
}
|
|
|
|
names := attr.Names()
|
|
|
|
|
2015-02-07 14:27:12 +02:00
|
|
|
found := map[string]bool{"integer": false, "string": false, "bool": false, "date_time": false}
|
2015-01-15 05:18:45 +02:00
|
|
|
for _, n := range names {
|
|
|
|
found[n] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range found {
|
|
|
|
if !v {
|
|
|
|
t.Error("Could not find:", k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAttributeMeta_Names(t *testing.T) {
|
2015-02-16 06:07:36 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2015-01-15 05:18:45 +02:00
|
|
|
meta := AttributeMeta{
|
2015-01-19 00:35:44 +02:00
|
|
|
"integer": Integer,
|
|
|
|
"string": String,
|
2015-02-07 14:27:12 +02:00
|
|
|
"bool": Bool,
|
2015-01-19 00:35:44 +02:00
|
|
|
"date_time": DateTime,
|
2015-01-15 05:18:45 +02:00
|
|
|
}
|
|
|
|
names := meta.Names()
|
|
|
|
|
2015-02-07 14:27:12 +02:00
|
|
|
found := map[string]bool{"integer": false, "string": false, "bool": false, "date_time": false}
|
2015-01-15 05:18:45 +02:00
|
|
|
for _, n := range names {
|
|
|
|
found[n] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range found {
|
|
|
|
if !v {
|
|
|
|
t.Error("Could not find:", k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-24 23:03:06 +02:00
|
|
|
func TestAttributeMeta_Helpers(t *testing.T) {
|
2015-03-31 21:34:03 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2015-02-24 23:03:06 +02:00
|
|
|
now := time.Now()
|
|
|
|
attr := Attributes{
|
2015-03-04 08:34:37 +02:00
|
|
|
"integer": int64(5),
|
2015-02-24 23:03:06 +02:00
|
|
|
"string": "a",
|
|
|
|
"bool": true,
|
|
|
|
"date_time": now,
|
|
|
|
}
|
|
|
|
|
|
|
|
if str, ok := attr.String("string"); !ok || str != "a" {
|
|
|
|
t.Error(str, ok)
|
|
|
|
}
|
|
|
|
if str, err := attr.StringErr("string"); err != nil || str != "a" {
|
|
|
|
t.Error(str, err)
|
|
|
|
}
|
|
|
|
if str, ok := attr.String("notstring"); ok {
|
|
|
|
t.Error(str, ok)
|
|
|
|
}
|
|
|
|
if str, err := attr.StringErr("notstring"); err == nil {
|
|
|
|
t.Error(str, err)
|
|
|
|
}
|
|
|
|
|
2015-03-04 08:34:37 +02:00
|
|
|
if integer, ok := attr.Int64("integer"); !ok || integer != 5 {
|
2015-02-24 23:03:06 +02:00
|
|
|
t.Error(integer, ok)
|
|
|
|
}
|
2015-03-04 08:34:37 +02:00
|
|
|
if integer, err := attr.Int64Err("integer"); err != nil || integer != 5 {
|
2015-02-24 23:03:06 +02:00
|
|
|
t.Error(integer, err)
|
|
|
|
}
|
2015-03-04 08:34:37 +02:00
|
|
|
if integer, ok := attr.Int64("notinteger"); ok {
|
2015-02-24 23:03:06 +02:00
|
|
|
t.Error(integer, ok)
|
|
|
|
}
|
2015-03-04 08:34:37 +02:00
|
|
|
if integer, err := attr.Int64Err("notinteger"); err == nil {
|
2015-02-24 23:03:06 +02:00
|
|
|
t.Error(integer, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if boolean, ok := attr.Bool("bool"); !ok || !boolean {
|
|
|
|
t.Error(boolean, ok)
|
|
|
|
}
|
|
|
|
if boolean, err := attr.BoolErr("bool"); err != nil || !boolean {
|
|
|
|
t.Error(boolean, err)
|
|
|
|
}
|
|
|
|
if boolean, ok := attr.Bool("notbool"); ok {
|
|
|
|
t.Error(boolean, ok)
|
|
|
|
}
|
|
|
|
if boolean, err := attr.BoolErr("notbool"); err == nil {
|
|
|
|
t.Error(boolean, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if date, ok := attr.DateTime("date_time"); !ok || date != now {
|
|
|
|
t.Error(date, ok)
|
|
|
|
}
|
|
|
|
if date, err := attr.DateTimeErr("date_time"); err != nil || date != now {
|
|
|
|
t.Error(date, err)
|
|
|
|
}
|
|
|
|
if date, ok := attr.DateTime("notdate_time"); ok {
|
|
|
|
t.Error(date, ok)
|
|
|
|
}
|
|
|
|
if date, err := attr.DateTimeErr("notdate_time"); err == nil {
|
|
|
|
t.Error(date, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-15 05:18:45 +02:00
|
|
|
func TestDataType_String(t *testing.T) {
|
2015-02-16 06:07:36 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2015-01-15 05:18:45 +02:00
|
|
|
if Integer.String() != "Integer" {
|
|
|
|
t.Error("Expected Integer:", Integer)
|
|
|
|
}
|
|
|
|
if String.String() != "String" {
|
|
|
|
t.Error("Expected String:", String)
|
|
|
|
}
|
2015-02-07 14:27:12 +02:00
|
|
|
if Bool.String() != "Bool" {
|
|
|
|
t.Error("Expected Bool:", String)
|
|
|
|
}
|
2015-01-15 05:18:45 +02:00
|
|
|
if DateTime.String() != "DateTime" {
|
|
|
|
t.Error("Expected DateTime:", DateTime)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-03 22:03:57 +02:00
|
|
|
func TestAttributes_Bind(t *testing.T) {
|
2015-02-16 06:07:36 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2015-01-03 22:03:57 +02:00
|
|
|
anInteger := 5
|
|
|
|
aString := "string"
|
2015-02-07 14:27:12 +02:00
|
|
|
aBool := true
|
2015-01-03 22:03:57 +02:00
|
|
|
aTime := time.Now()
|
|
|
|
|
|
|
|
data := Attributes{
|
2015-01-19 00:35:44 +02:00
|
|
|
"integer": anInteger,
|
|
|
|
"string": aString,
|
2015-02-07 14:27:12 +02:00
|
|
|
"bool": aBool,
|
2015-01-19 00:35:44 +02:00
|
|
|
"date_time": aTime,
|
2015-01-03 22:03:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
s := struct {
|
2015-01-19 00:35:44 +02:00
|
|
|
Integer int
|
|
|
|
String string
|
2015-02-07 14:27:12 +02:00
|
|
|
Bool bool
|
2015-01-19 00:35:44 +02:00
|
|
|
DateTime time.Time
|
2015-01-03 22:03:57 +02:00
|
|
|
}{}
|
|
|
|
|
2015-02-26 22:15:33 +02:00
|
|
|
if err := data.Bind(&s, false); err != nil {
|
2015-01-03 22:03:57 +02:00
|
|
|
t.Error("Unexpected Error:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.Integer != anInteger {
|
|
|
|
t.Error("Integer was not set.")
|
|
|
|
}
|
|
|
|
if s.String != aString {
|
|
|
|
t.Error("String was not set.")
|
|
|
|
}
|
2015-02-07 14:27:12 +02:00
|
|
|
if s.Bool != aBool {
|
|
|
|
t.Error("Bool was not set.")
|
|
|
|
}
|
2015-01-19 00:35:44 +02:00
|
|
|
if s.DateTime != aTime {
|
|
|
|
t.Error("DateTime was not set.")
|
2015-01-03 22:03:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-26 22:15:33 +02:00
|
|
|
func TestAttributes_BindIgnoreMissing(t *testing.T) {
|
2015-03-04 08:34:37 +02:00
|
|
|
t.Parallel()
|
2015-02-26 22:15:33 +02:00
|
|
|
|
|
|
|
anInteger := 5
|
|
|
|
aString := "string"
|
|
|
|
|
|
|
|
data := Attributes{
|
2015-03-04 08:34:37 +02:00
|
|
|
"integer": anInteger,
|
|
|
|
"string": aString,
|
2015-02-26 22:15:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
s := struct {
|
2015-03-04 08:34:37 +02:00
|
|
|
Integer int
|
2015-02-26 22:15:33 +02:00
|
|
|
}{}
|
|
|
|
|
|
|
|
if err := data.Bind(&s, false); err == nil {
|
|
|
|
t.Error("Expected error about missing attributes:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := data.Bind(&s, true); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.Integer != anInteger {
|
|
|
|
t.Error("Integer was not set.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-03 22:03:57 +02:00
|
|
|
func TestAttributes_BindNoPtr(t *testing.T) {
|
2015-02-16 06:07:36 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2015-01-03 22:03:57 +02:00
|
|
|
data := Attributes{}
|
|
|
|
s := struct{}{}
|
|
|
|
|
2015-02-26 22:15:33 +02:00
|
|
|
if err := data.Bind(s, false); err == nil {
|
2015-01-03 22:03:57 +02:00
|
|
|
t.Error("Expected an error.")
|
|
|
|
} else if !strings.Contains(err.Error(), "struct pointer") {
|
|
|
|
t.Error("Expected an error about pointers got:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAttributes_BindMissingField(t *testing.T) {
|
2015-02-16 06:07:36 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2015-01-15 02:07:15 +02:00
|
|
|
data := Attributes{"Integer": 5}
|
2015-01-03 22:03:57 +02:00
|
|
|
s := struct{}{}
|
|
|
|
|
2015-02-26 22:15:33 +02:00
|
|
|
if err := data.Bind(&s, false); err == nil {
|
2015-01-03 22:03:57 +02:00
|
|
|
t.Error("Expected an error.")
|
|
|
|
} else if !strings.Contains(err.Error(), "missing") {
|
|
|
|
t.Error("Expected an error about missing fields, got:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAttributes_BindTypeFail(t *testing.T) {
|
2015-02-16 06:07:36 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2015-01-03 22:03:57 +02:00
|
|
|
tests := []struct {
|
|
|
|
Attr Attributes
|
|
|
|
Err string
|
|
|
|
ToBind interface{}
|
|
|
|
}{
|
|
|
|
{
|
2015-01-15 03:48:14 +02:00
|
|
|
Attr: Attributes{"integer": 5},
|
2015-01-03 22:03:57 +02:00
|
|
|
Err: "should be int",
|
|
|
|
ToBind: &struct {
|
|
|
|
Integer string
|
|
|
|
}{},
|
|
|
|
},
|
|
|
|
{
|
2015-01-15 03:48:14 +02:00
|
|
|
Attr: Attributes{"string": ""},
|
2015-01-03 22:03:57 +02:00
|
|
|
Err: "should be string",
|
|
|
|
ToBind: &struct {
|
|
|
|
String int
|
|
|
|
}{},
|
|
|
|
},
|
2015-02-07 14:27:12 +02:00
|
|
|
{
|
|
|
|
Attr: Attributes{"bool": true},
|
|
|
|
Err: "should be bool",
|
|
|
|
ToBind: &struct {
|
|
|
|
Bool string
|
|
|
|
}{},
|
|
|
|
},
|
2015-01-03 22:03:57 +02:00
|
|
|
{
|
2015-01-15 03:48:14 +02:00
|
|
|
Attr: Attributes{"date": time.Time{}},
|
2015-01-03 22:03:57 +02:00
|
|
|
Err: "should be time.Time",
|
|
|
|
ToBind: &struct {
|
|
|
|
Date int
|
|
|
|
}{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
2015-02-26 22:15:33 +02:00
|
|
|
if err := test.Attr.Bind(test.ToBind, false); err == nil {
|
2015-01-03 22:03:57 +02:00
|
|
|
t.Errorf("%d> Expected an error.", i)
|
|
|
|
} else if !strings.Contains(err.Error(), test.Err) {
|
|
|
|
t.Errorf("%d> Expected an error about %q got: %q", i, test.Err, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2015-01-07 21:39:12 +02:00
|
|
|
|
2015-03-04 08:34:37 +02:00
|
|
|
func TestAttributes_BindScannerValues(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
s1 := struct {
|
|
|
|
Count sql.NullInt64
|
|
|
|
Time NullTime
|
|
|
|
}{
|
|
|
|
sql.NullInt64{},
|
|
|
|
NullTime{},
|
|
|
|
}
|
|
|
|
|
|
|
|
nowTime := time.Now()
|
|
|
|
|
|
|
|
attrs := Attributes{"count": 12, "time": nowTime}
|
|
|
|
if err := attrs.Bind(&s1, false); err != nil {
|
|
|
|
t.Error("Unexpected error:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !s1.Count.Valid {
|
|
|
|
t.Error("Expected valid NullInt64")
|
|
|
|
}
|
|
|
|
if s1.Count.Int64 != 12 {
|
|
|
|
t.Error("Unexpected value:", s1.Count.Int64)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !s1.Time.Valid {
|
|
|
|
t.Error("Expected valid time.Time")
|
|
|
|
}
|
|
|
|
if !s1.Time.Time.Equal(nowTime) {
|
|
|
|
t.Error("Unexpected value:", s1.Time.Time)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnbind(t *testing.T) {
|
2015-02-16 06:07:36 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2015-01-07 21:39:12 +02:00
|
|
|
s1 := struct {
|
|
|
|
Integer int
|
|
|
|
String string
|
2015-02-07 14:27:12 +02:00
|
|
|
Bool bool
|
2015-01-07 21:39:12 +02:00
|
|
|
Time time.Time
|
|
|
|
|
|
|
|
SomethingElse1 int32
|
|
|
|
SomethingElse2 *Config
|
|
|
|
|
|
|
|
unexported int
|
2015-02-07 14:27:12 +02:00
|
|
|
}{5, "string", true, time.Now(), 5, nil, 5}
|
2015-01-07 21:39:12 +02:00
|
|
|
|
2015-01-15 05:18:45 +02:00
|
|
|
attr := Unbind(&s1)
|
2015-02-07 14:27:12 +02:00
|
|
|
if len(attr) != 4 {
|
2015-03-04 08:34:37 +02:00
|
|
|
t.Error("Expected 4 fields, got:", len(attr))
|
2015-01-07 21:39:12 +02:00
|
|
|
}
|
|
|
|
|
2015-01-15 03:48:14 +02:00
|
|
|
if v, ok := attr["integer"]; !ok {
|
2015-01-07 21:39:12 +02:00
|
|
|
t.Error("Could not find Integer entry.")
|
2015-01-15 02:07:15 +02:00
|
|
|
} else if val, ok := v.(int); !ok {
|
2015-01-07 21:39:12 +02:00
|
|
|
t.Errorf("Underlying type is wrong: %T", v)
|
2015-01-15 02:07:15 +02:00
|
|
|
} else if s1.Integer != val {
|
|
|
|
t.Error("Underlying value is wrong:", val)
|
2015-01-07 21:39:12 +02:00
|
|
|
}
|
|
|
|
|
2015-01-15 03:48:14 +02:00
|
|
|
if v, ok := attr["string"]; !ok {
|
2015-01-07 21:39:12 +02:00
|
|
|
t.Error("Could not find String entry.")
|
2015-01-15 02:07:15 +02:00
|
|
|
} else if val, ok := v.(string); !ok {
|
2015-01-07 21:39:12 +02:00
|
|
|
t.Errorf("Underlying type is wrong: %T", v)
|
2015-01-15 02:07:15 +02:00
|
|
|
} else if s1.String != val {
|
|
|
|
t.Error("Underlying value is wrong:", val)
|
2015-01-07 21:39:12 +02:00
|
|
|
}
|
|
|
|
|
2015-02-07 14:27:12 +02:00
|
|
|
if v, ok := attr["bool"]; !ok {
|
|
|
|
t.Error("Could not find String entry.")
|
|
|
|
} else if val, ok := v.(bool); !ok {
|
|
|
|
t.Errorf("Underlying type is wrong: %T", v)
|
|
|
|
} else if s1.Bool != val {
|
|
|
|
t.Error("Underlying value is wrong:", val)
|
|
|
|
}
|
|
|
|
|
2015-01-15 03:48:14 +02:00
|
|
|
if v, ok := attr["time"]; !ok {
|
2015-01-07 21:39:12 +02:00
|
|
|
t.Error("Could not find Time entry.")
|
2015-01-15 02:07:15 +02:00
|
|
|
} else if val, ok := v.(time.Time); !ok {
|
2015-01-07 21:39:12 +02:00
|
|
|
t.Errorf("Underlying type is wrong: %T", v)
|
2015-01-15 02:07:15 +02:00
|
|
|
} else if s1.Time != val {
|
|
|
|
t.Error("Underlying value is wrong:", val)
|
2015-01-07 21:39:12 +02:00
|
|
|
}
|
|
|
|
}
|
2015-01-19 00:35:44 +02:00
|
|
|
|
2015-03-04 08:34:37 +02:00
|
|
|
func TestUnbind_Valuer(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
nowTime := time.Now()
|
|
|
|
|
|
|
|
s1 := struct {
|
|
|
|
Count sql.NullInt64
|
|
|
|
Time NullTime
|
|
|
|
}{
|
2015-03-16 23:51:44 +02:00
|
|
|
sql.NullInt64{Int64: 12, Valid: true},
|
2015-03-04 08:34:37 +02:00
|
|
|
NullTime{nowTime, true},
|
|
|
|
}
|
|
|
|
|
|
|
|
attr := Unbind(&s1)
|
|
|
|
|
|
|
|
if v, ok := attr["count"]; !ok {
|
|
|
|
t.Error("Could not find NullInt64 entry.")
|
|
|
|
} else if val, ok := v.(int64); !ok {
|
|
|
|
t.Errorf("Underlying type is wrong: %T", v)
|
|
|
|
} else if 12 != val {
|
|
|
|
t.Error("Underlying value is wrong:", val)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := attr["time"]; !ok {
|
|
|
|
t.Error("Could not find NulLTime entry.")
|
|
|
|
} else if val, ok := v.(time.Time); !ok {
|
|
|
|
t.Errorf("Underlying type is wrong: %T", v)
|
|
|
|
} else if !nowTime.Equal(val) {
|
|
|
|
t.Error("Underlying value is wrong:", val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-19 00:35:44 +02:00
|
|
|
func TestCasingStyleConversions(t *testing.T) {
|
2015-02-16 06:07:36 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2015-03-14 07:09:01 +02:00
|
|
|
tests := []struct {
|
|
|
|
In string
|
|
|
|
Out string
|
|
|
|
}{
|
|
|
|
{"SomethingInCamel", "something_in_camel"},
|
|
|
|
{"Oauth2Anything", "oauth2_anything"},
|
2015-01-19 00:35:44 +02:00
|
|
|
}
|
|
|
|
|
2015-03-14 07:09:01 +02:00
|
|
|
for i, test := range tests {
|
|
|
|
out := camelToUnder(test.In)
|
|
|
|
if out != test.Out {
|
|
|
|
t.Errorf("%d) Expected %q got %q", i, test.Out, out)
|
|
|
|
}
|
|
|
|
out = underToCamel(out)
|
|
|
|
if out != test.In {
|
2015-03-16 23:42:45 +02:00
|
|
|
t.Errorf("%d), Expected %q got %q", i, test.In, out)
|
2015-03-14 07:09:01 +02:00
|
|
|
}
|
2015-01-19 00:35:44 +02:00
|
|
|
}
|
|
|
|
}
|