2016-07-13 05:05:33 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
2016-08-09 09:59:30 +02:00
|
|
|
"github.com/vattle/sqlboiler/bdb"
|
2016-07-13 05:05:33 +02:00
|
|
|
)
|
|
|
|
|
2016-07-15 06:18:49 +02:00
|
|
|
func TestTextsFromForeignKey(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
tables, err := bdb.Tables(fakeDB(0))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
videos := bdb.GetTable(tables, "videos")
|
2016-07-17 08:56:09 +02:00
|
|
|
texts := textsFromForeignKey("models", tables, videos, videos.FKeys[0])
|
2016-07-15 06:18:49 +02:00
|
|
|
expect := RelationshipToOneTexts{}
|
|
|
|
|
2016-07-17 08:56:09 +02:00
|
|
|
expect.ForeignKey = videos.FKeys[0]
|
|
|
|
|
2016-07-15 06:18:49 +02:00
|
|
|
expect.LocalTable.NameGo = "Video"
|
2016-07-17 08:56:09 +02:00
|
|
|
expect.LocalTable.ColumnNameGo = "UserID"
|
2016-07-15 06:18:49 +02:00
|
|
|
|
2016-07-17 08:56:09 +02:00
|
|
|
expect.ForeignTable.Name = "users"
|
2016-07-15 06:18:49 +02:00
|
|
|
expect.ForeignTable.NameGo = "User"
|
2016-08-02 05:37:58 +02:00
|
|
|
expect.ForeignTable.NamePluralGo = "Users"
|
2016-07-17 08:56:09 +02:00
|
|
|
expect.ForeignTable.ColumnName = "id"
|
2016-07-15 06:18:49 +02:00
|
|
|
expect.ForeignTable.ColumnNameGo = "ID"
|
|
|
|
|
2016-07-17 08:56:09 +02:00
|
|
|
expect.Function.PackageName = "models"
|
|
|
|
expect.Function.Name = "User"
|
2016-07-15 06:18:49 +02:00
|
|
|
expect.Function.Varname = "user"
|
|
|
|
expect.Function.Receiver = "v"
|
2016-07-18 02:52:55 +02:00
|
|
|
expect.Function.ReverseInserts = false
|
2016-07-15 06:18:49 +02:00
|
|
|
|
|
|
|
expect.Function.LocalAssignment = "UserID.Int32"
|
|
|
|
expect.Function.ForeignAssignment = "ID"
|
|
|
|
|
2016-07-18 02:48:08 +02:00
|
|
|
if !reflect.DeepEqual(expect, texts) {
|
|
|
|
t.Errorf("Want:\n%s\nGot:\n%s\n", spew.Sdump(expect), spew.Sdump(texts))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTextsFromOneToOneRelationship(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
tables, err := bdb.Tables(fakeDB(0))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
users := bdb.GetTable(tables, "users")
|
|
|
|
texts := textsFromOneToOneRelationship("models", tables, users, users.ToManyRelationships[0])
|
|
|
|
expect := RelationshipToOneTexts{}
|
|
|
|
|
|
|
|
expect.ForeignKey = bdb.ForeignKey{
|
|
|
|
Name: "none",
|
|
|
|
Column: "id",
|
|
|
|
Nullable: false,
|
|
|
|
Unique: false,
|
|
|
|
|
|
|
|
ForeignTable: "videos",
|
|
|
|
ForeignColumn: "user_id",
|
|
|
|
ForeignColumnNullable: true,
|
|
|
|
ForeignColumnUnique: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
expect.LocalTable.NameGo = "User"
|
|
|
|
expect.LocalTable.ColumnNameGo = "ID"
|
|
|
|
|
|
|
|
expect.ForeignTable.Name = "videos"
|
|
|
|
expect.ForeignTable.NameGo = "Video"
|
2016-08-02 05:37:58 +02:00
|
|
|
expect.ForeignTable.NamePluralGo = "Videos"
|
2016-07-18 02:48:08 +02:00
|
|
|
expect.ForeignTable.ColumnName = "user_id"
|
|
|
|
expect.ForeignTable.ColumnNameGo = "UserID"
|
|
|
|
|
|
|
|
expect.Function.PackageName = "models"
|
|
|
|
expect.Function.Name = "Video"
|
|
|
|
expect.Function.Varname = "video"
|
|
|
|
expect.Function.Receiver = "u"
|
2016-07-18 02:52:55 +02:00
|
|
|
expect.Function.ReverseInserts = true
|
2016-07-18 02:48:08 +02:00
|
|
|
|
|
|
|
expect.Function.LocalAssignment = "ID"
|
|
|
|
expect.Function.ForeignAssignment = "UserID.Int32"
|
|
|
|
|
2016-07-15 06:18:49 +02:00
|
|
|
if !reflect.DeepEqual(expect, texts) {
|
|
|
|
t.Errorf("Want:\n%s\nGot:\n%s\n", spew.Sdump(expect), spew.Sdump(texts))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTextsFromRelationship(t *testing.T) {
|
2016-07-13 05:05:33 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
tables, err := bdb.Tables(fakeDB(0))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
users := bdb.GetTable(tables, "users")
|
2016-07-14 05:29:29 +02:00
|
|
|
texts := textsFromRelationship(tables, users, users.ToManyRelationships[0])
|
2016-07-13 05:05:33 +02:00
|
|
|
expect := RelationshipToManyTexts{}
|
|
|
|
expect.LocalTable.NameGo = "User"
|
|
|
|
expect.LocalTable.NameSingular = "user"
|
|
|
|
|
|
|
|
expect.ForeignTable.NameGo = "Video"
|
|
|
|
expect.ForeignTable.NameSingular = "video"
|
|
|
|
expect.ForeignTable.NamePluralGo = "Videos"
|
|
|
|
expect.ForeignTable.NameHumanReadable = "videos"
|
2016-07-16 22:23:15 +02:00
|
|
|
expect.ForeignTable.Slice = "VideoSlice"
|
2016-07-13 05:05:33 +02:00
|
|
|
|
|
|
|
expect.Function.Name = "Videos"
|
|
|
|
expect.Function.Receiver = "u"
|
|
|
|
expect.Function.LocalAssignment = "ID"
|
|
|
|
expect.Function.ForeignAssignment = "UserID.Int32"
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(expect, texts) {
|
|
|
|
t.Errorf("Want:\n%s\nGot:\n%s\n", spew.Sdump(expect), spew.Sdump(texts))
|
|
|
|
}
|
2016-07-14 05:29:29 +02:00
|
|
|
|
|
|
|
texts = textsFromRelationship(tables, users, users.ToManyRelationships[1])
|
|
|
|
expect = RelationshipToManyTexts{}
|
|
|
|
expect.LocalTable.NameGo = "User"
|
|
|
|
expect.LocalTable.NameSingular = "user"
|
|
|
|
|
|
|
|
expect.ForeignTable.NameGo = "Notification"
|
|
|
|
expect.ForeignTable.NameSingular = "notification"
|
|
|
|
expect.ForeignTable.NamePluralGo = "Notifications"
|
|
|
|
expect.ForeignTable.NameHumanReadable = "notifications"
|
2016-07-16 22:23:15 +02:00
|
|
|
expect.ForeignTable.Slice = "NotificationSlice"
|
2016-07-14 05:29:29 +02:00
|
|
|
|
|
|
|
expect.Function.Name = "Notifications"
|
|
|
|
expect.Function.Receiver = "u"
|
|
|
|
expect.Function.LocalAssignment = "ID"
|
|
|
|
expect.Function.ForeignAssignment = "UserID"
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(expect, texts) {
|
|
|
|
t.Errorf("Want:\n%s\nGot:\n%s\n", spew.Sdump(expect), spew.Sdump(texts))
|
|
|
|
}
|
|
|
|
|
|
|
|
texts = textsFromRelationship(tables, users, users.ToManyRelationships[2])
|
|
|
|
expect = RelationshipToManyTexts{}
|
|
|
|
expect.LocalTable.NameGo = "User"
|
|
|
|
expect.LocalTable.NameSingular = "user"
|
|
|
|
|
|
|
|
expect.ForeignTable.NameGo = "Notification"
|
|
|
|
expect.ForeignTable.NameSingular = "notification"
|
|
|
|
expect.ForeignTable.NamePluralGo = "Notifications"
|
|
|
|
expect.ForeignTable.NameHumanReadable = "notifications"
|
2016-07-16 22:23:15 +02:00
|
|
|
expect.ForeignTable.Slice = "NotificationSlice"
|
2016-07-14 05:29:29 +02:00
|
|
|
|
|
|
|
expect.Function.Name = "SourceNotifications"
|
|
|
|
expect.Function.Receiver = "u"
|
|
|
|
expect.Function.LocalAssignment = "ID"
|
|
|
|
expect.Function.ForeignAssignment = "SourceID.Int32"
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(expect, texts) {
|
|
|
|
t.Errorf("Want:\n%s\nGot:\n%s\n", spew.Sdump(expect), spew.Sdump(texts))
|
|
|
|
}
|
|
|
|
|
|
|
|
texts = textsFromRelationship(tables, users, users.ToManyRelationships[3])
|
|
|
|
expect = RelationshipToManyTexts{}
|
|
|
|
expect.LocalTable.NameGo = "User"
|
|
|
|
expect.LocalTable.NameSingular = "user"
|
|
|
|
|
|
|
|
expect.ForeignTable.NameGo = "Video"
|
|
|
|
expect.ForeignTable.NameSingular = "video"
|
|
|
|
expect.ForeignTable.NamePluralGo = "Videos"
|
|
|
|
expect.ForeignTable.NameHumanReadable = "videos"
|
2016-07-16 22:23:15 +02:00
|
|
|
expect.ForeignTable.Slice = "VideoSlice"
|
2016-07-14 05:29:29 +02:00
|
|
|
|
|
|
|
expect.Function.Name = "Videos"
|
|
|
|
expect.Function.Receiver = "u"
|
|
|
|
expect.Function.LocalAssignment = "ID"
|
|
|
|
expect.Function.ForeignAssignment = "ID"
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(expect, texts) {
|
|
|
|
t.Errorf("Want:\n%s\nGot:\n%s\n", spew.Sdump(expect), spew.Sdump(texts))
|
|
|
|
}
|
2016-07-13 05:05:33 +02:00
|
|
|
}
|