summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.go2
-rw-r--r--model/database.go24
-rw-r--r--model/recipe.go23
-rw-r--r--model/recipe_test.go40
-rw-r--r--model/recipes.go15
-rw-r--r--model/recipes_test.go4
-rw-r--r--model/sql/testdata.sql6
7 files changed, 62 insertions, 52 deletions
diff --git a/main.go b/main.go
index 1130ddb..70a1c00 100644
--- a/main.go
+++ b/main.go
@@ -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'))
-;