Finished hook tests

* Fixed hook bug
This commit is contained in:
Patrick O'brien 2016-07-16 01:21:09 +10:00
parent 7e17008f8d
commit 4fcfcfe24c
3 changed files with 27 additions and 8 deletions

View file

@ -8,7 +8,7 @@ var {{$varNameSingular}}AfterUpdateHooks []{{$tableNameSingular}}Hook
// doBeforeCreateHooks executes all "before create" hooks.
func (o *{{$tableNameSingular}}) doBeforeCreateHooks() (err error) {
for _, hook := range {{$varNameSingular}}BeforeCreateHooks {
if err := hook(o); err == nil {
if err := hook(o); err != nil {
return err
}
}
@ -19,7 +19,7 @@ func (o *{{$tableNameSingular}}) doBeforeCreateHooks() (err error) {
// doBeforeUpdateHooks executes all "before Update" hooks.
func (o *{{$tableNameSingular}}) doBeforeUpdateHooks() (err error) {
for _, hook := range {{$varNameSingular}}BeforeUpdateHooks {
if err := hook(o); err == nil {
if err := hook(o); err != nil {
return err
}
}
@ -30,7 +30,7 @@ func (o *{{$tableNameSingular}}) doBeforeUpdateHooks() (err error) {
// doAfterCreateHooks executes all "after create" hooks.
func (o *{{$tableNameSingular}}) doAfterCreateHooks() (err error) {
for _, hook := range {{$varNameSingular}}AfterCreateHooks {
if err := hook(o); err == nil {
if err := hook(o); err != nil {
return err
}
}
@ -41,7 +41,7 @@ func (o *{{$tableNameSingular}}) doAfterCreateHooks() (err error) {
// doAfterUpdateHooks executes all "after Update" hooks.
func (o *{{$tableNameSingular}}) doAfterUpdateHooks() (err error) {
for _, hook := range {{$varNameSingular}}AfterUpdateHooks {
if err := hook(o); err == nil {
if err := hook(o); err != nil {
return err
}
}
@ -49,7 +49,7 @@ func (o *{{$tableNameSingular}}) doAfterUpdateHooks() (err error) {
return nil
}
func (o *{{$tableNameSingular}}) {{$tableNameSingular}}AddHook(hookPoint boil.HookPoint, {{$varNameSingular}}Hook {{$tableNameSingular}}Hook) {
func {{$tableNameSingular}}AddHook(hookPoint boil.HookPoint, {{$varNameSingular}}Hook {{$tableNameSingular}}Hook) {
switch hookPoint {
case boil.HookBeforeCreate:
{{$varNameSingular}}BeforeCreateHooks = append({{$varNameSingular}}BeforeCreateHooks, {{$varNameSingular}}Hook)

View file

@ -4,24 +4,43 @@
{{- $varNamePlural := .Table.Name | plural | camelCase -}}
{{- $varNameSingular := .Table.Name | singular | camelCase -}}
func {{$varNameSingular}}BeforeCreateHook(o *{{$tableNameSingular}}) error {
*o = {{$tableNameSingular}}{}
return nil
}
func {{$varNameSingular}}AfterCreateHook(o *{{$tableNameSingular}}) error {
*o = {{$tableNameSingular}}{}
return nil
}
func {{$varNameSingular}}BeforeUpdateHook(o *{{$tableNameSingular}}) error {
*o = {{$tableNameSingular}}{}
return nil
}
func {{$varNameSingular}}AfterUpdateHook(o *{{$tableNameSingular}}) error {
*o = {{$tableNameSingular}}{}
return nil
}
func Test{{$tableNamePlural}}Hooks(t *testing.T) {
// var err error
var err error
empty := &{{$tableNameSingular}}{}
o := &{{$tableNameSingular}}{}
if err = boil.RandomizeStruct(o, {{$varNameSingular}}DBTypes, false); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} object: %s", err)
}
{{$tableNameSingular}}AddHook(boil.HookBeforeCreate, {{$varNameSingular}}BeforeCreateHook)
if err = o.doBeforeCreateHooks(); err != nil {
t.Errorf("Unable to execute doBeforeCreateHooks: %s", err)
}
if !reflect.DeepEqual(o, empty) {
t.Errorf("Expected BeforeCreateHook function to empty object, but got: %#v", o)
}
{{$varNameSingular}}BeforeCreateHooks = []{{$tableNameSingular}}Hook{}
{{$varNamePlural}}DeleteAllRows(t)
t.Skip("Hook tests not implemented")
}