Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: #954 - split the nutrition page code for better focus #1142

Merged
merged 1 commit into from
Feb 18, 2022

Conversation

monsieurtanuki
Copy link
Contributor

New file:

  • nutrition_container.dart: Nutrition data, for nutrient order and conversions.
  • tmp_to_off_nutriments.dart: Nutrient ids supported by Nutriments, to be moved to off-dart

Impacted file:

  • nutrition_page_loaded.dart: moved the nutrient and conversion code to new class NutritionContainer

What

I needed clarity in the code and I had to split the UI and the model.
It still does not work 100% of the time, but now at least it's easier to focus on the model.
My problem: I manage to save nutrition data for green tea 8712100824630 but not for Wasa 7300400481588. How come?

Part of

…r focus

New file:
* `nutrition_container.dart`: Nutrition data, for nutrient order and conversions.
* `tmp_to_off_nutriments.dart`: Nutrient ids supported by `Nutriments`, to be moved to off-dart

Impacted file:
* `nutrition_page_loaded.dart`: moved the nutrient and conversion code to new class `NutritionContainer`
@stephanegigandet
Copy link
Contributor

My problem: I manage to save nutrition data for green tea 8712100824630 but not for Wasa 7300400481588. How come?

Do you have a way to capture the request made to the API?

@monsieurtanuki
Copy link
Contributor Author

@stephanegigandet I'm going to create related tests on off-dart.

@monsieurtanuki
Copy link
Contributor Author

@stephanegigandet The test failed, with an initial value of .203g of magnesium, that I wanted to increase to .204:

00:04 +0 -1: test/api_saveProduct_test.dart: OpenFoodAPIClient add new products Wasa issue [E]
  Expected: <0.20400000000000001>
    Actual: <0.203>
test on Wasa / magnesium
// cf. https://github.com/openfoodfacts/smooth-app/pull/1142
test('Wasa issue', () async {
  const String barcode = '7300400481588';
  final ProductQueryConfiguration configurations =
      ProductQueryConfiguration(
    barcode,
    language: OpenFoodFactsLanguage.FRENCH,
    fields: [ProductField.ALL],
  );

  // Step 1: get the product
  ProductResult result = await OpenFoodAPIClient.getProduct(
    configurations,
    user: TestConstants.TEST_USER,
  );
  expect(result.status, 1);
  expect(result.product, isNotNull);
  expect(result.product!.nutriments, isNotNull);
  final double? initialMagnesium = result.product!.nutriments!.magnesium;
  expect(initialMagnesium, isNotNull); // in real life: 200mg = 0.2

  // Step 2: save the slightly altered product
  final double nextMagnesium = initialMagnesium! + .001;
  result.product!.nutriments!.magnesium = nextMagnesium;
  final Product savedProduct = Product(barcode: barcode);
  savedProduct.nutriments = result.product!.nutriments;
  final Status status = await OpenFoodAPIClient.saveProduct(
    TestConstants.TEST_USER,
    savedProduct,
  );
  expect(status.status, 1);
  expect(status.error, null);

  // Step 3: check if the value was correctly saved
  result = await OpenFoodAPIClient.getProduct(
    configurations,
    user: TestConstants.TEST_USER,
  );
  expect(result.status, 1);
  expect(result.product, isNotNull);
  expect(result.product!.nutriments, isNotNull);
  final double? latestMagnesium = result.product!.nutriments!.magnesium;
  expect(latestMagnesium, nextMagnesium);
});

uri: https://world.openfoodfacts.net/cgi/product_jqm2.pl

headers

{Accept: application/json, UserAgent: Dart API, From: - dart API test - openfoodfacts-dart - iloveflutter, authorization: Basic b2ZmOm9mZg==}

request body

{comment: dart API test, user_id: openfoodfacts-dart, password: iloveflutter, code: 7300400481588, lang: -, selected_images: {}, images: {}, imagesFreshnessInLanguages: null, ingredients_analysis_tags: [], nutriment_salt: 1.1, nutriment_fiber: 26.0, nutriment_sugars: 2.0, nutriment_fat: 5.0, nutriment_saturated-fat: 1.0, nutriment_proteins: 13.0, nutriment_nova-group: 3, nutriment_energy: 1396.0, nutriment_energy-kcal: 333.0, nutriment_carbohydrates: 46.0, nutriment_salt_serving: 0.11, nutriment_fiber_serving: 2.6, nutriment_sugars_serving: 0.2, nutriment_fat_serving: 0.5, nutriment_fat_unit: g, nutriment_saturated-fat_serving: 0.1, nutriment_proteins_serving: 1.3, nutriment_proteins_unit: g, nutriment_nova-group_serving: 3, nutriment_energy_serving: 140.0, nutriment_carbohydrates_serving: 4.6, nutriment_carbohydrates_unit: g, nutriment_energy_unit: kj, nutriment_energy-kcal_unit: kcal, nutriment_iron_serving: 0.0006, nutriment_iron: 0.006, nutriment_iron_unit: mg, nutriment_magnesium_serving: 0.0203, nutriment_magnesium: 0.20400000000000001, nutriment_magnesium_unit: mg, nutriment_sodium_serving: 0.044, nutriment_sodium: 0.44, nutriment_sodium_unit: g, nutriment_zinc_serving: 0.0005, nutriment_zinc: 0.005, nutriment_zinc_unit: mg, nutriment_copper_serving: 0.00006, nutriment_copper: 0.0006, nutriment_copper_unit: mg}

Copy link
Member

@M123-dev M123-dev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't say anything about if the implemenation works in combination with the BE but the code looks good

@monsieurtanuki monsieurtanuki merged commit 0c9552e into openfoodfacts:develop Feb 18, 2022
@monsieurtanuki
Copy link
Contributor Author

Thank you @M123-dev for your code review!
Still the "save to BE" part not working at 100% though...

@teolemon
Copy link
Member

aaah BE = Backend. Finally got it after 3 days :-)

@monsieurtanuki
Copy link
Contributor Author

@teolemon Deep inside I know I would rather say "server" (I'm old-fashioned) but I read "back-end" in some comments here and there and I wanted to pretend to be cool and decided to say "BE".
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants