forked from eatvio/eatvio_app
feat(insights): added kbju fetching for analitics
This commit is contained in:
@@ -31,24 +31,70 @@ void main() {
|
||||
};
|
||||
|
||||
test('parses commented_from_diet state', () {
|
||||
final model = PortionModel.fromJson(portionJson(state: 'commented_from_diet'));
|
||||
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'));
|
||||
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'));
|
||||
final model = PortionModel.fromJson(
|
||||
portionJson(state: 'some_future_state'),
|
||||
);
|
||||
|
||||
expect(model.portionState, PortionState.unknown);
|
||||
});
|
||||
});
|
||||
|
||||
group('DiaryDayModel.user (владелец дневника)', () {
|
||||
Map<String, dynamic> diaryJson({Object? user}) => {
|
||||
'id': 1,
|
||||
'date': '2026-11-09',
|
||||
'closed': 0,
|
||||
'comment': '',
|
||||
'state': {
|
||||
'has_chosen_diet': false,
|
||||
'has_chosen_plan': false,
|
||||
'last_state': null,
|
||||
},
|
||||
'meals': <dynamic>[],
|
||||
'chosen_plan_id': null,
|
||||
'day_of_chosen_diet': null,
|
||||
'chosen_diet': null,
|
||||
'user': ?user,
|
||||
};
|
||||
|
||||
test('user.slug попадает в entity как ownerSlug', () {
|
||||
final model = DiaryDayModel.fromJson(
|
||||
diaryJson(user: {'id': 3577, 'slug': 'id-3577'}),
|
||||
);
|
||||
|
||||
expect(model.owner?.id, 3577);
|
||||
expect(model.toEntity()!.ownerSlug, 'id-3577');
|
||||
});
|
||||
|
||||
test('старый ответ без user: ownerSlug == null', () {
|
||||
expect(DiaryDayModel.fromJson(diaryJson()).toEntity()!.ownerSlug, isNull);
|
||||
});
|
||||
|
||||
test('user.slug == null (у владельца нет slug): ownerSlug == null', () {
|
||||
final model = DiaryDayModel.fromJson(
|
||||
diaryJson(user: {'id': 1, 'slug': null}),
|
||||
);
|
||||
|
||||
expect(model.toEntity()!.ownerSlug, isNull);
|
||||
});
|
||||
});
|
||||
|
||||
group('DiaryDayWrapperModel', () {
|
||||
test('fromJson parses diary correctly', () {
|
||||
final json = {
|
||||
@@ -57,7 +103,11 @@ void main() {
|
||||
'date': '2024-01-15',
|
||||
'closed': 0,
|
||||
'comment': 'Test comment',
|
||||
'state': {'has_chosen_diet': false, 'has_chosen_plan': false, 'last_state': null},
|
||||
'state': {
|
||||
'has_chosen_diet': false,
|
||||
'has_chosen_plan': false,
|
||||
'last_state': null,
|
||||
},
|
||||
'meals': [],
|
||||
'chosen_plan_id': null,
|
||||
'day_of_chosen_diet': null,
|
||||
@@ -78,7 +128,11 @@ void main() {
|
||||
date: '2024-01-15',
|
||||
closed: 0,
|
||||
comment: 'Test comment',
|
||||
typeOfDiary: TypeOfDiaryModel(hasChosenDiet: false, hasChosenPlan: false, diaryState: null),
|
||||
typeOfDiary: TypeOfDiaryModel(
|
||||
hasChosenDiet: false,
|
||||
hasChosenPlan: false,
|
||||
diaryState: null,
|
||||
),
|
||||
meals: [],
|
||||
chosenPlanId: null,
|
||||
dayOfChosenDiet: null,
|
||||
@@ -119,7 +173,12 @@ void main() {
|
||||
|
||||
group('DiaryPhotoModel', () {
|
||||
test('fromJson parses all fields correctly', () {
|
||||
final json = {'id': 10, 'original_name': 'photo.jpg', 'path': '/path/to/photo.jpg', 'size': 1024};
|
||||
final json = {
|
||||
'id': 10,
|
||||
'original_name': 'photo.jpg',
|
||||
'path': '/path/to/photo.jpg',
|
||||
'size': 1024,
|
||||
};
|
||||
|
||||
final model = DiaryPhotoModel.fromJson(json);
|
||||
|
||||
@@ -130,7 +189,12 @@ void main() {
|
||||
});
|
||||
|
||||
test('toJson serializes all fields correctly', () {
|
||||
const model = DiaryPhotoModel(id: 10, originalName: 'photo.jpg', path: '/path/to/photo.jpg', size: 1024);
|
||||
const model = DiaryPhotoModel(
|
||||
id: 10,
|
||||
originalName: 'photo.jpg',
|
||||
path: '/path/to/photo.jpg',
|
||||
size: 1024,
|
||||
);
|
||||
|
||||
final json = model.toJson();
|
||||
|
||||
@@ -201,7 +265,11 @@ void main() {
|
||||
|
||||
group('ProductInMeal', () {
|
||||
test('fromJson parses all fields correctly', () {
|
||||
final json = {'product_name': 'Banana', 'portion': '1 piece', 'quantity': 2};
|
||||
final json = {
|
||||
'product_name': 'Banana',
|
||||
'portion': '1 piece',
|
||||
'quantity': 2,
|
||||
};
|
||||
|
||||
final model = ProductInMeal.fromJson(json);
|
||||
|
||||
@@ -211,7 +279,11 @@ void main() {
|
||||
});
|
||||
|
||||
test('toJson serializes all fields correctly', () {
|
||||
const model = ProductInMeal(productName: 'Banana', portion: '1 piece', quantity: 2);
|
||||
const model = ProductInMeal(
|
||||
productName: 'Banana',
|
||||
portion: '1 piece',
|
||||
quantity: 2,
|
||||
);
|
||||
|
||||
final json = model.toJson();
|
||||
|
||||
@@ -228,7 +300,11 @@ void main() {
|
||||
date: '2024-01-15',
|
||||
closed: 0,
|
||||
comment: 'Test',
|
||||
typeOfDiary: TypeOfDiaryModel(hasChosenDiet: false, hasChosenPlan: false, diaryState: null),
|
||||
typeOfDiary: TypeOfDiaryModel(
|
||||
hasChosenDiet: false,
|
||||
hasChosenPlan: false,
|
||||
diaryState: null,
|
||||
),
|
||||
meals: [],
|
||||
chosenPlanId: null,
|
||||
dayOfChosenDiet: null,
|
||||
@@ -248,7 +324,11 @@ void main() {
|
||||
retire: '22:00',
|
||||
closed: 0,
|
||||
comment: 'Test comment',
|
||||
typeOfDiary: TypeOfDiaryModel(hasChosenDiet: false, hasChosenPlan: false, diaryState: null),
|
||||
typeOfDiary: TypeOfDiaryModel(
|
||||
hasChosenDiet: false,
|
||||
hasChosenPlan: false,
|
||||
diaryState: null,
|
||||
),
|
||||
meals: [],
|
||||
chosenPlanId: null,
|
||||
dayOfChosenDiet: null,
|
||||
@@ -272,7 +352,11 @@ void main() {
|
||||
date: '2024-01-15',
|
||||
closed: 0,
|
||||
comment: 'Test',
|
||||
typeOfDiary: TypeOfDiaryModel(hasChosenDiet: true, hasChosenPlan: false, diaryState: null),
|
||||
typeOfDiary: TypeOfDiaryModel(
|
||||
hasChosenDiet: true,
|
||||
hasChosenPlan: false,
|
||||
diaryState: null,
|
||||
),
|
||||
meals: [],
|
||||
chosenPlanId: null,
|
||||
dayOfChosenDiet: 5,
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:eatvio_app/core/app_config.dart';
|
||||
import 'package:eatvio_app/core/app_initializer.dart';
|
||||
import 'package:eatvio_app/core/locator_service.dart';
|
||||
@@ -70,11 +72,13 @@ void main() {
|
||||
bool isDiaryFinished = false,
|
||||
bool isDiaryCommented = false,
|
||||
int closed = 0,
|
||||
String? ownerSlug,
|
||||
String wakeup = '07:00',
|
||||
}) {
|
||||
final diary = DiaryDayEntity(
|
||||
id: 1,
|
||||
date: date,
|
||||
wakeup: '07:00',
|
||||
wakeup: wakeup,
|
||||
retire: '23:00',
|
||||
closed: closed,
|
||||
comment: '',
|
||||
@@ -97,6 +101,7 @@ void main() {
|
||||
chosenPlanId: hasChosenPlan ? 10 : null,
|
||||
dayOfChosenDiet: null,
|
||||
diet: null,
|
||||
ownerSlug: ownerSlug,
|
||||
);
|
||||
const nutrition = NutritionDataModel(
|
||||
dailyNutritionIntake: NutritionModel(
|
||||
@@ -391,12 +396,14 @@ void main() {
|
||||
WidgetTester tester, {
|
||||
required bool isConsultantView,
|
||||
String? ownerSlug,
|
||||
String? diaryOwnerSlug,
|
||||
Stream<DiaryState>? diaryStates,
|
||||
required FakeInsightsRepository insightsRepository,
|
||||
}) async {
|
||||
final diaryCubit = MockDiaryCubit();
|
||||
final state = loadedState(hasChosenPlan: false).copyWith(isConsultantView: isConsultantView);
|
||||
final state = loadedState(hasChosenPlan: false, ownerSlug: diaryOwnerSlug).copyWith(isConsultantView: isConsultantView);
|
||||
when(diaryCubit.state).thenReturn(state);
|
||||
when(diaryCubit.stream).thenAnswer((_) => const Stream<DiaryState>.empty());
|
||||
when(diaryCubit.stream).thenAnswer((_) => diaryStates ?? const Stream<DiaryState>.empty());
|
||||
when(diaryCubit.close()).thenAnswer((_) async => Future<void>.value());
|
||||
when(
|
||||
diaryCubit.fetchDiaryV2(needLoading: anyNamed('needLoading'), diaryId: anyNamed('diaryId')),
|
||||
@@ -426,7 +433,7 @@ void main() {
|
||||
expect(find.byType(InsightsCard), findsOneWidget);
|
||||
expect(tester.widget<InsightsCard>(find.byType(InsightsCard)).slug, isNull);
|
||||
expect(tester.widget<InsightsCard>(find.byType(InsightsCard)).canAdd, isTrue);
|
||||
expect(insightsRepository.insightsRequests.single.slug, isNull);
|
||||
expect(insightsRepository.serverRequests.single.slug, isNull);
|
||||
});
|
||||
|
||||
testWidgets('consultant view shows insights card of the client without add button', (tester) async {
|
||||
@@ -437,7 +444,76 @@ void main() {
|
||||
final card = tester.widget<InsightsCard>(find.byType(InsightsCard));
|
||||
expect(card.slug, 'client');
|
||||
expect(card.canAdd, isFalse);
|
||||
expect(insightsRepository.insightsRequests.single.slug, 'client');
|
||||
expect(insightsRepository.serverRequests.single.slug, 'client');
|
||||
});
|
||||
|
||||
testWidgets('consultant view takes the owner slug from the diary (user.slug)', (tester) async {
|
||||
final insightsRepository = FakeInsightsRepository();
|
||||
|
||||
await pumpDiaryWithInsights(tester, isConsultantView: true, diaryOwnerSlug: 'client-from-diary', insightsRepository: insightsRepository);
|
||||
|
||||
expect(tester.widget<InsightsCard>(find.byType(InsightsCard)).slug, 'client-from-diary');
|
||||
expect(insightsRepository.serverRequests.single.slug, 'client-from-diary');
|
||||
});
|
||||
|
||||
testWidgets('diary owner slug wins over the route parameter', (tester) async {
|
||||
final insightsRepository = FakeInsightsRepository();
|
||||
|
||||
await pumpDiaryWithInsights(
|
||||
tester,
|
||||
isConsultantView: true,
|
||||
ownerSlug: 'from-route',
|
||||
diaryOwnerSlug: 'from-diary',
|
||||
insightsRepository: insightsRepository,
|
||||
);
|
||||
|
||||
expect(tester.widget<InsightsCard>(find.byType(InsightsCard)).slug, 'from-diary');
|
||||
});
|
||||
|
||||
testWidgets('own diary refreshes insights when meals or wake-up time change', (tester) async {
|
||||
final insightsRepository = FakeInsightsRepository();
|
||||
final states = StreamController<DiaryState>.broadcast();
|
||||
addTearDown(states.close);
|
||||
|
||||
await pumpDiaryWithInsights(tester, isConsultantView: false, diaryStates: states.stream, insightsRepository: insightsRepository);
|
||||
expect(insightsRepository.serverRequests, hasLength(1));
|
||||
|
||||
states.add(loadedState(hasChosenPlan: false, wakeup: '06:30'));
|
||||
await tester.pump(const Duration(milliseconds: 200));
|
||||
|
||||
expect(insightsRepository.serverRequests, hasLength(2));
|
||||
});
|
||||
|
||||
testWidgets('own diary does not refresh insights when nothing relevant changed', (tester) async {
|
||||
final insightsRepository = FakeInsightsRepository();
|
||||
final states = StreamController<DiaryState>.broadcast();
|
||||
addTearDown(states.close);
|
||||
|
||||
await pumpDiaryWithInsights(tester, isConsultantView: false, diaryStates: states.stream, insightsRepository: insightsRepository);
|
||||
|
||||
states.add(loadedState(hasChosenPlan: false));
|
||||
await tester.pump(const Duration(milliseconds: 200));
|
||||
|
||||
expect(insightsRepository.serverRequests, hasLength(1));
|
||||
});
|
||||
|
||||
testWidgets('consultant view does not refresh insights on diary changes', (tester) async {
|
||||
final insightsRepository = FakeInsightsRepository();
|
||||
final states = StreamController<DiaryState>.broadcast();
|
||||
addTearDown(states.close);
|
||||
|
||||
await pumpDiaryWithInsights(
|
||||
tester,
|
||||
isConsultantView: true,
|
||||
ownerSlug: 'client',
|
||||
diaryStates: states.stream,
|
||||
insightsRepository: insightsRepository,
|
||||
);
|
||||
|
||||
states.add(loadedState(hasChosenPlan: false, wakeup: '06:30').copyWith(isConsultantView: true));
|
||||
await tester.pump(const Duration(milliseconds: 200));
|
||||
|
||||
expect(insightsRepository.serverRequests, hasLength(1));
|
||||
});
|
||||
|
||||
testWidgets('consultant view without owner slug hides insights card', (tester) async {
|
||||
@@ -446,7 +522,7 @@ void main() {
|
||||
await pumpDiaryWithInsights(tester, isConsultantView: true, insightsRepository: insightsRepository);
|
||||
|
||||
expect(find.byType(InsightsCard), findsNothing);
|
||||
expect(insightsRepository.insightsRequests, isEmpty);
|
||||
expect(insightsRepository.serverRequests, isEmpty);
|
||||
});
|
||||
|
||||
testWidgets('consultant view hides insights card on 403', (tester) async {
|
||||
|
||||
Reference in New Issue
Block a user