Skip to content

Commit

Permalink
refactor: unit tests - so that they work again
Browse files Browse the repository at this point in the history
Impacted files:
* `api_get_product_test.dart`: split a big test into a group and several tests, for better clarity and in order to avoid time-outs
* `api_macthed_product_v2_tets.dart`:  slightly changed expected results as server data slightly changed
* `api_save_product_v3_test.dart`: skipped tests that are unreliable on TEST env
  • Loading branch information
monsieurtanuki committed Aug 2, 2023
1 parent b20a0be commit 1d47f9f
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 98 deletions.
183 changes: 93 additions & 90 deletions test/api_get_product_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1148,16 +1148,13 @@ void main() {
expect(result.product!.entryDates, hasLength(3));
});

test('get new packagings field', () async {
group('$OpenFoodAPIClient get new packagings field', () {
const String barcode = '3661344723290';
const String searchTerms = 'skyr les 2 vaches';
const OpenFoodFactsLanguage language = OpenFoodFactsLanguage.FRENCH;
const OpenFoodFactsCountry country = OpenFoodFactsCountry.FRANCE;
const ProductQueryVersion version = ProductQueryVersion.v3;

late ProductResultV3 productResult;
late SearchResult searchResult;

void checkProduct(final Product product) {
void checkLocalizedTag(final LocalizedTag? tag) {
expect(tag, isNotNull);
Expand All @@ -1175,104 +1172,110 @@ void main() {
}
}

// checking PACKAGINGS as a single field on a barcode search
productResult = await OpenFoodAPIClient.getProductV3(
ProductQueryConfiguration(
barcode,
fields: [ProductField.PACKAGINGS],
language: language,
country: country,
version: version,
),
);
expect(productResult.status, ProductResultV3.statusSuccess);
expect(productResult.product, isNotNull);
checkProduct(productResult.product!);
test('as a single field on a barcode search', () async {
final ProductResultV3 productResult =
await OpenFoodAPIClient.getProductV3(
ProductQueryConfiguration(
barcode,
fields: [ProductField.PACKAGINGS],
language: language,
country: country,
version: version,
),
);
expect(productResult.status, ProductResultV3.statusSuccess);
expect(productResult.product, isNotNull);
checkProduct(productResult.product!);
});

// checking PACKAGINGS as a part of ALL fields on a barcode search
productResult = await OpenFoodAPIClient.getProductV3(
ProductQueryConfiguration(
barcode,
fields: [ProductField.ALL],
language: language,
country: country,
version: version,
),
);
expect(productResult.status, ProductResultV3.statusSuccess);
expect(productResult.product, isNotNull);
checkProduct(productResult.product!);

late bool found;
// checking PACKAGINGS as a single field on a search query
searchResult = await OpenFoodAPIClient.searchProducts(
null,
ProductSearchQueryConfiguration(
parametersList: [
SearchTerms(terms: [searchTerms])
],
fields: [ProductField.PACKAGINGS, ProductField.BARCODE],
language: language,
country: country,
version: version,
),
);
expect(searchResult.products, isNotNull);
expect(searchResult.products, isNotEmpty);
found = false;
for (final Product product in searchResult.products!) {
if (product.barcode != barcode) {
continue;
}
found = true;
checkProduct(product);
}
expect(found, isTrue);

// checking PACKAGINGS as a part of ALL fields on a search query
searchResult = await OpenFoodAPIClient.searchProducts(
null,
ProductSearchQueryConfiguration(
parametersList: [
SearchTerms(terms: [searchTerms])
],
fields: [ProductField.ALL],
language: language,
country: country,
version: version,
),
);
expect(searchResult.products, isNotNull);
expect(searchResult.products, isNotEmpty);
found = false;
for (final Product product in searchResult.products!) {
if (product.barcode != barcode) {
continue;
test('as a part of ALL fields on a barcode search', () async {
final ProductResultV3 productResult =
await OpenFoodAPIClient.getProductV3(
ProductQueryConfiguration(
barcode,
fields: [ProductField.ALL],
language: language,
country: country,
version: version,
),
);
expect(productResult.status, ProductResultV3.statusSuccess);
expect(productResult.product, isNotNull);
checkProduct(productResult.product!);
});

test('as a single field on a search query', () async {
final SearchResult searchResult = await OpenFoodAPIClient.searchProducts(
null,
ProductSearchQueryConfiguration(
parametersList: [
SearchTerms(terms: [searchTerms])
],
fields: [ProductField.PACKAGINGS, ProductField.BARCODE],
language: language,
country: country,
version: version,
),
);
expect(searchResult.products, isNotNull);
expect(searchResult.products, isNotEmpty);
bool found = false;
for (final Product product in searchResult.products!) {
if (product.barcode != barcode) {
continue;
}
found = true;
checkProduct(product);
}
found = true;
checkProduct(product);
}
expect(found, isTrue);
expect(found, isTrue);
});

// checking PACKAGINGS as a part of RAW fields on a search query
try {
searchResult = await OpenFoodAPIClient.searchProducts(
test('as a part of ALL fields on a search query', () async {
final SearchResult searchResult = await OpenFoodAPIClient.searchProducts(
null,
ProductSearchQueryConfiguration(
parametersList: [
SearchTerms(terms: [searchTerms])
],
fields: [ProductField.RAW],
fields: [ProductField.ALL],
language: language,
country: country,
version: version,
),
);
} catch (e) {
// In RAW mode the packagings are mere String's instead of LocalizedTag's.
// Therefore we expect an Exception.
return;
}
fail('On Raw');
expect(searchResult.products, isNotNull);
expect(searchResult.products, isNotEmpty);
bool found = false;
for (final Product product in searchResult.products!) {
if (product.barcode != barcode) {
continue;
}
found = true;
checkProduct(product);
}
expect(found, isTrue);
});

test('as a part of RAW fields on a search query', () async {
try {
await OpenFoodAPIClient.searchProducts(
null,
ProductSearchQueryConfiguration(
parametersList: [
SearchTerms(terms: [searchTerms])
],
fields: [ProductField.RAW],
language: language,
country: country,
version: version,
),
);
} catch (e) {
// In RAW mode the packagings are mere String's instead of LocalizedTag's.
// Therefore we expect an Exception.
return;
}
fail('On Raw');
});
});
}
4 changes: 2 additions & 2 deletions test/api_matched_product_v2_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void main() {
BARCODE_ORIENTALES: _Score(100, MatchedProductStatusV2.VERY_GOOD_MATCH),
BARCODE_HACK: _Score(100, MatchedProductStatusV2.VERY_GOOD_MATCH),
BARCODE_SCHNITZEL: _Score(100, MatchedProductStatusV2.VERY_GOOD_MATCH),
BARCODE_CHIPOLATA: _Score(0, MatchedProductStatusV2.UNKNOWN_MATCH),
BARCODE_CHIPOLATA: _Score(100, MatchedProductStatusV2.VERY_GOOD_MATCH),
BARCODE_FLEISCHWURST: _Score(100, MatchedProductStatusV2.VERY_GOOD_MATCH),
BARCODE_POULET: _Score(0, MatchedProductStatusV2.UNKNOWN_MATCH),
BARCODE_SAUCISSON: _Score(0, MatchedProductStatusV2.DOES_NOT_MATCH),
Expand All @@ -63,13 +63,13 @@ void main() {
BARCODE_CHORIZO: _Score(0, MatchedProductStatusV2.DOES_NOT_MATCH),
};
final List<String> expectedBarcodeOrder = <String>[
BARCODE_CHIPOLATA,
BARCODE_FLEISCHWURST,
BARCODE_KNACKI,
BARCODE_CORDONBLEU,
BARCODE_ORIENTALES,
BARCODE_HACK,
BARCODE_SCHNITZEL,
BARCODE_CHIPOLATA,
BARCODE_POULET,
BARCODE_SAUCISSON,
BARCODE_PIZZA,
Expand Down
8 changes: 2 additions & 6 deletions test/api_save_product_v3_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void main() {
expect(answer.field, isNotNull);
expect(answer.field!.id, 'recycling');
expect(answer.field!.value, '${language.offTag}:$unknownRecycling');
}, skip: 'Avoiding tests on TEST env');
});

test('save packagings_complete', () async {
final List<bool> values = [false, true, false];
Expand Down Expand Up @@ -117,9 +117,5 @@ void main() {
expect(readStatus.product!.packagingsComplete, value);
}
});
},
timeout: Timeout(
// some tests can be slow here
Duration(seconds: 90),
));
}, skip: 'Avoiding tests on TEST env');
}

0 comments on commit 1d47f9f

Please sign in to comment.