Finish to_many base test case (no join table)

This commit is contained in:
Aaron L 2016-07-13 20:31:44 -07:00
parent f764c979e7
commit f1df86ab9e
2 changed files with 67 additions and 46 deletions

View file

@ -2,66 +2,49 @@
{{- else -}}
{{- $dot := . }}
{{- $table := .Table }}
{{- $localTableSing := .Table.Name | singular -}}
{{- $localTable := $localTableSing | titleCase -}}
{{- $colName := $localTableSing | printf "%s_id" -}}
{{- $receiver := .Table.Name | toLower | substring 0 1 -}}
{{- range toManyRelationships .Table.Name .Tables -}}
{{- $foreignTableSing := .ForeignTable | singular}}
{{- $foreignTable := $foreignTableSing | titleCase}}
{{- $foreignSlice := $foreignTableSing | camelCase | printf "%sSlice"}}
{{- $foreignTableHumanReadable := .ForeignTable | replace "_" " " -}}
{{- $foreignPluralNoun := .ForeignTable | plural | titleCase -}}
{{- $isForeignKeySimplyTableName := or (eq $colName .ForeignColumn) .ToJoinTable -}}
{{- if $isForeignKeySimplyTableName -}}
// {{$foreignPluralNoun}} retrieves all the {{$localTableSing}}'s {{$foreignTableHumanReadable}}.
func ({{$receiver}} *{{$localTable}}) {{$foreignPluralNoun}}(selectCols ...string) ({{$foreignSlice}}, error) {
return {{$receiver}}.{{$foreignPluralNoun}}X(boil.GetDB(), selectCols...)
{{- $rel := textsFromRelationship $dot.Tables $table . -}}
// {{$rel.Function.Name}} retrieves all the {{$rel.LocalTable.NameSingular}}'s {{$rel.ForeignTable.NameHumanReadable}}
{{- if not (eq $rel.Function.Name $rel.ForeignTable.NamePluralGo)}} via {{.ForeignColumn}} column{{- end}}.
func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.Function.Name}}(selectCols ...string) ({{$rel.ForeignTable.Slice}}, error) {
return {{$rel.Function.Receiver}}.{{$rel.Function.Name}}X(boil.GetDB(), selectCols...)
}
// {{$foreignPluralNoun}} retrieves all the {{$localTableSing}}'s {{$foreignTableHumanReadable}} with an executor.
func ({{$receiver}} *{{$localTable}}) {{$foreignPluralNoun}}X(
{{- else -}}
{{- $fnName := .ForeignColumn | remove "_id" | titleCase | printf "%[2]s%[1]s" $foreignPluralNoun -}}
// {{$fnName}} retrieves all the {{$localTableSing}}'s {{$foreignTableHumanReadable}} via {{.ForeignColumn}} column.
func ({{$receiver}} *{{$localTable}}) {{$fnName}}(selectCols ...string) ({{$foreignSlice}}, error) {
return {{$receiver}}.{{$fnName}}X(boil.GetDB(), selectCols...)
}
// {{$fnName}} retrieves all the {{$localTableSing}}'s {{$foreignTableHumanReadable}} via {{.ForeignColumn}} column.
func ({{$receiver}} *{{$localTable}}) {{$fnName}}X(
{{- end -}}
exec boil.Executor, selectCols ...string) ({{$foreignSlice}}, error) {
var ret {{$foreignSlice}}
// {{$rel.Function.Name}}X retrieves all the {{$rel.LocalTable.NameSingular}}'s {{$rel.ForeignTable.NameHumanReadable}} with an executor
{{- if not (eq $rel.Function.Name $rel.ForeignTable.NamePluralGo)}} via {{.ForeignColumn}} column{{- end}}.
func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.Function.Name}}X(exec boil.Executor, selectCols ...string) ({{$rel.ForeignTable.Slice}}, error) {
var ret {{$rel.ForeignTable.Slice}}
var selectColumns string
if len(selectCols) > 0 {
selectColumns = `"{{id 0}}".` + strings.Join(selectCols, `","{{id 0}}"."`)
} else {
selectColumns = "*"
}
{{if .ToJoinTable -}}
query := fmt.Sprintf(`select "%s" from {{.ForeignTable}} "{{id 0}}" inner join {{.JoinTable}} as "{{id 1}}" on "{{id 1}}"."{{.JoinForeignColumn}}" = "{{id 0}}"."{{.ForeignColumn}}" where "{{id 1}}"."{{.JoinLocalColumn}}"=$1`, `"{{id 1}}".` + strings.Join(selectCols, `","{{id 0}}"."`))
query := fmt.Sprintf(`select %s from {{.ForeignTable}} "{{id 0}}" inner join {{.JoinTable}} as "{{id 1}}" on "{{id 1}}"."{{.JoinForeignColumn}}" = "{{id 0}}"."{{.ForeignColumn}}" where "{{id 1}}"."{{.JoinLocalColumn}}"=$1`, selectColumns)
{{else -}}
query := fmt.Sprintf(`select "%s" from {{.ForeignTable}} where "{{.ForeignColumn}}"=$1`, strings.Join(selectCols, `","`))
query := fmt.Sprintf(`select %s from {{.ForeignTable}} "{{id 0}}" where "{{id 0}}"."{{.ForeignColumn}}"=$1`, selectColumns)
{{end}}
rows, err := exec.Query(query, {{.Column | titleCase | printf "%s.%s" $receiver }})
rows, err := exec.Query(query, {{.Column | titleCase | printf "%s.%s" $rel.Function.Receiver }})
if err != nil {
return nil, fmt.Errorf(`{{$dot.PkgName}}: unable to select from {{.ForeignTable}}: %v`, err)
}
defer rows.Close()
for rows.Next() {
next := new({{$foreignTable}})
next := new({{$rel.ForeignTable.NameGo}})
err = rows.Scan(boil.GetStructPointers(next, selectCols...)...)
if err != nil {
return nil, fmt.Errorf(`{{$dot.PkgName}}: unable to scan into {{$foreignTable}}: %v`, err)
return nil, fmt.Errorf(`{{$dot.PkgName}}: unable to scan into {{$rel.ForeignTable.NameGo}}: %v`, err)
}
ret = append(ret, next)
}
if err = rows.Err(); err != nil {
return nil, fmt.Errorf(`{{$dot.PkgName}}: unable to select from {{.ForeignTable}}: %v`, err)
return nil, fmt.Errorf(`{{$dot.PkgName}}: unable to select from {{$rel.ForeignTable.NameGo}}: %v`, err)
}
return ret, nil

View file

@ -1,20 +1,58 @@
{{- if .Table.IsJoinTable -}}
{{- else if false -}}
func TestIt(t *testing.T) {
var a A
var b, c B
{{- else -}}
{{- $dot := . }}
{{- $table := .Table }}
{{- range toManyRelationships .Table.Name .Tables -}}
{{- $rel := textsFromRelationship $dot.Tables $table . -}}
func Test{{$rel.LocalTable.NameGo}}ToMany{{$rel.Function.Name}}(t *testing.T) {
tx, err := boil.Begin()
if err != nil {
t.Fatal(err)
}
defer tx.Rollback()
if err := a.Insert(); err != nil {
var a {{$rel.LocalTable.NameGo}}
var b, c {{$rel.ForeignTable.NameGo}}
if err := a.InsertX(tx); err != nil {
t.Fatal(err)
}
b.user_id, c.user_id = a.ID, a.ID
if err := b.Insert(); err != nil {
boil.RandomizeStruct(&b)
boil.RandomizeStruct(&c)
b.{{$rel.Function.ForeignAssignment}} = a.{{$rel.Function.LocalAssignment}}
c.{{$rel.Function.ForeignAssignment}} = a.{{$rel.Function.LocalAssignment}}
if err := b.InsertX(tx); err != nil {
t.Fatal(err)
}
if err := c.Insert(); err != nil {
if err := c.InsertX(tx); err != nil {
t.Fatal(err)
}
{{$varname := $rel.ForeignTable.NamePluralGo | toLower -}}
{{$varname}}, err := a.{{$rel.Function.Name}}X(tx)
bFound, cFound := false, false
for _, v := range {{$varname}} {
if v.{{$rel.Function.ForeignAssignment}} == b.{{$rel.Function.ForeignAssignment}} {
bFound = true
}
if v.{{$rel.Function.ForeignAssignment}} == c.{{$rel.Function.ForeignAssignment}} {
cFound = true
}
}
if !bFound {
t.Error("expected to find b")
}
if !cFound {
t.Error("expected to find c")
}
if t.Failed() {
t.Logf("%#v", {{$varname}})
}
}
{{ end -}}{{- /* range */ -}}
{{- end -}}{{- /* outer if join table */ -}}