Files
eatvio_app/test/features/plans/models/chosen_plan_model_test.dart

317 lines
10 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 'package:eatvio_app/features/feed/models/diets_pagination_model.dart';
import 'package:eatvio_app/features/feed/models/plan_pagination_model.dart';
import 'package:eatvio_app/features/plans/enums/chosen_plan_state_enum.dart';
import 'package:eatvio_app/features/plans/models/chosen_plan_model.dart';
import 'package:flutter_test/flutter_test.dart';
import '../../../fixtures/fixture_reader.dart';
void main() {
group('ChosenPlanModel', () {
test('создает ChosenPlanModel с правильными значениями', () {
const plan = PlanModel(
id: 1,
slug: 'test-plan',
name: 'Test Plan',
image: 'https://example.com/image.jpg',
duration: 7,
state: 'active',
type: PlanType(fit: true, slim: false, behavior: false, wellness: false),
details: 'Test details',
packetsCount: 3,
author: Author(
authorSlug: 'author',
authorName: 'Author Name',
authorProfileIcon: 'https://example.com/icon.jpg',
authorIsMe: false,
isEntitySubscribedByMe: null,
authorPlaceInRating: null,
),
totalLikes: 10,
totalViews: 100,
totalComments: 5,
isFavorite: false,
minPriceOfOptions: 1000,
maxPriceOfOptions: 2000,
wasLikedByMe: false,
);
final chosenPlan = ChosenPlanModel(
id: 1,
optionType: 'options1',
startDate: DateTime(2024, 1, 1),
chosenPlanState: ChosenPlanStateEnum.stateInited,
chatId: 'chat-1',
consultantId: 1,
plan: plan,
);
expect(chosenPlan.id, 1);
expect(chosenPlan.optionType, 'options1');
expect(chosenPlan.chosenPlanState, ChosenPlanStateEnum.stateInited);
expect(chosenPlan.plan.id, 1);
});
test('fromJson создает ChosenPlanModel из JSON', () {
final json = {
'id': 1,
'option_type': 'options1',
'start_date': '2024-01-01T00:00:00Z',
'last_state': 'initied',
'chat_id': 'chat-1',
'consultant_id': 1,
'personal_diet_name': 'Индивидуальный план питания',
'plan': {
'id': 1,
'slug': 'test-plan',
'name': 'Test Plan',
'image': 'https://example.com/image.jpg',
'duration': 7,
'state': 'active',
'type': {'fit': true, 'slim': false, 'behavior': false, 'wellness': false},
'description_md': 'Test details',
'packets_count': 3,
'author': {
'author_slug': 'author',
'author_name': 'Author Name',
'author_profile_icon': 'https://example.com/icon.jpg',
'author_is_me': false,
'is_entity_subscribed_by_me': null,
'author_place_in_rating': null,
},
'total_likes': 10,
'total_views': 100,
'total_comments': 5,
'is_favorite': false,
'min_price_of_options': 1000.0,
'max_price_of_options': 2000.0,
'was_liked_by_me': false,
},
};
final chosenPlan = ChosenPlanModel.fromJson(json);
expect(chosenPlan.id, 1);
expect(chosenPlan.optionType, 'options1');
expect(chosenPlan.chosenPlanState, ChosenPlanStateEnum.stateInited);
expect(chosenPlan.chatId, 'chat-1');
expect(chosenPlan.personalDietName, 'Индивидуальный план питания');
});
test('toJson serializes ChosenPlanModel correctly', () {
const plan = PlanModel(
id: 1,
slug: 'test-plan',
name: 'Test Plan',
image: 'https://example.com/image.jpg',
duration: 7,
state: 'active',
type: PlanType(fit: true, slim: false, behavior: false, wellness: false),
details: 'Test details',
packetsCount: 3,
author: Author(
authorSlug: 'author',
authorName: 'Author Name',
authorProfileIcon: 'https://example.com/icon.jpg',
authorIsMe: false,
isEntitySubscribedByMe: null,
authorPlaceInRating: null,
),
totalLikes: 10,
totalViews: 100,
totalComments: 5,
isFavorite: false,
minPriceOfOptions: 1000,
maxPriceOfOptions: 2000,
wasLikedByMe: false,
);
final chosenPlan = ChosenPlanModel(
id: 1,
optionType: 'options1',
startDate: DateTime(2024, 1, 1),
chosenPlanState: ChosenPlanStateEnum.stateInited,
chatId: 'chat-1',
consultantId: 1,
plan: plan,
);
final json = chosenPlan.toJson();
expect(json['id'], 1);
expect(json['option_type'], 'options1');
expect(json['chat_id'], 'chat-1');
expect(json['consultant_id'], 1);
expect(json.containsKey('plan'), isTrue);
});
group('fromJson payment polling statuses', () {
test('fromJson парсит pending_payment из фикстуры', () {
final json = FixtureReader.readAsMap(
'chat/chosen_plan/chosen_plan_pending_payment_response.json',
);
final model = ChosenPlanModel.fromJson(json);
expect(model.id, 1);
expect(model.optionType, '1');
expect(model.chosenPlanState, ChosenPlanStateEnum.statePendingPayment);
expect(model.chatId, 'chat-1');
});
test('fromJson парсит paid из фикстуры', () {
final json = FixtureReader.readAsMap(
'chat/chosen_plan/chosen_plan_paid_response.json',
);
final model = ChosenPlanModel.fromJson(json);
expect(model.id, 1);
expect(model.optionType, '1');
expect(model.chosenPlanState, ChosenPlanStateEnum.statePaid);
expect(model.chatId, 'chat-1');
});
test('fromJson парсит payment_failed из фикстуры', () {
final json = FixtureReader.readAsMap(
'chat/chosen_plan/chosen_plan_payment_failed_response.json',
);
final model = ChosenPlanModel.fromJson(json);
expect(model.id, 1);
expect(model.optionType, '1');
expect(model.chosenPlanState, ChosenPlanStateEnum.statePaymentFailed);
expect(model.chatId, 'chat-1');
});
test('fromJson парсит canceled_by_the_initiator из фикстуры', () {
final json = FixtureReader.readAsMap(
'chat/chosen_plan/chosen_plan_canceled_by_the_initiator_response.json',
);
final model = ChosenPlanModel.fromJson(json);
expect(model.id, 1);
expect(model.optionType, '1');
expect(model.chosenPlanState, ChosenPlanStateEnum.stateCanceledByTheInitiator);
expect(model.chatId, 'chat-1');
});
});
});
group('chosenOption', () {
const plan = PlanModel(
id: 1,
slug: 'test-plan',
name: 'Test Plan',
image: 'https://example.com/image.jpg',
duration: 7,
state: 'active',
type: PlanType(fit: true, slim: false, behavior: false, wellness: false),
details: 'Test details',
packetsCount: 3,
packet1Options: PacketOptions(items: ['one'], price: 100, short: 'packet-1', hasPersonalDiet: false),
packet2Options: PacketOptions(items: ['two'], price: 200, short: 'packet-2', hasPersonalDiet: true),
packet3Options: PacketOptions(items: ['three'], price: 300, short: 'packet-3', hasPersonalDiet: false),
author: Author(
authorSlug: 'author',
authorName: 'Author Name',
authorProfileIcon: 'https://example.com/icon.jpg',
authorIsMe: false,
isEntitySubscribedByMe: null,
authorPlaceInRating: null,
),
totalLikes: 10,
totalViews: 100,
totalComments: 5,
isFavorite: false,
minPriceOfOptions: 100,
maxPriceOfOptions: 300,
wasLikedByMe: false,
);
ChosenPlanModel modelWith(String optionType) => ChosenPlanModel(
id: 1,
optionType: optionType,
startDate: DateTime(2024, 1, 1),
chosenPlanState: ChosenPlanStateEnum.stateInited,
chatId: 'chat-1',
consultantId: 1,
plan: plan,
);
test('возвращает первую опцию для options1', () {
expect(modelWith('options1').chosenOption?.short, 'packet-1');
});
test('возвращает вторую опцию для options2', () {
expect(modelWith('options2').chosenOption?.short, 'packet-2');
});
test('возвращает третью опцию для options3', () {
expect(modelWith('options3').chosenOption?.short, 'packet-3');
});
test('возвращает null для неизвестного значения', () {
expect(modelWith('unknown').chosenOption, isNull);
expect(modelWith('1').chosenOption, isNull);
expect(modelWith('').chosenOption, isNull);
});
});
group('parseChosenPlan', () {
test('parseChosenPlan создает ChosenPlanModel из Map', () {
final json = {
'id': 1,
'option_type': 'options1',
'start_date': '2024-01-01T00:00:00Z',
'last_state': 'initied',
'chat_id': 'chat-1',
'consultant_id': 1,
'personal_diet_name': 'Индивидуальный план питания',
'plan': {
'id': 1,
'slug': 'test-plan',
'name': 'Test Plan',
'image': 'https://example.com/image.jpg',
'duration': 7,
'state': 'active',
'type': {'fit': true, 'slim': false, 'behavior': false, 'wellness': false},
'description_md': 'Test details',
'packets_count': 3,
'author': {
'author_slug': 'author',
'author_name': 'Author Name',
'author_profile_icon': 'https://example.com/icon.jpg',
'author_is_me': false,
'is_entity_subscribed_by_me': null,
'author_place_in_rating': null,
},
'total_likes': 10,
'total_views': 100,
'total_comments': 5,
'is_favorite': false,
'min_price_of_options': 1000.0,
'max_price_of_options': 2000.0,
'was_liked_by_me': false,
},
};
final result = parseChosenPlan(json);
expect(result, isNotNull);
expect(result!.id, 1);
expect(result.optionType, 'options1');
});
test('parseChosenPlan возвращает null для не-Map значения', () {
expect(parseChosenPlan(null), isNull);
expect(parseChosenPlan('string'), isNull);
expect(parseChosenPlan(123), isNull);
expect(parseChosenPlan([]), isNull);
});
});
}