Relationship to many test work.

This commit is contained in:
Aaron L 2016-07-12 20:05:33 -07:00
parent c424780458
commit 272660ca75
3 changed files with 214 additions and 0 deletions

103
relationship_helpers.go Normal file
View file

@ -0,0 +1,103 @@
package main
import (
"fmt"
"strings"
"github.com/nullbio/sqlboiler/bdb"
"github.com/nullbio/sqlboiler/strmangle"
)
// RelationshipToManyTexts contains text that will be used by templates.
type RelationshipToManyTexts struct {
LocalTable struct {
NameGo string
NameSingular string
}
ForeignTable struct {
NameGo string
NameSingular string
NamePluralGo string
NameHumanReadable string
Slice string
}
Function struct {
Name string
Receiver string
LocalAssignment string
ForeignAssignment string
}
}
// createTextsFromRelationship creates a struct that does a lot of the text
// transformation in advance for a given relationship.
func createTextsFromRelationship(tables []bdb.Table, table bdb.Table, rel bdb.ToManyRelationship) RelationshipToManyTexts {
r := RelationshipToManyTexts{}
r.LocalTable.NameSingular = strmangle.Singular(table.Name)
r.LocalTable.NameGo = strmangle.TitleCase(r.LocalTable.NameSingular)
r.ForeignTable.NameSingular = strmangle.Singular(rel.ForeignTable)
r.ForeignTable.NamePluralGo = strmangle.TitleCase(strmangle.Plural(rel.ForeignTable))
r.ForeignTable.NameGo = strmangle.TitleCase(r.ForeignTable.NameSingular)
r.ForeignTable.Slice = fmt.Sprintf("%sSlice", strmangle.CamelCase(r.ForeignTable.NameSingular))
r.ForeignTable.NameHumanReadable = strings.Replace(rel.ForeignTable, "_", " ", -1)
r.Function.Receiver = strings.ToLower(table.Name[:1])
// Check to see if the foreign key name is the same as the local table name.
// Simple case: yes - we can name the function the same as the plural table name
// Not simple case: We have to name the function based off the foreign key and
if colName := strings.TrimSuffix(rel.ForeignColumn, "_id"); r.LocalTable.NameSingular == colName {
r.Function.Name = r.ForeignTable.NamePluralGo
} else {
r.Function.Name = r.ForeignTable.NamePluralGo + strmangle.TitleCase(colName)
}
if rel.Nullable {
col := table.GetColumn(rel.Column)
r.Function.LocalAssignment = fmt.Sprintf("%s.%s", strmangle.TitleCase(rel.Column), strings.TrimPrefix(col.Type, "null."))
} else {
r.Function.LocalAssignment = strmangle.TitleCase(rel.Column)
}
if !rel.ToJoinTable {
if rel.ForeignColumnNullable {
foreignTable := bdb.GetTable(tables, rel.ForeignTable)
col := foreignTable.GetColumn(rel.ForeignColumn)
r.Function.ForeignAssignment = fmt.Sprintf("%s.%s", strmangle.TitleCase(rel.ForeignColumn), strings.TrimPrefix(col.Type, "null."))
} else {
r.Function.ForeignAssignment = strmangle.TitleCase(rel.ForeignColumn)
}
return r
}
/*if rel.ForeignColumnNullable {
foreignTable := bdb.GetTable(tables, rel.ForeignTable)
col := foreignTable.GetColumn(rel.Column)
r.Function.ForeignAssignment = fmt.Sprintf("%s.%s", strmangle.TitleCase(rel.Column), strings.TrimPrefix("Null", col.Type))
} else {
r.Function.ForeignAssignment = strmangle.TitleCase(rel.ForeignColumn)
}*/
/*
{{if not .JoinTable -}}
{{- if .ForeignColumnNullable -}}
{{- $ftable := getTable $dot.Tables .ForeignTable -}}
{{- $fcol := getColumn $dot.Tables .ForeignColumn -}}
b.{{.ForeignColumn | camelCase}}.{{replace $fcol.Type "Null" ""}}, c.{{.ForeignColumn | camelCase}}.{{replace $fcol.Type "Null" ""}} = a.{{.Column}}{{if .Nullable}}, a.{{.Column}}
{{if .ForeignColumnNullable}}b.{{.ForeignColumn}}, c.{{.ForeignColumn}}{{else}}b.{{.ForeignColumn}}, c.{{.ForeignColumn}}{{end}} = a.ID, a.ID
{{- end -}}
{{- else -}}
b.user_id, c.user_id = a.ID, a.ID
{{- end -}}
{{- end}}
// {{$fnName}}X retrieves all the {{$localTableSing}}'s {{$foreignTableHumanReadable}} with an executor.
{{- if not $isForeignKeySimplyTableName}} via {{.ForeignColumn}} column.{{- end}}
func ({{$receiver}} *{{$localTable}}) {{$fnName}}X(exec boil.Executor, selectCols ...string) ({{$foreignSlice}}, error) {
var ret {{$foreignSlice}}
*/
return r
}

