Files
eatvio_app/test/features/diary/services/diary_service_test.dart
2025-11-06 00:38:08 +03:00

688 lines
20 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'dart:io';
import 'package:eatvio_app/features/diary/models/consultant_comment_model.dart';
import 'package:eatvio_app/features/diary/models/consultant_review_model.dart';
import 'package:eatvio_app/features/diary/models/create_day_model.dart';
import 'package:eatvio_app/features/diary/models/diary_day_model.dart';
import 'package:eatvio_app/features/diary/models/finish_diary_day_model.dart';
import 'package:eatvio_app/features/diary/models/nutrition_model.dart';
import 'package:eatvio_app/features/diary/services/diary_service.dart';
import 'package:eatvio_app/features/glossary/models/glossary_model.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'diary_service_test.mocks.dart';
@GenerateNiceMocks([MockSpec<DiaryService>()])
void main() {
group('DiaryService', () {
late MockDiaryService mockDiaryService;
setUp(() {
mockDiaryService = MockDiaryService();
});
test('getDiaryIndexByDate - возвращает список дневников по дате', () async {
const date = '2024-01-15';
final expectedDiaryList = [
const DiaryDayModel(
id: 1,
date: date,
closed: 0,
comment: '',
typeOfDiary: TypeOfDiaryModel(
hasChosenDiet: false,
hasChosenPlan: false,
diaryState: null,
),
meals: [],
chosenPlanId: null,
dayOfChosenDiet: null,
diet: null,
),
];
when(mockDiaryService.getDiaryIndexByDate(date: date))
.thenAnswer((_) async => expectedDiaryList);
final result = await mockDiaryService.getDiaryIndexByDate(date: date);
expect(result, isA<List<DiaryDayModel>>());
expect(result.length, 1);
expect(result.first.id, 1);
verify(mockDiaryService.getDiaryIndexByDate(date: date)).called(1);
});
test('getDiaryDay - возвращает дневник по ID', () async {
const diaryId = 1;
const expectedWrapper = DiaryDayWrapperModel(
diary: DiaryDayModel(
id: diaryId,
date: '2024-01-15',
closed: 0,
comment: '',
typeOfDiary: TypeOfDiaryModel(
hasChosenDiet: false,
hasChosenPlan: false,
diaryState: null,
),
meals: [],
chosenPlanId: null,
dayOfChosenDiet: null,
diet: null,
),
);
when(mockDiaryService.getDiaryDay(diary: diaryId))
.thenAnswer((_) async => expectedWrapper);
final result = await mockDiaryService.getDiaryDay(diary: diaryId);
expect(result, isA<DiaryDayWrapperModel>());
expect(result.diary.id, diaryId);
verify(mockDiaryService.getDiaryDay(diary: diaryId)).called(1);
});
test('getNutrition - возвращает данные о питании', () async {
const diaryId = 1;
const expectedNutrition = NutritionDataModel(
dailyNutritionIntake: NutritionModel(
energy: 2000.0,
proteins: 150.0,
fats: 80.0,
carbohydrates: 250.0,
),
dailyNutritionRecommendation: NutritionModel(
energy: 2200.0,
proteins: 160.0,
fats: 90.0,
carbohydrates: 270.0,
),
);
when(mockDiaryService.getNutrition(diary: diaryId))
.thenAnswer((_) async => expectedNutrition);
final result = await mockDiaryService.getNutrition(diary: diaryId);
expect(result, isA<NutritionDataModel>());
expect(result.dailyNutritionIntake.energy, 2000.0);
verify(mockDiaryService.getNutrition(diary: diaryId)).called(1);
});
test('uploadPhotoOfMeal - загружает фото приема пищи', () async {
const diaryId = 1;
const typeNo = 2;
final photo = File('test.jpg');
when(mockDiaryService.uploadPhotoOfMeal(
diary: diaryId,
typeNo: typeNo,
photo: photo,
)).thenAnswer((_) async => Future.value());
await mockDiaryService.uploadPhotoOfMeal(
diary: diaryId,
typeNo: typeNo,
photo: photo,
);
verify(mockDiaryService.uploadPhotoOfMeal(
diary: diaryId,
typeNo: typeNo,
photo: photo,
)).called(1);
});
test('deletePhotoOfMeal - удаляет фото приема пищи', () async {
const diaryId = 1;
const typeNo = 2;
const userAttachment = 5;
when(mockDiaryService.deletePhotoOfMeal(
diaryId: diaryId,
typeNo: typeNo,
userAttachment: userAttachment,
)).thenAnswer((_) async => Future.value());
await mockDiaryService.deletePhotoOfMeal(
diaryId: diaryId,
typeNo: typeNo,
userAttachment: userAttachment,
);
verify(mockDiaryService.deletePhotoOfMeal(
diaryId: diaryId,
typeNo: typeNo,
userAttachment: userAttachment,
)).called(1);
});
test('addProductToMeal - добавляет продукт в прием пищи', () async {
const diaryId = 1;
const typeNo = 2;
const productName = 'Яблоко';
const portion = '100г';
const quantity = 2;
const productNutritionalValueId = 10;
when(mockDiaryService.addProductToMeal(
diary: diaryId,
typeNo: typeNo,
productName: productName,
portion: portion,
quantity: quantity,
productNutritionalValueId: productNutritionalValueId,
)).thenAnswer((_) async => Future.value());
await mockDiaryService.addProductToMeal(
diary: diaryId,
typeNo: typeNo,
productName: productName,
portion: portion,
quantity: quantity,
productNutritionalValueId: productNutritionalValueId,
);
verify(mockDiaryService.addProductToMeal(
diary: diaryId,
typeNo: typeNo,
productName: productName,
portion: portion,
quantity: quantity,
productNutritionalValueId: productNutritionalValueId,
)).called(1);
});
test('addProductToMeal - добавляет продукт без nutritionalValueId', () async {
const diaryId = 1;
const typeNo = 2;
const productName = 'Яблоко';
const portion = '100г';
const quantity = 2;
when(mockDiaryService.addProductToMeal(
diary: diaryId,
typeNo: typeNo,
productName: productName,
portion: portion,
quantity: quantity,
productNutritionalValueId: null,
)).thenAnswer((_) async => Future.value());
await mockDiaryService.addProductToMeal(
diary: diaryId,
typeNo: typeNo,
productName: productName,
portion: portion,
quantity: quantity,
productNutritionalValueId: null,
);
verify(mockDiaryService.addProductToMeal(
diary: diaryId,
typeNo: typeNo,
productName: productName,
portion: portion,
quantity: quantity,
productNutritionalValueId: null,
)).called(1);
});
test('updateProductInMeal - обновляет продукт в приеме пищи', () async {
const diaryId = 1;
const typeNo = 2;
const portionModel = 5;
final productInMeal = {'product_name': 'Яблоко', 'quantity': 3};
when(mockDiaryService.updateProductInMeal(
diary: diaryId,
typeNo: typeNo,
portionModel: portionModel,
productInMeal: productInMeal,
)).thenAnswer((_) async => Future.value());
await mockDiaryService.updateProductInMeal(
diary: diaryId,
typeNo: typeNo,
portionModel: portionModel,
productInMeal: productInMeal,
);
verify(mockDiaryService.updateProductInMeal(
diary: diaryId,
typeNo: typeNo,
portionModel: portionModel,
productInMeal: productInMeal,
)).called(1);
});
test('markEaten - отмечает продукт как съеденный', () async {
const diaryId = 1;
const typeNo = 2;
const portionModel = 5;
when(mockDiaryService.markEaten(
diary: diaryId,
typeNo: typeNo,
portionModel: portionModel,
)).thenAnswer((_) async => Future.value());
await mockDiaryService.markEaten(
diary: diaryId,
typeNo: typeNo,
portionModel: portionModel,
);
verify(mockDiaryService.markEaten(
diary: diaryId,
typeNo: typeNo,
portionModel: portionModel,
)).called(1);
});
test('unmarkEaten - снимает отметку о съеденном продукте', () async {
const diaryId = 1;
const typeNo = 2;
const portionModel = 5;
when(mockDiaryService.unmarkEaten(
diary: diaryId,
typeNo: typeNo,
portionModel: portionModel,
)).thenAnswer((_) async => Future.value());
await mockDiaryService.unmarkEaten(
diary: diaryId,
typeNo: typeNo,
portionModel: portionModel,
);
verify(mockDiaryService.unmarkEaten(
diary: diaryId,
typeNo: typeNo,
portionModel: portionModel,
)).called(1);
});
test('restore - восстанавливает продукт', () async {
const diaryId = 1;
const typeNo = 2;
const portionModel = 5;
when(mockDiaryService.restore(
diary: diaryId,
typeNo: typeNo,
portionModel: portionModel,
)).thenAnswer((_) async => Future.value());
await mockDiaryService.restore(
diary: diaryId,
typeNo: typeNo,
portionModel: portionModel,
);
verify(mockDiaryService.restore(
diary: diaryId,
typeNo: typeNo,
portionModel: portionModel,
)).called(1);
});
test('deleteProductFromMeal - удаляет продукт из приема пищи', () async {
const diaryId = 1;
const typeNo = 2;
const portionModel = 5;
when(mockDiaryService.deleteProductFromMeal(
diary: diaryId,
typeNo: typeNo,
portionModel: portionModel,
)).thenAnswer((_) async => Future.value());
await mockDiaryService.deleteProductFromMeal(
diary: diaryId,
typeNo: typeNo,
portionModel: portionModel,
);
verify(mockDiaryService.deleteProductFromMeal(
diary: diaryId,
typeNo: typeNo,
portionModel: portionModel,
)).called(1);
});
test('finishDiaryDay - завершает день дневника', () async {
const diaryId = 1;
const finishDiaryDayModel = FinishDiaryDayModel(
wakeUp: '07:00',
retire: '23:00',
comment: 'Хороший день',
);
when(mockDiaryService.finishDiaryDay(
diary: diaryId,
finishDiaryDayModel: finishDiaryDayModel,
)).thenAnswer((_) async => Future.value());
await mockDiaryService.finishDiaryDay(
diary: diaryId,
finishDiaryDayModel: finishDiaryDayModel,
);
verify(mockDiaryService.finishDiaryDay(
diary: diaryId,
finishDiaryDayModel: finishDiaryDayModel,
)).called(1);
});
test('finishDiaryDay - завершает день без комментария', () async {
const diaryId = 1;
const finishDiaryDayModel = FinishDiaryDayModel(
wakeUp: '07:00',
retire: '23:00',
comment: null,
);
when(mockDiaryService.finishDiaryDay(
diary: diaryId,
finishDiaryDayModel: finishDiaryDayModel,
)).thenAnswer((_) async => Future.value());
await mockDiaryService.finishDiaryDay(
diary: diaryId,
finishDiaryDayModel: finishDiaryDayModel,
);
verify(mockDiaryService.finishDiaryDay(
diary: diaryId,
finishDiaryDayModel: finishDiaryDayModel,
)).called(1);
});
test('fillSatiety - заполняет сытость', () async {
const diaryId = 1;
const typeNo = 2;
const satiety = 5;
when(mockDiaryService.fillSatiety(
diary: diaryId,
typeNo: typeNo,
satiety: satiety,
)).thenAnswer((_) async => Future.value());
await mockDiaryService.fillSatiety(
diary: diaryId,
typeNo: typeNo,
satiety: satiety,
);
verify(mockDiaryService.fillSatiety(
diary: diaryId,
typeNo: typeNo,
satiety: satiety,
)).called(1);
});
test('fillMood - заполняет настроение', () async {
const diaryId = 1;
const typeNo = 2;
const mood = 4;
when(mockDiaryService.fillMood(
diary: diaryId,
typeNo: typeNo,
mood: mood,
)).thenAnswer((_) async => Future.value());
await mockDiaryService.fillMood(
diary: diaryId,
typeNo: typeNo,
mood: mood,
);
verify(mockDiaryService.fillMood(
diary: diaryId,
typeNo: typeNo,
mood: mood,
)).called(1);
});
test('fillTime - заполняет время приема пищи', () async {
const diaryId = 1;
const typeNo = 2;
const time = '12:30';
when(mockDiaryService.fillTime(
diary: diaryId,
typeNo: typeNo,
time: time,
)).thenAnswer((_) async => Future.value());
await mockDiaryService.fillTime(
diary: diaryId,
typeNo: typeNo,
time: time,
);
verify(mockDiaryService.fillTime(
diary: diaryId,
typeNo: typeNo,
time: time,
)).called(1);
});
test('fillWakeup - заполняет время пробуждения', () async {
const diaryId = 1;
const wakeUp = '07:00';
when(mockDiaryService.fillWakeup(
diary: diaryId,
wakeUp: wakeUp,
)).thenAnswer((_) async => Future.value());
await mockDiaryService.fillWakeup(
diary: diaryId,
wakeUp: wakeUp,
);
verify(mockDiaryService.fillWakeup(
diary: diaryId,
wakeUp: wakeUp,
)).called(1);
});
test('fillRetire - заполняет время отхода ко сну', () async {
const diaryId = 1;
const retire = '23:00';
when(mockDiaryService.fillRetire(
diary: diaryId,
retire: retire,
)).thenAnswer((_) async => Future.value());
await mockDiaryService.fillRetire(
diary: diaryId,
retire: retire,
);
verify(mockDiaryService.fillRetire(
diary: diaryId,
retire: retire,
)).called(1);
});
test('sendProductToModeration - отправляет продукт на модерацию', () async {
final data = {
'name': 'Новый продукт',
'energy': 100.0,
'proteins': 10.0,
'fats': 5.0,
'carbohydrates': 15.0,
};
const expectedModeration = GlossaryModerationModel(
glossary: GlossaryModel(
id: 1,
name: 'Новый продукт',
grInPortion: 100,
energyOn100Gr: 100.0,
proteinsOn100Gr: 10.0,
fatsOn100Gr: 5.0,
carbohydratesOn100Gr: 15.0,
energyOnPortion: 100.0,
proteinsOnPortion: 10.0,
fatsOnPortion: 5.0,
carbohydratesOnPortion: 15.0,
),
);
when(mockDiaryService.sendProductToModeration(data: data))
.thenAnswer((_) async => expectedModeration);
final result = await mockDiaryService.sendProductToModeration(data: data);
expect(result, isA<GlossaryModerationModel>());
expect(result.glossary.id, 1);
verify(mockDiaryService.sendProductToModeration(data: data)).called(1);
});
test('commentUserDay - комментирует день пользователя', () async {
const diaryId = 1;
final commentModel = ConsultantCommentModel(
comment: 'Хороший прогресс',
mealsComments: {},
portions: [],
);
when(mockDiaryService.commentUserDay(id: diaryId, body: commentModel))
.thenAnswer((_) async => Future.value());
await mockDiaryService.commentUserDay(id: diaryId, body: commentModel);
verify(mockDiaryService.commentUserDay(id: diaryId, body: commentModel))
.called(1);
});
test('getDiaryComments - получает комментарии дневника', () async {
const planId = 1;
const diaryId = 1;
final expectedReview = ConsultantReviewModel(
planName: 'План питания',
planDuration: 30,
currentPlanDay: 5,
consultantComments: ConsultantComments(
dayComment: 'Хороший день',
mealsComments: [],
portionsComments: [],
),
);
when(mockDiaryService.getDiaryComments(planId: planId, diaryId: diaryId))
.thenAnswer((_) async => expectedReview);
final result =
await mockDiaryService.getDiaryComments(planId: planId, diaryId: diaryId);
expect(result, isA<ConsultantReviewModel>());
expect(result.planName, 'План питания');
verify(mockDiaryService.getDiaryComments(planId: planId, diaryId: diaryId))
.called(1);
});
test('viewCommentOfDiaryDayByConsultant - отмечает комментарий как просмотренный', () async {
const diaryId = 1;
when(mockDiaryService.viewCommentOfDiaryDayByConsultant(diaryId: diaryId))
.thenAnswer((_) async => Future.value());
await mockDiaryService.viewCommentOfDiaryDayByConsultant(diaryId: diaryId);
verify(mockDiaryService.viewCommentOfDiaryDayByConsultant(diaryId: diaryId))
.called(1);
});
test('getGlossariesIndex - получает список глоссариев', () async {
final filters = {'filters[id][\$in][0]': 1, 'filters[id][\$in][1]': 2};
const expectedGlossary = GlossaryPaginationModel(
glossariesList: [
GlossaryModel(
id: 1,
name: 'Яблоко',
grInPortion: 100,
energyOn100Gr: 52.0,
proteinsOn100Gr: 0.3,
fatsOn100Gr: 0.2,
carbohydratesOn100Gr: 14.0,
energyOnPortion: 52.0,
proteinsOnPortion: 0.3,
fatsOnPortion: 0.2,
carbohydratesOnPortion: 14.0,
),
],
);
when(mockDiaryService.getGlossariesIndex(filters: filters))
.thenAnswer((_) async => expectedGlossary);
final result = await mockDiaryService.getGlossariesIndex(filters: filters);
expect(result, isA<GlossaryPaginationModel>());
expect(result.glossariesList.length, 1);
verify(mockDiaryService.getGlossariesIndex(filters: filters)).called(1);
});
test('getGlossariesIndex - получает список без фильтров', () async {
const expectedGlossary = GlossaryPaginationModel(glossariesList: []);
when(mockDiaryService.getGlossariesIndex(filters: null))
.thenAnswer((_) async => expectedGlossary);
final result = await mockDiaryService.getGlossariesIndex(filters: null);
expect(result, isA<GlossaryPaginationModel>());
verify(mockDiaryService.getGlossariesIndex(filters: null)).called(1);
});
test('createDay - создает новый день дневника', () async {
final createDayModel = CreateDayModel(day: '2024-01-15');
when(mockDiaryService.createDay(body: createDayModel))
.thenAnswer((_) async => Future.value());
await mockDiaryService.createDay(body: createDayModel);
verify(mockDiaryService.createDay(body: createDayModel)).called(1);
});
test('getDiaryDayV2 - получает день дневника v2', () async {
const day = '2024-01-15';
const expectedDiary = DiaryDayModel(
id: 1,
date: day,
closed: 0,
comment: '',
typeOfDiary: TypeOfDiaryModel(
hasChosenDiet: false,
hasChosenPlan: false,
diaryState: null,
),
meals: [],
chosenPlanId: null,
dayOfChosenDiet: null,
diet: null,
);
when(mockDiaryService.getDiaryDayV2(day: day))
.thenAnswer((_) async => expectedDiary);
final result = await mockDiaryService.getDiaryDayV2(day: day);
expect(result, isA<DiaryDayModel>());
expect(result.id, 1);
expect(result.date, day);
verify(mockDiaryService.getDiaryDayV2(day: day)).called(1);
});
});
}