diff options
author | xengineering <me@xengineering.eu> | 2024-03-03 20:06:40 +0100 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2024-03-04 20:33:58 +0100 |
commit | b62c686702622b747ab082b7b12b3e611e1d7e1f (patch) | |
tree | aa0f31ad0381dbeb3247870744d07bc9c81ed31b /model/recipe_test.go | |
parent | 4dcd846aa7c8d15c96ba13380f48b985fb2c992e (diff) | |
download | ceres-b62c686702622b747ab082b7b12b3e611e1d7e1f.tar ceres-b62c686702622b747ab082b7b12b3e611e1d7e1f.tar.zst ceres-b62c686702622b747ab082b7b12b3e611e1d7e1f.zip |
model: Create test data with Go instead of SQL
This allows to formulate the test data with an object-based model which
is easier than writing it down in a relational model.
Diffstat (limited to 'model/recipe_test.go')
-rw-r--r-- | model/recipe_test.go | 40 |
1 files changed, 5 insertions, 35 deletions
diff --git a/model/recipe_test.go b/model/recipe_test.go index 02b3ead..0057a3d 100644 --- a/model/recipe_test.go +++ b/model/recipe_test.go @@ -1,35 +1,10 @@ package model import ( - "encoding/json" - "testing" "reflect" + "testing" ) -var recipes = [...]string { - `{"id":1,"title":"My recipe","portions":4,"url":"https://example.org","notes":"Sooo delicious","created":1707591792,"last_changed":1707591799}`, - `{"id":2,"title":"My nice recipe","portions":2,"url":"http://example.org","notes":"Sooooo delicious","created":1707591800,"last_changed":1707591900}`, -} - -func TestRecipeJson(t *testing.T) { - for _, v := range recipes { - var r Recipe - err := json.Unmarshal([]byte(v), &r) - if err != nil { - t.Fatal(err) - } - var encoded []byte - encoded, err = json.Marshal(&r) - if err != nil { - t.Fatal(err) - } - if string(encoded) != v { - t.Fatalf("Encoded JSON '%s' does not match original '%s'", - string(encoded), v) - } - } -} - func TestRecipeCrud(t *testing.T) { InitStorage() defer RemoveStorage() @@ -39,12 +14,11 @@ func TestRecipeCrud(t *testing.T) { var original, readback, update, updated, deleted Recipe - err := json.Unmarshal([]byte(recipes[0]), &original) - if err != nil { - t.Fatalf("Failed to unmarshal test recipe: %v\n", err) - } + recipes := RecipeTestData() + original = recipes[0] + update = recipes[1] - err = original.Create() + err := original.Create() if err != nil { t.Fatalf("Failed to create test recipe in DB: %v\n", err) } @@ -59,10 +33,6 @@ func TestRecipeCrud(t *testing.T) { t.Fatalf("Recipes did not match after create / read cycle") } - err = json.Unmarshal([]byte(recipes[1]), &update) - if err != nil { - t.Fatalf("Failed to unmarshal test recipe: %v\n", err) - } update.Id = original.Id err = update.Update() |