View file

@ -0,0 +1,91 @@
package main
import (
"reflect"
"testing"
"github.com/davecgh/go-spew/spew"
"github.com/nullbio/sqlboiler/bdb"
"github.com/nullbio/sqlboiler/strmangle"
)
type fakeDB int
func (fakeDB) TableNames() ([]string, error) {
return []string{"users", "videos", "contests", "notifications", "users_videos_tags"}, nil
}
func (fakeDB) Columns(tableName string) ([]bdb.Column, error) {
return map[string][]bdb.Column{
"users": []bdb.Column{{Name: "id", Type: "int32"}},
"contests": []bdb.Column{{Name: "id", Type: "int32", Nullable: true}},
"videos": []bdb.Column{
{Name: "id", Type: "int32"},
{Name: "user_id", Type: "int32", Nullable: true},
{Name: "contest_id", Type: "int32"},
},
"notifications": []bdb.Column{
{Name: "user_id", Type: "int32"},
{Name: "source_id", Type: "int32", Nullable: true},
},
"users_videos_tags": []bdb.Column{
{Name: "user_id", Type: "int32"},
{Name: "video_id", Type: "int32"},
},
}[tableName], nil
}
func (fakeDB) ForeignKeyInfo(tableName string) ([]bdb.ForeignKey, error) {
return map[string][]bdb.ForeignKey{
"videos": []bdb.ForeignKey{
{Name: "videos_user_id_fk", Column: "user_id", ForeignTable: "users", ForeignColumn: "id"},
{Name: "videos_contest_id_fk", Column: "contest_id", ForeignTable: "contests", ForeignColumn: "id"},
},
"notifications": []bdb.ForeignKey{
{Name: "notifications_user_id_fk", Column: "user_id", ForeignTable: "users", ForeignColumn: "id"},
{Name: "notifications_source_id_fk", Column: "source_id", ForeignTable: "users", ForeignColumn: "id"},
},
"users_video_tags": []bdb.ForeignKey{
{Name: "user_id_fk", Column: "user_id", ForeignTable: "users", ForeignColumn: "id"},
{Name: "video_id_fk", Column: "video_id", ForeignTable: "videos", ForeignColumn: "id"},
},
}[tableName], nil
}
func (fakeDB) TranslateColumnType(c bdb.Column) bdb.Column {
if c.Nullable {
c.Type = "null." + strmangle.TitleCase(c.Type)
}
return c
}
func (fakeDB) PrimaryKeyInfo(tableName string) (*bdb.PrimaryKey, error) { return nil, nil }
func (fakeDB) Open() error { return nil }
func (fakeDB) Close() {}
func TestCreateTextsFromRelationship(t *testing.T) {
t.Parallel()
tables, err := bdb.Tables(fakeDB(0))
if err != nil {
t.Fatal(err)
}
users := bdb.GetTable(tables, "users")
texts := createTextsFromRelationship(tables, users, users.ToManyRelationships[0])
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"
expect.ForeignTable.Slice = "videoSlice"
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))
}
}

View file

@ -0,0 +1,20 @@
{{- if .Table.IsJoinTable -}}
{{- else if false -}}
func TestIt(t *testing.T) {
var a A
var b, c B
if err := a.Insert(); err != nil {
t.Fatal(err)
}
b.user_id, c.user_id = a.ID, a.ID
if err := b.Insert(); err != nil {
t.Fatal(err)
}
if err := c.Insert(); err != nil {
t.Fatal(err)
}
}
{{- end -}}{{- /* outer if join table */ -}}