From 246162fb6df4f25e78f28d17053d58440bb51cf6 Mon Sep 17 00:00:00 2001 From: Adrian Halko Date: Tue, 23 Nov 2021 17:10:22 +0100 Subject: [PATCH] Added forgotten nutrition provider class. Changed tests names. --- lib/providers/nutrition.dart | 26 +++++++++++++++ .../nutritional_meal_item_form_test.dart | 32 +++++++++---------- 2 files changed, 42 insertions(+), 16 deletions(-) diff --git a/lib/providers/nutrition.dart b/lib/providers/nutrition.dart index 5fcc9d62..b9043c6d 100644 --- a/lib/providers/nutrition.dart +++ b/lib/providers/nutrition.dart @@ -326,6 +326,32 @@ class NutritionPlansProvider extends WgerBaseProvider with ChangeNotifier { return json.decode(utf8.decode(response.bodyBytes))['suggestions'] as List; } + /// Searches for an ingredient with code + Future searchIngredientWithCode(String code) async { + + if (code.isEmpty ) { + return null; + } + + Ingredient ingredient; + + // Send the request + final data = await fetch( + makeUrl( + _ingredientPath, + query: {'code': code}, + ), + ); + + if(data['count'] != 0) { + ingredient = Ingredient.fromJson(data['results'][0]); + return ingredient; + }else{ + return null; + } + + } + /// Log meal to nutrition diary Future logMealToDiary(Meal meal) async { for (final item in meal.mealItems) { diff --git a/test/nutrition/nutritional_meal_item_form_test.dart b/test/nutrition/nutritional_meal_item_form_test.dart index 4a3334d9..d1663142 100644 --- a/test/nutrition/nutritional_meal_item_form_test.dart +++ b/test/nutrition/nutritional_meal_item_form_test.dart @@ -113,7 +113,7 @@ void main() { group('Test the AlertDialogs for scanning result', () { - testWidgets('empty code',(WidgetTester tester) async { + testWidgets('with empty code',(WidgetTester tester) async { await tester.pumpWidget(createMealItemFormScreen(meal1, '', true)); await tester.tap(find.byKey(const Key('scan-button'))); @@ -123,7 +123,7 @@ void main() { }); - testWidgets('right code',(WidgetTester tester) async { + testWidgets('with correct code',(WidgetTester tester) async { await tester.pumpWidget(createMealItemFormScreen(meal1, '123', true)); await tester.tap(find.byKey(const Key('scan-button'))); @@ -133,7 +133,7 @@ void main() { }); - testWidgets('wrong code',(WidgetTester tester) async { + testWidgets('with incorrect code',(WidgetTester tester) async { await tester.pumpWidget(createMealItemFormScreen(meal1, '222', true)); await tester.tap(find.byKey(const Key('scan-button'))); @@ -144,25 +144,25 @@ void main() { }); - group('searchIngredientWithCode()', () { - test('Test with correct code', () async { + group('Test searchIngredientWithCode() function', () { + test('with correct code', () async { final Ingredient? ingredient = await mockNutritionWithClient.searchIngredientWithCode('123'); expect(ingredient!.id, 9436); }); - test('Test with incorrect code', () async { + test('with incorrect code', () async { final Ingredient? ingredient = await mockNutritionWithClient.searchIngredientWithCode('222'); expect(ingredient, null); }); - test('Test with empty code', () async { + test('with empty code', () async { final Ingredient? ingredient = await mockNutritionWithClient.searchIngredientWithCode(''); expect(ingredient, null); }); }); - group('Weight formfield tests', (){ - testWidgets('adding empty weight', (WidgetTester tester) async { + group('Test weight formfield', (){ + testWidgets('add empty weight', (WidgetTester tester) async { await tester.pumpWidget(createMealItemFormScreen(meal1, '123', true)); await tester.enterText(find.byKey(const Key('field-weight')), ''); @@ -175,7 +175,7 @@ void main() { }); - testWidgets('adding correct weight type', (WidgetTester tester) async { + testWidgets('add correct weight type', (WidgetTester tester) async { await tester.pumpWidget(createMealItemFormScreen(meal1, '123', true)); await tester.enterText(find.byKey(const Key('field-weight')), '2'); @@ -187,7 +187,7 @@ void main() { expect(find.text('Please enter a valid number'), findsNothing); }); - testWidgets('adding incorrect weight type', (WidgetTester tester) async { + testWidgets('add incorrect weight type', (WidgetTester tester) async { await tester.pumpWidget(createMealItemFormScreen(meal1, '123', true)); await tester.enterText(find.byKey(const Key('field-weight')), 'test'); @@ -201,7 +201,7 @@ void main() { }); }); - group('Found ingredient dialog', (){ + group('Test ingredient found dialog', (){ testWidgets('confirm found ingredient dialog', (WidgetTester tester) async { await tester.pumpWidget(createMealItemFormScreen(meal1, '123', true)); @@ -238,7 +238,7 @@ void main() { group('Test the adding a new item to meal', (){ - testWidgets('saving empty ingredient', (WidgetTester tester) async { + testWidgets('save empty ingredient', (WidgetTester tester) async { await tester.pumpWidget(createMealItemFormScreen(meal1, '123', true)); await tester.tap(find.byKey(const Key(SUBMIT_BUTTON_KEY_NAME))); @@ -250,7 +250,7 @@ void main() { }); - testWidgets('saving ingredient without weight', (WidgetTester tester) async { + testWidgets('save ingredient without weight', (WidgetTester tester) async { await tester.pumpWidget(createMealItemFormScreen(meal1, '123', true)); await tester.tap(find.byKey(const Key('scan-button'))); @@ -268,7 +268,7 @@ void main() { }); - testWidgets('saving ingredient with incorrect weight input type', (WidgetTester tester) async { + testWidgets('save ingredient with incorrect weight input type', (WidgetTester tester) async { await tester.pumpWidget(createMealItemFormScreen(meal1, '123', true)); await tester.tap(find.byKey(const Key('scan-button'))); @@ -286,7 +286,7 @@ void main() { }); - testWidgets('saving complete ingredient with correct weight input type', (WidgetTester tester) async { + testWidgets('save complete ingredient with correct weight input type', (WidgetTester tester) async { await tester.pumpWidget(createMealItemFormScreen(meal1, '123', true)); final MealItemForm formScreen = tester.widget(find.byType(MealItemForm));