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-08-18 07:26:24 +02:00
|
|
|
"github.com/vattle/sqlboiler/bdb/drivers"
|
2016-07-13 05:05:33 +02:00
|
|
|
)
|
|
|
|
|
2016-07-15 06:18:49 +02:00
|
|
|
func TestTextsFromForeignKey(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2016-08-18 14:11:02 +02:00
|
|
|
tables, err := bdb.Tables(&drivers.MockDriver{})
|
2016-07-15 06:18:49 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-08-18 07:26:24 +02:00
|
|
|
jets := bdb.GetTable(tables, "jets")
|
|
|
|
texts := textsFromForeignKey("models", tables, jets, jets.FKeys[0])
|
2016-07-15 06:18:49 +02:00
|
|
|
expect := RelationshipToOneTexts{}
|
|
|
|
|
2016-08-18 07:26:24 +02:00
|
|
|
expect.ForeignKey = jets.FKeys[0]
|
2016-07-17 08:56:09 +02:00
|
|
|
|
2016-08-18 07:26:24 +02:00
|
|
|
expect.LocalTable.NameGo = "Jet"
|
|
|
|
expect.LocalTable.ColumnNameGo = "PilotID"
|
2016-07-15 06:18:49 +02:00
|
|
|
|
2016-08-18 07:26:24 +02:00
|
|
|
expect.ForeignTable.Name = "pilots"
|
|
|
|
expect.ForeignTable.NameGo = "Pilot"
|
|
|
|
expect.ForeignTable.NamePluralGo = "Pilots"
|
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"
|
2016-08-18 07:26:24 +02:00
|
|
|
expect.Function.Name = "Pilot"
|
|
|
|
expect.Function.Varname = "pilot"
|
|
|
|
expect.Function.Receiver = "j"
|
2016-07-18 02:52:55 +02:00
|
|
|
expect.Function.ReverseInserts = false
|
2016-07-15 06:18:49 +02:00
|
|
|
|
2016-08-18 14:11:02 +02:00
|
|
|
expect.Function.LocalAssignment = "PilotID.Int"
|
2016-07-15 06:18:49 +02:00
|
|
|
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()
|
|
|
|
|
2016-08-18 14:11:02 +02:00
|
|
|
tables, err := bdb.Tables(&drivers.MockDriver{})
|
2016-07-18 02:48:08 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-08-18 07:26:24 +02:00
|
|
|
pilots := bdb.GetTable(tables, "pilots")
|
|
|
|
texts := textsFromOneToOneRelationship("models", tables, pilots, pilots.ToManyRelationships[0])
|
2016-07-18 02:48:08 +02:00
|
|
|
expect := RelationshipToOneTexts{}
|
|
|
|
|
|
|
|
expect.ForeignKey = bdb.ForeignKey{
|
|
|
|
Name: "none",
|
|
|
|
Column: "id",
|
|
|
|
Nullable: false,
|
|
|
|
Unique: false,
|
|
|
|
|
2016-08-18 07:26:24 +02:00
|
|
|
ForeignTable: "jets",
|
|
|
|
ForeignColumn: "pilot_id",
|
2016-07-18 02:48:08 +02:00
|
|
|
ForeignColumnNullable: true,
|
|
|
|
ForeignColumnUnique: true,
|
|
|
|
}
|
|
|
|
|
2016-08-18 07:26:24 +02:00
|
|
|
expect.LocalTable.NameGo = "Pilot"
|
2016-07-18 02:48:08 +02:00
|
|
|
expect.LocalTable.ColumnNameGo = "ID"
|
|
|
|
|
2016-08-18 07:26:24 +02:00
|
|
|
expect.ForeignTable.Name = "jets"
|
|
|
|
expect.ForeignTable.NameGo = "Jet"
|
|
|
|
expect.ForeignTable.NamePluralGo = "Jets"
|
|
|
|
expect.ForeignTable.ColumnName = "pilot_id"
|
|
|
|
expect.ForeignTable.ColumnNameGo = "PilotID"
|
2016-07-18 02:48:08 +02:00
|
|
|
|
|
|
|
expect.Function.PackageName = "models"
|
2016-08-18 07:26:24 +02:00
|
|
|
expect.Function.Name = "Jet"
|
|
|
|
expect.Function.Varname = "jet"
|
|
|
|
expect.Function.Receiver = "p"
|
2016-07-18 02:52:55 +02:00
|
|
|
expect.Function.ReverseInserts = true
|
2016-07-18 02:48:08 +02:00
|
|
|
|
|
|
|
expect.Function.LocalAssignment = "ID"
|
2016-08-18 14:11:02 +02:00
|
|
|
expect.Function.ForeignAssignment = "PilotID.Int"
|
2016-07-18 02:48:08 +02:00
|
|
|
|
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()
|
|
|
|
|
2016-08-18 14:11:02 +02:00
|
|
|
tables, err := bdb.Tables(&drivers.MockDriver{})
|
2016-07-13 05:05:33 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-08-18 07:26:24 +02:00
|
|
|
pilots := bdb.GetTable(tables, "pilots")
|
|
|
|
texts := textsFromRelationship(tables, pilots, pilots.ToManyRelationships[0])
|
2016-07-13 05:05:33 +02:00
|
|
|
expect := RelationshipToManyTexts{}
|
2016-08-18 07:26:24 +02:00
|
|
|
expect.LocalTable.NameGo = "Pilot"
|
|
|
|
expect.LocalTable.NameSingular = "pilot"
|
2016-07-13 05:05:33 +02:00
|
|
|
|
2016-08-18 07:26:24 +02:00
|
|
|
expect.ForeignTable.NameGo = "Jet"
|
|
|
|
expect.ForeignTable.NameSingular = "jet"
|
|
|
|
expect.ForeignTable.NamePluralGo = "Jets"
|
|
|
|
expect.ForeignTable.NameHumanReadable = "jets"
|
|
|
|
expect.ForeignTable.Slice = "JetSlice"
|
2016-07-13 05:05:33 +02:00
|
|
|
|
2016-08-18 07:26:24 +02:00
|
|
|
expect.Function.Name = "Jets"
|
|
|
|
expect.Function.Receiver = "p"
|
2016-07-13 05:05:33 +02:00
|
|
|
expect.Function.LocalAssignment = "ID"
|
2016-08-18 14:11:02 +02:00
|
|
|
expect.Function.ForeignAssignment = "PilotID.Int"
|
2016-07-13 05:05:33 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
2016-08-18 07:26:24 +02:00
|
|
|
texts = textsFromRelationship(tables, pilots, pilots.ToManyRelationships[1])
|
2016-07-14 05:29:29 +02:00
|
|
|
expect = RelationshipToManyTexts{}
|
2016-08-18 07:26:24 +02:00
|
|
|
expect.LocalTable.NameGo = "Pilot"
|
|
|
|
expect.LocalTable.NameSingular = "pilot"
|
2016-07-14 05:29:29 +02:00
|
|
|
|
2016-08-18 07:26:24 +02:00
|
|
|
expect.ForeignTable.NameGo = "License"
|
|
|
|
expect.ForeignTable.NameSingular = "license"
|
|
|
|
expect.ForeignTable.NamePluralGo = "Licenses"
|
|
|
|
expect.ForeignTable.NameHumanReadable = "licenses"
|
|
|
|
expect.ForeignTable.Slice = "LicenseSlice"
|
2016-07-14 05:29:29 +02:00
|
|
|
|
2016-08-18 07:26:24 +02:00
|
|
|
expect.Function.Name = "Licenses"
|
|
|
|
expect.Function.Receiver = "p"
|
2016-07-14 05:29:29 +02:00
|
|
|
expect.Function.LocalAssignment = "ID"
|
2016-08-18 07:26:24 +02:00
|
|
|
expect.Function.ForeignAssignment = "PilotID"
|
2016-07-14 05:29:29 +02:00
|
|
|
|
|
|
|
if !reflect.DeepEqual(expect, texts) {
|
|
|
|
t.Errorf("Want:\n%s\nGot:\n%s\n", spew.Sdump(expect), spew.Sdump(texts))
|
|
|
|
}
|
|
|
|
|
2016-08-18 07:26:24 +02:00
|
|
|
texts = textsFromRelationship(tables, pilots, pilots.ToManyRelationships[2])
|
2016-07-14 05:29:29 +02:00
|
|
|
expect = RelationshipToManyTexts{}
|
2016-08-18 07:26:24 +02:00
|
|
|
expect.LocalTable.NameGo = "Pilot"
|
|
|
|
expect.LocalTable.NameSingular = "pilot"
|
2016-07-14 05:29:29 +02:00
|
|
|
|
2016-08-18 07:26:24 +02:00
|
|
|
expect.ForeignTable.NameGo = "License"
|
|
|
|
expect.ForeignTable.NameSingular = "license"
|
|
|
|
expect.ForeignTable.NamePluralGo = "Licenses"
|
|
|
|
expect.ForeignTable.NameHumanReadable = "licenses"
|
|
|
|
expect.ForeignTable.Slice = "LicenseSlice"
|
2016-07-14 05:29:29 +02:00
|
|
|
|
2016-08-18 07:26:24 +02:00
|
|
|
expect.Function.Name = "SourceLicenses"
|
|
|
|
expect.Function.Receiver = "p"
|
2016-07-14 05:29:29 +02:00
|
|
|
expect.Function.LocalAssignment = "ID"
|
2016-08-18 14:11:02 +02:00
|
|
|
expect.Function.ForeignAssignment = "SourceID.Int"
|
2016-07-14 05:29:29 +02:00
|
|
|
|
|
|
|
if !reflect.DeepEqual(expect, texts) {
|
|
|
|
t.Errorf("Want:\n%s\nGot:\n%s\n", spew.Sdump(expect), spew.Sdump(texts))
|
|
|
|
}
|
|
|
|
|
2016-08-18 07:26:24 +02:00
|
|
|
texts = textsFromRelationship(tables, pilots, pilots.ToManyRelationships[3])
|
2016-07-14 05:29:29 +02:00
|
|
|
expect = RelationshipToManyTexts{}
|
2016-08-18 07:26:24 +02:00
|
|
|
expect.LocalTable.NameGo = "Pilot"
|
|
|
|
expect.LocalTable.NameSingular = "pilot"
|
2016-07-14 05:29:29 +02:00
|
|
|
|
2016-08-18 07:26:24 +02:00
|
|
|
expect.ForeignTable.NameGo = "Jet"
|
|
|
|
expect.ForeignTable.NameSingular = "jet"
|
|
|
|
expect.ForeignTable.NamePluralGo = "Jets"
|
|
|
|
expect.ForeignTable.NameHumanReadable = "jets"
|
|
|
|
expect.ForeignTable.Slice = "JetSlice"
|
2016-07-14 05:29:29 +02:00
|
|
|
|
2016-08-18 07:26:24 +02:00
|
|
|
expect.Function.Name = "Jets"
|
|
|
|
expect.Function.Receiver = "p"
|
2016-07-14 05:29:29 +02:00
|
|
|
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
|
|
|
}
|