forked from openfoodfacts/openfoodfacts-dart
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 802 - new "get product" method specific to OBF OPF OPFF
New files: * `api_not_food_get_product_test.dart`: tests around OBF OPF OPFF and `getOldProduct` * `old_product_result.dart`: Product Result (old style). * `old_product_result.g.dart`: generated Impacted files: * `open_food_api_client.dart`: new `getOldProduct` method, dedicated to OBF OPF OPFF, and supposed to disappear when they support API V3. * `openfoodfacts.dart`: exported new class.
- Loading branch information
1 parent
7753cb0
commit 24f57bb
Showing
5 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import 'package:json_annotation/json_annotation.dart'; | ||
import '../interface/json_object.dart'; | ||
import 'product.dart'; | ||
|
||
part 'old_product_result.g.dart'; | ||
|
||
/// Product Result (old style). | ||
// TODO(monsieurtanuki): get rid of it when OBF OPF OPFF support api v3 | ||
@JsonSerializable() | ||
class OldProductResult extends JsonObject { | ||
final int? status; | ||
@JsonKey(name: 'code') | ||
final String? barcode; | ||
@JsonKey(name: 'status_verbose') | ||
final String? statusVerbose; | ||
final Product? product; | ||
|
||
const OldProductResult( | ||
{this.status, this.barcode, this.statusVerbose, this.product}); | ||
|
||
factory OldProductResult.fromJson(Map<String, dynamic> json) => | ||
_$OldProductResultFromJson(json); | ||
|
||
@override | ||
Map<String, dynamic> toJson() => _$OldProductResultToJson(this); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import 'package:openfoodfacts/openfoodfacts.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
import 'test_constants.dart'; | ||
|
||
void main() { | ||
OpenFoodAPIConfiguration.userAgent = TestConstants.TEST_USER_AGENT; | ||
OpenFoodAPIConfiguration.globalUser = TestConstants.PROD_USER; | ||
|
||
const UriProductHelper uriHelperBeautyProd = UriProductHelper( | ||
host: 'world.openbeautyfacts.org', | ||
imageUrlBase: 'https://static.openbeautyfacts.org/images/products/', | ||
); | ||
const UriProductHelper uriHelperProductsProd = UriProductHelper( | ||
host: 'world.openproductsfacts.org', | ||
imageUrlBase: 'https://static.openproductsfacts.org/images/products/', | ||
); | ||
const UriProductHelper uriHelperPetFoodProd = UriProductHelper( | ||
host: 'world.openpetfoodfacts.org', | ||
imageUrlBase: 'https://static.openpetfoodfacts.org/images/products/', | ||
); | ||
|
||
const String beautyBarcode = '4056489234692'; | ||
const String productsBarcode = '7898927451035'; | ||
const String petFoodBarcode = '3564700266809'; | ||
|
||
group('$OpenFoodAPIClient get not food products', () { | ||
Future<Product?> findProduct( | ||
final String barcode, | ||
final UriProductHelper uriHelper, | ||
final bool shouldBeThere, | ||
) async { | ||
final ProductQueryConfiguration configurations = | ||
ProductQueryConfiguration( | ||
barcode, | ||
language: OpenFoodFactsLanguage.ENGLISH, | ||
fields: [ProductField.ALL], | ||
version: ProductQueryVersion(2), | ||
); | ||
final OldProductResult result = await OpenFoodAPIClient.getOldProduct( | ||
configurations, | ||
uriHelper: uriHelper, | ||
); | ||
if (shouldBeThere) { | ||
expect(result.status, 1); | ||
expect(result.barcode, barcode); | ||
expect(result.product, isNotNull); | ||
expect(result.product!.barcode, barcode); | ||
} 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); | ||
}); | ||
}, | ||
timeout: Timeout( | ||
// some tests can be slow here | ||
Duration(seconds: 300), | ||
)); | ||
} |