Skip to content

Commit

Permalink
fix: toBeCompleted - fixed uri and parameters for to be completed and…
Browse files Browse the repository at this point in the history
… user queries (#506)

Impacted files:
* `api_getToBeCompletedProducts_test.dart`: now tests for several countries and languages
* `api_getUserProducts_test.dart`: now tests for several languages
* `ToBeCompletedConfiguration.dart`: fixed uri and parameters
* `UserProductSearchQueryConfiguration.dart`: fixed uri and parameters
  • Loading branch information
monsieurtanuki authored Jul 6, 2022
1 parent b7c16ed commit 67e2137
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 37 deletions.
9 changes: 8 additions & 1 deletion lib/utils/ToBeCompletedConfiguration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,12 @@ class ToBeCompletedQueryConfiguration extends AbstractQueryConfiguration {
}

@override
String getUriPath() => '/state/to-be-completed.json';
Map<String, String> getParametersMap() {
final Map<String, String> parameters = super.getParametersMap();
parameters['states_tags'] = 'en:to-be-completed';
return parameters;
}

@override
String getUriPath() => '/api/v2/search';
}
45 changes: 41 additions & 4 deletions lib/utils/UserProductSearchQueryConfiguration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,21 @@ class UserProductSearchQueryConfiguration extends AbstractQueryConfiguration {

@override
String getUriPath() => type.getPath(userId);

@override
Map<String, String> getParametersMap() {
final Map<String, String> parameters = super.getParametersMap();
String? tmp;
tmp = type.getUserTag();
if (tmp != null) {
parameters[tmp] = userId;
}
tmp = type.getStateTag();
if (tmp != null) {
parameters['states_tags'] = tmp;
}
return parameters;
}
}

