2016-06-23 08:09:56 +02:00
|
|
|
package bdb
|
2016-04-03 09:15:35 +02:00
|
|
|
|
2016-04-03 10:02:41 +02:00
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
type testInterface struct{}
|
|
|
|
|
|
|
|
func (t testInterface) TableNames() ([]string, error) {
|
|
|
|
return []string{"table1", "table2"}, nil
|
|
|
|
}
|
|
|
|
|
2016-07-12 08:49:42 +02:00
|
|
|
var testCols = []Column{
|
2016-08-08 09:46:06 +02:00
|
|
|
{Name: "col1", Type: "character varying"},
|
|
|
|
{Name: "col2", Type: "character varying", Nullable: true},
|
2016-07-12 08:49:42 +02:00
|
|
|
}
|
|
|
|
|
2016-04-03 10:02:41 +02:00
|
|
|
func (t testInterface) Columns(tableName string) ([]Column, error) {
|
2016-07-12 08:49:42 +02:00
|
|
|
return testCols, nil
|
2016-04-03 10:02:41 +02:00
|
|
|
}
|
|
|
|
|
2016-07-12 08:49:42 +02:00
|
|
|
var testPkey = &PrimaryKey{Name: "pkey1", Columns: []string{"col1", "col2"}}
|
|
|
|
|
2016-04-03 10:02:41 +02:00
|
|
|
func (t testInterface) PrimaryKeyInfo(tableName string) (*PrimaryKey, error) {
|
2016-07-12 08:49:42 +02:00
|
|
|
return testPkey, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var testFkeys = []ForeignKey{
|
|
|
|
{
|
|
|
|
Name: "fkey1",
|
|
|
|
Column: "col1",
|
|
|
|
ForeignTable: "table2",
|
|
|
|
ForeignColumn: "col2",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "fkey2",
|
|
|
|
Column: "col2",
|
|
|
|
ForeignTable: "table1",
|
|
|
|
ForeignColumn: "col1",
|
|
|
|
},
|
2016-04-03 10:02:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t testInterface) ForeignKeyInfo(tableName string) ([]ForeignKey, error) {
|
2016-07-12 08:49:42 +02:00
|
|
|
return testFkeys, nil
|
2016-04-03 10:02:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t testInterface) TranslateColumnType(column Column) Column {
|
|
|
|
column.Type = "string"
|
|
|
|
return column
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t testInterface) Open() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t testInterface) Close() {}
|
2016-04-03 09:15:35 +02:00
|
|
|
|
|
|
|
func TestTables(t *testing.T) {
|
2016-04-03 10:02:41 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
tables, err := Tables(testInterface{})
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2016-04-03 09:15:35 +02:00
|
|
|
|
2016-04-03 10:02:41 +02:00
|
|
|
if len(tables) != 2 {
|
|
|
|
t.Errorf("Expected len 2, got: %d\n", len(tables))
|
|
|
|
}
|
|
|
|
|
2016-07-12 08:49:42 +02:00
|
|
|
if !reflect.DeepEqual(tables[0].Columns, testCols) {
|
|
|
|
t.Errorf("Did not get expected columns, got:\n%#v\n%#v", tables[0].Columns, testCols)
|
2016-04-03 10:02:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if !tables[0].IsJoinTable || !tables[1].IsJoinTable {
|
|
|
|
t.Errorf("Expected IsJoinTable to be true")
|
|
|
|
}
|
|
|
|
|
2016-07-12 08:49:42 +02:00
|
|
|
if !reflect.DeepEqual(tables[0].PKey, testPkey) {
|
|
|
|
t.Errorf("Did not get expected PKey, got:\n#%v\n%#v", tables[0].PKey, testPkey)
|
2016-04-03 10:02:41 +02:00
|
|
|
}
|
|
|
|
|
2016-07-12 08:49:42 +02:00
|
|
|
if !reflect.DeepEqual(tables[0].FKeys, testFkeys) {
|
|
|
|
t.Errorf("Did not get expected Fkey, got:\n%#v\n%#v", tables[0].FKeys, testFkeys)
|
2016-04-03 10:02:41 +02:00
|
|
|
}
|
|
|
|
|
2016-07-12 08:49:42 +02:00
|
|
|
if len(tables[0].ToManyRelationships) != 1 {
|
|
|
|
t.Error("wanted a to many relationship")
|
|
|
|
}
|
|
|
|
if len(tables[1].ToManyRelationships) != 1 {
|
|
|
|
t.Error("wanted a to many relationship")
|
2016-04-03 10:02:41 +02:00
|
|
|
}
|
2016-04-03 09:15:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetIsJoinTable(t *testing.T) {
|
2016-04-03 10:02:41 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-04-03 09:15:35 +02:00
|
|
|
tests := []struct {
|
|
|
|
Pkey []string
|
|
|
|
Fkey []string
|
|
|
|
Should bool
|
|
|
|
}{
|
2016-04-03 10:02:41 +02:00
|
|
|
{Pkey: []string{"one", "two"}, Fkey: []string{"one", "two"}, Should: true},
|
|
|
|
{Pkey: []string{"two", "one"}, Fkey: []string{"one", "two"}, Should: true},
|
|
|
|
|
|
|
|
{Pkey: []string{"one"}, Fkey: []string{"one"}, Should: false},
|
|
|
|
{Pkey: []string{"one", "two", "three"}, Fkey: []string{"one", "two"}, Should: false},
|
|
|
|
{Pkey: []string{"one", "two", "three"}, Fkey: []string{"one", "two", "three"}, Should: false},
|
|
|
|
{Pkey: []string{"one"}, Fkey: []string{"one", "two"}, Should: false},
|
|
|
|
{Pkey: []string{"one", "two"}, Fkey: []string{"one"}, Should: false},
|
2016-04-03 09:15:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
|
|
|
var table Table
|
|
|
|
|
|
|
|
table.PKey = &PrimaryKey{Columns: test.Pkey}
|
|
|
|
for _, k := range test.Fkey {
|
|
|
|
table.FKeys = append(table.FKeys, ForeignKey{Column: k})
|
|
|
|
}
|
|
|
|
|
|
|
|
setIsJoinTable(&table)
|
|
|
|
if is := table.IsJoinTable; is != test.Should {
|
|
|
|
t.Errorf("%d) want: %t, got: %t\nTest: %#v", i, test.Should, is, test)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-09 18:53:21 +02:00
|
|
|
|
2016-07-15 21:09:32 +02:00
|
|
|
func TestSetForeignKeyConstraints(t *testing.T) {
|
2016-07-09 18:53:21 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-07-12 17:09:26 +02:00
|
|
|
tables := []Table{
|
2016-08-08 09:46:06 +02:00
|
|
|
{
|
2016-07-12 17:09:26 +02:00
|
|
|
Name: "one",
|
|
|
|
Columns: []Column{
|
2016-08-08 09:46:06 +02:00
|
|
|
{Name: "id1", Type: "string", Nullable: false, Unique: false},
|
|
|
|
{Name: "id2", Type: "string", Nullable: true, Unique: true},
|
2016-07-12 17:09:26 +02:00
|
|
|
},
|
2016-07-09 18:53:21 +02:00
|
|
|
},
|
2016-08-08 09:46:06 +02:00
|
|
|
{
|
2016-07-12 17:09:26 +02:00
|
|
|
Name: "other",
|
|
|
|
Columns: []Column{
|
2016-08-08 09:46:06 +02:00
|
|
|
{Name: "one_id_1", Type: "string", Nullable: false, Unique: false},
|
|
|
|
{Name: "one_id_2", Type: "string", Nullable: true, Unique: true},
|
2016-07-09 18:53:21 +02:00
|
|
|
},
|
2016-07-12 17:09:26 +02:00
|
|
|
FKeys: []ForeignKey{
|
|
|
|
{Column: "one_id_1", ForeignTable: "one", ForeignColumn: "id1"},
|
|
|
|
{Column: "one_id_2", ForeignTable: "one", ForeignColumn: "id2"},
|
2016-07-09 18:53:21 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-07-15 21:09:32 +02:00
|
|
|
setForeignKeyConstraints(&tables[0], tables)
|
|
|
|
setForeignKeyConstraints(&tables[1], tables)
|
2016-07-09 18:53:21 +02:00
|
|
|
|
2016-07-12 17:09:26 +02:00
|
|
|
first := tables[1].FKeys[0]
|
|
|
|
second := tables[1].FKeys[1]
|
|
|
|
if first.Nullable {
|
2016-07-09 18:53:21 +02:00
|
|
|
t.Error("should not be nullable")
|
|
|
|
}
|
2016-07-15 21:09:32 +02:00
|
|
|
if first.Unique {
|
|
|
|
t.Error("should not be unique")
|
|
|
|
}
|
2016-07-12 17:09:26 +02:00
|
|
|
if first.ForeignColumnNullable {
|
|
|
|
t.Error("should be nullable")
|
|
|
|
}
|
2016-07-15 21:09:32 +02:00
|
|
|
if first.ForeignColumnUnique {
|
|
|
|
t.Error("should be unique")
|
|
|
|
}
|
2016-07-12 17:09:26 +02:00
|
|
|
if !second.Nullable {
|
|
|
|
t.Error("should be nullable")
|
|
|
|
}
|
2016-07-15 21:09:32 +02:00
|
|
|
if !second.Unique {
|
|
|
|
t.Error("should be unique")
|
|
|
|
}
|
2016-07-12 17:09:26 +02:00
|
|
|
if !second.ForeignColumnNullable {
|
2016-07-09 18:53:21 +02:00
|
|
|
t.Error("should be nullable")
|
|
|
|
}
|
2016-07-15 21:09:32 +02:00
|
|
|
if !second.ForeignColumnUnique {
|
|
|
|
t.Error("should be unique")
|
|
|
|
}
|
2016-07-09 18:53:21 +02:00
|
|
|
}
|
2016-07-12 08:49:42 +02:00
|
|
|
|
|
|
|
func TestSetRelationships(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
tables := []Table{
|
2016-08-08 09:46:06 +02:00
|
|
|
{
|
2016-07-12 08:49:42 +02:00
|
|
|
Name: "one",
|
|
|
|
Columns: []Column{
|
2016-08-08 09:46:06 +02:00
|
|
|
{Name: "id", Type: "string"},
|
2016-07-12 08:49:42 +02:00
|
|
|
},
|
|
|
|
},
|
2016-08-08 09:46:06 +02:00
|
|
|
{
|
2016-07-12 08:49:42 +02:00
|
|
|
Name: "other",
|
|
|
|
Columns: []Column{
|
2016-08-08 09:46:06 +02:00
|
|
|
{Name: "other_id", Type: "string"},
|
2016-07-12 08:49:42 +02:00
|
|
|
},
|
|
|
|
FKeys: []ForeignKey{{Column: "other_id", ForeignTable: "one", ForeignColumn: "id", Nullable: true}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
setRelationships(&tables[0], tables)
|
|
|
|
setRelationships(&tables[1], tables)
|
|
|
|
|
|
|
|
if got := len(tables[0].ToManyRelationships); got != 1 {
|
|
|
|
t.Error("should have a relationship:", got)
|
|
|
|
}
|
|
|
|
if got := len(tables[1].ToManyRelationships); got != 0 {
|
|
|
|
t.Error("should have no to many relationships:", got)
|
|
|
|
}
|
|
|
|
|
|
|
|
rel := tables[0].ToManyRelationships[0]
|
|
|
|
if rel.Column != "id" {
|
|
|
|
t.Error("wrong column:", rel.Column)
|
|
|
|
}
|
|
|
|
if rel.ForeignTable != "other" {
|
|
|
|
t.Error("wrong table:", rel.ForeignTable)
|
|
|
|
}
|
|
|
|
if rel.ForeignColumn != "other_id" {
|
|
|
|
t.Error("wrong column:", rel.ForeignColumn)
|
|
|
|
}
|
|
|
|
if rel.ToJoinTable {
|
|
|
|
t.Error("should not be a join table")
|
|
|
|
}
|
|
|
|
}
|
2016-08-13 16:07:22 +02:00
|
|
|
|
|
|
|
func TestDriverUsesLastInsertID(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
if DriverUsesLastInsertID("postgres") {
|
|
|
|
t.Error("postgres does not support LastInsertId")
|
|
|
|
}
|
|
|
|
if !DriverUsesLastInsertID("mysql") {
|
|
|
|
t.Error("postgres does support LastInsertId")
|
|
|
|
}
|
|
|
|
}
|