Added forgotten nutrition provider class. Changed tests names.

This commit is contained in:
Adrian Halko
2021-11-23 17:10:22 +01:00
parent 6939c6400f
commit 246162fb6d
2 changed files with 42 additions and 16 deletions

View File

@@ -326,6 +326,32 @@ class NutritionPlansProvider extends WgerBaseProvider with ChangeNotifier {
return json.decode(utf8.decode(response.bodyBytes))['suggestions'] as List<dynamic>;
}
/// Searches for an ingredient with code
Future<Ingredient?> 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<void> logMealToDiary(Meal meal) async {
for (final item in meal.mealItems) {

View File

@@ -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));