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

Add some additional basic nutritional values validation for ingredients #1726

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

rolandgeider
Copy link
Member

See discussion in wger-project/flutter#614

@rolandgeider
Copy link
Member Author

@Dieterbe does the logic in wger/nutrition/dataclasses.py look ok to you? should just be some basic logic allowing us to discard obviously incorrect data

if energy_protein > self.energy:
raise ValueError(
f'Energy calculated from protein is greater than total energy: {energy_protein} > {self.energy}'
)
Copy link
Contributor

Choose a reason for hiding this comment

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

The protein, carb and fat energy values are rough. Checking this too strictly might trigger false positives , especially for ingredients that only contain 1 macro (eg egg whites)

For sanity checking maybe allow 20% error or so ? I would be curious to have a look at ingredients for which we trigger errors here

Copy link
Contributor

Choose a reason for hiding this comment

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

Also all these checks are quite verbose. Do we need to raise ValueErrors explicitly? Could we replace these with simple, single line assert statements ? If the code is more obvious, it also reduces the need for unit testing

raise ValueError(
f'Total energy calculated is greater than energy: {energy_calculated} > {self.energy}'
)

Copy link
Contributor

Choose a reason for hiding this comment

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

Also this is not fool proof. Btw I thought we already had such a check somewhere

Copy link
Contributor

Choose a reason for hiding this comment

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

Note also that in current form, if this triggers, one of the previous ones will have triggered too.

'protein': {'kg': 4, 'lb': 113},
'carbohydrates': {'kg': 4, 'lb': 113},
'fat': {'kg': 9, 'lb': 225},
'protein': {'metric': 4, 'imperial': 113},
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are we replacing clear explicit units with abstract opaque terms?

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

Successfully merging this pull request may close these issues.

2 participants