From a39ba24b6a9daac6e36669b559891a536fcd27bf Mon Sep 17 00:00:00 2001 From: monsieurtanuki Date: Wed, 3 Aug 2022 15:53:48 +0200 Subject: [PATCH] feat: #517 - added ECOSCORE and NUTRISCORE sort-by options (#522) Impacted files: * `SortBy.dart`: added ECOSCORE and NUTRISCORE sort by option * `api_searchProducts_test.dart`: added a test comparing the product orders generated by all sort options --- lib/model/parameter/SortBy.dart | 4 ++ test/api_searchProducts_test.dart | 81 +++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) diff --git a/lib/model/parameter/SortBy.dart b/lib/model/parameter/SortBy.dart index 584c4ee6cc..4a4da45e25 100644 --- a/lib/model/parameter/SortBy.dart +++ b/lib/model/parameter/SortBy.dart @@ -8,6 +8,8 @@ class SortBy extends Parameter { SortOption.EDIT: 'last_modified_t', SortOption.POPULARITY: 'unique_scans_n', SortOption.NOTHING: 'nothing', + SortOption.ECOSCORE: 'ecoscore_score', + SortOption.NUTRISCORE: 'nutriscore_score', }; @override @@ -28,4 +30,6 @@ enum SortOption { CREATED, EDIT, NOTHING, + ECOSCORE, + NUTRISCORE, } diff --git a/test/api_searchProducts_test.dart b/test/api_searchProducts_test.dart index 02d07099e7..1a5b6f2060 100644 --- a/test/api_searchProducts_test.dart +++ b/test/api_searchProducts_test.dart @@ -38,6 +38,87 @@ void main() { UNKNOWN_BARCODE, ]; + /// Checks that all the sort options return different orders but same count. + /// + /// We can relatively assume that the top 100 pizzas in France are in + /// different orders. + test('search with all sort-by options', () async { + final Map> previousValues = + >{}; + int? checkCount; + final List sortOptions = []; + sortOptions.addAll(SortOption.values); + sortOptions.add(null); + for (final SortOption? currentOption in sortOptions) { + final List parameters = [ + const SearchTerms(terms: ['pizza']), + const PageNumber(page: 1), + const PageSize(size: 100), + if (currentOption != null) SortBy(option: currentOption) + ]; + + final SearchResult result = await OpenFoodAPIClient.searchProducts( + TestConstants.PROD_USER, + ProductSearchQueryConfiguration( + parametersList: parameters, + fields: [ProductField.BARCODE], + language: OpenFoodFactsLanguage.FRENCH, + country: OpenFoodFactsCountry.FRANCE, + ), + queryType: QueryType.PROD, + ); + + expect(result.products, isNotNull); + final List barcodes = []; + for (final Product product in result.products!) { + barcodes.add(product.barcode!); + } + + for (final SortOption? previousOption in previousValues.keys) { + final Matcher matcher = equals(previousValues[previousOption]); + // special case: NOTHING and EDIT seem to be the same. + if ((previousOption == SortOption.NOTHING && + currentOption == SortOption.EDIT) || + (previousOption == SortOption.EDIT && + currentOption == SortOption.NOTHING)) { + expect( + barcodes, + matcher, + reason: + 'Should be identical for $currentOption and $previousOption', + ); + } + // special case: POPULARITY and no sort option seem to be the same. + else if ((previousOption == null && + currentOption == SortOption.POPULARITY) || + (previousOption == SortOption.POPULARITY && + currentOption == null)) { + expect( + barcodes, + matcher, + reason: + 'Should be identical for $currentOption and $previousOption', + ); + } else { + expect( + barcodes, + isNot(matcher), + reason: + 'Should be different for $currentOption and $previousOption', + ); + } + } + previousValues[currentOption] = barcodes; + + expect(result.count, isNotNull); + if (checkCount == null) { + checkCount = result.count; // first value + } else { + expect(result.count, checkCount); // check if same value + } + } + }); + test('search favorite products', () async { final parameters = [ const PageNumber(page: 1),