forked from eatvio/eatvio_app
290 lines
8.7 KiB
Dart
290 lines
8.7 KiB
Dart
import 'package:eatvio_app/features/diary/models/diary_day_model.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
void main() {
|
|
group('PortionState enum', () {
|
|
test('has all expected values', () {
|
|
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);
|
|
});
|
|
});
|
|
|
|
group('DiaryDayWrapperModel', () {
|
|
test('fromJson parses diary correctly', () {
|
|
final json = {
|
|
'diary': {
|
|
'id': 1,
|
|
'date': '2024-01-15',
|
|
'closed': 0,
|
|
'comment': 'Test comment',
|
|
'state': {'has_chosen_diet': false, 'has_chosen_plan': false, 'last_state': null},
|
|
'meals': [],
|
|
'chosen_plan_id': null,
|
|
'day_of_chosen_diet': null,
|
|
'chosen_diet': null,
|
|
},
|
|
};
|
|
|
|
final model = DiaryDayWrapperModel.fromJson(json);
|
|
|
|
expect(model.diary.id, 1);
|
|
expect(model.diary.date, '2024-01-15');
|
|
expect(model.diary.comment, 'Test comment');
|
|
});
|
|
|
|
test('toJson serializes diary correctly', () {
|
|
const diary = DiaryDayModel(
|
|
id: 1,
|
|
date: '2024-01-15',
|
|
closed: 0,
|
|
comment: 'Test comment',
|
|
typeOfDiary: TypeOfDiaryModel(hasChosenDiet: false, hasChosenPlan: false, diaryState: null),
|
|
meals: [],
|
|
chosenPlanId: null,
|
|
dayOfChosenDiet: null,
|
|
diet: null,
|
|
);
|
|
const model = DiaryDayWrapperModel(diary: diary);
|
|
|
|
final json = model.toJson();
|
|
|
|
expect(json, isA<Map<String, dynamic>>());
|
|
expect(json.containsKey('diary'), isTrue);
|
|
// Freezed may not serialize nested objects automatically, so we just verify the key exists
|
|
expect(json['diary'], isNotNull);
|
|
});
|
|
});
|
|
|
|
group('DiaryDietModel', () {
|
|
test('fromJson parses all fields correctly', () {
|
|
final json = {'id': 5, 'duration': 30, 'name': 'Test Diet'};
|
|
|
|
final model = DiaryDietModel.fromJson(json);
|
|
|
|
expect(model.id, 5);
|
|
expect(model.duration, 30);
|
|
expect(model.name, 'Test Diet');
|
|
});
|
|
|
|
test('toJson serializes all fields correctly', () {
|
|
const model = DiaryDietModel(id: 5, duration: 30, name: 'Test Diet');
|
|
|
|
final json = model.toJson();
|
|
|
|
expect(json['id'], 5);
|
|
expect(json['duration'], 30);
|
|
expect(json['name'], 'Test Diet');
|
|
});
|
|
});
|
|
|
|
group('DiaryPhotoModel', () {
|
|
test('fromJson parses all fields correctly', () {
|
|
final json = {'id': 10, 'original_name': 'photo.jpg', 'path': '/path/to/photo.jpg', 'size': 1024};
|
|
|
|
final model = DiaryPhotoModel.fromJson(json);
|
|
|
|
expect(model.id, 10);
|
|
expect(model.originalName, 'photo.jpg');
|
|
expect(model.path, '/path/to/photo.jpg');
|
|
expect(model.size, 1024);
|
|
});
|
|
|
|
test('toJson serializes all fields correctly', () {
|
|
const model = DiaryPhotoModel(id: 10, originalName: 'photo.jpg', path: '/path/to/photo.jpg', size: 1024);
|
|
|
|
final json = model.toJson();
|
|
|
|
expect(json['id'], 10);
|
|
expect(json['original_name'], 'photo.jpg');
|
|
expect(json['path'], '/path/to/photo.jpg');
|
|
expect(json['size'], 1024);
|
|
});
|
|
});
|
|
|
|
group('ProductCaloriesInfoModel', () {
|
|
test('fromJson parses all fields correctly', () {
|
|
final json = {
|
|
'id': 1,
|
|
'product_name': 'Apple',
|
|
'energy_on_100gr': 52.0,
|
|
'proteins_on_100gr': 0.3,
|
|
'fats_on_100gr': 0.2,
|
|
'carbohydrates_on_100gr': 14.0,
|
|
'energy_on_portion': 26.0,
|
|
'proteins_on_portion': 0.15,
|
|
'fats_on_portion': 0.1,
|
|
'carbohydrates_on_portion': 7.0,
|
|
};
|
|
|
|
final model = ProductCaloriesInfoModel.fromJson(json);
|
|
|
|
expect(model.id, 1);
|
|
expect(model.productName, 'Apple');
|
|
expect(model.calories, 52.0);
|
|
expect(model.proteins, 0.3);
|
|
expect(model.fats, 0.2);
|
|
expect(model.carbohydrates, 14.0);
|
|
expect(model.energyOnPortion, 26.0);
|
|
expect(model.proteinsOnPortion, 0.15);
|
|
expect(model.fatsOnPortion, 0.1);
|
|
expect(model.carbohydratesOnPortion, 7.0);
|
|
});
|
|
|
|
test('toJson serializes all fields correctly', () {
|
|
const model = ProductCaloriesInfoModel(
|
|
id: 1,
|
|
productName: 'Apple',
|
|
calories: 52.0,
|
|
proteins: 0.3,
|
|
fats: 0.2,
|
|
carbohydrates: 14.0,
|
|
energyOnPortion: 26.0,
|
|
proteinsOnPortion: 0.15,
|
|
fatsOnPortion: 0.1,
|
|
carbohydratesOnPortion: 7.0,
|
|
);
|
|
|
|
final json = model.toJson();
|
|
|
|
expect(json['id'], 1);
|
|
expect(json['product_name'], 'Apple');
|
|
expect(json['energy_on_100gr'], 52.0);
|
|
expect(json['proteins_on_100gr'], 0.3);
|
|
expect(json['fats_on_100gr'], 0.2);
|
|
expect(json['carbohydrates_on_100gr'], 14.0);
|
|
expect(json['energy_on_portion'], 26.0);
|
|
expect(json['proteins_on_portion'], 0.15);
|
|
expect(json['fats_on_portion'], 0.1);
|
|
expect(json['carbohydrates_on_portion'], 7.0);
|
|
});
|
|
});
|
|
|
|
group('ProductInMeal', () {
|
|
test('fromJson parses all fields correctly', () {
|
|
final json = {'product_name': 'Banana', 'portion': '1 piece', 'quantity': 2};
|
|
|
|
final model = ProductInMeal.fromJson(json);
|
|
|
|
expect(model.productName, 'Banana');
|
|
expect(model.portion, '1 piece');
|
|
expect(model.quantity, 2);
|
|
});
|
|
|
|
test('toJson serializes all fields correctly', () {
|
|
const model = ProductInMeal(productName: 'Banana', portion: '1 piece', quantity: 2);
|
|
|
|
final json = model.toJson();
|
|
|
|
expect(json['product_name'], 'Banana');
|
|
expect(json['portion'], '1 piece');
|
|
expect(json['quantity'], 2);
|
|
});
|
|
});
|
|
|
|
group('DiaryDayModel', () {
|
|
test('toEntity returns null when id is null', () {
|
|
const model = DiaryDayModel(
|
|
id: null,
|
|
date: '2024-01-15',
|
|
closed: 0,
|
|
comment: 'Test',
|
|
typeOfDiary: TypeOfDiaryModel(hasChosenDiet: false, hasChosenPlan: false, diaryState: null),
|
|
meals: [],
|
|
chosenPlanId: null,
|
|
dayOfChosenDiet: null,
|
|
diet: null,
|
|
);
|
|
|
|
final entity = model.toEntity();
|
|
|
|
expect(entity, isNull);
|
|
});
|
|
|
|
test('toEntity returns entity when id is not null', () {
|
|
const model = DiaryDayModel(
|
|
id: 1,
|
|
date: '2024-01-15',
|
|
wakeup: '08:00',
|
|
retire: '22:00',
|
|
closed: 0,
|
|
comment: 'Test comment',
|
|
typeOfDiary: TypeOfDiaryModel(hasChosenDiet: false, hasChosenPlan: false, diaryState: null),
|
|
meals: [],
|
|
chosenPlanId: null,
|
|
dayOfChosenDiet: null,
|
|
diet: null,
|
|
);
|
|
|
|
final entity = model.toEntity();
|
|
|
|
expect(entity, isNotNull);
|
|
expect(entity?.id, 1);
|
|
expect(entity?.date, '2024-01-15');
|
|
expect(entity?.wakeup, '08:00');
|
|
expect(entity?.retire, '22:00');
|
|
expect(entity?.comment, 'Test comment');
|
|
});
|
|
|
|
test('toDiaryFutureDayEntity returns correct entity', () {
|
|
const diet = DiaryDietModel(id: 1, duration: 30, name: 'Test Diet');
|
|
const model = DiaryDayModel(
|
|
id: 1,
|
|
date: '2024-01-15',
|
|
closed: 0,
|
|
comment: 'Test',
|
|
typeOfDiary: TypeOfDiaryModel(hasChosenDiet: true, hasChosenPlan: false, diaryState: null),
|
|
meals: [],
|
|
chosenPlanId: null,
|
|
dayOfChosenDiet: 5,
|
|
diet: diet,
|
|
);
|
|
|
|
final entity = model.toDiaryFutureDayEntity();
|
|
|
|
expect(entity.date, '2024-01-15');
|
|
expect(entity.dayOfChosenDiet, 5);
|
|
expect(entity.diet, diet);
|
|
});
|
|
});
|
|
}
|