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 | |
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.
-rw-r--r-- | main.go | 2 | ||||
-rw-r--r-- | model/database.go | 24 | ||||
-rw-r--r-- | model/recipe.go | 23 | ||||
-rw-r--r-- | model/recipe_test.go | 40 | ||||
-rw-r--r-- | model/recipes.go | 15 | ||||
-rw-r--r-- | model/recipes_test.go | 4 | ||||
-rw-r--r-- | model/sql/testdata.sql | 6 |
7 files changed, 62 insertions, 52 deletions
@@ -23,6 +23,8 @@ func main() { model.InitDatabase() defer model.CloseDatabase() + model.InjectTestRecipes() + view.Init() var srv *http.Server = startServer("127.0.0.1:8080") diff --git a/model/database.go b/model/database.go index fac4d35..265b438 100644 --- a/model/database.go +++ b/model/database.go @@ -38,15 +38,21 @@ func InitDatabase() { log.Fatal(err) } - for _, v := range []string{`migrate`, `testdata`} { - query, err := GetSql(v) - if err != nil { - log.Fatal(err) - } - _, err = db.Exec(query) - if err != nil { - log.Fatal(err) - } + query, err := GetSql(`migrate`) + if err != nil { + log.Fatal(err) + } + _, err = db.Exec(query) + if err != nil { + log.Fatal(err) + } +} + +func InjectTestRecipes() { + recipes := RecipeTestData() + + for _, recipe := range recipes { + recipe.Create() } } diff --git a/model/recipe.go b/model/recipe.go index c9ef6fb..ce16ee4 100644 --- a/model/recipe.go +++ b/model/recipe.go @@ -126,3 +126,26 @@ func (r *Recipe) Delete() error { return nil } + +func RecipeTestData() []Recipe { + return []Recipe{ + { + Id: "1", + Title: "Pancakes", + Portions: "4", + Url: "https://example.org", + Notes: "Very fluffy", + Created: "", + LastChanged: "", + }, + { + Id: "2", + Title: "Burger", + Portions: "2", + Url: "https://xengineering.eu/git/ceres", + Notes: "Delicious!", + Created: "", + LastChanged: "", + }, + } +} 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() diff --git a/model/recipes.go b/model/recipes.go index bdd1cc1..7604fe2 100644 --- a/model/recipes.go +++ b/model/recipes.go @@ -5,7 +5,7 @@ import ( ) type RecipesElement struct { - Id int64 + Id int64 // TODO change to string Title string } @@ -35,3 +35,16 @@ func (r *Recipes) Read() error { return nil } + +func RecipesTestData() Recipes { + return []RecipesElement{ + { + Id: 1, + Title: "Pancakes", + }, + { + Id: 2, + Title: "Burger", + }, + } +} diff --git a/model/recipes_test.go b/model/recipes_test.go index 1d9eda1..ac5d870 100644 --- a/model/recipes_test.go +++ b/model/recipes_test.go @@ -11,6 +11,8 @@ func TestRecipesRead(t *testing.T) { InitDatabase() defer CloseDatabase() + InjectTestRecipes() + r := make(Recipes, 0) err := r.Read() @@ -18,7 +20,7 @@ func TestRecipesRead(t *testing.T) { t.Fatalf("Failed to read Recipes: %v\n", err) } - if len(r) != 2 { // TODO this needs to be aligned with model/sql/testdata.sql + if len(r) != 2 { // TODO this needs to be aligned with model/testrecipes.go t.Fatalf("Expected a list of one recipe but got %d", len(r)) } } diff --git a/model/sql/testdata.sql b/model/sql/testdata.sql deleted file mode 100644 index e47bde9..0000000 --- a/model/sql/testdata.sql +++ /dev/null @@ -1,6 +0,0 @@ -INSERT INTO 'recipes' - (id, title, portions, url, notes, created, last_changed) -VALUES - (1, 'Pancakes', 4, 'https://example.org', 'Very fluffy', strftime('%s', 'now'), strftime('%s', 'now')), - (2, 'Burger', 2, 'https://xengineering.eu/git/ceres', 'Delicious!', strftime('%s', 'now'), strftime('%s', 'now')) -; |