sqlboiler/templates_test/finishers.tpl

122 lines
3.5 KiB
Smarty
Raw Normal View History

{{- $tableNameSingular := .Table.Name | singular | titleCase -}}
{{- $tableNamePlural := .Table.Name | plural | titleCase -}}
{{- $varNamePlural := .Table.Name | plural | camelCase -}}
{{- $varNameSingular := .Table.Name | singular | camelCase -}}
func Test{{$tableNamePlural}}Bind(t *testing.T) {
2016-08-14 10:58:36 +02:00
t.Parallel()
2016-08-14 10:58:36 +02:00
{{$varNameSingular}} := &{{$tableNameSingular}}{}
if err := boil.RandomizeStruct({{$varNameSingular}}, {{$varNameSingular}}DBTypes, true, {{$varNameSingular}}ColumnsWithDefault...); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err)
}
2016-08-14 10:58:36 +02:00
tx, err := boil.Begin()
if err != nil {
2016-08-14 10:58:36 +02:00
t.Fatal(err)
}
2016-08-14 10:58:36 +02:00
defer tx.Rollback()
2016-08-14 10:58:36 +02:00
if err = {{$varNameSingular}}.Insert(tx); err != nil {
t.Error(err)
}
2016-08-14 10:58:36 +02:00
if err = {{$tableNamePlural}}(tx).Bind({{$varNameSingular}}); err != nil {
t.Error(err)
}
}
func Test{{$tableNamePlural}}One(t *testing.T) {
2016-08-14 10:58:36 +02:00
t.Parallel()
2016-08-14 10:58:36 +02:00
{{$varNameSingular}} := &{{$tableNameSingular}}{}
if err := boil.RandomizeStruct({{$varNameSingular}}, {{$varNameSingular}}DBTypes, true, {{$varNameSingular}}ColumnsWithDefault...); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err)
}
2016-08-14 10:58:36 +02:00
tx, err := boil.Begin()
if err != nil {
2016-08-14 10:58:36 +02:00
t.Fatal(err)
}
2016-08-14 10:58:36 +02:00
defer tx.Rollback()
2016-08-14 10:58:36 +02:00
if err = {{$varNameSingular}}.Insert(tx); err != nil {
t.Error(err)
}
2016-08-14 10:58:36 +02:00
if x, err := {{$tableNamePlural}}(tx).One(); err != nil {
t.Error(err)
} else if x == nil {
t.Error("expected to get a non nil record")
}
}
func Test{{$tableNamePlural}}All(t *testing.T) {
2016-08-14 10:58:36 +02:00
t.Parallel()
2016-08-14 10:58:36 +02:00
{{$varNameSingular}}One := &{{$tableNameSingular}}{}
{{$varNameSingular}}Two := &{{$tableNameSingular}}{}
if err := boil.RandomizeStruct({{$varNameSingular}}One, {{$varNameSingular}}DBTypes, true, {{$varNameSingular}}ColumnsWithDefault...); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err)
}
2016-08-14 10:58:36 +02:00
if err := boil.RandomizeStruct({{$varNameSingular}}Two, {{$varNameSingular}}DBTypes, true, {{$varNameSingular}}ColumnsWithDefault...); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err)
}
2016-08-14 10:58:36 +02:00
tx, err := boil.Begin()
if err != nil {
2016-08-14 10:58:36 +02:00
t.Fatal(err)
}
2016-08-14 10:58:36 +02:00
defer tx.Rollback()
2016-08-14 10:58:36 +02:00
if err = {{$varNameSingular}}One.Insert(tx); err != nil {
t.Error(err)
}
if err = {{$varNameSingular}}Two.Insert(tx); err != nil {
t.Error(err)
}
2016-08-14 10:58:36 +02:00
slice, err := {{$tableNamePlural}}(tx).All()
if err != nil {
t.Error(err)
}
2016-08-14 10:58:36 +02:00
if len(slice) != 2 {
t.Error("want 2 records, got:", len(slice))
}
}
func Test{{$tableNamePlural}}Count(t *testing.T) {
2016-08-14 10:58:36 +02:00
t.Parallel()
2016-08-14 10:58:36 +02:00
{{$varNameSingular}}One := &{{$tableNameSingular}}{}
{{$varNameSingular}}Two := &{{$tableNameSingular}}{}
if err := boil.RandomizeStruct({{$varNameSingular}}One, {{$varNameSingular}}DBTypes, true, {{$varNameSingular}}ColumnsWithDefault...); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err)
}
2016-08-14 10:58:36 +02:00
if err := boil.RandomizeStruct({{$varNameSingular}}Two, {{$varNameSingular}}DBTypes, true, {{$varNameSingular}}ColumnsWithDefault...); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err)
}
2016-06-14 15:15:32 +02:00
2016-08-14 10:58:36 +02:00
tx, err := boil.Begin()
2016-06-14 15:15:32 +02:00
if err != nil {
2016-08-14 10:58:36 +02:00
t.Fatal(err)
}
defer tx.Rollback()
if err = {{$varNameSingular}}One.Insert(tx); err != nil {
t.Error(err)
}
if err = {{$varNameSingular}}Two.Insert(tx); err != nil {
t.Error(err)
2016-06-14 15:15:32 +02:00
}
2016-08-14 10:58:36 +02:00
count, err := {{$tableNamePlural}}(tx).Count()
if err != nil {
t.Error(err)
2016-06-14 15:15:32 +02:00
}
2016-08-14 10:58:36 +02:00
if count != 2 {
t.Error("want 2 records, got:", count)
}
}