/// Types of user-related searches.
Expand All @@ -63,13 +78,35 @@ extension UserProductSearchTypeExtension on UserProductSearchType {
String getPath(final String userId) {
switch (this) {
case UserProductSearchType.CONTRIBUTOR:
return '/contributor/$userId.json';
return '/contributor/$userId.json'; // TODO(monsieurtanuki): use '/api/v2/search' instead
case UserProductSearchType.INFORMER:
return '/informer/$userId.json';
case UserProductSearchType.PHOTOGRAPHER:
return '/photographer/$userId.json';
case UserProductSearchType.TO_BE_COMPLETED:
return '/informer/$userId/state/to-be-completed.json';
return '/api/v2/search';
}
}

String? getUserTag() {
switch (this) {
case UserProductSearchType.CONTRIBUTOR:
return null;
case UserProductSearchType.PHOTOGRAPHER:
return 'photographers_tags';
case UserProductSearchType.INFORMER:
return 'informers_tags';
case UserProductSearchType.TO_BE_COMPLETED:
return 'informers_tags';
}
}

String? getStateTag() {
switch (this) {
case UserProductSearchType.CONTRIBUTOR:
case UserProductSearchType.PHOTOGRAPHER:
case UserProductSearchType.INFORMER:
return null;
case UserProductSearchType.TO_BE_COMPLETED:
return 'en:to-be-completed';
}
}
}
78 changes: 69 additions & 9 deletions test/api_getToBeCompletedProducts_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:openfoodfacts/utils/CountryHelper.dart';
import 'package:openfoodfacts/utils/OpenFoodAPIConfiguration.dart';
import 'package:openfoodfacts/utils/QueryType.dart';
import 'package:openfoodfacts/utils/ToBeCompletedConfiguration.dart';
Expand All @@ -12,16 +13,75 @@ void main() {
OpenFoodAPIConfiguration.globalQueryType = QueryType.PROD;

group('$OpenFoodAPIClient get all to-be-completed products', () {
test('all types', () async {
final SearchResult result = await OpenFoodAPIClient.getProducts(
OpenFoodAPIConfiguration.globalUser,
ToBeCompletedQueryConfiguration(),
queryType: OpenFoodAPIConfiguration.globalQueryType,
Future<int> _getCount(
final OpenFoodFactsCountry country,
final OpenFoodFactsLanguage language,
) async {
final String reason = '($country, $language)';
final ToBeCompletedQueryConfiguration configuration =
ToBeCompletedQueryConfiguration(
country: country,
language: language,
);

expect(result.page, 1); // default
expect(result.products, isNotNull);
expect(result.count, greaterThan(1000000)); // 20220630: was 2403413
});
final SearchResult result;
try {
result = await OpenFoodAPIClient.getProducts(
OpenFoodAPIConfiguration.globalUser,
configuration,
queryType: OpenFoodAPIConfiguration.globalQueryType,
);
} catch (e) {
fail('Could not retrieve data for $reason: $e');
}
expect(result.page, 1, reason: reason); // default
expect(result.products, isNotNull, reason: reason);
return result.count!;
}

Future<int> _getCountForAllLanguages(
final OpenFoodFactsCountry country,
) async {
final List<OpenFoodFactsLanguage> languages = <OpenFoodFactsLanguage>[
OpenFoodFactsLanguage.ENGLISH,
OpenFoodFactsLanguage.FRENCH,
OpenFoodFactsLanguage.ITALIAN,
];
int? result;
for (final OpenFoodFactsLanguage language in languages) {
final int count = await _getCount(country, language);
if (result != null) {
expect(count, result, reason: language.toString());
}
result = count;
}
return result!;
}

Future<void> _checkTypeCount(
final OpenFoodFactsCountry country,
final int minimalExpectedCount,
) async {
final int count = await _getCountForAllLanguages(country);
expect(count, greaterThanOrEqualTo(minimalExpectedCount));
}

test(
'in France',
() async => _checkTypeCount(
OpenFoodFactsCountry.FRANCE, 800000) // 20220706: was 910148
);

test(
'in Italy',
() async => _checkTypeCount(
OpenFoodFactsCountry.ITALY, 100000) // 20220706: was 171488
);

test(
'in Spain',
() async => _checkTypeCount(
OpenFoodFactsCountry.SPAIN, 200000) // 20220706: was 272194
);
});
}
101 changes: 78 additions & 23 deletions test/api_getUserProducts_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,91 @@ void main() {

group('$OpenFoodAPIClient get user products', () {
const String userId = 'monsieurtanuki';
const int pageSize = 100; // should be big enough to get everything on page1

test('all types', () async {
const int pageSize = 100;
final Map<UserProductSearchType, int> minimumResultLength =
<UserProductSearchType, int>{
UserProductSearchType.CONTRIBUTOR: 2, // as of 20220626
UserProductSearchType.INFORMER: 56, // as of 20220626
UserProductSearchType.PHOTOGRAPHER: 44, // as of 20220626
UserProductSearchType.TO_BE_COMPLETED: 0, // you never know...
};
for (final MapEntry<UserProductSearchType, int> entry
in minimumResultLength.entries) {
final UserProductSearchQueryConfiguration configuration =
UserProductSearchQueryConfiguration(
type: entry.key,
userId: userId,
pageSize: pageSize,
);
Future<int> _getCount(
final UserProductSearchType type,
final OpenFoodFactsLanguage language,
) async {
final String reason = '($language, $type)';
final UserProductSearchQueryConfiguration configuration =
UserProductSearchQueryConfiguration(
type: type,
userId: userId,
pageSize: pageSize,
language: language,
);

final SearchResult result = await OpenFoodAPIClient.getProducts(
final SearchResult result;
try {
result = await OpenFoodAPIClient.getProducts(
OpenFoodAPIConfiguration.globalUser,
configuration,
queryType: OpenFoodAPIConfiguration.globalQueryType,
);
} catch (e) {
fail('Could not retrieve data for $reason: $e');
}
expect(result.page, 1, reason: reason); // default
expect(result.pageSize, pageSize, reason: reason);
expect(result.products, isNotNull, reason: reason);
expect(result.products!.length, result.count, reason: reason);
return result.count!;
}

expect(result.page, 1); // default
expect(result.pageSize, pageSize);
expect(result.products, isNotNull);
expect(result.products!.length, greaterThanOrEqualTo(entry.value));
Future<int> _getCountForAllLanguages(
final UserProductSearchType type,
) async {
final List<OpenFoodFactsLanguage> languages = <OpenFoodFactsLanguage>[
OpenFoodFactsLanguage.ENGLISH,
OpenFoodFactsLanguage.FRENCH,
OpenFoodFactsLanguage.ITALIAN,
];
int? result;
for (final OpenFoodFactsLanguage language in languages) {
final int count = await _getCount(type, language);
if (result != null) {
expect(count, result, reason: language.toString());
}
result = count;
}
});
return result!;
}

Future<void> _checkTypeCount(
final UserProductSearchType type,
final int minimalExpectedCount,
) async {
final int count = await _getCountForAllLanguages(type);
expect(count, greaterThanOrEqualTo(minimalExpectedCount));
}

test(
'contributor',
() async => _checkTypeCount(
UserProductSearchType.CONTRIBUTOR, 2) // as of 20220706
,
);

test(
'informer',
() async =>
_checkTypeCount(UserProductSearchType.INFORMER, 56) // as of 20220706
,
);

test(
'photographer',
() async => _checkTypeCount(
UserProductSearchType.PHOTOGRAPHER, 44) // as of 20220706
,
);

test(
'to be completed',
() async => _checkTypeCount(
UserProductSearchType.TO_BE_COMPLETED, 0) // you never know...
,
);
});
}

0 comments on commit 67e2137

Please sign in to comment.