sqlboiler/bdb/interface_test.go

291 lines
9 KiB
Go
Raw Normal View History

package bdb
2016-04-03 09:15:35 +02:00
2016-04-03 10:02:41 +02:00
import (
"testing"
2016-08-28 04:42:25 +02:00
"github.com/vattle/sqlboiler/strmangle"
)
2016-08-14 01:27:34 +02:00
2016-09-09 17:06:07 +02:00
type testMockDriver struct{}
2016-09-09 17:06:07 +02:00
func (m testMockDriver) TranslateColumnType(c Column) Column { return c }
func (m testMockDriver) UseLastInsertID() bool { return false }
func (m testMockDriver) UseTopClause() bool { return false }
2016-09-09 17:06:07 +02:00
func (m testMockDriver) Open() error { return nil }
func (m testMockDriver) Close() {}
2016-09-09 17:06:07 +02:00
func (m testMockDriver) TableNames(schema string, whitelist, blacklist []string) ([]string, error) {
2016-09-05 16:41:12 +02:00
if len(whitelist) > 0 {
return whitelist, nil
}
2016-08-28 04:42:25 +02:00
tables := []string{"pilots", "jets", "airports", "licenses", "hangars", "languages", "pilot_languages"}
2016-09-09 07:41:57 +02:00
return strmangle.SetComplement(tables, blacklist), nil
2016-04-03 10:02:41 +02:00
}
2016-08-28 04:42:25 +02:00
// Columns returns a list of mock columns
2016-09-09 17:06:07 +02:00
func (m testMockDriver) Columns(schema, tableName string) ([]Column, error) {
2016-08-28 04:42:25 +02:00
return map[string][]Column{
"pilots": {
{Name: "id", Type: "int", DBType: "integer"},
{Name: "name", Type: "string", DBType: "character"},
},
"airports": {
{Name: "id", Type: "int", DBType: "integer"},
{Name: "size", Type: "null.Int", DBType: "integer", Nullable: true},
},
"jets": {
{Name: "id", Type: "int", DBType: "integer"},
{Name: "pilot_id", Type: "int", DBType: "integer", Nullable: true, Unique: true},
{Name: "airport_id", Type: "int", DBType: "integer"},
{Name: "name", Type: "string", DBType: "character", Nullable: false},
{Name: "color", Type: "null.String", DBType: "character", Nullable: true},
{Name: "uuid", Type: "string", DBType: "uuid", Nullable: true},
{Name: "identifier", Type: "string", DBType: "uuid", Nullable: false},
{Name: "cargo", Type: "[]byte", DBType: "bytea", Nullable: false},
{Name: "manifest", Type: "[]byte", DBType: "bytea", Nullable: true, Unique: true},
},
"licenses": {
{Name: "id", Type: "int", DBType: "integer"},
{Name: "pilot_id", Type: "int", DBType: "integer"},
},
"hangars": {
{Name: "id", Type: "int", DBType: "integer"},
{Name: "name", Type: "string", DBType: "character", Nullable: true, Unique: true},
{Name: "hangar_id", Type: "int", DBType: "integer", Nullable: true},
},
"languages": {
{Name: "id", Type: "int", DBType: "integer"},
{Name: "language", Type: "string", DBType: "character", Nullable: false, Unique: true},
},
"pilot_languages": {
{Name: "pilot_id", Type: "int", DBType: "integer"},
{Name: "language_id", Type: "int", DBType: "integer"},
},
}[tableName], nil
2016-04-03 10:02:41 +02:00
}
2016-08-28 04:42:25 +02:00
// ForeignKeyInfo returns a list of mock foreignkeys
2016-09-09 17:06:07 +02:00
func (m testMockDriver) ForeignKeyInfo(schema, tableName string) ([]ForeignKey, error) {
2016-08-28 04:42:25 +02:00
return map[string][]ForeignKey{
"jets": {
{Table: "jets", Name: "jets_pilot_id_fk", Column: "pilot_id", ForeignTable: "pilots", ForeignColumn: "id", ForeignColumnUnique: true},
{Table: "jets", Name: "jets_airport_id_fk", Column: "airport_id", ForeignTable: "airports", ForeignColumn: "id"},
},
"licenses": {
{Table: "licenses", Name: "licenses_pilot_id_fk", Column: "pilot_id", ForeignTable: "pilots", ForeignColumn: "id"},
},
"pilot_languages": {
{Table: "pilot_languages", Name: "pilot_id_fk", Column: "pilot_id", ForeignTable: "pilots", ForeignColumn: "id"},
{Table: "pilot_languages", Name: "jet_id_fk", Column: "language_id", ForeignTable: "languages", ForeignColumn: "id"},
},
"hangars": {
{Table: "hangars", Name: "hangar_fk_id", Column: "hangar_id", ForeignTable: "hangars", ForeignColumn: "id"},
},
}[tableName], nil
2016-04-03 10:02:41 +02:00
}
2016-08-28 04:42:25 +02:00
// PrimaryKeyInfo returns mock primary key info for the passed in table name
2016-09-09 17:06:07 +02:00
func (m testMockDriver) PrimaryKeyInfo(schema, tableName string) (*PrimaryKey, error) {
2016-08-28 04:42:25 +02:00
return map[string]*PrimaryKey{
"pilots": {Name: "pilot_id_pkey", Columns: []string{"id"}},
"airports": {Name: "airport_id_pkey", Columns: []string{"id"}},
"jets": {Name: "jet_id_pkey", Columns: []string{"id"}},
"licenses": {Name: "license_id_pkey", Columns: []string{"id"}},
"hangars": {Name: "hangar_id_pkey", Columns: []string{"id"}},
"languages": {Name: "language_id_pkey", Columns: []string{"id"}},
"pilot_languages": {Name: "pilot_languages_pkey", Columns: []string{"pilot_id", "language_id"}},
}[tableName], nil
2016-04-03 10:02:41 +02:00
}
2016-09-09 19:35:32 +02:00
// RightQuote is the quoting character for the right side of the identifier
func (m testMockDriver) RightQuote() byte {
2016-09-09 19:35:32 +02:00
return '"'
}
// LeftQuote is the quoting character for the left side of the identifier
func (m testMockDriver) LeftQuote() byte {
2016-09-09 19:35:32 +02:00
return '"'
}
// IndexPlaceholders returns true to indicate fake support of indexed placeholders
func (m testMockDriver) IndexPlaceholders() bool {
2016-09-09 19:35:32 +02:00
return false
}
2016-04-03 09:15:35 +02:00
func TestTables(t *testing.T) {
2016-04-03 10:02:41 +02:00
t.Parallel()
2016-09-09 17:06:07 +02:00
tables, err := Tables(testMockDriver{}, "public", nil, nil)
2016-04-03 10:02:41 +02:00
if err != nil {
t.Error(err)
}
2016-04-03 09:15:35 +02:00
2016-08-28 04:42:25 +02:00
if len(tables) != 7 {
t.Errorf("Expected len 7, got: %d\n", len(tables))
2016-04-03 10:02:41 +02:00
}
2016-08-28 04:42:25 +02:00
pilots := GetTable(tables, "pilots")
if len(pilots.Columns) != 2 {
t.Error()
2016-04-03 10:02:41 +02:00
}
2016-09-18 08:11:50 +02:00
if pilots.ToOneRelationships[0].ForeignTable != "jets" {
2016-08-28 04:42:25 +02:00
t.Error("want a to many to jets")
}
2016-09-18 08:11:50 +02:00
if pilots.ToManyRelationships[0].ForeignTable != "licenses" {
2016-08-28 04:42:25 +02:00
t.Error("want a to many to languages")
}
2016-09-18 08:11:50 +02:00
if pilots.ToManyRelationships[1].ForeignTable != "languages" {
2016-08-28 04:42:25 +02:00
t.Error("want a to many to languages")
2016-04-03 10:02:41 +02:00
}
2016-08-28 04:42:25 +02:00
jets := GetTable(tables, "jets")
if len(jets.ToManyRelationships) != 0 {
t.Error("want no to many relationships")
2016-04-03 10:02:41 +02:00
}
2016-08-28 04:42:25 +02:00
languages := GetTable(tables, "pilot_languages")
if !languages.IsJoinTable {
t.Error("languages is a join table")
2016-04-03 10:02:41 +02:00
}
2016-08-28 04:42:25 +02:00
hangars := GetTable(tables, "hangars")
if len(hangars.ToManyRelationships) != 1 || hangars.ToManyRelationships[0].ForeignTable != "hangars" {
t.Error("want 1 to many relationships")
}
2016-08-28 04:42:25 +02:00
if len(hangars.FKeys) != 1 || hangars.FKeys[0].ForeignTable != "hangars" {
t.Error("want one hangar foreign key to itself")
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-15 21:09:32 +02:00
func TestSetForeignKeyConstraints(t *testing.T) {
t.Parallel()
tables := []Table{
{
Name: "one",
Columns: []Column{
{Name: "id1", Type: "string", Nullable: false, Unique: false},
{Name: "id2", Type: "string", Nullable: true, Unique: true},
},
},
{
Name: "other",
Columns: []Column{
{Name: "one_id_1", Type: "string", Nullable: false, Unique: false},
{Name: "one_id_2", Type: "string", Nullable: true, Unique: true},
},
FKeys: []ForeignKey{
{Column: "one_id_1", ForeignTable: "one", ForeignColumn: "id1"},
{Column: "one_id_2", ForeignTable: "one", ForeignColumn: "id2"},
},
},
}
2016-07-15 21:09:32 +02:00
setForeignKeyConstraints(&tables[0], tables)
setForeignKeyConstraints(&tables[1], tables)
first := tables[1].FKeys[0]
second := tables[1].FKeys[1]
if first.Nullable {
t.Error("should not be nullable")
}
2016-07-15 21:09:32 +02:00
if first.Unique {
t.Error("should not be unique")
}
if first.ForeignColumnNullable {
t.Error("should be nullable")
}
2016-07-15 21:09:32 +02:00
if first.ForeignColumnUnique {
t.Error("should be unique")
}
if !second.Nullable {
t.Error("should be nullable")
}
2016-07-15 21:09:32 +02:00
if !second.Unique {
t.Error("should be unique")
}
if !second.ForeignColumnNullable {
t.Error("should be nullable")
}
2016-07-15 21:09:32 +02:00
if !second.ForeignColumnUnique {
t.Error("should be unique")
}
}
func TestSetRelationships(t *testing.T) {
t.Parallel()
tables := []Table{
{
Name: "one",
Columns: []Column{
{Name: "id", Type: "string"},
},
},
{
Name: "other",
Columns: []Column{
{Name: "other_id", Type: "string"},
},
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")
}
}