-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathplanner_test.go
197 lines (164 loc) · 6.94 KB
/
planner_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package planner_test
import (
"context"
"errors"
"github.com/quii/go-fakes-and-contracts/adapters/driven/persistence/inmemory"
"github.com/quii/go-fakes-and-contracts/adapters/driven/persistence/sqlite"
"github.com/quii/go-fakes-and-contracts/domain/ingredients"
"github.com/quii/go-fakes-and-contracts/domain/planner"
"github.com/quii/go-fakes-and-contracts/domain/planner/internal/expect"
"github.com/quii/go-fakes-and-contracts/domain/planner/internal/plannertest"
"testing"
"time"
)
func TestRecipePlanner(t *testing.T) {
// for local, snappy integration test with a fake (which we can be confident is correct due to it conforming to the store contract)
t.Run("with in memory store", func(t *testing.T) {
RecipePlannerTest{
CreateDependencies: func() (planner.RecipeBook, planner.Pantry, Cleanup) {
return inmemory.NewRecipeStore(), inmemory.NewPantry(), func() {
// nothing to clean up
}
},
}.Test(t)
})
// we can run a broader integration test with a "real" db if we wish, using this contract approach
t.Run("with sqlite", func(t *testing.T) {
if !testing.Short() {
RecipePlannerTest{
CreateDependencies: func() (planner.RecipeBook, planner.Pantry, Cleanup) {
client := sqlite.NewSQLiteClient()
return sqlite.NewRecipeStore(client), sqlite.NewPantry(client), func() {
if err := client.Close(); err != nil {
t.Error(err)
}
}
},
}.Test(t)
}
})
}
type Cleanup func()
type RecipePlannerTest struct {
CreateDependencies func() (planner.RecipeBook, planner.Pantry, Cleanup)
}
func (r RecipePlannerTest) Test(t *testing.T) {
t.Run("planning meals", func(t *testing.T) {
t.Run("happy path, have ingredients for a recipe, schedule it, update pantry", func(t *testing.T) {
var (
ctx = context.Background()
lasagna = plannertest.RandomRecipe()
recipeBook, pantry, teardown = r.CreateDependencies()
sut = planner.New(recipeBook, pantry)
)
t.Cleanup(teardown)
expect.NoErr(t, recipeBook.AddRecipes(ctx, lasagna))
expect.NoErr(t, pantry.Store(ctx, lasagna.Ingredients...))
expect.NoErr(t, sut.ScheduleMeal(ctx, lasagna, time.Now()))
remainingIngredients, err := pantry.GetIngredients(ctx)
expect.NoErr(t, err)
expect.Equal(t, 0, len(remainingIngredients))
})
t.Run("returns a missing ingredients error if you try to schedule a meal without all the ingredients", func(t *testing.T) {
var (
ctx = context.Background()
lasagna = plannertest.RandomRecipe()
recipeBook, store, teardown = r.CreateDependencies()
sut = planner.New(recipeBook, store)
)
t.Cleanup(teardown)
expect.NoErr(t, recipeBook.AddRecipes(ctx, lasagna))
err := sut.ScheduleMeal(ctx, lasagna, time.Now())
expect.Err(t, err)
missingIngredientsErr, ok := err.(planner.ErrorMissingIngredients)
expect.True(t, ok)
expect.DeepEqual(t, planner.ErrorMissingIngredients{
MissingIngredients: lasagna.Ingredients,
}, missingIngredientsErr)
})
t.Run("when recipeBook fails to get ingredients, we get an error", func(t *testing.T) {
var (
ctx = context.Background()
lasagna = plannertest.RandomRecipe()
recipeBook, pantry, teardown = r.CreateDependencies()
failingPantry = planner.NewPantryDelegate(pantry)
)
t.Cleanup(teardown)
failingPantry.GetIngredientsFunc = func(ctx context.Context) (ingredients.Ingredients, error) {
return nil, errors.New("oh no")
}
sut := planner.New(recipeBook, failingPantry)
expect.NoErr(t, recipeBook.AddRecipes(ctx, lasagna))
expect.NoErr(t, pantry.Store(ctx, lasagna.Ingredients...))
err := sut.ScheduleMeal(ctx, lasagna, time.Now())
expect.Err(t, err)
})
t.Run("returns the specific ingredients missing if you try to schedule a meal with some missing ingredients", func(t *testing.T) {
var (
ctx = context.Background()
recipeBook, pantry, teardown = r.CreateDependencies()
lasagna = plannertest.RandomRecipe()
sut = planner.New(recipeBook, pantry)
)
t.Cleanup(teardown)
expect.NoErr(t, recipeBook.AddRecipes(ctx, lasagna))
missingIngredient, ingredientsWeHave := lasagna.Ingredients[0], lasagna.Ingredients[1:]
expect.NoErr(t, pantry.Store(ctx, ingredientsWeHave...))
err := sut.ScheduleMeal(ctx, lasagna, time.Now())
expect.Err(t, err)
missingIngredientsErr, ok := err.(planner.ErrorMissingIngredients)
expect.True(t, ok)
expect.DeepEqual(t, planner.ErrorMissingIngredients{
MissingIngredients: []ingredients.Ingredient{missingIngredient},
}, missingIngredientsErr)
})
})
t.Run("suggesting recipes", func(t *testing.T) {
t.Run("if don't have the ingredients for a meal, we cant make it", func(t *testing.T) {
var (
ctx = context.Background()
pie = plannertest.RandomRecipe()
recipeBook, pantry, teardown = r.CreateDependencies()
sut = planner.New(recipeBook, pantry)
)
t.Cleanup(teardown)
expect.NoErr(t, recipeBook.AddRecipes(ctx, pie))
recipes, err := sut.SuggestRecipes(ctx)
expect.NoErr(t, err)
plannertest.AssertDoesntHaveRecipe(t, recipes, pie)
})
t.Run("if we have the ingredients for a recipe we can make it", func(t *testing.T) {
var (
ctx = context.Background()
bananaBread = plannertest.RandomRecipe()
aRecipeWeWontHaveIngredientsFor = plannertest.RandomRecipe()
recipeBook, pantry, teardown = r.CreateDependencies()
sut = planner.New(recipeBook, pantry)
)
t.Cleanup(teardown)
expect.NoErr(t, recipeBook.AddRecipes(ctx, bananaBread, aRecipeWeWontHaveIngredientsFor))
expect.NoErr(t, pantry.Store(ctx, bananaBread.Ingredients...))
recipes, err := sut.SuggestRecipes(ctx)
expect.NoErr(t, err)
plannertest.AssertHasRecipe(t, recipes, bananaBread)
plannertest.AssertDoesntHaveRecipe(t, recipes, aRecipeWeWontHaveIngredientsFor)
})
t.Run("if we have ingredients for 2 recipes, we can make both", func(t *testing.T) {
var (
ctx = context.Background()
bananaBread = plannertest.RandomRecipe()
bananaMilkshake = plannertest.RandomRecipe()
recipeBook, store, teardown = r.CreateDependencies()
sut = planner.New(recipeBook, store)
)
t.Cleanup(teardown)
expect.NoErr(t, recipeBook.AddRecipes(ctx, bananaBread, bananaMilkshake))
expect.NoErr(t, store.Store(ctx, bananaBread.Ingredients...))
expect.NoErr(t, store.Store(ctx, bananaMilkshake.Ingredients...))
recipes, err := sut.SuggestRecipes(ctx)
expect.NoErr(t, err)
plannertest.AssertHasRecipe(t, recipes, bananaBread)
plannertest.AssertHasRecipe(t, recipes, bananaMilkshake)
})
})
}