Skip to content

Commit

Permalink
feat: product_type as new product field (#994)
Browse files Browse the repository at this point in the history
Impacted files:
* `api_not_food_get_product_test.dart`: additional tests with new product field product_type
* `product_fields.dart`: added product_type as new product field
  • Loading branch information
monsieurtanuki authored Nov 18, 2024
1 parent 09d203a commit 12e72b4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 42 deletions.
1 change: 1 addition & 0 deletions lib/src/utils/product_fields.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import '../model/off_tagged.dart';
/// Fields of a [Product]
enum ProductField implements OffTagged {
BARCODE(offTag: 'code'),
PRODUCT_TYPE(offTag: 'product_type'),
NAME(
offTag: 'product_name',
inLanguagesProductField: ProductField.NAME_IN_LANGUAGES,
Expand Down
77 changes: 35 additions & 42 deletions test/api_not_food_get_product_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,74 +7,67 @@ void main() {
OpenFoodAPIConfiguration.userAgent = TestConstants.TEST_USER_AGENT;
OpenFoodAPIConfiguration.globalUser = TestConstants.PROD_USER;

const UriProductHelper uriHelperBeautyProd = UriProductHelper(
domain: 'openbeautyfacts.org',
);
const UriProductHelper uriHelperProductsProd = UriProductHelper(
domain: 'openproductsfacts.org',
);
const UriProductHelper uriHelperPetFoodProd = UriProductHelper(
domain: 'openpetfoodfacts.org',
);
const Map<ProductType, String> domains = <ProductType, String>{
ProductType.beauty: 'openbeautyfacts.org',
ProductType.product: 'openproductsfacts.org',
ProductType.petFood: 'openpetfoodfacts.org',
ProductType.food: 'openfoodfacts.org',
};

const String beautyBarcode = '3600551054476';
const String productsBarcode = '7898927451035';
const String petFoodBarcode = '3564700266809';
const Map<ProductType, String> barcodes = <ProductType, String>{
ProductType.beauty: '3600551054476',
ProductType.product: '7898927451035',
ProductType.petFood: '3564700266809',
};

group('$OpenFoodAPIClient get not food products', () {
Future<Product?> findProduct(
Future<void> findProduct(
final String barcode,
final UriProductHelper uriHelper,
final bool shouldBeThere,
final ProductType expectedProductType,
final ProductType serverProductType,
) async {
final ProductQueryConfiguration configurations =
ProductQueryConfiguration(
barcode,
language: OpenFoodFactsLanguage.ENGLISH,
fields: [ProductField.BARCODE],
fields: [
ProductField.BARCODE,
ProductField.PRODUCT_TYPE,
],
version: ProductQueryVersion(2),
);
await getProductTooManyRequestsManager.waitIfNeeded();
final OldProductResult result = await OpenFoodAPIClient.getOldProduct(
configurations,
uriHelper: uriHelper,
uriHelper: UriProductHelper(
domain: domains[serverProductType]!,
),
);
if (shouldBeThere) {
if (expectedProductType == serverProductType) {
expect(result.status, 1);
expect(result.barcode, barcode);
expect(result.product, isNotNull);
expect(result.product!.barcode, barcode);
expect(result.product!.productType, expectedProductType);
} else {
expect(result.status, 0);
expect(result.barcode, barcode);
expect(result.product, isNull);
}
return result.product;
}

test('get beauty product', () async {
final String barcode = beautyBarcode;
await findProduct(barcode, uriHelperBeautyProd, true);
await findProduct(barcode, uriHelperProductsProd, false);
await findProduct(barcode, uriHelperPetFoodProd, false);
await findProduct(barcode, uriHelperPetFoodProd, false);
await findProduct(barcode, uriHelperFoodProd, false);
});

test('get products product', () async {
final String barcode = productsBarcode;
await findProduct(barcode, uriHelperBeautyProd, false);
await findProduct(barcode, uriHelperProductsProd, true);
await findProduct(barcode, uriHelperPetFoodProd, false);
await findProduct(barcode, uriHelperFoodProd, false);
});

test('get pet food product', () async {
final String barcode = petFoodBarcode;
await findProduct(barcode, uriHelperBeautyProd, false);
await findProduct(barcode, uriHelperProductsProd, false);
await findProduct(barcode, uriHelperPetFoodProd, true);
await findProduct(barcode, uriHelperFoodProd, false);
test('get OxF product', () async {
for (MapEntry<ProductType, String> item in barcodes.entries) {
final ProductType productType = item.key;
final String barcode = item.value;
for (final ProductType serverProductType in ProductType.values) {
await findProduct(
barcode,
productType,
serverProductType,
);
}
}
});
},
timeout: Timeout(
Expand Down

0 comments on commit 12e72b4

Please sign in to comment.