Skip to content

Commit

Permalink
feat: 1003 - new price type field (openfoodfacts#1016)
Browse files Browse the repository at this point in the history
* feat: 1003 - new price type field

New files:
* `price_type.dart`: Type of a Price.

Impacted files:
* `api_prices_test.dart`: additional test on price type
* `openfoodfacts.dart`: exported the new file
* `price.dart`: added the price type field
* `price.g.dart`: generated

* Updated LICENSE copyright year

* Unit tests minor fixes

* Unit tests minor fixes
  • Loading branch information
monsieurtanuki authored Jan 3, 2025
1 parent c583648 commit 5f7b911
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 27 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright 2018-2024 OpenFoodFacts
Copyright 2018-2025 OpenFoodFacts

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
1 change: 1 addition & 0 deletions lib/openfoodfacts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export 'src/prices/price.dart';
export 'src/prices/price_per.dart';
export 'src/prices/price_product.dart';
export 'src/prices/price_total_stats.dart';
export 'src/prices/price_type.dart';
export 'src/prices/price_user.dart';
export 'src/prices/proof.dart';
export 'src/prices/proof_type.dart';
Expand Down
5 changes: 5 additions & 0 deletions lib/src/prices/price.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'location.dart';
import 'location_osm_type.dart';
import 'price_per.dart';
import 'price_product.dart';
import 'price_type.dart';
import 'proof.dart';
import '../interface/json_object.dart';
import '../utils/json_helper.dart';
Expand Down Expand Up @@ -134,6 +135,10 @@ class Price extends JsonObject {
@JsonKey(name: 'receipt_quantity')
int? receiptQuantity;

/// Type.
@JsonKey()
PriceType? type;

/// Owner. Read-only.
@JsonKey()
late String owner;
Expand Down
7 changes: 7 additions & 0 deletions lib/src/prices/price.g.dart

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

22 changes: 22 additions & 0 deletions lib/src/prices/price_type.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import 'package:json_annotation/json_annotation.dart';
import '../model/off_tagged.dart';

/// Type of a Price.
enum PriceType implements OffTagged {
@JsonValue('PRODUCT')
product(offTag: 'PRODUCT'),

@JsonValue('CATEGORY')
category(offTag: 'CATEGORY');

const PriceType({
required this.offTag,
});

@override
final String offTag;

/// Returns the first [PriceType] that matches the [offTag].
static PriceType? fromOffTag(final String? offTag) =>
OffTagged.fromOffTag(offTag, PriceType.values) as PriceType?;
}
4 changes: 1 addition & 3 deletions test/api_get_product_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ void main() {
'Invertzuckersirup',
'natürliches Aroma',
'Schokolade Mürbegebäck',
'PflanzenPalmfett',
'Schokoladenstückchen',
'Kakaomasse',
'Kakaobutter',
Expand Down Expand Up @@ -655,7 +654,7 @@ void main() {
expect(nutritionalQuality.first.settingNote, isNull);
expect(nutritionalQuality.first.description, '');
expect(nutritionalQuality.first.descriptionShort,
'Poor nutritional quality');
'Lower nutritional quality');
expect(nutritionalQuality.first.title, 'Nutri-Score D');
expect(nutritionalQuality.first.name, 'Nutri-Score');
expect(nutritionalQuality.first.match,
Expand Down Expand Up @@ -884,7 +883,6 @@ void main() {

test('get knowledge panels', () async {
const Set<String> someExpectedKeys = <String>{
'ecoscore',
'environment_card',
'health_card',
'ingredients',
Expand Down
35 changes: 12 additions & 23 deletions test/api_prices_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ void main() {
test('create', () async {
final Price initialPrice = Price()
..productCode = '3560071492755'
..type = PriceType.product
..price = 3.99
..currency = Currency.EUR
..locationOSMId = 4966187139
Expand All @@ -175,12 +176,11 @@ void main() {
..priceIsDiscounted = true;

const String invalidBearerToken = 'invalid bearer token';
String bearerToken = invalidBearerToken;

// failing price creation with invalid token
MaybeError<Price?> addedPrice = await OpenPricesAPIClient.createPrice(
price: initialPrice,
bearerToken: bearerToken,
bearerToken: invalidBearerToken,
uriHelper: uriHelper,
);
expect(addedPrice.isError, isTrue);
Expand All @@ -206,19 +206,6 @@ void main() {
final MediaType initialMediaType =
HttpHelper().imagineMediaType(initialImageUri.path)!;

// failing proof upload with invalid token
MaybeError<Proof> uploadProof = await OpenPricesAPIClient.uploadProof(
proofType: uploadProofType,
imageUri: initialImageUri,
mediaType: initialMediaType,
bearerToken: bearerToken,
uriHelper: uriHelper,
);
expect(uploadProof.isError, isTrue);
expect(uploadProof.statusCode, Session.invalidActionWithAuthStatusCode);
expect(uploadProof.detailError,
contains(Session.invalidActionWithAuthMessage));

// authentication
final MaybeError<String> token =
await OpenPricesAPIClient.getAuthenticationToken(
Expand All @@ -228,10 +215,11 @@ void main() {
);
expect(token.isError, isFalse);
expect(token.value, isNotEmpty);
bearerToken = token.value;
final String bearerToken = token.value;

// successful proof upload with valid token
uploadProof = await OpenPricesAPIClient.uploadProof(
final MaybeError<Proof> uploadProof =
await OpenPricesAPIClient.uploadProof(
proofType: uploadProofType,
imageUri: initialImageUri,
mediaType: initialMediaType,
Expand Down Expand Up @@ -296,6 +284,7 @@ void main() {
expect(addedPrice.isError, isFalse);
final Price addedValue = addedPrice.value!;
expect(addedValue.productCode, initialPrice.productCode);
expect(addedValue.type, initialPrice.type);
expect(addedValue.price, initialPrice.price);
expect(
addedValue.priceWithoutDiscount, initialPrice.priceWithoutDiscount);
Expand Down Expand Up @@ -812,13 +801,13 @@ void main() {
// value as of 2024-12-05
expect(result.total, greaterThanOrEqualTo(2040));

// values as of 2024-12-05
// values as of 2025-01-03
const Map<Flavor?, int> expectedMinCounts = <Flavor?, int>{
Flavor.openFoodFacts: 3625952,
Flavor.openBeautyFacts: 31463,
Flavor.openPetFoodFacts: 9955,
Flavor.openProductFacts: 15741,
null: 3688608,
Flavor.openFoodFacts: 3679133,
Flavor.openBeautyFacts: 34557,
Flavor.openPetFoodFacts: 10252,
Flavor.openProductFacts: 15736,
null: 3745994,
};
for (final Flavor? flavor in expectedMinCounts.keys) {
parameters = GetPriceProductsParameters()..source = flavor;
Expand Down

0 comments on commit 5f7b911

Please sign in to comment.