-
-
Notifications
You must be signed in to change notification settings - Fork 67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: #132 - new method OpenFoodAPIClient.addProductFields #436
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/// Fields of a [Product] that can be simply added. | ||
enum AddableProductField { | ||
BRANDS, | ||
STORES, | ||
COUNTRIES, | ||
} | ||
|
||
extension AddableProductFieldExtension on AddableProductField { | ||
static const Map<AddableProductField, String> _KEYS = { | ||
AddableProductField.BRANDS: 'add_brands', | ||
AddableProductField.STORES: 'add_stores', | ||
AddableProductField.COUNTRIES: 'add_countries', | ||
}; | ||
|
||
/// Returns the key of the product field | ||
String get addableKey => _KEYS[this]!; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import 'package:openfoodfacts/openfoodfacts.dart'; | ||
import 'package:openfoodfacts/utils/AddableProductFields.dart'; | ||
import 'package:openfoodfacts/utils/OpenFoodAPIConfiguration.dart'; | ||
import 'package:openfoodfacts/utils/QueryType.dart'; | ||
import 'package:test/test.dart'; | ||
import 'test_constants.dart'; | ||
|
||
/// Unit/Integration tests around OpenFoodAPIClient.addProductFields | ||
void main() { | ||
OpenFoodAPIConfiguration.globalQueryType = QueryType.TEST; | ||
|
||
group('$OpenFoodAPIClient add product fields', () { | ||
const String barcode = '0048151623426'; | ||
const List<String> additionalValues = <String>['djobi', 'djoba']; | ||
const OpenFoodFactsLanguage language = OpenFoodFactsLanguage.ENGLISH; | ||
const User user = TestConstants.TEST_USER; | ||
const Timeout timeout = Timeout(Duration(seconds: 90)); | ||
|
||
void _checkStatus(final Status status) { | ||
expect(status.status, 1); | ||
expect(status.statusVerbose, 'fields saved'); | ||
} | ||
|
||
String? _getValue( | ||
final AddableProductField field, | ||
final Product product, | ||
) { | ||
switch (field) { | ||
case AddableProductField.BRANDS: | ||
return product.brands; | ||
case AddableProductField.COUNTRIES: | ||
return product.countries; | ||
case AddableProductField.STORES: | ||
return product.stores; | ||
} | ||
} | ||
|
||
Future<void> _checkValue( | ||
final AddableProductField field, | ||
final String expectedValue, | ||
) async { | ||
final ProductQueryConfiguration configuration = ProductQueryConfiguration( | ||
barcode, | ||
language: language, | ||
fields: [ProductField.ALL], | ||
); | ||
|
||
final ProductResult productResult = await OpenFoodAPIClient.getProduct( | ||
configuration, | ||
user: user, | ||
); | ||
expect(productResult.product, isNotNull); | ||
final String? actualValue = _getValue(field, productResult.product!); | ||
expect(actualValue, equalsIgnoringCase(expectedValue)); | ||
} | ||
|
||
Future<void> _checkAddableField( | ||
final AddableProductField field, | ||
final Product initialProduct, | ||
) async { | ||
late Status status; | ||
late Product product; | ||
|
||
// from scratch | ||
product = initialProduct; | ||
status = await OpenFoodAPIClient.saveProduct(user, product); | ||
_checkStatus(status); | ||
|
||
// cumulative list | ||
String expectedValue = _getValue(field, product)!; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @g123k There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @g123k I had a hunch about test code const String initialString = "init";
const List<String> additionalValues = <String>['djobi', 'djoba'];
final List<String> result = <String>[];
late int start;
late int end;
result.clear();
start = DateTime.now().microsecondsSinceEpoch;
final StringBuffer asStringBuffer = StringBuffer(initialString);
result.add(asStringBuffer.toString());
for (final String additionalValue in additionalValues) {
asStringBuffer.write(', $additionalValue');
result.add(asStringBuffer.toString());
}
end = DateTime.now().microsecondsSinceEpoch;
print('${end - start}: $result');
result.clear();
start = DateTime.now().microsecondsSinceEpoch;
String asString = initialString;
result.add(asString);
for (final String additionalValue in additionalValues) {
asString += ', $additionalValue';
result.add(asString);
}
end = DateTime.now().microsecondsSinceEpoch;
print('${end - start}: $result'); After several runs, I've noticed that the first block run is a bit slower than the other - regardless of |
||
|
||
await _checkValue(field, expectedValue); | ||
|
||
for (final String additionalValue in additionalValues) { | ||
expectedValue += ', $additionalValue'; | ||
|
||
status = await OpenFoodAPIClient.addProductFields( | ||
user, | ||
barcode, | ||
<AddableProductField, String>{field: additionalValue}, | ||
); | ||
_checkStatus(status); | ||
|
||
await _checkValue(field, expectedValue); | ||
} | ||
} | ||
|
||
test( | ||
'add brand', | ||
() async => _checkAddableField( | ||
AddableProductField.BRANDS, | ||
Product( | ||
barcode: barcode, lang: language, brands: 'Golden Cookies'), | ||
), | ||
timeout: timeout); | ||
|
||
test( | ||
'add country', | ||
() async => _checkAddableField( | ||
AddableProductField.COUNTRIES, | ||
Product( | ||
barcode: barcode, lang: language, countries: 'United States'), | ||
), | ||
timeout: timeout); | ||
|
||
test( | ||
'add stores', | ||
() async => _checkAddableField( | ||
AddableProductField.STORES, | ||
Product(barcode: barcode, lang: language, stores: 'Intermarché'), | ||
), | ||
timeout: timeout); | ||
}); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By curiosity, why don't you call
Map.from(user.toData)
?