2016-06-23 08:09:56 +02:00
|
|
|
package bdb
|
2016-06-23 07:03:05 +02:00
|
|
|
|
|
|
|
import "testing"
|
|
|
|
|
|
|
|
func TestToManyRelationships(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
tables := []Table{
|
|
|
|
Table{
|
2016-06-27 02:43:13 +02:00
|
|
|
Name: "videos",
|
|
|
|
IsJoinTable: true,
|
2016-06-23 07:03:05 +02:00
|
|
|
FKeys: []ForeignKey{
|
2016-06-23 08:09:56 +02:00
|
|
|
{Name: "videos_user_id_fk", Column: "user_id", ForeignTable: "users", ForeignColumn: "id"},
|
|
|
|
{Name: "videos_contest_id_fk", Column: "contest_id", ForeignTable: "contests", ForeignColumn: "id"},
|
2016-06-23 07:03:05 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Table{
|
|
|
|
Name: "notifications",
|
|
|
|
FKeys: []ForeignKey{
|
2016-06-23 08:09:56 +02:00
|
|
|
{Name: "notifications_user_id_fk", Column: "user_id", ForeignTable: "users", ForeignColumn: "id"},
|
|
|
|
{Name: "notifications_source_id_fk", Column: "source_id", ForeignTable: "users", ForeignColumn: "id"},
|
2016-06-23 07:03:05 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
relationships := ToManyRelationships("users", tables)
|
|
|
|
r := relationships[0]
|
2016-06-26 05:16:38 +02:00
|
|
|
if r.Column != "id" {
|
|
|
|
t.Error("wrong local column:", r.Column)
|
2016-06-23 07:03:05 +02:00
|
|
|
}
|
|
|
|
if r.ForeignTable != "videos" {
|
|
|
|
t.Error("wrong foreign table:", r.ForeignTable)
|
|
|
|
}
|
|
|
|
if r.ForeignColumn != "user_id" {
|
|
|
|
t.Error("wrong foreign column:", r.ForeignColumn)
|
|
|
|
}
|
2016-06-27 02:43:13 +02:00
|
|
|
if !r.ToJoinTable {
|
|
|
|
t.Error("expected a join table - kind of - not really but we're faking it")
|
|
|
|
}
|
2016-06-23 07:03:05 +02:00
|
|
|
}
|