2016-06-23 08:09:56 +02:00
|
|
|
package bdb
|
2016-06-23 07:03:05 +02:00
|
|
|
|
2016-07-15 21:09:32 +02:00
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
2016-06-23 07:03:05 +02:00
|
|
|
|
2016-09-18 08:11:50 +02:00
|
|
|
func TestToOneRelationships(t *testing.T) {
|
2016-06-23 07:03:05 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
tables := []Table{
|
2016-08-08 09:46:06 +02:00
|
|
|
{
|
2016-09-18 08:11:50 +02:00
|
|
|
Name: "pilots",
|
|
|
|
Columns: []Column{{Name: "id", Unique: true}, {Name: "name", Unique: true}}},
|
|
|
|
{
|
|
|
|
Name: "airports",
|
|
|
|
Columns: []Column{{Name: "id", Unique: true}, {Name: "size", Unique: true}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "jets",
|
|
|
|
Columns: []Column{{Name: "id", Unique: true}, {Name: "pilot_id", Unique: true}, {Name: "airport_id", Unique: true}},
|
|
|
|
FKeys: []ForeignKey{
|
|
|
|
{Name: "jets_pilot_id_fk", Column: "pilot_id", ForeignTable: "pilots", ForeignColumn: "id", Unique: true},
|
|
|
|
{Name: "jets_airport_id_fk", Column: "airport_id", ForeignTable: "airports", ForeignColumn: "id", Unique: true},
|
2016-08-23 05:38:43 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2016-09-18 08:11:50 +02:00
|
|
|
Name: "licenses",
|
|
|
|
Columns: []Column{{Name: "id", Unique: true}, {Name: "pilot_id", Unique: true}},
|
|
|
|
FKeys: []ForeignKey{
|
|
|
|
{Name: "licenses_pilot_id_fk", Column: "pilot_id", ForeignTable: "pilots", ForeignColumn: "id", Unique: true},
|
2016-08-23 05:38:43 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2016-09-18 08:11:50 +02:00
|
|
|
Name: "hangars",
|
|
|
|
Columns: []Column{{Name: "id", Unique: true}, {Name: "name", Unique: true}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "languages",
|
|
|
|
Columns: []Column{{Name: "id", Unique: true}, {Name: "language", Unique: true}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "pilot_languages",
|
|
|
|
IsJoinTable: true,
|
|
|
|
Columns: []Column{{Name: "pilot_id", Unique: true}, {Name: "language_id", Unique: true}},
|
|
|
|
FKeys: []ForeignKey{
|
|
|
|
{Name: "pilot_id_fk", Column: "pilot_id", ForeignTable: "pilots", ForeignColumn: "id", Unique: true},
|
|
|
|
{Name: "language_id_fk", Column: "language_id", ForeignTable: "languages", ForeignColumn: "id", Unique: true},
|
2016-07-12 08:49:42 +02:00
|
|
|
},
|
2016-09-18 08:11:50 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
relationships := ToOneRelationships("pilots", tables)
|
|
|
|
|
|
|
|
expected := []ToOneRelationship{
|
|
|
|
{
|
|
|
|
Table: "pilots",
|
|
|
|
Column: "id",
|
|
|
|
Nullable: false,
|
|
|
|
Unique: false,
|
|
|
|
|
|
|
|
ForeignTable: "jets",
|
|
|
|
ForeignColumn: "pilot_id",
|
|
|
|
ForeignColumnNullable: false,
|
|
|
|
ForeignColumnUnique: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Table: "pilots",
|
|
|
|
Column: "id",
|
|
|
|
Nullable: false,
|
|
|
|
Unique: false,
|
|
|
|
|
|
|
|
ForeignTable: "licenses",
|
|
|
|
ForeignColumn: "pilot_id",
|
|
|
|
ForeignColumnNullable: false,
|
|
|
|
ForeignColumnUnique: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(relationships) != 2 {
|
|
|
|
t.Error("wrong # of relationships", len(relationships))
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, v := range relationships {
|
|
|
|
if !reflect.DeepEqual(v, expected[i]) {
|
|
|
|
t.Errorf("[%d] Mismatch between relationships:\n\nwant:%#v\n\ngot:%#v\n\n", i, expected[i], v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestToManyRelationships(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
tables := []Table{
|
|
|
|
{
|
|
|
|
Name: "pilots",
|
|
|
|
Columns: []Column{{Name: "id"}, {Name: "name"}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "airports",
|
|
|
|
Columns: []Column{{Name: "id"}, {Name: "size"}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "jets",
|
|
|
|
Columns: []Column{{Name: "id"}, {Name: "pilot_id"}, {Name: "airport_id"}},
|
2016-06-23 07:03:05 +02:00
|
|
|
FKeys: []ForeignKey{
|
2016-08-23 05:38:43 +02:00
|
|
|
{Name: "jets_pilot_id_fk", Column: "pilot_id", ForeignTable: "pilots", ForeignColumn: "id"},
|
|
|
|
{Name: "jets_airport_id_fk", Column: "airport_id", ForeignTable: "airports", ForeignColumn: "id"},
|
2016-06-23 07:03:05 +02:00
|
|
|
},
|
|
|
|
},
|
2016-08-08 09:46:06 +02:00
|
|
|
{
|
2016-09-18 08:11:50 +02:00
|
|
|
Name: "licenses",
|
|
|
|
Columns: []Column{{Name: "id"}, {Name: "pilot_id"}},
|
2016-06-23 07:03:05 +02:00
|
|
|
FKeys: []ForeignKey{
|
2016-08-23 05:38:43 +02:00
|
|
|
{Name: "licenses_pilot_id_fk", Column: "pilot_id", ForeignTable: "pilots", ForeignColumn: "id"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2016-09-18 08:11:50 +02:00
|
|
|
Name: "hangars",
|
|
|
|
Columns: []Column{{Name: "id"}, {Name: "name"}},
|
2016-06-23 07:03:05 +02:00
|
|
|
},
|
2016-08-08 09:46:06 +02:00
|
|
|
{
|
2016-09-18 08:11:50 +02:00
|
|
|
Name: "languages",
|
|
|
|
Columns: []Column{{Name: "id"}, {Name: "language"}},
|
2016-08-23 05:38:43 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "pilot_languages",
|
2016-07-01 19:34:40 +02:00
|
|
|
IsJoinTable: true,
|
2016-09-18 08:11:50 +02:00
|
|
|
Columns: []Column{{Name: "pilot_id"}, {Name: "language_id"}},
|
2016-07-01 19:34:40 +02:00
|
|
|
FKeys: []ForeignKey{
|
2016-08-23 05:38:43 +02:00
|
|
|
{Name: "pilot_id_fk", Column: "pilot_id", ForeignTable: "pilots", ForeignColumn: "id"},
|
|
|
|
{Name: "language_id_fk", Column: "language_id", ForeignTable: "languages", ForeignColumn: "id"},
|
2016-07-01 19:34:40 +02:00
|
|
|
},
|
|
|
|
},
|
2016-06-23 07:03:05 +02:00
|
|
|
}
|
|
|
|
|
2016-08-23 05:38:43 +02:00
|
|
|
relationships := ToManyRelationships("pilots", tables)
|
2016-07-01 19:34:40 +02:00
|
|
|
|
2016-07-15 21:09:32 +02:00
|
|
|
expected := []ToManyRelationship{
|
2016-08-08 09:46:06 +02:00
|
|
|
{
|
2016-08-28 02:04:51 +02:00
|
|
|
Table: "pilots",
|
2016-07-15 21:09:32 +02:00
|
|
|
Column: "id",
|
|
|
|
Nullable: false,
|
|
|
|
Unique: false,
|
2016-07-12 08:49:42 +02:00
|
|
|
|
2016-08-23 05:38:43 +02:00
|
|
|
ForeignTable: "jets",
|
|
|
|
ForeignColumn: "pilot_id",
|
2016-07-15 21:09:32 +02:00
|
|
|
ForeignColumnNullable: false,
|
|
|
|
ForeignColumnUnique: false,
|
|
|
|
|
|
|
|
ToJoinTable: false,
|
|
|
|
},
|
2016-08-08 09:46:06 +02:00
|
|
|
{
|
2016-08-28 02:04:51 +02:00
|
|
|
Table: "pilots",
|
2016-07-15 21:09:32 +02:00
|
|
|
Column: "id",
|
|
|
|
Nullable: false,
|
|
|
|
Unique: false,
|
|
|
|
|
2016-08-23 05:38:43 +02:00
|
|
|
ForeignTable: "licenses",
|
|
|
|
ForeignColumn: "pilot_id",
|
2016-07-15 21:09:32 +02:00
|
|
|
ForeignColumnNullable: false,
|
|
|
|
ForeignColumnUnique: false,
|
|
|
|
|
|
|
|
ToJoinTable: false,
|
|
|
|
},
|
2016-08-08 09:46:06 +02:00
|
|
|
{
|
2016-08-27 18:51:06 +02:00
|
|
|
Table: "pilots",
|
2016-07-15 21:09:32 +02:00
|
|
|
Column: "id",
|
|
|
|
Nullable: false,
|
|
|
|
Unique: false,
|
|
|
|
|
2016-08-23 05:38:43 +02:00
|
|
|
ForeignTable: "languages",
|
2016-07-15 21:09:32 +02:00
|
|
|
ForeignColumn: "id",
|
|
|
|
ForeignColumnNullable: false,
|
|
|
|
ForeignColumnUnique: false,
|
|
|
|
|
|
|
|
ToJoinTable: true,
|
2016-08-23 05:38:43 +02:00
|
|
|
JoinTable: "pilot_languages",
|
2016-07-15 21:09:32 +02:00
|
|
|
|
2016-08-23 05:38:43 +02:00
|
|
|
JoinLocalColumn: "pilot_id",
|
2016-07-15 21:09:32 +02:00
|
|
|
JoinLocalColumnNullable: false,
|
|
|
|
JoinLocalColumnUnique: false,
|
|
|
|
|
2016-08-23 05:38:43 +02:00
|
|
|
JoinForeignColumn: "language_id",
|
2016-07-15 21:09:32 +02:00
|
|
|
JoinForeignColumnNullable: false,
|
|
|
|
JoinForeignColumnUnique: false,
|
|
|
|
},
|
2016-07-12 08:49:42 +02:00
|
|
|
}
|
2016-07-15 21:09:32 +02:00
|
|
|
|
2016-08-23 05:38:43 +02:00
|
|
|
if len(relationships) != 3 {
|
2016-07-15 21:09:32 +02:00
|
|
|
t.Error("wrong # of relationships:", len(relationships))
|
2016-07-12 08:49:42 +02:00
|
|
|
}
|
2016-07-15 21:09:32 +02:00
|
|
|
|
|
|
|
for i, v := range relationships {
|
|
|
|
if !reflect.DeepEqual(v, expected[i]) {
|
2016-09-18 08:11:50 +02:00
|
|
|
t.Errorf("[%d] Mismatch between relationships:\n\nwant:%#v\n\ngot:%#v\n\n", i, expected[i], v)
|
2016-07-15 21:09:32 +02:00
|
|
|
}
|
2016-07-12 08:49:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestToManyRelationshipsNull(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
tables := []Table{
|
2016-08-08 09:46:06 +02:00
|
|
|
{
|
2016-09-18 08:11:50 +02:00
|
|
|
Name: "pilots",
|
|
|
|
Columns: []Column{{Name: "id", Nullable: true}, {Name: "name", Nullable: true}}},
|
2016-08-23 05:38:43 +02:00
|
|
|
{
|
2016-09-18 08:11:50 +02:00
|
|
|
Name: "airports",
|
|
|
|
Columns: []Column{{Name: "id", Nullable: true}, {Name: "size", Nullable: true}},
|
2016-08-23 05:38:43 +02:00
|
|
|
},
|
|
|
|
{
|
2016-09-18 08:11:50 +02:00
|
|
|
Name: "jets",
|
|
|
|
Columns: []Column{{Name: "id", Nullable: true}, {Name: "pilot_id", Nullable: true}, {Name: "airport_id", Nullable: true}},
|
2016-07-12 08:49:42 +02:00
|
|
|
FKeys: []ForeignKey{
|
2016-09-18 08:11:50 +02:00
|
|
|
{Name: "jets_pilot_id_fk", Column: "pilot_id", ForeignTable: "pilots", ForeignColumn: "id", Nullable: true},
|
|
|
|
{Name: "jets_airport_id_fk", Column: "airport_id", ForeignTable: "airports", ForeignColumn: "id", Nullable: true},
|
2016-07-12 08:49:42 +02:00
|
|
|
},
|
|
|
|
},
|
2016-08-08 09:46:06 +02:00
|
|
|
{
|
2016-09-18 08:11:50 +02:00
|
|
|
Name: "licenses",
|
|
|
|
Columns: []Column{{Name: "id", Nullable: true}, {Name: "pilot_id", Nullable: true}},
|
2016-07-12 08:49:42 +02:00
|
|
|
FKeys: []ForeignKey{
|
2016-09-18 08:11:50 +02:00
|
|
|
{Name: "licenses_pilot_id_fk", Column: "pilot_id", ForeignTable: "pilots", ForeignColumn: "id", Nullable: true},
|
2016-08-23 05:38:43 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2016-09-18 08:11:50 +02:00
|
|
|
Name: "hangars",
|
|
|
|
Columns: []Column{{Name: "id", Nullable: true}, {Name: "name", Nullable: true}},
|
2016-07-12 08:49:42 +02:00
|
|
|
},
|
2016-08-08 09:46:06 +02:00
|
|
|
{
|
2016-09-18 08:11:50 +02:00
|
|
|
Name: "languages",
|
|
|
|
Columns: []Column{{Name: "id", Nullable: true}, {Name: "language", Nullable: true}},
|
2016-08-23 05:38:43 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "pilot_languages",
|
2016-07-12 08:49:42 +02:00
|
|
|
IsJoinTable: true,
|
2016-09-18 08:11:50 +02:00
|
|
|
Columns: []Column{{Name: "pilot_id", Nullable: true}, {Name: "language_id", Nullable: true}},
|
2016-07-12 08:49:42 +02:00
|
|
|
FKeys: []ForeignKey{
|
2016-09-18 08:11:50 +02:00
|
|
|
{Name: "pilot_id_fk", Column: "pilot_id", ForeignTable: "pilots", ForeignColumn: "id", Nullable: true},
|
|
|
|
{Name: "language_id_fk", Column: "language_id", ForeignTable: "languages", ForeignColumn: "id", Nullable: true},
|
2016-07-12 08:49:42 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-08-23 05:38:43 +02:00
|
|
|
relationships := ToManyRelationships("pilots", tables)
|
|
|
|
if len(relationships) != 3 {
|
2016-07-12 08:49:42 +02:00
|
|
|
t.Error("wrong # of relationships:", len(relationships))
|
|
|
|
}
|
|
|
|
|
2016-07-15 21:09:32 +02:00
|
|
|
expected := []ToManyRelationship{
|
2016-08-08 09:46:06 +02:00
|
|
|
{
|
2016-08-28 02:04:51 +02:00
|
|
|
Table: "pilots",
|
2016-07-15 21:09:32 +02:00
|
|
|
Column: "id",
|
|
|
|
Nullable: true,
|
2016-09-18 08:11:50 +02:00
|
|
|
Unique: false,
|
2016-07-01 19:34:40 +02:00
|
|
|
|
2016-08-23 05:38:43 +02:00
|
|
|
ForeignTable: "jets",
|
|
|
|
ForeignColumn: "pilot_id",
|
2016-07-15 21:09:32 +02:00
|
|
|
ForeignColumnNullable: true,
|
2016-09-18 08:11:50 +02:00
|
|
|
ForeignColumnUnique: false,
|
2016-07-15 21:09:32 +02:00
|
|
|
|
|
|
|
ToJoinTable: false,
|
|
|
|
},
|
2016-08-08 09:46:06 +02:00
|
|
|
{
|
2016-08-28 02:04:51 +02:00
|
|
|
Table: "pilots",
|
2016-07-15 21:09:32 +02:00
|
|
|
Column: "id",
|
|
|
|
Nullable: true,
|
2016-09-18 08:11:50 +02:00
|
|
|
Unique: false,
|
2016-07-15 21:09:32 +02:00
|
|
|
|
2016-08-23 05:38:43 +02:00
|
|
|
ForeignTable: "licenses",
|
|
|
|
ForeignColumn: "pilot_id",
|
2016-07-15 21:09:32 +02:00
|
|
|
ForeignColumnNullable: true,
|
2016-09-18 08:11:50 +02:00
|
|
|
ForeignColumnUnique: false,
|
2016-07-15 21:09:32 +02:00
|
|
|
|
|
|
|
ToJoinTable: false,
|
|
|
|
},
|
2016-08-08 09:46:06 +02:00
|
|
|
{
|
2016-08-27 18:51:06 +02:00
|
|
|
Table: "pilots",
|
2016-07-15 21:09:32 +02:00
|
|
|
Column: "id",
|
|
|
|
Nullable: true,
|
2016-09-18 08:11:50 +02:00
|
|
|
Unique: false,
|
2016-07-15 21:09:32 +02:00
|
|
|
|
2016-08-23 05:38:43 +02:00
|
|
|
ForeignTable: "languages",
|
2016-07-15 21:09:32 +02:00
|
|
|
ForeignColumn: "id",
|
|
|
|
ForeignColumnNullable: true,
|
2016-09-18 08:11:50 +02:00
|
|
|
ForeignColumnUnique: false,
|
2016-07-15 21:09:32 +02:00
|
|
|
|
|
|
|
ToJoinTable: true,
|
2016-08-23 05:38:43 +02:00
|
|
|
JoinTable: "pilot_languages",
|
2016-07-15 21:09:32 +02:00
|
|
|
|
2016-08-23 05:38:43 +02:00
|
|
|
JoinLocalColumn: "pilot_id",
|
2016-07-15 21:09:32 +02:00
|
|
|
JoinLocalColumnNullable: true,
|
2016-09-18 08:11:50 +02:00
|
|
|
JoinLocalColumnUnique: false,
|
2016-07-15 21:09:32 +02:00
|
|
|
|
2016-08-23 05:38:43 +02:00
|
|
|
JoinForeignColumn: "language_id",
|
2016-07-15 21:09:32 +02:00
|
|
|
JoinForeignColumnNullable: true,
|
2016-09-18 08:11:50 +02:00
|
|
|
JoinForeignColumnUnique: false,
|
2016-07-15 21:09:32 +02:00
|
|
|
},
|
2016-07-12 08:49:42 +02:00
|
|
|
}
|
2016-07-15 21:09:32 +02:00
|
|
|
|
|
|
|
for i, v := range relationships {
|
|
|
|
if !reflect.DeepEqual(v, expected[i]) {
|
2016-09-18 08:11:50 +02:00
|
|
|
t.Errorf("[%d] Mismatch between relationships:\n\nwant:%#v\n\ngot:%#v\n\n", i, expected[i], v)
|
2016-07-15 21:09:32 +02:00
|
|
|
}
|
2016-06-27 02:43:13 +02:00
|
|
|
}
|
2016-06-23 07:03:05 +02:00
|
|
|
}
|