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
|
|
|
|
|
|
|
func TestToManyRelationships(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
tables := []Table{
|
2016-08-08 09:46:06 +02:00
|
|
|
{Name: "users", Columns: []Column{{Name: "id"}}},
|
|
|
|
{Name: "contests", Columns: []Column{{Name: "id"}}},
|
|
|
|
{
|
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
|
|
|
},
|
|
|
|
},
|
2016-08-08 09:46:06 +02:00
|
|
|
{
|
2016-06-23 07:03:05 +02:00
|
|
|
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-08-08 09:46:06 +02:00
|
|
|
{
|
2016-07-01 19:34:40 +02:00
|
|
|
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
|
|
|
|
2016-07-15 21:09:32 +02:00
|
|
|
expected := []ToManyRelationship{
|
2016-08-08 09:46:06 +02:00
|
|
|
{
|
2016-07-15 21:09:32 +02:00
|
|
|
Column: "id",
|
|
|
|
Nullable: false,
|
|
|
|
Unique: false,
|
2016-07-12 08:49:42 +02:00
|
|
|
|
2016-07-15 21:09:32 +02:00
|
|
|
ForeignTable: "videos",
|
|
|
|
ForeignColumn: "user_id",
|
|
|
|
ForeignColumnNullable: false,
|
|
|
|
ForeignColumnUnique: false,
|
2016-07-12 08:49:42 +02:00
|
|
|
|
2016-07-15 21:09:32 +02:00
|
|
|
ToJoinTable: false,
|
|
|
|
},
|
2016-08-08 09:46:06 +02:00
|
|
|
{
|
2016-07-15 21:09:32 +02:00
|
|
|
Column: "id",
|
|
|
|
Nullable: false,
|
|
|
|
Unique: false,
|
2016-07-12 08:49:42 +02:00
|
|
|
|
2016-07-15 21:09:32 +02:00
|
|
|
ForeignTable: "notifications",
|
|
|
|
ForeignColumn: "user_id",
|
|
|
|
ForeignColumnNullable: false,
|
|
|
|
ForeignColumnUnique: false,
|
|
|
|
|
|
|
|
ToJoinTable: false,
|
|
|
|
},
|
2016-08-08 09:46:06 +02:00
|
|
|
{
|
2016-07-15 21:09:32 +02:00
|
|
|
Column: "id",
|
|
|
|
Nullable: false,
|
|
|
|
Unique: false,
|
|
|
|
|
|
|
|
ForeignTable: "notifications",
|
|
|
|
ForeignColumn: "source_id",
|
|
|
|
ForeignColumnNullable: false,
|
|
|
|
ForeignColumnUnique: false,
|
|
|
|
|
|
|
|
ToJoinTable: false,
|
|
|
|
},
|
2016-08-08 09:46:06 +02:00
|
|
|
{
|
2016-07-15 21:09:32 +02:00
|
|
|
Column: "id",
|
|
|
|
Nullable: false,
|
|
|
|
Unique: false,
|
|
|
|
|
|
|
|
ForeignTable: "videos",
|
|
|
|
ForeignColumn: "id",
|
|
|
|
ForeignColumnNullable: false,
|
|
|
|
ForeignColumnUnique: false,
|
|
|
|
|
|
|
|
ToJoinTable: true,
|
|
|
|
JoinTable: "users_video_tags",
|
|
|
|
|
|
|
|
JoinLocalColumn: "user_id",
|
|
|
|
JoinLocalColumnNullable: false,
|
|
|
|
JoinLocalColumnUnique: false,
|
|
|
|
|
|
|
|
JoinForeignColumn: "video_id",
|
|
|
|
JoinForeignColumnNullable: false,
|
|
|
|
JoinForeignColumnUnique: false,
|
|
|
|
},
|
2016-07-12 08:49:42 +02:00
|
|
|
}
|
2016-07-15 21:09:32 +02:00
|
|
|
|
|
|
|
if len(relationships) != 4 {
|
|
|
|
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]) {
|
|
|
|
t.Errorf("[%d] Mismatch between relationships:\n\n%#v\n\n%#v\n\n", i, v, expected[i])
|
|
|
|
}
|
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
|
|
|
{Name: "users", Columns: []Column{{Name: "id", Nullable: true, Unique: true}}},
|
|
|
|
{Name: "contests", Columns: []Column{{Name: "id", Nullable: true, Unique: true}}},
|
|
|
|
{
|
2016-07-12 08:49:42 +02:00
|
|
|
Name: "videos",
|
|
|
|
Columns: []Column{
|
2016-07-15 21:09:32 +02:00
|
|
|
{Name: "id", Nullable: true, Unique: true},
|
|
|
|
{Name: "user_id", Nullable: true, Unique: true},
|
|
|
|
{Name: "contest_id", Nullable: true, Unique: true},
|
2016-07-12 08:49:42 +02:00
|
|
|
},
|
|
|
|
FKeys: []ForeignKey{
|
2016-07-15 21:09:32 +02:00
|
|
|
{Name: "videos_user_id_fk", Column: "user_id", ForeignTable: "users", ForeignColumn: "id", Nullable: true, Unique: true},
|
|
|
|
{Name: "videos_contest_id_fk", Column: "contest_id", ForeignTable: "contests", ForeignColumn: "id", Nullable: true, Unique: true},
|
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: "notifications",
|
|
|
|
Columns: []Column{
|
2016-07-15 21:09:32 +02:00
|
|
|
{Name: "user_id", Nullable: true, Unique: true},
|
|
|
|
{Name: "source_id", Nullable: true, Unique: true},
|
2016-07-12 08:49:42 +02:00
|
|
|
},
|
|
|
|
FKeys: []ForeignKey{
|
2016-07-15 21:09:32 +02:00
|
|
|
{Name: "notifications_user_id_fk", Column: "user_id", ForeignTable: "users", ForeignColumn: "id", Nullable: true, Unique: true},
|
|
|
|
{Name: "notifications_source_id_fk", Column: "source_id", ForeignTable: "users", ForeignColumn: "id", Nullable: true, Unique: true},
|
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: "users_video_tags",
|
|
|
|
IsJoinTable: true,
|
|
|
|
Columns: []Column{
|
2016-07-15 21:09:32 +02:00
|
|
|
{Name: "user_id", Nullable: true, Unique: true},
|
|
|
|
{Name: "video_id", Nullable: true, Unique: true},
|
2016-07-12 08:49:42 +02:00
|
|
|
},
|
|
|
|
FKeys: []ForeignKey{
|
2016-07-15 21:09:32 +02:00
|
|
|
{Name: "user_id_fk", Column: "user_id", ForeignTable: "users", ForeignColumn: "id", Nullable: true, Unique: true},
|
|
|
|
{Name: "video_id_fk", Column: "video_id", ForeignTable: "videos", ForeignColumn: "id", Nullable: true, Unique: true},
|
2016-07-12 08:49:42 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
relationships := ToManyRelationships("users", tables)
|
|
|
|
if len(relationships) != 4 {
|
|
|
|
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-07-15 21:09:32 +02:00
|
|
|
Column: "id",
|
|
|
|
Nullable: true,
|
|
|
|
Unique: true,
|
2016-07-01 19:34:40 +02:00
|
|
|
|
2016-07-15 21:09:32 +02:00
|
|
|
ForeignTable: "videos",
|
|
|
|
ForeignColumn: "user_id",
|
|
|
|
ForeignColumnNullable: true,
|
|
|
|
ForeignColumnUnique: true,
|
2016-07-01 19:34:40 +02:00
|
|
|
|
2016-07-15 21:09:32 +02:00
|
|
|
ToJoinTable: false,
|
|
|
|
},
|
2016-08-08 09:46:06 +02:00
|
|
|
{
|
2016-07-15 21:09:32 +02:00
|
|
|
Column: "id",
|
|
|
|
Nullable: true,
|
|
|
|
Unique: true,
|
2016-07-01 19:34:40 +02:00
|
|
|
|
2016-07-15 21:09:32 +02:00
|
|
|
ForeignTable: "notifications",
|
|
|
|
ForeignColumn: "user_id",
|
|
|
|
ForeignColumnNullable: true,
|
|
|
|
ForeignColumnUnique: true,
|
|
|
|
|
|
|
|
ToJoinTable: false,
|
|
|
|
},
|
2016-08-08 09:46:06 +02:00
|
|
|
{
|
2016-07-15 21:09:32 +02:00
|
|
|
Column: "id",
|
|
|
|
Nullable: true,
|
|
|
|
Unique: true,
|
|
|
|
|
|
|
|
ForeignTable: "notifications",
|
|
|
|
ForeignColumn: "source_id",
|
|
|
|
ForeignColumnNullable: true,
|
|
|
|
ForeignColumnUnique: true,
|
|
|
|
|
|
|
|
ToJoinTable: false,
|
|
|
|
},
|
2016-08-08 09:46:06 +02:00
|
|
|
{
|
2016-07-15 21:09:32 +02:00
|
|
|
Column: "id",
|
|
|
|
Nullable: true,
|
|
|
|
Unique: true,
|
|
|
|
|
|
|
|
ForeignTable: "videos",
|
|
|
|
ForeignColumn: "id",
|
|
|
|
ForeignColumnNullable: true,
|
|
|
|
ForeignColumnUnique: true,
|
|
|
|
|
|
|
|
ToJoinTable: true,
|
|
|
|
JoinTable: "users_video_tags",
|
|
|
|
|
|
|
|
JoinLocalColumn: "user_id",
|
|
|
|
JoinLocalColumnNullable: true,
|
|
|
|
JoinLocalColumnUnique: true,
|
|
|
|
|
|
|
|
JoinForeignColumn: "video_id",
|
|
|
|
JoinForeignColumnNullable: true,
|
|
|
|
JoinForeignColumnUnique: true,
|
|
|
|
},
|
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]) {
|
|
|
|
t.Errorf("[%d] Mismatch between relationships null:\n\n%#v\n\n%#v\n\n", i, v, expected[i])
|
|
|
|
}
|
2016-06-27 02:43:13 +02:00
|
|
|
}
|
2016-06-23 07:03:05 +02:00
|
|
|
}
|