2016-06-23 08:09:56 +02:00
|
|
|
package bdb
|
|
|
|
|
|
|
|
import "testing"
|
|
|
|
|
|
|
|
func TestSQLColDefinitions(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
cols := []Column{
|
|
|
|
{Name: "one", Type: "int64"},
|
|
|
|
{Name: "two", Type: "string"},
|
|
|
|
{Name: "three", Type: "string"},
|
|
|
|
}
|
|
|
|
|
|
|
|
defs := SQLColDefinitions(cols, []string{"one"})
|
|
|
|
if len(defs) != 1 {
|
|
|
|
t.Error("wrong number of defs:", len(defs))
|
|
|
|
}
|
|
|
|
if got := defs[0].String(); got != "one int64" {
|
|
|
|
t.Error("wrong def:", got)
|
|
|
|
}
|
|
|
|
|
|
|
|
defs = SQLColDefinitions(cols, []string{"one", "three"})
|
|
|
|
if len(defs) != 2 {
|
|
|
|
t.Error("wrong number of defs:", len(defs))
|
|
|
|
}
|
|
|
|
if got := defs[0].String(); got != "one int64" {
|
|
|
|
t.Error("wrong def:", got)
|
|
|
|
}
|
|
|
|
if got := defs[1].String(); got != "three string" {
|
|
|
|
t.Error("wrong def:", got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-23 08:48:49 +02:00
|
|
|
func TestSQLDefStrings(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
cols := []Column{
|
|
|
|
{Name: "one", Type: "int64"},
|
|
|
|
{Name: "two", Type: "string"},
|
|
|
|
{Name: "three", Type: "string"},
|
|
|
|
}
|
|
|
|
|
|
|
|
defs := SQLColDefinitions(cols, []string{"one", "three"})
|
|
|
|
strs := SQLColDefStrings(defs)
|
|
|
|
|
|
|
|
if got := strs[0]; got != "one int64" {
|
|
|
|
t.Error("wrong str:", got)
|
|
|
|
}
|
|
|
|
if got := strs[1]; got != "three string" {
|
|
|
|
t.Error("wrong str:", got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-23 08:09:56 +02:00
|
|
|
func TestAutoIncPrimaryKey(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
tests := map[string]struct {
|
|
|
|
Ok bool
|
|
|
|
Expect Column
|
|
|
|
Pkey *PrimaryKey
|
|
|
|
Columns []Column
|
|
|
|
}{
|
|
|
|
"nillcase": {
|
|
|
|
Ok: false,
|
|
|
|
Pkey: nil,
|
|
|
|
Columns: nil,
|
|
|
|
},
|
|
|
|
"easycase": {
|
|
|
|
Ok: true,
|
2016-07-09 19:13:35 +02:00
|
|
|
Expect: Column{Name: "one", Type: "int32", Nullable: false, Default: `nextval('abc'::regclass)`},
|
2016-06-23 08:09:56 +02:00
|
|
|
Pkey: &PrimaryKey{Name: "pkey", Columns: []string{"one"}},
|
2016-07-09 19:13:35 +02:00
|
|
|
Columns: []Column{Column{Name: "one", Type: "int32", Nullable: false, Default: `nextval('abc'::regclass)`}},
|
2016-06-23 08:09:56 +02:00
|
|
|
},
|
|
|
|
"missingcase": {
|
|
|
|
Ok: false,
|
|
|
|
Pkey: &PrimaryKey{Name: "pkey", Columns: []string{"two"}},
|
2016-07-09 19:13:35 +02:00
|
|
|
Columns: []Column{Column{Name: "one", Type: "int32", Nullable: false, Default: `nextval('abc'::regclass)`}},
|
2016-06-23 08:09:56 +02:00
|
|
|
},
|
|
|
|
"wrongtype": {
|
|
|
|
Ok: false,
|
|
|
|
Pkey: &PrimaryKey{Name: "pkey", Columns: []string{"one"}},
|
2016-07-09 19:13:35 +02:00
|
|
|
Columns: []Column{Column{Name: "one", Type: "string", Nullable: false, Default: `nextval('abc'::regclass)`}},
|
2016-06-23 08:09:56 +02:00
|
|
|
},
|
|
|
|
"nodefault": {
|
|
|
|
Ok: false,
|
|
|
|
Pkey: &PrimaryKey{Name: "pkey", Columns: []string{"one"}},
|
2016-07-09 19:13:35 +02:00
|
|
|
Columns: []Column{Column{Name: "one", Type: "string", Nullable: false, Default: ``}},
|
2016-06-23 08:09:56 +02:00
|
|
|
},
|
|
|
|
"nullable": {
|
|
|
|
Ok: false,
|
|
|
|
Pkey: &PrimaryKey{Name: "pkey", Columns: []string{"one"}},
|
2016-07-09 19:13:35 +02:00
|
|
|
Columns: []Column{Column{Name: "one", Type: "string", Nullable: true, Default: `nextval('abc'::regclass)`}},
|
2016-06-23 08:09:56 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for testName, test := range tests {
|
2016-06-23 08:48:49 +02:00
|
|
|
pkey := AutoIncPrimaryKey(test.Columns, test.Pkey)
|
|
|
|
if ok := (pkey != nil); ok != test.Ok {
|
2016-06-23 08:09:56 +02:00
|
|
|
t.Errorf("%s) found state was wrong, want: %t, got: %t", testName, test.Ok, ok)
|
2016-06-23 08:48:49 +02:00
|
|
|
} else if test.Ok && *pkey != test.Expect {
|
2016-06-23 08:09:56 +02:00
|
|
|
t.Errorf("%s) wrong primary key, want: %#v, got %#v", testName, test.Expect, pkey)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|