Files
eatvio_app/test/features/feed/models/diets_pagination_models_test.dart
2026-01-14 18:47:11 +03:00

256 lines
7.2 KiB
Dart

import 'package:eatvio_app/features/feed/models/diets_pagination_model.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
group('Portion enum', () {
test('has all expected values', () {
expect(Portion.values.length, 3);
expect(Portion.grams, isNotNull);
expect(Portion.milliliters, isNotNull);
expect(Portion.pcs, isNotNull);
});
test('getPortionFromValue returns correct enum for valid values', () {
expect(Portion.getPortionFromValue('gr'), Portion.grams);
expect(Portion.getPortionFromValue('milliliters'), Portion.milliliters);
expect(Portion.getPortionFromValue('pcs'), Portion.pcs);
});
test('getPortionFromValue returns null for invalid values', () {
expect(Portion.getPortionFromValue('invalid'), isNull);
expect(Portion.getPortionFromValue(''), isNull);
});
test('getValue returns correct string values', () {
expect(Portion.grams.getValue(), 'gr');
expect(Portion.milliliters.getValue(), 'milliliters');
expect(Portion.pcs.getValue(), 'pcs');
});
});
group('DietType', () {
test('hasAtLeastOneType returns true when at least one type is true', () {
const dietType = DietType(light: true, veg: false, carb: false, meat: false);
expect(dietType.hasAtLeastOneType(), isTrue);
});
test('hasAtLeastOneType returns true when veg is true', () {
const dietType = DietType(light: null, veg: true, carb: null, meat: null);
expect(dietType.hasAtLeastOneType(), isTrue);
});
test('hasAtLeastOneType returns true when carb is true', () {
const dietType = DietType(light: null, veg: null, carb: true, meat: null);
expect(dietType.hasAtLeastOneType(), isTrue);
});
test('hasAtLeastOneType returns true when meat is true', () {
const dietType = DietType(light: null, veg: null, carb: null, meat: true);
expect(dietType.hasAtLeastOneType(), isTrue);
});
test('hasAtLeastOneType returns false when all types are false', () {
const dietType = DietType(light: false, veg: false, carb: false, meat: false);
expect(dietType.hasAtLeastOneType(), isFalse);
});
test('hasAtLeastOneType returns false when all types are null', () {
const dietType = DietType(light: null, veg: null, carb: null, meat: null);
expect(dietType.hasAtLeastOneType(), isFalse);
});
test('fromJson parses all fields correctly', () {
final json = {
'light': true,
'veg': false,
'carb': true,
'meat': false,
};
final model = DietType.fromJson(json);
expect(model.light, isTrue);
expect(model.veg, isFalse);
expect(model.carb, isTrue);
expect(model.meat, isFalse);
});
test('toJson serializes all fields correctly', () {
const model = DietType(light: true, veg: false, carb: true, meat: false);
final json = model.toJson();
expect(json['light'], isTrue);
expect(json['veg'], isFalse);
expect(json['carb'], isTrue);
expect(json['meat'], isFalse);
});
});
group('CreatedDiet', () {
test('fromJson parses id correctly', () {
final json = {'id': 123};
final model = CreatedDiet.fromJson(json);
expect(model.id, 123);
});
test('toJson serializes id correctly', () {
const model = CreatedDiet(id: 123);
final json = model.toJson();
expect(json['id'], 123);
});
});
group('DailyNutrition', () {
test('fromJson parses all fields correctly', () {
final json = {
'energy': 2000.0,
'proteins': 100.0,
'fats': 50.0,
'carbohydrates': 250.0,
};
final model = DailyNutrition.fromJson(json);
expect(model.energy, 2000.0);
expect(model.proteins, 100.0);
expect(model.fats, 50.0);
expect(model.carbohydrates, 250.0);
});
test('toJson serializes all fields correctly', () {
const model = DailyNutrition(
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('DailySchedule', () {
test('fromJson parses all fields correctly', () {
final json = {
'name': 'Breakfast',
'portion': 'gr',
'quantity': '100',
'nutrition': {
'energy': 200.0,
'proteins': 10.0,
'fats': 5.0,
'carbohydrates': 25.0,
},
};
final model = DailySchedule.fromJson(json);
expect(model.name, 'Breakfast');
expect(model.portion, Portion.grams);
expect(model.quantity, 100);
expect(model.dailyNutrition, isNotNull);
expect(model.dailyNutrition?.energy, 200.0);
});
test('fromJson parses quantity as int', () {
final json = {
'name': 'Lunch',
'portion': 'milliliters',
'quantity': 200,
};
final model = DailySchedule.fromJson(json);
expect(model.quantity, 200);
});
test('toJson serializes all fields correctly', () {
const nutrition = DailyNutrition(
energy: 200.0,
proteins: 10.0,
fats: 5.0,
carbohydrates: 25.0,
);
const model = DailySchedule(
name: 'Breakfast',
portion: Portion.grams,
quantity: 100,
dailyNutrition: nutrition,
);
final json = model.toJson();
expect(json['name'], 'Breakfast');
expect(json['portion'], 'gr');
expect(json['quantity'], 100);
});
});
group('Author', () {
test('fromJson parses all fields correctly', () {
final json = {
'author_slug': 'author-1',
'author_name': 'Author Name',
'author_profile_icon': 'icon.jpg',
'author_is_me': true,
'is_entity_subscribed_by_me': false,
'author_place_in_rating': 5,
};
final model = Author.fromJson(json);
expect(model.authorSlug, 'author-1');
expect(model.authorName, 'Author Name');
expect(model.authorProfileIcon, 'icon.jpg');
expect(model.authorIsMe, isTrue);
expect(model.isEntitySubscribedByMe, isFalse);
expect(model.authorPlaceInRating, 5);
});
test('fromJson handles null optional fields', () {
final json = {
'author_slug': 'author-1',
'author_name': 'Author Name',
'author_profile_icon': 'icon.jpg',
'author_is_me': false,
};
final model = Author.fromJson(json);
expect(model.isEntitySubscribedByMe, isNull);
expect(model.authorPlaceInRating, isNull);
});
test('toJson serializes all fields correctly', () {
const model = Author(
authorSlug: 'author-1',
authorName: 'Author Name',
authorProfileIcon: 'icon.jpg',
authorIsMe: true,
isEntitySubscribedByMe: false,
authorPlaceInRating: 5,
);
final json = model.toJson();
expect(json['author_slug'], 'author-1');
expect(json['author_name'], 'Author Name');
expect(json['author_profile_icon'], 'icon.jpg');
expect(json['author_is_me'], isTrue);
expect(json['is_entity_subscribed_by_me'], isFalse);
expect(json['author_place_in_rating'], 5);
});
});
}