Skip to content

Commit

Permalink
feat: #517 - added ECOSCORE and NUTRISCORE sort-by options (#522)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
monsieurtanuki authored Aug 3, 2022
1 parent 2961310 commit a39ba24
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/model/parameter/SortBy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -28,4 +30,6 @@ enum SortOption {
CREATED,
EDIT,
NOTHING,
ECOSCORE,
NUTRISCORE,
}
81 changes: 81 additions & 0 deletions test/api_searchProducts_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<SortOption?, List<String>> previousValues =
<SortOption?, List<String>>{};
int? checkCount;
final List<SortOption?> sortOptions = <SortOption?>[];
sortOptions.addAll(SortOption.values);
sortOptions.add(null);
for (final SortOption? currentOption in sortOptions) {
final List<Parameter> parameters = <Parameter>[
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<String> barcodes = <String>[];
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 = <Parameter>[
const PageNumber(page: 1),
Expand Down

0 comments on commit a39ba24

Please sign in to comment.