@@ -5,12 +5,14 @@ import 'package:flutter_test/flutter_test.dart';
5
5
import 'package:mockito/mockito.dart' ;
6
6
import 'package:wger/database/ingredients/ingredients_database.dart' ;
7
7
import 'package:wger/models/nutrition/ingredient.dart' ;
8
+ import 'package:wger/models/nutrition/nutritional_plan.dart' ;
8
9
import 'package:wger/providers/nutrition.dart' ;
9
10
10
11
import '../fixtures/fixture_reader.dart' ;
11
12
import '../measurements/measurement_provider_test.mocks.dart' ;
12
13
13
14
void main () {
15
+ final now = DateTime .now ();
14
16
late NutritionPlansProvider nutritionProvider;
15
17
late MockWgerBaseProvider mockWgerBaseProvider;
16
18
late IngredientDatabase database;
@@ -110,6 +112,108 @@ void main() {
110
112
});
111
113
});
112
114
115
+ group ('currentPlan' , () {
116
+ test ('gibt den aktiven Plan zurück, wenn nur einer aktiv ist' , () {
117
+ final plan = NutritionalPlan (
118
+ id: 1 ,
119
+ description: 'Aktiver Plan' ,
120
+ startDate: now.subtract (const Duration (days: 1 )),
121
+ endDate: now.add (const Duration (days: 1 )),
122
+ creationDate: now.subtract (const Duration (days: 2 )),
123
+ );
124
+ nutritionProvider = NutritionPlansProvider (mockWgerBaseProvider, [plan], database: database);
125
+ expect (nutritionProvider.currentPlan, equals (plan));
126
+ });
127
+
128
+ test ('gibt den neuesten aktiven Plan zurück, wenn mehrere aktiv sind' , () {
129
+ final olderPlan = NutritionalPlan (
130
+ id: 1 ,
131
+ description: 'Älterer aktiver Plan' ,
132
+ startDate: now.subtract (const Duration (days: 10 )),
133
+ endDate: now.add (const Duration (days: 10 )),
134
+ creationDate: now.subtract (const Duration (days: 10 )),
135
+ );
136
+ final newerPlan = NutritionalPlan (
137
+ id: 2 ,
138
+ description: 'Neuerer aktiver Plan' ,
139
+ startDate: now.subtract (const Duration (days: 5 )),
140
+ endDate: now.add (const Duration (days: 5 )),
141
+ creationDate: now.subtract (const Duration (days: 2 )),
142
+ );
143
+ nutritionProvider =
144
+ NutritionPlansProvider (mockWgerBaseProvider, [olderPlan, newerPlan], database: database);
145
+ expect (nutritionProvider.currentPlan, equals (newerPlan));
146
+ });
147
+ });
148
+
149
+ group ('currentPlan correctly returns the active plan' , () {
150
+ test ('no plans available -> null' , () {
151
+ nutritionProvider = NutritionPlansProvider (mockWgerBaseProvider, [], database: database);
152
+ expect (nutritionProvider.currentPlan, isNull);
153
+ });
154
+
155
+ test ('no active plan -> null' , () {
156
+ final plans = [
157
+ NutritionalPlan (
158
+ id: 1 ,
159
+ description: 'plan 1' ,
160
+ startDate: now.subtract (const Duration (days: 30 )),
161
+ endDate: now.subtract (const Duration (days: 5 )),
162
+ ),
163
+ NutritionalPlan (
164
+ id: 2 ,
165
+ description: 'plan 2' ,
166
+ startDate: now.add (const Duration (days: 100 )),
167
+ endDate: now.add (const Duration (days: 50 )),
168
+ ),
169
+ ];
170
+ nutritionProvider = NutritionPlansProvider (mockWgerBaseProvider, plans, database: database);
171
+ expect (nutritionProvider.currentPlan, isNull);
172
+ });
173
+
174
+ test ('active plan exists -> return it' , () {
175
+ final plan = NutritionalPlan (
176
+ description: 'Active plan' ,
177
+ startDate: now.subtract (const Duration (days: 10 )),
178
+ endDate: now.add (const Duration (days: 10 )),
179
+ );
180
+ nutritionProvider = NutritionPlansProvider (mockWgerBaseProvider, [plan], database: database);
181
+ expect (nutritionProvider.currentPlan, equals (plan));
182
+ });
183
+
184
+ test ('inactive plans are ignored' , () {
185
+ final inactivePlan = NutritionalPlan (
186
+ description: 'Inactive plan' ,
187
+ startDate: now.subtract (const Duration (days: 10 )),
188
+ endDate: now.add (const Duration (days: 5 )),
189
+ );
190
+ final plan = NutritionalPlan (
191
+ description: 'Active plan' ,
192
+ startDate: now.subtract (const Duration (days: 10 )),
193
+ endDate: now.add (const Duration (days: 10 )),
194
+ );
195
+ nutritionProvider =
196
+ NutritionPlansProvider (mockWgerBaseProvider, [plan, inactivePlan], database: database);
197
+ expect (nutritionProvider.currentPlan, equals (plan));
198
+ });
199
+
200
+ test ('several active plans exists -> return newest' , () {
201
+ final olderPlan = NutritionalPlan (
202
+ description: 'Old active plan' ,
203
+ startDate: now.subtract (const Duration (days: 10 )),
204
+ endDate: now.add (const Duration (days: 10 )),
205
+ );
206
+ final newerPlan = NutritionalPlan (
207
+ description: 'Newer active plan' ,
208
+ startDate: now.subtract (const Duration (days: 5 )),
209
+ endDate: now.add (const Duration (days: 5 )),
210
+ );
211
+ nutritionProvider =
212
+ NutritionPlansProvider (mockWgerBaseProvider, [olderPlan, newerPlan], database: database);
213
+ expect (nutritionProvider.currentPlan, equals (newerPlan));
214
+ });
215
+ });
216
+
113
217
group ('Ingredient cache DB' , () {
114
218
test ('that if there is already valid data in the DB, the API is not hit' , () async {
115
219
// Arrange
0 commit comments