feat(tests): updated tests coverage to 66.5%

This commit is contained in:
misha
2026-01-14 18:47:11 +03:00
parent 497ff19cfa
commit 13ccf56df0
102 changed files with 10146 additions and 379 deletions

View File

@@ -18,6 +18,14 @@ void main() {
expect(model.day, '2024-01-01');
});
test('toJson serializes CreateDayModel correctly', () {
final model = CreateDayModel(day: '2024-01-01');
final json = model.toJson();
expect(json['day'], '2024-01-01');
});
});
}

View File

@@ -0,0 +1,255 @@
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, 7);
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.reviewedByUser, isNotNull);
});
});
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);
});
});
}

View File

@@ -0,0 +1,29 @@
import 'package:eatvio_app/features/diary/models/diary_index_model.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
group('DiaryIndexModel', () {
test('fromJson parses id correctly', () {
final json = {'id': 123};
final model = DiaryIndexModel.fromJson(json);
expect(model.id, 123);
});
test('toJson serializes id correctly', () {
const model = DiaryIndexModel(id: 123);
final json = model.toJson();
expect(json['id'], 123);
});
test('creates instance with id', () {
const model = DiaryIndexModel(id: 456);
expect(model.id, 456);
});
});
}

View File

@@ -3,29 +3,7 @@ import 'package:flutter_test/flutter_test.dart';
void main() {
group('FinishDiaryDayModel', () {
test('создает FinishDiaryDayModel с правильными значениями', () {
const model = FinishDiaryDayModel(
wakeUp: '08:00',
retire: '22:00',
comment: 'Test comment',
);
expect(model.wakeUp, '08:00');
expect(model.retire, '22:00');
expect(model.comment, 'Test comment');
});
test('создает FinishDiaryDayModel без комментария', () {
const model = FinishDiaryDayModel(
wakeUp: '08:00',
retire: '22:00',
comment: null,
);
expect(model.comment, isNull);
});
test('fromJson создает FinishDiaryDayModel из JSON', () {
test('fromJson parses all fields correctly', () {
final json = {
'wakeUp': '08:00',
'retire': '22:00',
@@ -39,7 +17,7 @@ void main() {
expect(model.comment, 'Test comment');
});
test('fromJson создает FinishDiaryDayModel из JSON без комментария', () {
test('fromJson handles null comment', () {
final json = {
'wakeUp': '08:00',
'retire': '22:00',
@@ -51,6 +29,32 @@ void main() {
expect(model.retire, '22:00');
expect(model.comment, isNull);
});
test('toJson serializes all fields correctly', () {
const model = FinishDiaryDayModel(
wakeUp: '08:00',
retire: '22:00',
comment: 'Test comment',
);
final json = model.toJson();
expect(json['wakeUp'], '08:00');
expect(json['retire'], '22:00');
expect(json['comment'], 'Test comment');
});
test('toJson handles null comment', () {
const model = FinishDiaryDayModel(
wakeUp: '08:00',
retire: '22:00',
);
final json = model.toJson();
expect(json['wakeUp'], '08:00');
expect(json['retire'], '22:00');
// includeIfNull: false may or may not include the field depending on freezed implementation
});
});
}

View File

@@ -0,0 +1,29 @@
import 'package:eatvio_app/features/diary/models/get_day_model.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
group('GetDayModel', () {
test('fromJson parses day correctly', () {
final json = {'day': '2024-01-15'};
final model = GetDayModel.fromJson(json);
expect(model.day, '2024-01-15');
});
test('toJson serializes day correctly', () {
final model = GetDayModel(day: '2024-01-15');
final json = model.toJson();
expect(json['day'], '2024-01-15');
});
test('creates instance with day', () {
final model = GetDayModel(day: '2024-12-31');
expect(model.day, '2024-12-31');
});
});
}

View File

@@ -30,6 +30,22 @@ void main() {
expect(model.energy, 2000.0);
expect(model.proteins, 100.0);
});
test('toJson serializes NutritionModel correctly', () {
const model = NutritionModel(
energy: 2000.0,
proteins: 100.0,
fats: 50.0,
carbohydrates: 250.0,
);
final json = model.toJson();
expect(json['energy'], 2000.0);
expect(json['proteins'], 100.0);
expect(json['fats'], 50.0);
expect(json['carbohydrates'], 250.0);
});
});
group('NutritionDataModel', () {
@@ -56,6 +72,54 @@ void main() {
expect(model.dailyNutritionIntake.energy, 2000.0);
expect(model.dailyNutritionRecommendation.energy, 2500.0);
});
test('fromJson создает NutritionDataModel из JSON', () {
final json = {
'dailyNutritionIntake': {
'energy': 2000.0,
'proteins': 100.0,
'fats': 50.0,
'carbohydrates': 250.0,
},
'dailyNutritionRecommendation': {
'energy': 2500.0,
'proteins': 120.0,
'fats': 60.0,
'carbohydrates': 300.0,
},
};
final model = NutritionDataModel.fromJson(json);
expect(model.dailyNutritionIntake.energy, 2000.0);
expect(model.dailyNutritionRecommendation.energy, 2500.0);
});
test('toJson serializes NutritionDataModel correctly', () {
const intake = NutritionModel(
energy: 2000.0,
proteins: 100.0,
fats: 50.0,
carbohydrates: 250.0,
);
const recommendation = NutritionModel(
energy: 2500.0,
proteins: 120.0,
fats: 60.0,
carbohydrates: 300.0,
);
const model = NutritionDataModel(
dailyNutritionIntake: intake,
dailyNutritionRecommendation: recommendation,
);
final json = model.toJson();
expect(json.containsKey('dailyNutritionIntake'), isTrue);
expect(json.containsKey('dailyNutritionRecommendation'), isTrue);
});
});
}

View File

@@ -33,6 +33,24 @@ void main() {
expect(model.name, 'Apple');
expect(model.energy, 52.0);
});
test('toJson serializes ProductModerationModel correctly', () {
const model = ProductModerationModel(
name: 'Apple',
energy: 52.0,
proteins: 0.3,
fats: 0.2,
carbohydrates: 14.0,
);
final json = model.toJson();
expect(json['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);
});
});
}