2017-01-14 04:38:40 +01:00
|
|
|
package boilingcore
|
2016-07-13 05:05:33 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
2017-05-08 19:25:15 +02:00
|
|
|
"github.com/lbryio/sqlboiler/bdb"
|
|
|
|
"github.com/lbryio/sqlboiler/bdb/drivers"
|
2016-07-13 05:05:33 +02:00
|
|
|
)
|
|
|
|
|
2016-09-18 08:36:10 +02:00
|
|
|
func TestTxtsFromOne(t *testing.T) {
|
2016-07-15 06:18:49 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-09-09 17:06:07 +02:00
|
|
|
tables, err := bdb.Tables(&drivers.MockDriver{}, "public", nil, nil)
|
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")
|
2016-09-18 19:31:11 +02:00
|
|
|
texts := txtsFromFKey(tables, jets, jets.FKeys[0])
|
2016-09-18 08:36:10 +02:00
|
|
|
expect := TxtToOne{}
|
2016-07-15 06:18:49 +02:00
|
|
|
|
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.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-08-18 07:26:24 +02:00
|
|
|
expect.Function.Name = "Pilot"
|
2016-08-28 08:01:26 +02:00
|
|
|
expect.Function.ForeignName = "Jet"
|
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))
|
|
|
|
}
|
2016-08-28 08:01:26 +02:00
|
|
|
|
2016-09-18 19:31:11 +02:00
|
|
|
texts = txtsFromFKey(tables, jets, jets.FKeys[1])
|
2016-09-18 08:36:10 +02:00
|
|
|
expect = TxtToOne{}
|
2016-08-28 08:01:26 +02:00
|
|
|
expect.ForeignKey = jets.FKeys[1]
|
|
|
|
|
|
|
|
expect.LocalTable.NameGo = "Jet"
|
|
|
|
expect.LocalTable.ColumnNameGo = "AirportID"
|
|
|
|
|
|
|
|
expect.ForeignTable.NameGo = "Airport"
|
|
|
|
expect.ForeignTable.NamePluralGo = "Airports"
|
|
|
|
expect.ForeignTable.ColumnName = "id"
|
|
|
|
expect.ForeignTable.ColumnNameGo = "ID"
|
|
|
|
|
|
|
|
expect.Function.Name = "Airport"
|
|
|
|
expect.Function.ForeignName = "Jets"
|
|
|
|
|
|
|
|
expect.Function.LocalAssignment = "AirportID"
|
|
|
|
expect.Function.ForeignAssignment = "ID"
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(expect, texts) {
|
|
|
|
t.Errorf("Want:\n%s\nGot:\n%s\n", spew.Sdump(expect), spew.Sdump(texts))
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(expect, texts) {
|
|
|
|
t.Errorf("Want:\n%s\nGot:\n%s\n", spew.Sdump(expect), spew.Sdump(texts))
|
|
|
|
}
|
2016-07-18 02:48:08 +02:00
|
|
|
}
|
|
|
|
|
2016-09-18 08:36:10 +02:00
|
|
|
func TestTxtsFromOneToOne(t *testing.T) {
|
2016-07-18 02:48:08 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-09-09 17:06:07 +02:00
|
|
|
tables, err := bdb.Tables(&drivers.MockDriver{}, "public", nil, nil)
|
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")
|
2016-09-18 19:31:11 +02:00
|
|
|
texts := txtsFromOneToOne(tables, pilots, pilots.ToOneRelationships[0])
|
2016-09-18 08:36:10 +02:00
|
|
|
expect := TxtToOne{}
|
2016-07-18 02:48:08 +02:00
|
|
|
|
|
|
|
expect.ForeignKey = bdb.ForeignKey{
|
2016-09-18 08:36:10 +02:00
|
|
|
Name: "none",
|
|
|
|
|
|
|
|
Table: "jets",
|
|
|
|
Column: "pilot_id",
|
|
|
|
Nullable: true,
|
|
|
|
Unique: true,
|
|
|
|
|
|
|
|
ForeignTable: "pilots",
|
|
|
|
ForeignColumn: "id",
|
|
|
|
ForeignColumnNullable: false,
|
|
|
|
ForeignColumnUnique: false,
|
2016-07-18 02:48:08 +02:00
|
|
|
}
|
|
|
|
|
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.NameGo = "Jet"
|
|
|
|
expect.ForeignTable.NamePluralGo = "Jets"
|
|
|
|
expect.ForeignTable.ColumnName = "pilot_id"
|
|
|
|
expect.ForeignTable.ColumnNameGo = "PilotID"
|
2016-07-18 02:48:08 +02:00
|
|
|
|
2016-08-18 07:26:24 +02:00
|
|
|
expect.Function.Name = "Jet"
|
2016-08-28 02:20:26 +02:00
|
|
|
expect.Function.ForeignName = "Pilot"
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-18 08:36:10 +02:00
|
|
|
func TestTxtsFromMany(t *testing.T) {
|
2016-07-13 05:05:33 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-09-09 17:06:07 +02:00
|
|
|
tables, err := bdb.Tables(&drivers.MockDriver{}, "public", nil, nil)
|
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")
|
2016-09-18 08:36:10 +02:00
|
|
|
texts := txtsFromToMany(tables, pilots, pilots.ToManyRelationships[0])
|
|
|
|
expect := TxtToMany{}
|
2016-08-18 07:26:24 +02:00
|
|
|
expect.LocalTable.NameGo = "Pilot"
|
2016-08-27 21:00:42 +02:00
|
|
|
expect.LocalTable.ColumnNameGo = "ID"
|
2016-07-14 05:29:29 +02:00
|
|
|
|
2016-08-18 07:26:24 +02:00
|
|
|
expect.ForeignTable.NameGo = "License"
|
|
|
|
expect.ForeignTable.NamePluralGo = "Licenses"
|
|
|
|
expect.ForeignTable.NameHumanReadable = "licenses"
|
2016-08-27 21:00:42 +02:00
|
|
|
expect.ForeignTable.ColumnNameGo = "PilotID"
|
2016-08-18 07:26:24 +02:00
|
|
|
expect.ForeignTable.Slice = "LicenseSlice"
|
2016-07-14 05:29:29 +02:00
|
|
|
|
2016-08-18 07:26:24 +02:00
|
|
|
expect.Function.Name = "Licenses"
|
2016-08-29 01:01:43 +02:00
|
|
|
expect.Function.ForeignName = "Pilot"
|
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-09-18 08:36:10 +02:00
|
|
|
texts = txtsFromToMany(tables, pilots, pilots.ToManyRelationships[1])
|
|
|
|
expect = TxtToMany{}
|
2016-08-18 07:26:24 +02:00
|
|
|
expect.LocalTable.NameGo = "Pilot"
|
2016-08-27 21:00:42 +02:00
|
|
|
expect.LocalTable.ColumnNameGo = "ID"
|
2016-07-14 05:29:29 +02:00
|
|
|
|
2016-08-23 05:38:43 +02:00
|
|
|
expect.ForeignTable.NameGo = "Language"
|
|
|
|
expect.ForeignTable.NamePluralGo = "Languages"
|
|
|
|
expect.ForeignTable.NameHumanReadable = "languages"
|
2016-08-27 21:00:42 +02:00
|
|
|
expect.ForeignTable.ColumnNameGo = "ID"
|
2016-08-23 05:38:43 +02:00
|
|
|
expect.ForeignTable.Slice = "LanguageSlice"
|
2016-07-14 05:29:29 +02:00
|
|
|
|
2016-08-23 05:38:43 +02:00
|
|
|
expect.Function.Name = "Languages"
|
2016-08-28 02:20:26 +02:00
|
|
|
expect.Function.ForeignName = "Pilots"
|
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
|
|
|
}
|
2016-09-20 07:00:39 +02:00
|
|
|
|
2016-09-22 08:36:29 +02:00
|
|
|
func TestTxtNameToOne(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
Table string
|
|
|
|
Column string
|
|
|
|
Unique bool
|
|
|
|
ForeignTable string
|
|
|
|
ForeignColumn string
|
|
|
|
ForeignColumnUnique bool
|
|
|
|
|
|
|
|
LocalFn string
|
|
|
|
ForeignFn string
|
|
|
|
}{
|
|
|
|
{"jets", "airport_id", false, "airports", "id", true, "Airport", "Jets"},
|
|
|
|
{"jets", "airport_id", true, "airports", "id", true, "Airport", "Jet"},
|
|
|
|
|
|
|
|
{"jets", "holiday_id", false, "airports", "id", true, "Holiday", "HolidayJets"},
|
|
|
|
{"jets", "holiday_id", true, "airports", "id", true, "Holiday", "HolidayJet"},
|
|
|
|
|
|
|
|
{"jets", "holiday_airport_id", false, "airports", "id", true, "HolidayAirport", "HolidayAirportJets"},
|
|
|
|
{"jets", "holiday_airport_id", true, "airports", "id", true, "HolidayAirport", "HolidayAirportJet"},
|
|
|
|
|
|
|
|
{"jets", "jet_id", false, "jets", "id", true, "Jet", "Jets"},
|
|
|
|
{"jets", "jet_id", true, "jets", "id", true, "Jet", "Jet"},
|
|
|
|
{"jets", "plane_id", false, "jets", "id", true, "Plane", "PlaneJets"},
|
|
|
|
{"jets", "plane_id", true, "jets", "id", true, "Plane", "PlaneJet"},
|
2016-09-23 07:11:30 +02:00
|
|
|
|
|
|
|
{"race_result_scratchings", "results_id", false, "race_results", "id", true, "Result", "ResultRaceResultScratchings"},
|
2016-09-22 08:36:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
|
|
|
fk := bdb.ForeignKey{
|
|
|
|
Table: test.Table, Column: test.Column, Unique: test.Unique,
|
|
|
|
ForeignTable: test.ForeignTable, ForeignColumn: test.ForeignColumn, ForeignColumnUnique: test.ForeignColumnUnique,
|
|
|
|
}
|
|
|
|
|
|
|
|
local, foreign := txtNameToOne(fk)
|
|
|
|
if local != test.LocalFn {
|
|
|
|
t.Error(i, "local wrong:", local, "want:", test.LocalFn)
|
|
|
|
}
|
|
|
|
if foreign != test.ForeignFn {
|
|
|
|
t.Error(i, "foreign wrong:", foreign, "want:", test.ForeignFn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTxtNameToMany(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
Table string
|
|
|
|
Column string
|
|
|
|
|
|
|
|
ForeignTable string
|
|
|
|
ForeignColumn string
|
|
|
|
|
|
|
|
ToJoinTable bool
|
|
|
|
JoinLocalColumn string
|
|
|
|
JoinForeignColumn string
|
|
|
|
|
|
|
|
LocalFn string
|
|
|
|
ForeignFn string
|
|
|
|
}{
|
|
|
|
{"airports", "id", "jets", "airport_id", false, "", "", "Jets", "Airport"},
|
|
|
|
{"airports", "id", "jets", "holiday_airport_id", false, "", "", "HolidayAirportJets", "HolidayAirport"},
|
|
|
|
|
|
|
|
{"jets", "id", "jets", "jet_id", false, "", "", "Jets", "Jet"},
|
|
|
|
{"jets", "id", "jets", "plane_id", false, "", "", "PlaneJets", "Plane"},
|
|
|
|
|
|
|
|
{"pilots", "id", "languages", "id", true, "pilot_id", "language_id", "Languages", "Pilots"},
|
|
|
|
{"pilots", "id", "languages", "id", true, "captain_id", "lingo_id", "LingoLanguages", "CaptainPilots"},
|
|
|
|
|
|
|
|
{"pilots", "id", "pilots", "id", true, "pilot_id", "mentor_id", "MentorPilots", "Pilots"},
|
|
|
|
{"pilots", "id", "pilots", "id", true, "mentor_id", "pilot_id", "Pilots", "MentorPilots"},
|
|
|
|
{"pilots", "id", "pilots", "id", true, "captain_id", "mentor_id", "MentorPilots", "CaptainPilots"},
|
2016-09-23 07:11:30 +02:00
|
|
|
|
|
|
|
{"race_results", "id", "race_result_scratchings", "results_id", false, "", "", "ResultRaceResultScratchings", "Result"},
|
2016-09-22 08:36:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
|
|
|
fk := bdb.ToManyRelationship{
|
|
|
|
Table: test.Table, Column: test.Column,
|
|
|
|
ForeignTable: test.ForeignTable, ForeignColumn: test.ForeignColumn,
|
|
|
|
ToJoinTable: test.ToJoinTable,
|
|
|
|
JoinLocalColumn: test.JoinLocalColumn, JoinForeignColumn: test.JoinForeignColumn,
|
|
|
|
}
|
|
|
|
|
|
|
|
local, foreign := txtNameToMany(fk)
|
|
|
|
if local != test.LocalFn {
|
|
|
|
t.Error(i, "local wrong:", local, "want:", test.LocalFn)
|
|
|
|
}
|
|
|
|
if foreign != test.ForeignFn {
|
|
|
|
t.Error(i, "foreign wrong:", foreign, "want:", test.ForeignFn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-20 07:00:39 +02:00
|
|
|
func TestTrimSuffixes(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
for _, s := range identifierSuffixes {
|
|
|
|
a := "hello" + s
|
|
|
|
|
|
|
|
if z := trimSuffixes(a); z != "hello" {
|
|
|
|
t.Errorf("got %s", z)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|