Skip to content

Commit

Permalink
chore: test TEST server first before some tests around getProductV3 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
monsieurtanuki authored Dec 27, 2023
1 parent e24eefe commit 153ec19
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions test/api_get_save_product_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,40 @@ void main() {
const String russianIngredientsAll = 'Мука, вода';
const List<String> russianIngredientsSplit = <String>['Мука', 'Вода'];

/// Returns true if the (TEST) server is in a good mood.
///
/// Will return false if the server is NOT in a good mood, with an explicit
/// "Bad gateway" exception that condemns the whole purpose of those tests.
/// No need to run tests in that case, and we're not here to check if the
/// TEST server works but only if OUR CODE works.
/// May also throw an exception.
Future<bool> checkServer() async {
try {
await OpenFoodAPIClient.getProductV3(
ProductQueryConfiguration(
'7300400481588',
fields: [ProductField.BARCODE],
version: ProductQueryVersion.v3,
),
uriHelper: uriHelper,
);
return true;
} catch (e) {
const String badGatewayError =
'Exception: JSON expected, html found: <head><title>502 Bad Gateway</title></head>';
if (e.toString().contains(badGatewayError)) {
return false;
}
rethrow;
}
}

group('$OpenFoodAPIClient get-save products', () {
test('get product Coca Cola Light', () async {
if (!await checkServer()) {
return;
}

final String barcode = getBookBarcode(0);
const OpenFoodFactsLanguage language = OpenFoodFactsLanguage.GERMAN;
const String genericName = 'Softdrink';
Expand Down Expand Up @@ -129,6 +161,10 @@ void main() {
test(
'localized fields when a product is not available in a second language',
() async {
if (!await checkServer()) {
return;
}

const String barcode = '3333333333333';

final Product englishInputProduct = Product(
Expand Down Expand Up @@ -249,6 +285,10 @@ void main() {

test('localized fields when a product is available in a second language',
() async {
if (!await checkServer()) {
return;
}

const String barcode = '2222222222222';

final Product englishInputProduct = Product(
Expand Down Expand Up @@ -386,6 +426,10 @@ void main() {
});

test('multiple languages and in-languages fields', () async {
if (!await checkServer()) {
return;
}

const String barcode = '2222222222224';

final Product inputProduct = Product(
Expand Down Expand Up @@ -485,6 +529,10 @@ void main() {
});

test('all-languages fields', () async {
if (!await checkServer()) {
return;
}

const String barcode = '2222222222226';

final Product inputProduct = Product(
Expand Down Expand Up @@ -541,6 +589,10 @@ void main() {
test(
'requesting all-langs and in-langs fields together does not break anything',
() async {
if (!await checkServer()) {
return;
}

const String barcode = '2222222222227';

final Product inputProduct = Product(
Expand Down Expand Up @@ -604,6 +656,10 @@ void main() {
test(
'product with quotes',
() async {
if (!await checkServer()) {
return;
}

const String barcode = '2222222222223';
const String productName = 'Quoted Coca "cola"';
const String brands = 'Quoted Coca "Cola"';
Expand Down Expand Up @@ -642,6 +698,10 @@ void main() {
));

test('get minified product', () async {
if (!await checkServer()) {
return;
}

const String barcode = '111111555555';
const String genericName = 'Softdrink';
const String labels = 'MyTestLabel';
Expand Down Expand Up @@ -707,6 +767,10 @@ void main() {
);

test('Without nutriments', () async {
if (!await checkServer()) {
return;
}

await uploadProduct(noNutritionData: true);

final ProductQueryConfiguration configurations =
Expand All @@ -729,6 +793,10 @@ void main() {
}, skip: 'Random results');

test('With nutriments', () async {
if (!await checkServer()) {
return;
}

await uploadProduct(noNutritionData: false);

final ProductQueryConfiguration configurations =
Expand Down

0 comments on commit 153ec19

Please sign in to comment.