Skip to content

Commit

Permalink
fix: 617 - user related queries now support api v3 (#666)
Browse files Browse the repository at this point in the history
Impacted files:
* `api_get_user_products_test.dart`: now supporting api v3
* `tag_filter.dart`: added filters `PHOTOGRAPHERS` and `INFORMERS`
* `user_product_search_query_configuration.dart`: deprecated
  • Loading branch information
monsieurtanuki committed Dec 31, 2022
1 parent ddd61a3 commit 11f678a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 19 deletions.
8 changes: 8 additions & 0 deletions lib/src/model/parameter/tag_filter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,16 @@ enum TagFilterType implements OffTagged {
INGREDIENTS(offTag: 'ingredients'),
NOVA_GROUPS(offTag: 'nova_groups'),
LANGUAGES(offTag: 'languages'),

/// User who created this product
CREATOR(offTag: 'creator'),
EDITORS(offTag: 'editors'),

/// User who photographed for this product
PHOTOGRAPHERS(offTag: 'photographers'),

/// Users who contributed to this product
INFORMERS(offTag: 'informers'),
LANG(offTag: 'lang');

const TagFilterType({
Expand Down
12 changes: 12 additions & 0 deletions lib/src/utils/user_product_search_query_configuration.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
// ignore_for_file: deprecated_member_use_from_same_package

import '../interface/parameter.dart';
import '../model/parameter/page_number.dart';
import '../model/parameter/page_size.dart';
import 'abstract_query_configuration.dart';
import 'language_helper.dart';
import 'product_fields.dart';

// TODO: deprecated from 2022-12-29; remove when old enough
/// Query Configuration for user-related searches.
@Deprecated('Use standard queries instead')
class UserProductSearchQueryConfiguration extends AbstractQueryConfiguration {
UserProductSearchQueryConfiguration({
required this.type,
Expand Down Expand Up @@ -58,21 +62,29 @@ class UserProductSearchQueryConfiguration extends AbstractQueryConfiguration {
}
}

// TODO: deprecated from 2022-12-29; remove when old enough
/// Types of user-related searches.
@Deprecated('Use standard queries instead')
enum UserProductSearchType {
/// Where the user created the product.
@Deprecated('Use a standard query with TagFilterType.CREATOR instead')
CONTRIBUTOR,

/// Where the user edited the product.
@Deprecated('Use a standard query with TagFilterType.INFORMERS instead')
INFORMER,

/// Where the user photographed the product.
@Deprecated('Use a standard query with TagFilterType.PHOTOGRAPHERS instead')
PHOTOGRAPHER,

/// Where the user edited a product that still needs to be completed.
@Deprecated(
'Use a standard query with TagFilterType.INFORMERS and TagFilterType.STATES=ProductState.COMPLETED.toBeCompletedTag instead')
TO_BE_COMPLETED,
}

// TODO: deprecated from 2022-12-29; remove when old enough
extension UserProductSearchTypeExtension on UserProductSearchType {
/// Returns the URI path for the search.
String getPath(final String userId) {
Expand Down
52 changes: 33 additions & 19 deletions test/api_get_user_products_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,32 @@ void main() {
group('$OpenFoodAPIClient get user products', () {
const String userId = 'monsieurtanuki';
const int pageSize = 100; // should be big enough to get everything on page1
final String toBeCompletedTag = ProductState.COMPLETED.toBeCompletedTag;

Future<int> getCount(
final UserProductSearchType type,
final OpenFoodFactsLanguage language, {
final TagFilterType type,
final OpenFoodFactsLanguage language,
final bool toBeCompleted, {
final void Function(Product)? additionalCheck,
}) async {
final String reason = '($language, $type)';
final UserProductSearchQueryConfiguration configuration =
UserProductSearchQueryConfiguration(
type: type,
userId: userId,
pageSize: pageSize,
final ProductSearchQueryConfiguration configuration =
ProductSearchQueryConfiguration(
parametersList: [
TagFilter.fromType(tagFilterType: type, tagName: userId),
PageSize(size: pageSize),
if (toBeCompleted)
TagFilter.fromType(
tagFilterType: TagFilterType.STATES,
tagName: toBeCompletedTag,
),
],
language: language,
fields: [
ProductField.BARCODE,
ProductField.STATES_TAGS,
],
version: ProductQueryVersion.v3,
);

final SearchResult result;
Expand All @@ -52,7 +61,8 @@ void main() {
}

Future<int> getCountForAllLanguages(
final UserProductSearchType type, {
final TagFilterType type,
final bool toBeCompleted, {
final void Function(Product)? additionalCheck,
}) async {
final List<OpenFoodFactsLanguage> languages = <OpenFoodFactsLanguage>[
Expand All @@ -65,6 +75,7 @@ void main() {
final int count = await getCount(
type,
language,
toBeCompleted,
additionalCheck: additionalCheck,
);
if (result != null) {
Expand All @@ -76,46 +87,49 @@ void main() {
}

Future<void> checkTypeCount(
final UserProductSearchType type,
final TagFilterType type,
final int minimalExpectedCount, {
final void Function(Product)? additionalCheck,
final bool toBeCompleted = false,
}) async {
final int count = await getCountForAllLanguages(
type,
toBeCompleted,
additionalCheck: additionalCheck,
);
expect(count, greaterThanOrEqualTo(minimalExpectedCount));
}

test(
'contributor',
() async =>
checkTypeCount(UserProductSearchType.CONTRIBUTOR, 2) // as of 20220706
() async => checkTypeCount(TagFilterType.CREATOR, 2) // as of 20221229
,
);

test(
'informer',
() async =>
checkTypeCount(UserProductSearchType.INFORMER, 56) // as of 20220706
await checkTypeCount(TagFilterType.INFORMERS, 73) // as of 20221229
,
);

test(
'photographer',
() async => checkTypeCount(
UserProductSearchType.PHOTOGRAPHER, 44) // as of 20220706
() async =>
checkTypeCount(TagFilterType.PHOTOGRAPHERS, 48) // as of 20221229
,
);

test(
'to be completed',
() async => checkTypeCount(
UserProductSearchType.TO_BE_COMPLETED, 0, // you never know...
additionalCheck: (final Product product) {
expect(product.statesTags, isNotNull);
expect(product.statesTags, contains('en:to-be-completed'));
}),
TagFilterType.INFORMERS, 0, // you never know...
toBeCompleted: true,
additionalCheck: (final Product product) {
expect(product.statesTags, isNotNull);
expect(product.statesTags, contains(toBeCompletedTag));
},
),
);
});
}

0 comments on commit 11f678a

Please sign in to comment.