@@ -13,18 +13,26 @@ import '../measurements/measurement_provider_test.mocks.dart';
13
13
void main () {
14
14
late NutritionPlansProvider nutritionProvider;
15
15
late MockWgerBaseProvider mockWgerBaseProvider;
16
+ late IngredientDatabase database;
17
+ late Map <String , dynamic > ingredient59887Response;
18
+
19
+ // Needs to be configured here, setUp runs on every test, setUpAll only once
20
+ setUpAll (() {
21
+ database = IngredientDatabase .inMemory (NativeDatabase .memory ());
22
+ });
16
23
17
24
setUp (() {
18
25
mockWgerBaseProvider = MockWgerBaseProvider ();
19
26
nutritionProvider = NutritionPlansProvider (
20
27
mockWgerBaseProvider,
21
28
[],
22
- database: IngredientDatabase . inMemory ( NativeDatabase . memory ()) ,
29
+ database: database ,
23
30
);
24
31
25
32
const String planInfoUrl = 'nutritionplaninfo' ;
26
33
const String planUrl = 'nutritionplan' ;
27
34
const String diaryUrl = 'nutritiondiary' ;
35
+ const String ingredientInfoUrl = 'ingredientinfo' ;
28
36
29
37
final Map <String , dynamic > nutritionalPlanInfoResponse = jsonDecode (
30
38
fixture ('nutrition/nutritional_plan_info_detail_response.json' ),
@@ -35,7 +43,7 @@ void main() {
35
43
final List <dynamic > nutritionDiaryResponse = jsonDecode (
36
44
fixture ('nutrition/nutrition_diary_response.json' ),
37
45
)['results' ];
38
- final Map < String , dynamic > ingredient59887Response = jsonDecode (
46
+ ingredient59887Response = jsonDecode (
39
47
fixture ('nutrition/ingredientinfo_59887.json' ),
40
48
);
41
49
final Map <String , dynamic > ingredient10065Response = jsonDecode (
@@ -68,9 +76,16 @@ void main() {
68
76
host: 'localhost' ,
69
77
path: 'api/v2/$diaryUrl ' ,
70
78
);
79
+ final Uri ingredientUri = Uri (
80
+ scheme: 'http' ,
81
+ host: 'localhost' ,
82
+ path: 'api/v2/$ingredientInfoUrl ' ,
83
+ );
71
84
when (mockWgerBaseProvider.makeUrl (planInfoUrl, id: anyNamed ('id' ))).thenReturn (planInfoUri);
72
85
when (mockWgerBaseProvider.makeUrl (planUrl, id: anyNamed ('id' ))).thenReturn (planUri);
73
86
when (mockWgerBaseProvider.makeUrl (diaryUrl, query: anyNamed ('query' ))).thenReturn (diaryUri);
87
+ when (mockWgerBaseProvider.makeUrl (ingredientInfoUrl, id: anyNamed ('id' )))
88
+ .thenReturn (ingredientUri);
74
89
when (mockWgerBaseProvider.fetch (planInfoUri)).thenAnswer (
75
90
(realInvocation) => Future .value (nutritionalPlanInfoResponse),
76
91
);
@@ -80,6 +95,9 @@ void main() {
80
95
when (mockWgerBaseProvider.fetchPaginated (diaryUri)).thenAnswer (
81
96
(realInvocation) => Future .value (nutritionDiaryResponse),
82
97
);
98
+ when (mockWgerBaseProvider.fetch (ingredientUri)).thenAnswer (
99
+ (realInvocation) => Future .value (ingredient10065Response),
100
+ );
83
101
});
84
102
85
103
group ('fetchAndSetPlanFull' , () {
@@ -91,4 +109,48 @@ void main() {
91
109
expect (nutritionProvider.items.isEmpty, false );
92
110
});
93
111
});
112
+
113
+ group ('Ingredient cache DB' , () {
114
+ test ('that if there is already valid data in the DB, the API is not hit' , () async {
115
+ // Arrange
116
+ nutritionProvider.ingredients = [];
117
+ await database.into (database.ingredients).insert (
118
+ IngredientsCompanion .insert (
119
+ id: ingredient59887Response['id' ],
120
+ data: json.encode (ingredient59887Response),
121
+ lastFetched: DateTime .now (),
122
+ ),
123
+ );
124
+
125
+ // Act
126
+ await nutritionProvider.fetchIngredient (59887 , database: database);
127
+
128
+ // Assert
129
+ expect (nutritionProvider.ingredients.length, 1 );
130
+ expect (nutritionProvider.ingredients.first.id, 59887 );
131
+ expect (nutritionProvider.ingredients.first.name, 'Baked Beans' );
132
+ verifyNever (mockWgerBaseProvider.fetchPaginated (any));
133
+ });
134
+
135
+ test ('fetching an ingredient not present in the DB, the API is hit' , () async {
136
+ // Arrange
137
+ nutritionProvider.ingredients = [];
138
+ await database.into (database.ingredients).insert (
139
+ IngredientsCompanion .insert (
140
+ id: ingredient59887Response['id' ],
141
+ data: json.encode (ingredient59887Response),
142
+ lastFetched: DateTime .now (),
143
+ ),
144
+ );
145
+
146
+ // Act
147
+ await nutritionProvider.fetchIngredient (10065 , database: database);
148
+
149
+ // Assert
150
+ expect (nutritionProvider.ingredients.length, 1 );
151
+ expect (nutritionProvider.ingredients.first.id, 10065 );
152
+ expect (nutritionProvider.ingredients.first.name, "'Old Times' Orange Fine Cut Marmalade" );
153
+ verify (mockWgerBaseProvider.fetch (any));
154
+ });
155
+ });
94
156
}
0 commit comments