sqlboiler/templates_test/hooks.tpl

50 lines
1.5 KiB
Smarty
Raw Normal View History

2016-08-28 12:48:50 +02:00
{{- if eq .NoHooks false -}}
{{- $tableNameSingular := .Table.Name | singular | titleCase -}}
{{- $tableNamePlural := .Table.Name | plural | titleCase -}}
{{- $varNamePlural := .Table.Name | plural | camelCase -}}
{{- $varNameSingular := .Table.Name | singular | camelCase -}}
func {{$varNameSingular}}BeforeInsertHook(o *{{$tableNameSingular}}) error {
2016-07-15 17:21:09 +02:00
*o = {{$tableNameSingular}}{}
return nil
}
func {{$varNameSingular}}AfterInsertHook(o *{{$tableNameSingular}}) error {
2016-07-15 17:21:09 +02:00
*o = {{$tableNameSingular}}{}
return nil
}
func {{$varNameSingular}}BeforeUpdateHook(o *{{$tableNameSingular}}) error {
2016-07-15 17:21:09 +02:00
*o = {{$tableNameSingular}}{}
return nil
}
func {{$varNameSingular}}AfterUpdateHook(o *{{$tableNameSingular}}) error {
2016-07-15 17:21:09 +02:00
*o = {{$tableNameSingular}}{}
return nil
}
func test{{$tableNamePlural}}Hooks(t *testing.T) {
t.Parallel()
2016-07-15 17:21:09 +02:00
var err error
2016-07-15 17:21:09 +02:00
empty := &{{$tableNameSingular}}{}
o := &{{$tableNameSingular}}{}
2016-08-18 09:06:28 +02:00
seed := randomize.NewSeed()
if err = randomize.Struct(seed, o, {{$varNameSingular}}DBTypes, false); err != nil {
2016-07-15 17:21:09 +02:00
t.Errorf("Unable to randomize {{$tableNameSingular}} object: %s", err)
}
{{$tableNameSingular}}AddHook(boil.HookBeforeInsert, {{$varNameSingular}}BeforeInsertHook)
if err = o.doBeforeInsertHooks(); err != nil {
t.Errorf("Unable to execute doBeforeInsertHooks: %s", err)
2016-07-15 17:21:09 +02:00
}
if !reflect.DeepEqual(o, empty) {
t.Errorf("Expected BeforeInsertHook function to empty object, but got: %#v", o)
2016-07-15 17:21:09 +02:00
}
{{$varNameSingular}}BeforeInsertHooks = []{{$tableNameSingular}}Hook{}
}
2016-08-28 12:48:50 +02:00
{{- end}}