Skip to content
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: Add localized fields for conservation conditions and customer service in Product object #1020

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
14 changes: 14 additions & 0 deletions lib/src/model/product.dart
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,20 @@ class Product extends JsonObject {
toJson: LanguageHelper.toJsonStringMap)
Map<OpenFoodFactsLanguage, String>? productNameInLanguages;

/// Localized conservation conditions of the product, stored in a map where each language is represented by its respective key.
@JsonKey(
name: 'conservation_conditions_in_languages',
fromJson: LanguageHelper.fromJsonStringMap,
toJson: LanguageHelper.toJsonStringMap)
Map<OpenFoodFactsLanguage, String>? conservationConditionsInLanguages;

/// Localized customer service information for the product, stored in a map where each language is represented by its respective key.
@JsonKey(
name: 'customer_service_in_languages',
fromJson: LanguageHelper.fromJsonStringMap,
toJson: LanguageHelper.toJsonStringMap)
Map<OpenFoodFactsLanguage, String>? customerServiceInLanguages;

/// Common name. Example: "Chocolate bar with milk and hazelnuts".
@JsonKey(name: 'generic_name')
String? genericName;
Expand Down
10 changes: 10 additions & 0 deletions lib/src/model/product.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions lib/src/utils/product_fields.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ enum ProductField implements OffTagged {
inLanguagesProductField: ProductField.NAME_IN_LANGUAGES,
isAllLanguages: true,
),
CONSERVATION_CONDITIONS_ALL_LANGUAGES(
offTag: 'conservation_conditions_languages',
isAllLanguages: true,
),
CUSTOMER_SERVICE_ALL_LANGUAGES(
offTag: 'customer_service_languages',
isAllLanguages: true,
),
GENERIC_NAME(
offTag: 'generic_name',
inLanguagesProductField: ProductField.GENERIC_NAME_IN_LANGUAGES,
Expand Down Expand Up @@ -310,6 +318,8 @@ const Set<ProductField> fieldsInLanguages = {
@Deprecated('Use ProductField.getAllLanguagesList() instead')
const Set<ProductField> fieldsAllLanguages = {
ProductField.NAME_ALL_LANGUAGES,
ProductField.CUSTOMER_SERVICE_ALL_LANGUAGES,
ProductField.CONSERVATION_CONDITIONS_ALL_LANGUAGES,
ProductField.GENERIC_NAME_ALL_LANGUAGES,
ProductField.ABBREVIATED_NAME_ALL_LANGUAGES,
ProductField.INGREDIENTS_TEXT_ALL_LANGUAGES,
Expand Down
45 changes: 45 additions & 0 deletions test/api_get_product_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,51 @@ void main() {
expect(nutriments.getValue(Nutrient.vitaminC, perSize), 0.0339);
});

test('get localized conservation conditions and customer service',
() async {
ProductQueryConfiguration configuration = ProductQueryConfiguration(
'3017620425035',
fields: [
ProductField.CONSERVATION_CONDITIONS_ALL_LANGUAGES,
ProductField.CUSTOMER_SERVICE_ALL_LANGUAGES,
],
version: ProductQueryVersion.v3,
AffanShaikhsurab marked this conversation as resolved.
Show resolved Hide resolved
language: OpenFoodFactsLanguage.ALL_LANGUAGES,
);

ProductResultV3 result = await getProductV3InProd(configuration);

expect(result.status, ProductResultV3.statusSuccess);
expect(result.product, isNotNull);

final conservationConditions =
result.product!.conservationConditionsInLanguages;
expect(conservationConditions, isNotNull);
expect(conservationConditions, isNotEmpty);
expect(
conservationConditions!
.containsKey(OpenFoodFactsLanguage.ALL_LANGUAGES),
isTrue);
expect(conservationConditions[OpenFoodFactsLanguage.ALL_LANGUAGES],
isNotEmpty);
expect(
conservationConditions[OpenFoodFactsLanguage.ALL_LANGUAGES],
isA<String>()
.having((s) => s.trim().isNotEmpty, 'non-empty string', isTrue),
);

final customerService = result.product!.customerServiceInLanguages;
expect(customerService, isNotNull);
expect(customerService, isNotEmpty);
expect(customerService!.containsKey(OpenFoodFactsLanguage.ALL_LANGUAGES),
isTrue);
expect(customerService[OpenFoodFactsLanguage.ALL_LANGUAGES], isNotEmpty);
expect(
customerService[OpenFoodFactsLanguage.ALL_LANGUAGES],
isA<String>()
.having((s) => s.trim().isNotEmpty, 'non-empty string', isTrue),
);
});
test('get uncommon nutrients', () async {
// PROD data as of 2021-07-16
const OpenFoodFactsLanguage language = OpenFoodFactsLanguage.FRENCH;
Expand Down
Loading