diff options
Diffstat (limited to 'model/recipe_test.go')
-rw-r--r-- | model/recipe_test.go | 129 |
1 files changed, 62 insertions, 67 deletions
diff --git a/model/recipe_test.go b/model/recipe_test.go index 7b6c14c..74ccfa2 100644 --- a/model/recipe_test.go +++ b/model/recipe_test.go @@ -1,11 +1,12 @@ package model import ( + "database/sql" + "fmt" + "os" + "path/filepath" "reflect" "testing" - "path/filepath" - "os" - "fmt" ) func TestRecipeCrud(t *testing.T) { @@ -25,68 +26,62 @@ func TestRecipeCrud(t *testing.T) { InitDatabase(filepath.Join(storage.Path, "ceres.sqlite3")) defer CloseDatabase() - tx, err := NewTx() - if err != nil { - t.Fatalf("Failed to inject test recipes: %v\n", err) - } - - var original, readback, update, updated, deleted Recipe - - recipes := RecipeTestData() - original = recipes[0] - update = recipes[1] - - err = original.Create(tx) - if err != nil { - t.Fatalf("Failed to create test recipe in DB: %v\n", err) - } - - readback.Id = original.Id - err = readback.Read(tx) - if err != nil { - t.Fatalf("Failed to read test recipe from DB: %v\n", err) - } - - if !reflect.DeepEqual(original, readback) { - t.Fatalf("Recipes did not match after create / read cycle:\n"+ - "Before: %s\nAfter: %s\n", original, readback) - } - - update.Id = original.Id - - err = update.Update(tx) - if err != nil { - t.Fatalf("Failed to update recipe: %v\n", err) - } - - updated.Id = original.Id - err = updated.Read(tx) - if err != nil { - t.Fatalf("Failed to read back updated recipe: %v\n", err) - } - - if !reflect.DeepEqual(update, updated) { - t.Fatalf("Recipes did not match after update / read cycle:\n"+ - "Update: %s\nUpdated: %s\n", update, updated) - } - - if reflect.DeepEqual(updated, original) { - t.Fatalf("Updated and original recipe match") - } - - err = updated.Delete(tx) - if err != nil { - t.Fatalf("Failed to delete updated recipe: %v\n", err) - } - - deleted.Id = updated.Id - err = deleted.Read(tx) - if err == nil { - t.Fatalf("Was able to read back deleted recipe") - } - - err = tx.Commit() - if err != nil { - t.Fatalf("Unable to commit test transaction") - } + Transaction(func(tx *sql.Tx) error { + var original, readback, update, updated, deleted Recipe + + recipes := RecipeTestData() + original = recipes[0] + update = recipes[1] + + err = original.Create(tx) + if err != nil { + t.Fatalf("Failed to create test recipe in DB: %v\n", err) + } + + readback.Id = original.Id + err = readback.Read(tx) + if err != nil { + t.Fatalf("Failed to read test recipe from DB: %v\n", err) + } + + if !reflect.DeepEqual(original, readback) { + t.Fatalf("Recipes did not match after create / read cycle:\n"+ + "Before: %s\nAfter: %s\n", original, readback) + } + + update.Id = original.Id + + err = update.Update(tx) + if err != nil { + t.Fatalf("Failed to update recipe: %v\n", err) + } + + updated.Id = original.Id + err = updated.Read(tx) + if err != nil { + t.Fatalf("Failed to read back updated recipe: %v\n", err) + } + + if !reflect.DeepEqual(update, updated) { + t.Fatalf("Recipes did not match after update / read cycle:\n"+ + "Update: %s\nUpdated: %s\n", update, updated) + } + + if reflect.DeepEqual(updated, original) { + t.Fatalf("Updated and original recipe match") + } + + err = updated.Delete(tx) + if err != nil { + t.Fatalf("Failed to delete updated recipe: %v\n", err) + } + + deleted.Id = updated.Id + err = deleted.Read(tx) + if err == nil { + t.Fatalf("Was able to read back deleted recipe") + } + + return nil + }) } |