Skip to content

Commit

Permalink
fix: failing nutriment persize serving test (#873)
Browse files Browse the repository at this point in the history
* Adding failing test

* Change Serving size to a safe number for all iterations of the product save loop.

* Fix for sending "PerSize.serving" nutrients

* Consolidated suffix stripping code for nutrient serialization

* consolidated tests

* Update lib/src/open_food_api_client.dart

- import 'package:openfoodfacts/openfoodfacts.dart';
+ import 'model/per_size.dart';

Co-authored-by: monsieurtanuki <[email protected]>

* Update test/api_save_product_test.dart

Co-authored-by: monsieurtanuki <[email protected]>

* Update test/api_save_product_test.dart

Co-authored-by: monsieurtanuki <[email protected]>

* Removed missed "PerSize.serving" test

---------

Co-authored-by: monsieurtanuki <[email protected]>
  • Loading branch information
davidpryor and monsieurtanuki authored Jan 31, 2024
1 parent a353a53 commit 31f9fab
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 60 deletions.
9 changes: 6 additions & 3 deletions lib/src/open_food_api_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:async';
import 'dart:convert';

import 'package:http/http.dart';
import 'model/per_size.dart';

import 'interface/json_object.dart';
import 'model/login_status.dart';
Expand Down Expand Up @@ -105,9 +106,11 @@ class OpenFoodAPIClient {
final Map<String, String> rawNutrients = product.nutriments!.toData();
for (final MapEntry<String, String> entry in rawNutrients.entries) {
String key = 'nutriment_${entry.key}';
final int pos = key.indexOf('_100g');
if (pos != -1) {
key = key.substring(0, pos);
for (final option in PerSize.values) {
final int pos = key.indexOf('_${option.offTag}');
if (pos != -1) {
key = key.substring(0, pos);
}
}
parameterMap[key] = entry.value;
}
Expand Down
127 changes: 70 additions & 57 deletions test/api_save_product_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -329,70 +329,83 @@ Like that:
const double PROTEINS = 6;
const double FAT = 0.1;
const double VITAMIN_B12 = 0.15;
final List<Nutrient> nutrients = <Nutrient>[
Nutrient.energyKJ,
Nutrient.carbohydrates,
Nutrient.proteins,
Nutrient.fat,
Nutrient.vitaminB12,
];

const String BARCODE = '7340011364184';
const String PRODUCT_NAME = 'Chili beans';
final String nutrimentDataPer = PerSize.oneHundredGrams.offTag;

const PerSize perSize = PerSize.oneHundredGrams;
for (int i = 2; i >= 0; i--) {
final Nutriments nutriments = Nutriments.empty()
..setValue(Nutrient.energyKJ, perSize, ENERGY + i)
..setValue(Nutrient.carbohydrates, perSize, CARBOHYDRATES + i)
..setValue(Nutrient.proteins, perSize, PROTEINS + i)
..setValue(Nutrient.vitaminB12, perSize, VITAMIN_B12 + i)
..setValue(Nutrient.fat, perSize, FAT + i);

final Product newProduct = Product(
barcode: BARCODE,
productName: PRODUCT_NAME,
nutrimentDataPer: nutrimentDataPer,
nutriments: nutriments,
);

final Status status = await OpenFoodAPIClient.saveProduct(
USER,
newProduct,
uriHelper: uriHelper,
);

expect(status.status, 1);
expect(status.statusVerbose, 'fields saved');

final ProductResultV3 result = await OpenFoodAPIClient.getProductV3(
ProductQueryConfiguration(
BARCODE,
language: OpenFoodFactsLanguage.ENGLISH,
version: ProductQueryVersion.v3,
),
user: USER,
uriHelper: uriHelper,
);
for (final PerSize perSize in PerSize.values) {
final String nutrimentDataPer = perSize.offTag;
for (int i = 0; i <= 2; i++) {
final Nutriments nutriments = Nutriments.empty()
..setValue(Nutrient.energyKJ, perSize, ENERGY + i)
..setValue(Nutrient.carbohydrates, perSize, CARBOHYDRATES + i)
..setValue(Nutrient.proteins, perSize, PROTEINS + i)
..setValue(Nutrient.vitaminB12, perSize, VITAMIN_B12 + i)
..setValue(Nutrient.fat, perSize, FAT + i);

final Product newProduct = Product(
barcode: BARCODE,
productName: PRODUCT_NAME,
nutrimentDataPer: nutrimentDataPer,
servingSize: "30g",
nutriments: nutriments,
);

final Status status = await OpenFoodAPIClient.saveProduct(
USER,
newProduct,
uriHelper: uriHelper,
);

expect(status.status, 1);
expect(status.statusVerbose, 'fields saved');

final ProductResultV3 result = await OpenFoodAPIClient.getProductV3(
ProductQueryConfiguration(
BARCODE,
language: OpenFoodFactsLanguage.ENGLISH,
version: ProductQueryVersion.v3,
),
user: USER,
uriHelper: uriHelper,
);

expect(result.status, ProductResultV3.statusSuccess);
expect(result.barcode, BARCODE);

final Product? searchedProduct = result.product;
if (searchedProduct == null) {
fail('Saved product not found');
}

expect(result.status, ProductResultV3.statusSuccess);
expect(result.barcode, BARCODE);
final Product? searchedProduct = result.product;
expect(searchedProduct != null, true);
if (searchedProduct != null) {
expect(searchedProduct.barcode, BARCODE);
expect(searchedProduct.productName, PRODUCT_NAME);
expect(searchedProduct.nutrimentDataPer, nutrimentDataPer);
var searchedNutriments = searchedProduct.nutriments;

final searchedNutriments = searchedProduct.nutriments;
expect(searchedNutriments, isNotNull);
if (searchedNutriments != null) {
final List<Nutrient> nutrients = <Nutrient>[
Nutrient.energyKJ,
Nutrient.carbohydrates,
Nutrient.proteins,
Nutrient.fat,
Nutrient.vitaminB12,
];
for (final Nutrient nutrient in nutrients) {
expect(
searchedNutriments.getValue(nutrient, perSize),
nutriments.getValue(nutrient, perSize),
reason: 'should be the same values for $nutrient',
);
}
if (searchedNutriments == null) {
fail('Nutrients are null');
}
for (final Nutrient nutrient in nutrients) {
final double? actual =
searchedNutriments.getValue(nutrient, perSize);
expect(
actual,
isNotNull,
reason: 'should not be null for $nutrient',
);
expect(
actual,
nutriments.getValue(nutrient, perSize),
reason: 'should be the same values for $nutrient',
);
}
}
}
Expand Down

0 comments on commit 31f9fab

Please sign in to comment.