Skip to content

feat: trigger Open Food Facts lookup on manual barcode text search#2351

Open
Ewin7 wants to merge 3 commits into
wger-project:masterfrom
Ewin7:fix/barcode-search
Open

feat: trigger Open Food Facts lookup on manual barcode text search#2351
Ewin7 wants to merge 3 commits into
wger-project:masterfrom
Ewin7:fix/barcode-search

Conversation

@Ewin7
Copy link
Copy Markdown
Contributor

@Ewin7 Ewin7 commented May 21, 2026

Proposed Changes

  • Introduces a small improvement to the ingredient search, addressing my comment in Improve ingredient search #2340.
  • Currently OFF lookups are only triggered when scanning a barcode via the camera. If a user manually enters a barcode into the search field, the system defaults to text search.
  • This PR tries a barcode lookup first when the input looks like a barcode, and otherwise falls back to standard full-text search.

Changes

  • Updated search_name_fulltext in nutrition/api/filtersets.py. If the input is numeric and matches standard retail barcode lengths (8, 12, 13, 14 digits) the query is passed to search_barcode.
  • Unit Testing: Added tests in nutrition/tests/test_search_api.py.

Related Issue(s)

Ewin7 added 2 commits May 22, 2026 01:31
- Added logic to `search_name_fulltext` to detect 8,12,13,14 digit numeric strings
- Searches through the existing `search_barcode` method
- Fallback to standard full-text search if no barcode match is found
- Added unit tests
Comment thread wger/nutrition/tests/test_search_api.py Outdated
Searching for an 8-14 digit number in the name field should trigger the OFF API lookup
"""
self.client.get(self.url + '?name__search=1300000000000')
mock_fetch.assert_called_once_with('1300000000000')
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

we could simplify the tests here, just mock search_barcode and only test that the correct parameters are passed, that search_barcode itself works is already tested somewhere else (plus one that checks that the search directly returns if the search found an ingredient)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I updated the tests to mock search_barcode. I also updated the first test to verify the correct arguments are passed, and added a test to check if a barcode match is returned in the search response.

- Mock the search_barcode function instead of fetch_ingredient_from_off.
- Assert barcode search is called with expected arguments
- Added a test ensuring a barcode match is returned directly
@Ewin7 Ewin7 force-pushed the fix/barcode-search branch from 294fb67 to f6b2bdb Compare May 23, 2026 13:09
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