2016-06-23 08:09:56 +02:00
|
|
|
package bdb
|
2016-06-23 07:03:05 +02:00
|
|
|
|
2016-07-09 18:53:21 +02:00
|
|
|
import "testing"
|
2016-06-23 07:03:05 +02:00
|
|
|
|
|
|
|
func TestToManyRelationships(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
tables := []Table{
|
2016-07-12 08:49:42 +02:00
|
|
|
Table{Name: "users", Columns: []Column{{Name: "id"}}},
|
|
|
|
Table{Name: "contests", Columns: []Column{{Name: "id"}}},
|
2016-06-23 07:03:05 +02:00
|
|
|
Table{
|
2016-07-01 19:34:40 +02:00
|
|
|
Name: "videos",
|
2016-07-12 08:49:42 +02:00
|
|
|
Columns: []Column{
|
|
|
|
{Name: "id"},
|
|
|
|
{Name: "user_id"},
|
|
|
|
{Name: "contest_id"},
|
|
|
|
},
|
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",
|
2016-07-12 08:49:42 +02:00
|
|
|
Columns: []Column{
|
|
|
|
{Name: "user_id"},
|
|
|
|
{Name: "source_id"},
|
|
|
|
},
|
2016-06-23 07:03:05 +02:00
|
|
|
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
|
|
|
},
|
|
|
|
},
|
2016-07-01 19:34:40 +02:00
|
|
|
Table{
|
|
|
|
Name: "users_video_tags",
|
|
|
|
IsJoinTable: true,
|
2016-07-12 08:49:42 +02:00
|
|
|
Columns: []Column{
|
|
|
|
{Name: "user_id"},
|
|
|
|
{Name: "video_id"},
|
|
|
|
},
|
2016-07-01 19:34:40 +02:00
|
|
|
FKeys: []ForeignKey{
|
|
|
|
{Name: "user_id_fk", Column: "user_id", ForeignTable: "users", ForeignColumn: "id"},
|
|
|
|
{Name: "video_id_fk", Column: "video_id", ForeignTable: "videos", ForeignColumn: "id"},
|
|
|
|
},
|
|
|
|
},
|
2016-06-23 07:03:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
relationships := ToManyRelationships("users", tables)
|
2016-07-01 19:34:40 +02:00
|
|
|
if len(relationships) != 4 {
|
|
|
|
t.Error("wrong # of relationships:", len(relationships))
|
|
|
|
}
|
|
|
|
|
2016-06-23 07:03:05 +02:00
|
|
|
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
|
|
|
}
|
2016-07-12 08:49:42 +02:00
|
|
|
if r.Nullable {
|
|
|
|
t.Error("should not be nullable")
|
|
|
|
}
|
|
|
|
if r.ForeignTable != "videos" {
|
|
|
|
t.Error("wrong foreign table:", r.ForeignTable)
|
|
|
|
}
|
|
|
|
if r.ForeignColumn != "user_id" {
|
|
|
|
t.Error("wrong foreign column:", r.ForeignColumn)
|
|
|
|
}
|
|
|
|
if r.ForeignColumnNullable {
|
|
|
|
t.Error("should not be nullable")
|
|
|
|
}
|
|
|
|
if r.ToJoinTable {
|
|
|
|
t.Error("not a join table")
|
|
|
|
}
|
|
|
|
|
|
|
|
r = relationships[1]
|
|
|
|
if r.Column != "id" {
|
|
|
|
t.Error("wrong local column:", r.Column)
|
|
|
|
}
|
|
|
|
if r.Nullable {
|
|
|
|
t.Error("should not be nullable")
|
|
|
|
}
|
|
|
|
if r.ForeignTable != "notifications" {
|
|
|
|
t.Error("wrong foreign table:", r.ForeignTable)
|
|
|
|
}
|
|
|
|
if r.ForeignColumn != "user_id" {
|
|
|
|
t.Error("wrong foreign column:", r.ForeignColumn)
|
|
|
|
}
|
|
|
|
if r.ForeignColumnNullable {
|
|
|
|
t.Error("should not be nullable")
|
|
|
|
}
|
|
|
|
if r.ToJoinTable {
|
|
|
|
t.Error("not a join table")
|
|
|
|
}
|
|
|
|
|
|
|
|
r = relationships[2]
|
|
|
|
if r.Column != "id" {
|
|
|
|
t.Error("wrong local column:", r.Column)
|
|
|
|
}
|
|
|
|
if r.Nullable {
|
|
|
|
t.Error("should not be nullable")
|
|
|
|
}
|
|
|
|
if r.ForeignTable != "notifications" {
|
|
|
|
t.Error("wrong foreign table:", r.ForeignTable)
|
|
|
|
}
|
|
|
|
if r.ForeignColumn != "source_id" {
|
|
|
|
t.Error("wrong foreign column:", r.ForeignColumn)
|
|
|
|
}
|
|
|
|
if r.ForeignColumnNullable {
|
|
|
|
t.Error("should not be nullable")
|
|
|
|
}
|
|
|
|
if r.ToJoinTable {
|
|
|
|
t.Error("not a join table")
|
|
|
|
}
|
|
|
|
|
|
|
|
r = relationships[3]
|
|
|
|
if r.Column != "id" {
|
|
|
|
t.Error("wrong local column:", r.Column)
|
|
|
|
}
|
|
|
|
if r.Nullable {
|
|
|
|
t.Error("should not be nullable")
|
|
|
|
}
|
|
|
|
if r.ForeignColumn != "id" {
|
|
|
|
t.Error("wrong foreign column:", r.Column)
|
|
|
|
}
|
|
|
|
if r.ForeignColumnNullable {
|
|
|
|
t.Error("should not be nullable")
|
|
|
|
}
|
|
|
|
if r.ForeignTable != "videos" {
|
|
|
|
t.Error("wrong foreign table:", r.ForeignTable)
|
|
|
|
}
|
|
|
|
if r.JoinTable != "users_video_tags" {
|
|
|
|
t.Error("wrong join table:", r.ForeignTable)
|
|
|
|
}
|
|
|
|
if r.JoinLocalColumn != "user_id" {
|
|
|
|
t.Error("wrong local join column:", r.JoinLocalColumn)
|
|
|
|
}
|
|
|
|
if r.JoinLocalColumnNullable {
|
|
|
|
t.Error("should not be nullable")
|
|
|
|
}
|
|
|
|
if r.JoinForeignColumn != "video_id" {
|
|
|
|
t.Error("wrong foreign join column:", r.JoinForeignColumn)
|
|
|
|
}
|
|
|
|
if r.JoinForeignColumnNullable {
|
|
|
|
t.Error("should not be nullable")
|
|
|
|
}
|
|
|
|
if !r.ToJoinTable {
|
|
|
|
t.Error("expected a join table")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestToManyRelationshipsNull(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
tables := []Table{
|
|
|
|
Table{Name: "users", Columns: []Column{{Name: "id", Nullable: true}}},
|
|
|
|
Table{Name: "contests", Columns: []Column{{Name: "id", Nullable: true}}},
|
|
|
|
Table{
|
|
|
|
Name: "videos",
|
|
|
|
Columns: []Column{
|
|
|
|
{Name: "id", Nullable: true},
|
|
|
|
{Name: "user_id", Nullable: true},
|
|
|
|
{Name: "contest_id", Nullable: true},
|
|
|
|
},
|
|
|
|
FKeys: []ForeignKey{
|
|
|
|
{Name: "videos_user_id_fk", Column: "user_id", ForeignTable: "users", ForeignColumn: "id", Nullable: true},
|
|
|
|
{Name: "videos_contest_id_fk", Column: "contest_id", ForeignTable: "contests", ForeignColumn: "id", Nullable: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Table{
|
|
|
|
Name: "notifications",
|
|
|
|
Columns: []Column{
|
|
|
|
{Name: "user_id", Nullable: true},
|
|
|
|
{Name: "source_id", Nullable: true},
|
|
|
|
},
|
|
|
|
FKeys: []ForeignKey{
|
|
|
|
{Name: "notifications_user_id_fk", Column: "user_id", ForeignTable: "users", ForeignColumn: "id", Nullable: true},
|
|
|
|
{Name: "notifications_source_id_fk", Column: "source_id", ForeignTable: "users", ForeignColumn: "id", Nullable: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Table{
|
|
|
|
Name: "users_video_tags",
|
|
|
|
IsJoinTable: true,
|
|
|
|
Columns: []Column{
|
|
|
|
{Name: "user_id", Nullable: true},
|
|
|
|
{Name: "video_id", Nullable: true},
|
|
|
|
},
|
|
|
|
FKeys: []ForeignKey{
|
|
|
|
{Name: "user_id_fk", Column: "user_id", ForeignTable: "users", ForeignColumn: "id", Nullable: true},
|
|
|
|
{Name: "video_id_fk", Column: "video_id", ForeignTable: "videos", ForeignColumn: "id", Nullable: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
relationships := ToManyRelationships("users", tables)
|
|
|
|
if len(relationships) != 4 {
|
|
|
|
t.Error("wrong # of relationships:", len(relationships))
|
|
|
|
}
|
|
|
|
|
|
|
|
r := relationships[0]
|
|
|
|
if r.Column != "id" {
|
|
|
|
t.Error("wrong local column:", r.Column)
|
|
|
|
}
|
|
|
|
if !r.Nullable {
|
|
|
|
t.Error("should be nullable")
|
|
|
|
}
|
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-07-12 08:49:42 +02:00
|
|
|
if !r.ForeignColumnNullable {
|
|
|
|
t.Error("should be nullable")
|
|
|
|
}
|
2016-07-01 19:34:40 +02:00
|
|
|
if r.ToJoinTable {
|
|
|
|
t.Error("not a join table")
|
|
|
|
}
|
|
|
|
|
|
|
|
r = relationships[1]
|
|
|
|
if r.Column != "id" {
|
|
|
|
t.Error("wrong local column:", r.Column)
|
|
|
|
}
|
2016-07-12 08:49:42 +02:00
|
|
|
if !r.Nullable {
|
|
|
|
t.Error("should be nullable")
|
|
|
|
}
|
2016-07-01 19:34:40 +02:00
|
|
|
if r.ForeignTable != "notifications" {
|
|
|
|
t.Error("wrong foreign table:", r.ForeignTable)
|
|
|
|
}
|
|
|
|
if r.ForeignColumn != "user_id" {
|
|
|
|
t.Error("wrong foreign column:", r.ForeignColumn)
|
|
|
|
}
|
2016-07-12 08:49:42 +02:00
|
|
|
if !r.ForeignColumnNullable {
|
|
|
|
t.Error("should be nullable")
|
|
|
|
}
|
2016-07-01 19:34:40 +02:00
|
|
|
if r.ToJoinTable {
|
|
|
|
t.Error("not a join table")
|
|
|
|
}
|
|
|
|
|
|
|
|
r = relationships[2]
|
|
|
|
if r.Column != "id" {
|
|
|
|
t.Error("wrong local column:", r.Column)
|
|
|
|
}
|
2016-07-12 08:49:42 +02:00
|
|
|
if !r.Nullable {
|
|
|
|
t.Error("should be nullable")
|
|
|
|
}
|
2016-07-01 19:34:40 +02:00
|
|
|
if r.ForeignTable != "notifications" {
|
|
|
|
t.Error("wrong foreign table:", r.ForeignTable)
|
|
|
|
}
|
|
|
|
if r.ForeignColumn != "source_id" {
|
|
|
|
t.Error("wrong foreign column:", r.ForeignColumn)
|
|
|
|
}
|
2016-07-12 08:49:42 +02:00
|
|
|
if !r.ForeignColumnNullable {
|
|
|
|
t.Error("should be nullable")
|
|
|
|
}
|
2016-07-01 19:34:40 +02:00
|
|
|
if r.ToJoinTable {
|
|
|
|
t.Error("not a join table")
|
|
|
|
}
|
|
|
|
|
|
|
|
r = relationships[3]
|
|
|
|
if r.Column != "id" {
|
|
|
|
t.Error("wrong local column:", r.Column)
|
|
|
|
}
|
2016-07-12 08:49:42 +02:00
|
|
|
if !r.Nullable {
|
|
|
|
t.Error("should be nullable")
|
|
|
|
}
|
2016-07-01 19:34:40 +02:00
|
|
|
if r.ForeignColumn != "id" {
|
|
|
|
t.Error("wrong foreign column:", r.Column)
|
|
|
|
}
|
2016-07-12 08:49:42 +02:00
|
|
|
if !r.ForeignColumnNullable {
|
|
|
|
t.Error("should be nullable")
|
|
|
|
}
|
2016-07-01 19:34:40 +02:00
|
|
|
if r.ForeignTable != "videos" {
|
|
|
|
t.Error("wrong foreign table:", r.ForeignTable)
|
|
|
|
}
|
|
|
|
if r.JoinTable != "users_video_tags" {
|
|
|
|
t.Error("wrong join table:", r.ForeignTable)
|
|
|
|
}
|
|
|
|
if r.JoinLocalColumn != "user_id" {
|
|
|
|
t.Error("wrong local join column:", r.JoinLocalColumn)
|
|
|
|
}
|
2016-07-12 08:49:42 +02:00
|
|
|
if !r.JoinLocalColumnNullable {
|
|
|
|
t.Error("should be nullable")
|
|
|
|
}
|
2016-07-01 19:34:40 +02:00
|
|
|
if r.JoinForeignColumn != "video_id" {
|
|
|
|
t.Error("wrong foreign join column:", r.JoinForeignColumn)
|
|
|
|
}
|
2016-07-12 08:49:42 +02:00
|
|
|
if !r.JoinForeignColumnNullable {
|
|
|
|
t.Error("should be nullable")
|
|
|
|
}
|
2016-06-27 02:43:13 +02:00
|
|
|
if !r.ToJoinTable {
|
2016-07-01 19:34:40 +02:00
|
|
|
t.Error("expected a join table")
|
2016-06-27 02:43:13 +02:00
|
|
|
}
|
2016-06-23 07:03:05 +02:00
|
|
|
}
|