2016-06-23 08:09:56 +02:00
|
|
|
package bdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestColumnNames(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
cols := []Column{
|
|
|
|
Column{Name: "one"},
|
|
|
|
Column{Name: "two"},
|
|
|
|
Column{Name: "three"},
|
|
|
|
}
|
|
|
|
|
|
|
|
out := strings.Join(ColumnNames(cols), " ")
|
|
|
|
if out != "one two three" {
|
|
|
|
t.Error("output was wrong:", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-13 18:51:40 +02:00
|
|
|
func TestColumnDBTypes(t *testing.T) {
|
|
|
|
cols := []Column{
|
|
|
|
Column{Name: "test_one", DBType: "integer"},
|
|
|
|
Column{Name: "test_two", DBType: "interval"},
|
|
|
|
}
|
|
|
|
|
|
|
|
res := ColumnDBTypes(cols)
|
|
|
|
if res["TestOne"] != "integer" {
|
|
|
|
t.Errorf(`Expected res["TestOne"]="integer", got: %s`, res["TestOne"])
|
|
|
|
}
|
|
|
|
if res["TestTwo"] != "interval" {
|
|
|
|
t.Errorf(`Expected res["TestOne"]="interval", got: %s`, res["TestOne"])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-23 08:09:56 +02:00
|
|
|
func TestFilterColumnsByDefault(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
cols := []Column{
|
|
|
|
{Name: "col1", Default: ""},
|
|
|
|
{Name: "col2", Default: "things"},
|
|
|
|
{Name: "col3", Default: ""},
|
|
|
|
{Name: "col4", Default: "things2"},
|
|
|
|
}
|
|
|
|
|
2016-06-23 08:48:49 +02:00
|
|
|
res := FilterColumnsByDefault(false, cols)
|
2016-06-23 08:09:56 +02:00
|
|
|
if res[0].Name != `col1` {
|
|
|
|
t.Errorf("Invalid result: %#v", res)
|
|
|
|
}
|
|
|
|
if res[1].Name != `col3` {
|
|
|
|
t.Errorf("Invalid result: %#v", res)
|
|
|
|
}
|
|
|
|
|
2016-06-23 08:48:49 +02:00
|
|
|
res = FilterColumnsByDefault(true, cols)
|
2016-06-23 08:09:56 +02:00
|
|
|
if res[0].Name != `col2` {
|
|
|
|
t.Errorf("Invalid result: %#v", res)
|
|
|
|
}
|
|
|
|
if res[1].Name != `col4` {
|
|
|
|
t.Errorf("Invalid result: %#v", res)
|
|
|
|
}
|
|
|
|
|
2016-06-23 08:48:49 +02:00
|
|
|
res = FilterColumnsByDefault(false, []Column{})
|
2016-06-23 08:09:56 +02:00
|
|
|
if res != nil {
|
|
|
|
t.Errorf("Invalid result: %#v", res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-08 18:39:36 +02:00
|
|
|
func TestDefaultValues(t *testing.T) {
|
2016-07-09 18:53:21 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-07-06 08:02:35 +02:00
|
|
|
c := Column{}
|
|
|
|
|
|
|
|
c.Default = `\x12345678`
|
|
|
|
c.Type = "[]byte"
|
|
|
|
|
2016-07-08 18:39:36 +02:00
|
|
|
res := DefaultValues([]Column{c})
|
|
|
|
if len(res) != 1 {
|
|
|
|
t.Errorf("Expected res len 1, got %d", len(res))
|
|
|
|
}
|
|
|
|
if res[0] != `[]byte{0x12, 0x34, 0x56, 0x78}` {
|
2016-07-06 08:02:35 +02:00
|
|
|
t.Errorf("Invalid result: %#v", res)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Default = `\x`
|
|
|
|
|
2016-07-08 18:39:36 +02:00
|
|
|
res = DefaultValues([]Column{c})
|
|
|
|
if res[0] != `[]byte{}` {
|
2016-07-06 08:02:35 +02:00
|
|
|
t.Errorf("Invalid result: %#v", res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-23 08:09:56 +02:00
|
|
|
func TestFilterColumnsByAutoIncrement(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
cols := []Column{
|
|
|
|
{Name: "col1", Default: `nextval("thing"::thing)`},
|
|
|
|
{Name: "col2", Default: "things"},
|
|
|
|
{Name: "col3", Default: ""},
|
|
|
|
{Name: "col4", Default: `nextval("thing"::thing)`},
|
|
|
|
}
|
|
|
|
|
2016-07-06 08:02:35 +02:00
|
|
|
res := FilterColumnsByAutoIncrement(true, cols)
|
2016-06-23 08:09:56 +02:00
|
|
|
if res[0].Name != `col1` {
|
|
|
|
t.Errorf("Invalid result: %#v", res)
|
|
|
|
}
|
|
|
|
if res[1].Name != `col4` {
|
|
|
|
t.Errorf("Invalid result: %#v", res)
|
|
|
|
}
|
|
|
|
|
2016-07-06 08:02:35 +02:00
|
|
|
res = FilterColumnsByAutoIncrement(false, cols)
|
|
|
|
if res[0].Name != `col2` {
|
|
|
|
t.Errorf("Invalid result: %#v", res)
|
|
|
|
}
|
|
|
|
if res[1].Name != `col3` {
|
|
|
|
t.Errorf("Invalid result: %#v", res)
|
|
|
|
}
|
|
|
|
|
|
|
|
res = FilterColumnsByAutoIncrement(true, []Column{})
|
2016-06-23 08:09:56 +02:00
|
|
|
if res != nil {
|
|
|
|
t.Errorf("Invalid result: %#v", res)
|
|
|
|
}
|
|
|
|
}
|