diff --git a/templates_test/relationship_one_to_one.tpl b/templates_test/relationship_one_to_one.tpl index 221a01f..7712476 100644 --- a/templates_test/relationship_one_to_one.tpl +++ b/templates_test/relationship_one_to_one.tpl @@ -3,12 +3,23 @@ {{- $dot := . -}} {{- range .Table.ToOneRelationships -}} {{- $txt := txtsFromOneToOne $dot.Tables $dot.Table . -}} + {{- $foreignNameSingular := $txt.ForeignTable.Name | singular | camelCase -}} + {{- $localNameSingular := $txt.LocalTable.Name | singular | camelCase -}} func test{{$txt.LocalTable.NameGo}}OneToOne{{$txt.ForeignTable.NameGo}}_{{$txt.Function.Name}}(t *testing.T) { tx := MustTx(boil.Begin()) defer tx.Rollback() var foreign {{$txt.ForeignTable.NameGo}} var local {{$txt.LocalTable.NameGo}} + + seed := randomize.NewSeed() + if err := randomize.Struct(seed, &foreign, {{$foreignNameSingular}}DBTypes, true, {{$foreignNameSingular}}ColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize {{$txt.ForeignTable.NameGo}} struct: %s", err) + } + if err := randomize.Struct(seed, &local, {{$localNameSingular}}DBTypes, true, {{$localNameSingular}}ColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize {{$txt.LocalTable.NameGo}} struct: %s", err) + } + {{if .ForeignColumnNullable -}} foreign.{{$txt.ForeignTable.ColumnNameGo}}.Valid = true {{- end}} diff --git a/templates_test/relationship_to_many.tpl b/templates_test/relationship_to_many.tpl index a238666..40f3916 100644 --- a/templates_test/relationship_to_many.tpl +++ b/templates_test/relationship_to_many.tpl @@ -3,22 +3,27 @@ {{- $dot := . }} {{- $table := .Table }} {{- range .Table.ToManyRelationships -}} - {{- $rel := txtsFromToMany $dot.Tables $table .}} -func test{{$rel.LocalTable.NameGo}}ToMany{{$rel.Function.Name}}(t *testing.T) { + {{- $txt := txtsFromToMany $dot.Tables $table .}} + {{- $localNameSingular := $txt.LocalTable.Name | singular | camelCase -}} +func test{{$txt.LocalTable.NameGo}}ToMany{{$txt.Function.Name}}(t *testing.T) { var err error tx := MustTx(boil.Begin()) defer tx.Rollback() - var a {{$rel.LocalTable.NameGo}} - var b, c {{$rel.ForeignTable.NameGo}} + var a {{$txt.LocalTable.NameGo}} + var b, c {{$txt.ForeignTable.NameGo}} + + seed := randomize.NewSeed() + if err = randomize.Struct(seed, &a, {{$localNameSingular}}DBTypes, true, {{$localNameSingular}}ColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize {{$txt.LocalTable.NameGo}} struct: %s", err) + } if err := a.Insert(tx); err != nil { t.Fatal(err) } - seed := randomize.NewSeed() - randomize.Struct(seed, &b, {{$rel.ForeignTable.NameSingular | camelCase}}DBTypes, false{{if not $rel.Function.UsesBytes}}, "{{.ForeignColumn}}"{{end}}) - randomize.Struct(seed, &c, {{$rel.ForeignTable.NameSingular | camelCase}}DBTypes, false{{if not $rel.Function.UsesBytes}}, "{{.ForeignColumn}}"{{end}}) + randomize.Struct(seed, &b, {{$txt.ForeignTable.NameSingular | camelCase}}DBTypes, false{{if not $txt.Function.UsesBytes}}, "{{.ForeignColumn}}"{{end}}) + randomize.Struct(seed, &c, {{$txt.ForeignTable.NameSingular | camelCase}}DBTypes, false{{if not $txt.Function.UsesBytes}}, "{{.ForeignColumn}}"{{end}}) {{if .Nullable -}} a.{{.Column | titleCase}}.Valid = true {{- end}} @@ -27,8 +32,8 @@ func test{{$rel.LocalTable.NameGo}}ToMany{{$rel.Function.Name}}(t *testing.T) { c.{{.ForeignColumn | titleCase}}.Valid = true {{- end}} {{if not .ToJoinTable -}} - b.{{$rel.Function.ForeignAssignment}} = a.{{$rel.Function.LocalAssignment}} - c.{{$rel.Function.ForeignAssignment}} = a.{{$rel.Function.LocalAssignment}} + b.{{$txt.Function.ForeignAssignment}} = a.{{$txt.Function.LocalAssignment}} + c.{{$txt.Function.ForeignAssignment}} = a.{{$txt.Function.LocalAssignment}} {{- end}} if err = b.Insert(tx); err != nil { t.Fatal(err) @@ -38,36 +43,36 @@ func test{{$rel.LocalTable.NameGo}}ToMany{{$rel.Function.Name}}(t *testing.T) { } {{if .ToJoinTable -}} - _, err = tx.Exec("insert into {{.JoinTable | $dot.SchemaTable}} ({{.JoinLocalColumn | $dot.Quotes}}, {{.JoinForeignColumn | $dot.Quotes}}) values {{if $dot.Dialect.IndexPlaceholders}}($1, $2){{else}}(?, ?){{end}}", a.{{$rel.LocalTable.ColumnNameGo}}, b.{{$rel.ForeignTable.ColumnNameGo}}) + _, err = tx.Exec("insert into {{.JoinTable | $dot.SchemaTable}} ({{.JoinLocalColumn | $dot.Quotes}}, {{.JoinForeignColumn | $dot.Quotes}}) values {{if $dot.Dialect.IndexPlaceholders}}($1, $2){{else}}(?, ?){{end}}", a.{{$txt.LocalTable.ColumnNameGo}}, b.{{$txt.ForeignTable.ColumnNameGo}}) if err != nil { t.Fatal(err) } - _, err = tx.Exec("insert into {{.JoinTable | $dot.SchemaTable}} ({{.JoinLocalColumn | $dot.Quotes}}, {{.JoinForeignColumn | $dot.Quotes}}) values {{if $dot.Dialect.IndexPlaceholders}}($1, $2){{else}}(?, ?){{end}}", a.{{$rel.LocalTable.ColumnNameGo}}, c.{{$rel.ForeignTable.ColumnNameGo}}) + _, err = tx.Exec("insert into {{.JoinTable | $dot.SchemaTable}} ({{.JoinLocalColumn | $dot.Quotes}}, {{.JoinForeignColumn | $dot.Quotes}}) values {{if $dot.Dialect.IndexPlaceholders}}($1, $2){{else}}(?, ?){{end}}", a.{{$txt.LocalTable.ColumnNameGo}}, c.{{$txt.ForeignTable.ColumnNameGo}}) if err != nil { t.Fatal(err) } {{end}} {{$varname := .ForeignTable | singular | camelCase -}} - {{$varname}}, err := a.{{$rel.Function.Name}}(tx).All() + {{$varname}}, err := a.{{$txt.Function.Name}}(tx).All() if err != nil { t.Fatal(err) } bFound, cFound := false, false for _, v := range {{$varname}} { - {{if $rel.Function.UsesBytes -}} - if 0 == bytes.Compare(v.{{$rel.Function.ForeignAssignment}}, b.{{$rel.Function.ForeignAssignment}}) { + {{if $txt.Function.UsesBytes -}} + if 0 == bytes.Compare(v.{{$txt.Function.ForeignAssignment}}, b.{{$txt.Function.ForeignAssignment}}) { bFound = true } - if 0 == bytes.Compare(v.{{$rel.Function.ForeignAssignment}}, c.{{$rel.Function.ForeignAssignment}}) { + if 0 == bytes.Compare(v.{{$txt.Function.ForeignAssignment}}, c.{{$txt.Function.ForeignAssignment}}) { cFound = true } {{else -}} - if v.{{$rel.Function.ForeignAssignment}} == b.{{$rel.Function.ForeignAssignment}} { + if v.{{$txt.Function.ForeignAssignment}} == b.{{$txt.Function.ForeignAssignment}} { bFound = true } - if v.{{$rel.Function.ForeignAssignment}} == c.{{$rel.Function.ForeignAssignment}} { + if v.{{$txt.Function.ForeignAssignment}} == c.{{$txt.Function.ForeignAssignment}} { cFound = true } {{end -}} @@ -80,19 +85,19 @@ func test{{$rel.LocalTable.NameGo}}ToMany{{$rel.Function.Name}}(t *testing.T) { t.Error("expected to find c") } - slice := {{$rel.LocalTable.NameGo}}Slice{&a} - if err = a.L.Load{{$rel.Function.Name}}(tx, false, &slice); err != nil { + slice := {{$txt.LocalTable.NameGo}}Slice{&a} + if err = a.L.Load{{$txt.Function.Name}}(tx, false, &slice); err != nil { t.Fatal(err) } - if got := len(a.R.{{$rel.Function.Name}}); got != 2 { + if got := len(a.R.{{$txt.Function.Name}}); got != 2 { t.Error("number of eager loaded records wrong, got:", got) } - a.R.{{$rel.Function.Name}} = nil - if err = a.L.Load{{$rel.Function.Name}}(tx, true, &a); err != nil { + a.R.{{$txt.Function.Name}} = nil + if err = a.L.Load{{$txt.Function.Name}}(tx, true, &a); err != nil { t.Fatal(err) } - if got := len(a.R.{{$rel.Function.Name}}); got != 2 { + if got := len(a.R.{{$txt.Function.Name}}); got != 2 { t.Error("number of eager loaded records wrong, got:", got) } diff --git a/templates_test/relationship_to_one.tpl b/templates_test/relationship_to_one.tpl index 478ad6b..add3fc5 100644 --- a/templates_test/relationship_to_one.tpl +++ b/templates_test/relationship_to_one.tpl @@ -3,12 +3,23 @@ {{- $dot := . -}} {{- range .Table.FKeys -}} {{- $txt := txtsFromFKey $dot.Tables $dot.Table . -}} + {{- $foreignNameSingular := $txt.ForeignTable.Name | singular | camelCase -}} + {{- $localNameSingular := $txt.LocalTable.Name | singular | camelCase -}} func test{{$txt.LocalTable.NameGo}}ToOne{{$txt.ForeignTable.NameGo}}_{{$txt.Function.Name}}(t *testing.T) { tx := MustTx(boil.Begin()) defer tx.Rollback() var local {{$txt.LocalTable.NameGo}} var foreign {{$txt.ForeignTable.NameGo}} + + seed := randomize.NewSeed() + if err := randomize.Struct(seed, &foreign, {{$foreignNameSingular}}DBTypes, true, {{$foreignNameSingular}}ColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize {{$txt.ForeignTable.NameGo}} struct: %s", err) + } + if err := randomize.Struct(seed, &local, {{$localNameSingular}}DBTypes, true, {{$localNameSingular}}ColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize {{$txt.LocalTable.NameGo}} struct: %s", err) + } + {{if .Nullable -}} local.{{$txt.LocalTable.ColumnNameGo}}.Valid = true {{- end}} diff --git a/templates_test/update.tpl b/templates_test/update.tpl index 98421c9..38aa71a 100644 --- a/templates_test/update.tpl +++ b/templates_test/update.tpl @@ -27,7 +27,7 @@ func test{{$tableNamePlural}}Update(t *testing.T) { t.Error("want one record, got:", count) } - if err = randomize.Struct(seed, {{$varNameSingular}}, {{$varNameSingular}}DBTypes, true, {{$varNameSingular}}PrimaryKeyColumns...); err != nil { + if err = randomize.Struct(seed, {{$varNameSingular}}, {{$varNameSingular}}DBTypes, true, {{$varNameSingular}}ColumnsWithDefault...); err != nil { t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err) } diff --git a/text_helpers.go b/text_helpers.go index f71bec3..d157609 100644 --- a/text_helpers.go +++ b/text_helpers.go @@ -14,6 +14,7 @@ type TxtToOne struct { ForeignKey bdb.ForeignKey LocalTable struct { + Name string NameGo string ColumnNameGo string } @@ -44,6 +45,7 @@ func txtsFromFKey(tables []bdb.Table, table bdb.Table, fkey bdb.ForeignKey) TxtT r.ForeignKey = fkey + r.LocalTable.Name = table.Name r.LocalTable.NameGo = strmangle.TitleCase(strmangle.Singular(table.Name)) r.LocalTable.ColumnNameGo = strmangle.TitleCase(strmangle.Singular(fkey.Column)) @@ -115,12 +117,14 @@ func txtsFromOneToOne(tables []bdb.Table, table bdb.Table, oneToOne bdb.ToOneRel // TxtToMany contains text that will be used by many-to-one relationships. type TxtToMany struct { LocalTable struct { + Name string NameGo string NameSingular string ColumnNameGo string } ForeignTable struct { + Name string NameGo string NameSingular string NamePluralGo string @@ -145,10 +149,12 @@ type TxtToMany struct { // transformation in advance for a given relationship. func txtsFromToMany(tables []bdb.Table, table bdb.Table, rel bdb.ToManyRelationship) TxtToMany { r := TxtToMany{} + r.LocalTable.Name = table.Name r.LocalTable.NameSingular = strmangle.Singular(table.Name) r.LocalTable.NameGo = strmangle.TitleCase(r.LocalTable.NameSingular) r.LocalTable.ColumnNameGo = strmangle.TitleCase(rel.Column) + r.ForeignTable.Name = rel.ForeignTable r.ForeignTable.NameSingular = strmangle.Singular(rel.ForeignTable) r.ForeignTable.NamePluralGo = strmangle.TitleCase(strmangle.Plural(rel.ForeignTable)) r.ForeignTable.NameGo = strmangle.TitleCase(r.ForeignTable.NameSingular)