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

View file

@ -4,24 +4,43 @@
{{- $varNamePlural := .Table.Name | plural | camelCase -}} {{- $varNamePlural := .Table.Name | plural | camelCase -}}
{{- $varNameSingular := .Table.Name | singular | camelCase -}} {{- $varNameSingular := .Table.Name | singular | camelCase -}}
func {{$varNameSingular}}BeforeCreateHook(o *{{$tableNameSingular}}) error { func {{$varNameSingular}}BeforeCreateHook(o *{{$tableNameSingular}}) error {
*o = {{$tableNameSingular}}{}
return nil return nil
} }
func {{$varNameSingular}}AfterCreateHook(o *{{$tableNameSingular}}) error { func {{$varNameSingular}}AfterCreateHook(o *{{$tableNameSingular}}) error {
*o = {{$tableNameSingular}}{}
return nil return nil
} }
func {{$varNameSingular}}BeforeUpdateHook(o *{{$tableNameSingular}}) error { func {{$varNameSingular}}BeforeUpdateHook(o *{{$tableNameSingular}}) error {
*o = {{$tableNameSingular}}{}
return nil return nil
} }
func {{$varNameSingular}}AfterUpdateHook(o *{{$tableNameSingular}}) error { func {{$varNameSingular}}AfterUpdateHook(o *{{$tableNameSingular}}) error {
*o = {{$tableNameSingular}}{}
return nil return nil
} }
func Test{{$tableNamePlural}}Hooks(t *testing.T) { 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) {{$varNamePlural}}DeleteAllRows(t)
t.Skip("Hook tests not implemented")
} }

View file

@ -18,7 +18,7 @@ func TestMain(m *testing.M) {
// Set DebugMode so we can see generated sql statements // Set DebugMode so we can see generated sql statements
boil.DebugMode = *flagDebugMode boil.DebugMode = *flagDebugMode
var err error var err error
if err = setup(); err != nil { if err = setup(); err != nil {
fmt.Println("Unable to execute setup:", err) fmt.Println("Unable to execute setup:", err)