diff options
author | xengineering <me@xengineering.eu> | 2024-04-06 13:12:17 +0200 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2024-04-06 13:12:17 +0200 |
commit | f65b11a5b3011f370df5b4d32239225f3708ecd5 (patch) | |
tree | 360261244c95e80cf2a56160527f5be7c6f5e2d7 /model/recipe_test.go | |
parent | 9985d6f86fcee9d3c1d1148a5977190b673a8e8e (diff) | |
download | ceres-f65b11a5b3011f370df5b4d32239225f3708ecd5.tar ceres-f65b11a5b3011f370df5b4d32239225f3708ecd5.tar.zst ceres-f65b11a5b3011f370df5b4d32239225f3708ecd5.zip |
model: Always pass *sql.Tx to CRUD methods
When nesting objects like steps into other objects like recipes it is
required to pass a *sql.Tx value to the CRUD methods of the inner
object to be able to roll back the whole transaction.
The top level object used to be responsible for the creation of this
*sql.Tx inside its CRUD methods.
This is now moved to the caller of the CRUD methods (here the HTTP
handler function). The advantage is that all CRUD methods now accept a
*sql.Tx as only argument which makes those methods more consistent.
Diffstat (limited to 'model/recipe_test.go')
-rw-r--r-- | model/recipe_test.go | 22 |
1 files changed, 16 insertions, 6 deletions
diff --git a/model/recipe_test.go b/model/recipe_test.go index 55041a6..b9a44e1 100644 --- a/model/recipe_test.go +++ b/model/recipe_test.go @@ -12,19 +12,24 @@ func TestRecipeCrud(t *testing.T) { InitDatabase() 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() + 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() + err = readback.Read(tx) if err != nil { t.Fatalf("Failed to create test recipe in DB: %v\n", err) } @@ -36,13 +41,13 @@ func TestRecipeCrud(t *testing.T) { update.Id = original.Id - err = update.Update() + err = update.Update(tx) if err != nil { t.Fatalf("Failed to update recipe: %v\n", err) } updated.Id = original.Id - err = updated.Read() + err = updated.Read(tx) if err != nil { t.Fatalf("Failed to read back updated recipe: %v\n", err) } @@ -56,14 +61,19 @@ func TestRecipeCrud(t *testing.T) { t.Fatalf("Updated and original recipe match") } - err = updated.Delete() + err = updated.Delete(tx) if err != nil { t.Fatalf("Failed to delete updated recipe: %v\n", err) } deleted.Id = updated.Id - err = deleted.Read() + 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") + } } |