fix(diary): parse commented_from_diet/reviewed_from_diet portion states, fallback unknown states to PortionState.unknown

This commit is contained in:
Anatoliy Poloz
2026-10-16 21:12:40 +03:00
parent 293601f1e1
commit 39a512193e
2 changed files with 41 additions and 2 deletions

View File

@@ -18,8 +18,13 @@ enum PortionState {
@JsonValue('eaten_edited')
eatenEdited,
commented,
@JsonValue('commented_from_diet')
commentedFromDiet,
@JsonValue('reviewed_by_user')
reviewedByUser,
@JsonValue('reviewed_from_diet')
reviewedFromDiet,
unknown,
}
@freezed
@@ -138,7 +143,7 @@ abstract class PortionModel with _$PortionModel {
@JsonKey(name: "quantity") required int quantity,
@JsonKey(name: "portion") required String portion,
@JsonKey(name: "from_diet") required bool fromDiet,
@JsonKey(name: "state") required PortionState portionState,
@JsonKey(name: "state", unknownEnumValue: PortionState.unknown) required PortionState portionState,
}) = _PortionModel;
factory PortionModel.fromJson(Map<String, dynamic> json) => _$PortionModelFromJson(json);

View File

@@ -4,14 +4,48 @@ import 'package:flutter_test/flutter_test.dart';
void main() {
group('PortionState enum', () {
test('has all expected values', () {
expect(PortionState.values.length, 7);
expect(PortionState.values.length, 10);
expect(PortionState.filledDiet, isNotNull);
expect(PortionState.eatenFromDiet, isNotNull);
expect(PortionState.eaten, isNotNull);
expect(PortionState.eatenDeleted, isNotNull);
expect(PortionState.eatenEdited, isNotNull);
expect(PortionState.commented, isNotNull);
expect(PortionState.commentedFromDiet, isNotNull);
expect(PortionState.reviewedByUser, isNotNull);
expect(PortionState.reviewedFromDiet, isNotNull);
expect(PortionState.unknown, isNotNull);
});
});
group('PortionModel.fromJson', () {
Map<String, dynamic> portionJson({required String state}) => {
'id': 1,
'product_nutritional_value_id': null,
'product_calories_info': null,
'product_name': 'Oatmeal',
'quantity': 1,
'portion': '100g',
'from_diet': true,
'state': state,
};
test('parses commented_from_diet state', () {
final model = PortionModel.fromJson(portionJson(state: 'commented_from_diet'));
expect(model.portionState, PortionState.commentedFromDiet);
});
test('parses reviewed_from_diet state', () {
final model = PortionModel.fromJson(portionJson(state: 'reviewed_from_diet'));
expect(model.portionState, PortionState.reviewedFromDiet);
});
test('does not throw on unknown state from backend', () {
final model = PortionModel.fromJson(portionJson(state: 'some_future_state'));
expect(model.portionState, PortionState.unknown);
});
});