forked from eatvio/eatvio_app
feat(diary): tests
This commit is contained in:
468
test/core/local_db/services/diary_sync_runner_test.dart
Normal file
468
test/core/local_db/services/diary_sync_runner_test.dart
Normal file
@@ -0,0 +1,468 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:eatvio_app/core/interceptors/offline_error_interceptor.dart';
|
||||
import 'package:eatvio_app/core/internet_connection_cubit/internet_connection_cubit.dart';
|
||||
import 'package:eatvio_app/core/internet_connection_cubit/internet_connection_state.dart';
|
||||
import 'package:eatvio_app/core/local_db/app_database.dart';
|
||||
import 'package:eatvio_app/core/local_db/diary_local_data_source.dart';
|
||||
import 'package:eatvio_app/core/local_db/services/diary_sync_runner.dart';
|
||||
import 'package:eatvio_app/features/diary/services/diary_service.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mockito/annotations.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
|
||||
@GenerateNiceMocks([
|
||||
MockSpec<DiaryLocalDataSource>(),
|
||||
MockSpec<DiaryService>(),
|
||||
MockSpec<InternetConnectionCubit>(),
|
||||
])
|
||||
import 'diary_sync_runner_test.mocks.dart';
|
||||
|
||||
void main() {
|
||||
late DiarySyncRunner runner;
|
||||
late MockDiaryLocalDataSource mockLocalDataSource;
|
||||
late MockDiaryService mockDiaryService;
|
||||
late MockInternetConnectionCubit mockConnectionCubit;
|
||||
|
||||
setUp(() {
|
||||
mockLocalDataSource = MockDiaryLocalDataSource();
|
||||
mockDiaryService = MockDiaryService();
|
||||
mockConnectionCubit = MockInternetConnectionCubit();
|
||||
|
||||
when(mockConnectionCubit.stream).thenAnswer((_) => const Stream.empty());
|
||||
when(mockConnectionCubit.state).thenReturn(InternetConnectionState(isInternetAvailable: false));
|
||||
|
||||
runner = DiarySyncRunner(
|
||||
localDataSource: mockLocalDataSource,
|
||||
diaryService: mockDiaryService,
|
||||
connectionCubit: mockConnectionCubit,
|
||||
);
|
||||
});
|
||||
|
||||
tearDown(() {
|
||||
runner.dispose();
|
||||
});
|
||||
|
||||
group('DiarySyncRunner', () {
|
||||
test('start() подписывается на connectionCubit.stream', () {
|
||||
runner.start();
|
||||
verify(mockConnectionCubit.stream).called(1);
|
||||
});
|
||||
|
||||
test('start() запускает _processQueue если уже online', () async {
|
||||
when(mockConnectionCubit.state).thenReturn(InternetConnectionState(isInternetAvailable: true));
|
||||
when(mockLocalDataSource.getPendingOps()).thenAnswer((_) async => []);
|
||||
when(mockLocalDataSource.clearSyncedOps()).thenAnswer((_) async {});
|
||||
|
||||
runner.start();
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
|
||||
verify(mockLocalDataSource.getPendingOps()).called(1);
|
||||
});
|
||||
|
||||
test('dispose() отменяет подписку', () {
|
||||
runner.start();
|
||||
runner.dispose();
|
||||
// Не должно быть ошибок
|
||||
});
|
||||
|
||||
test('processQueue пропускает пустую очередь', () async {
|
||||
when(mockConnectionCubit.state).thenReturn(InternetConnectionState(isInternetAvailable: true));
|
||||
when(mockLocalDataSource.getPendingOps()).thenAnswer((_) async => []);
|
||||
when(mockLocalDataSource.clearSyncedOps()).thenAnswer((_) async {});
|
||||
|
||||
runner.start();
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
|
||||
verify(mockLocalDataSource.getPendingOps()).called(1);
|
||||
verifyNever(mockDiaryService.createDay(idempotencyKey: anyNamed('idempotencyKey'), body: anyNamed('body')));
|
||||
});
|
||||
|
||||
test('processQueue обрабатывает create_day операцию', () async {
|
||||
final op = PendingOperationsTableData(
|
||||
id: 'test-id',
|
||||
type: 'create_day',
|
||||
payload: jsonEncode({'day': '2024-01-01T00:00:00+00:00'}),
|
||||
status: 'PENDING',
|
||||
retryCount: 0,
|
||||
createdAt: DateTime.now().toIso8601String(),
|
||||
localFilePath: null,
|
||||
);
|
||||
|
||||
when(mockConnectionCubit.state).thenReturn(InternetConnectionState(isInternetAvailable: true));
|
||||
when(mockLocalDataSource.getPendingOps()).thenAnswer((_) async => [op]);
|
||||
when(mockLocalDataSource.markSynced(any)).thenAnswer((_) async {});
|
||||
when(mockLocalDataSource.clearSyncedOps()).thenAnswer((_) async {});
|
||||
when(mockDiaryService.createDay(
|
||||
idempotencyKey: anyNamed('idempotencyKey'),
|
||||
body: anyNamed('body'),
|
||||
)).thenAnswer((_) async {});
|
||||
|
||||
runner.start();
|
||||
await Future.delayed(const Duration(milliseconds: 400));
|
||||
|
||||
verify(mockDiaryService.createDay(
|
||||
idempotencyKey: 'test-id',
|
||||
body: anyNamed('body'),
|
||||
)).called(1);
|
||||
verify(mockLocalDataSource.markSynced('test-id')).called(1);
|
||||
});
|
||||
|
||||
test('processQueue обрабатывает create_portion операцию', () async {
|
||||
final op = PendingOperationsTableData(
|
||||
id: 'portion-id',
|
||||
type: 'create_portion',
|
||||
payload: jsonEncode({
|
||||
'diary_id': 1,
|
||||
'meal_id': 2,
|
||||
'product_name': 'Test Product',
|
||||
'portion': '100g',
|
||||
'quantity': 1,
|
||||
'product_nutritional_value_id': 10,
|
||||
}),
|
||||
status: 'PENDING',
|
||||
retryCount: 0,
|
||||
createdAt: DateTime.now().toIso8601String(),
|
||||
localFilePath: null,
|
||||
);
|
||||
|
||||
when(mockConnectionCubit.state).thenReturn(InternetConnectionState(isInternetAvailable: true));
|
||||
when(mockLocalDataSource.getPendingOps()).thenAnswer((_) async => [op]);
|
||||
when(mockLocalDataSource.markSynced(any)).thenAnswer((_) async {});
|
||||
when(mockLocalDataSource.clearSyncedOps()).thenAnswer((_) async {});
|
||||
when(mockDiaryService.addProductToMealIdempotent(
|
||||
idempotencyKey: anyNamed('idempotencyKey'),
|
||||
diary: anyNamed('diary'),
|
||||
typeNo: anyNamed('typeNo'),
|
||||
productName: anyNamed('productName'),
|
||||
portion: anyNamed('portion'),
|
||||
quantity: anyNamed('quantity'),
|
||||
productNutritionalValueId: anyNamed('productNutritionalValueId'),
|
||||
)).thenAnswer((_) async {});
|
||||
|
||||
runner.start();
|
||||
await Future.delayed(const Duration(milliseconds: 400));
|
||||
|
||||
verify(mockLocalDataSource.markSynced('portion-id')).called(1);
|
||||
});
|
||||
|
||||
test('processQueue обрабатывает delete_portion операцию', () async {
|
||||
final op = PendingOperationsTableData(
|
||||
id: 'del-id',
|
||||
type: 'delete_portion',
|
||||
payload: jsonEncode({'diary_id': 1, 'meal_id': 2, 'portion_id': 3}),
|
||||
status: 'PENDING',
|
||||
retryCount: 0,
|
||||
createdAt: DateTime.now().toIso8601String(),
|
||||
localFilePath: null,
|
||||
);
|
||||
|
||||
when(mockConnectionCubit.state).thenReturn(InternetConnectionState(isInternetAvailable: true));
|
||||
when(mockLocalDataSource.getPendingOps()).thenAnswer((_) async => [op]);
|
||||
when(mockLocalDataSource.markSynced(any)).thenAnswer((_) async {});
|
||||
when(mockLocalDataSource.clearSyncedOps()).thenAnswer((_) async {});
|
||||
when(mockDiaryService.deleteProductFromMealIdempotent(
|
||||
idempotencyKey: anyNamed('idempotencyKey'),
|
||||
diary: anyNamed('diary'),
|
||||
typeNo: anyNamed('typeNo'),
|
||||
portionModel: anyNamed('portionModel'),
|
||||
)).thenAnswer((_) async {});
|
||||
|
||||
runner.start();
|
||||
await Future.delayed(const Duration(milliseconds: 400));
|
||||
|
||||
verify(mockLocalDataSource.markSynced('del-id')).called(1);
|
||||
});
|
||||
|
||||
test('processQueue обрабатывает fill_satiety операцию', () async {
|
||||
final op = PendingOperationsTableData(
|
||||
id: 'sat-id',
|
||||
type: 'fill_satiety',
|
||||
payload: jsonEncode({'diary_id': 1, 'type_no': 2, 'satiety': 5}),
|
||||
status: 'PENDING',
|
||||
retryCount: 0,
|
||||
createdAt: DateTime.now().toIso8601String(),
|
||||
localFilePath: null,
|
||||
);
|
||||
|
||||
when(mockConnectionCubit.state).thenReturn(InternetConnectionState(isInternetAvailable: true));
|
||||
when(mockLocalDataSource.getPendingOps()).thenAnswer((_) async => [op]);
|
||||
when(mockLocalDataSource.markSynced(any)).thenAnswer((_) async {});
|
||||
when(mockLocalDataSource.clearSyncedOps()).thenAnswer((_) async {});
|
||||
when(mockDiaryService.fillSatiety(diary: anyNamed('diary'), typeNo: anyNamed('typeNo'), satiety: anyNamed('satiety'))).thenAnswer((_) async {});
|
||||
|
||||
runner.start();
|
||||
await Future.delayed(const Duration(milliseconds: 400));
|
||||
|
||||
verify(mockLocalDataSource.markSynced('sat-id')).called(1);
|
||||
});
|
||||
|
||||
test('processQueue обрабатывает fill_mood операцию', () async {
|
||||
final op = PendingOperationsTableData(
|
||||
id: 'mood-id',
|
||||
type: 'fill_mood',
|
||||
payload: jsonEncode({'diary_id': 1, 'type_no': 2, 'mood': 3}),
|
||||
status: 'PENDING',
|
||||
retryCount: 0,
|
||||
createdAt: DateTime.now().toIso8601String(),
|
||||
localFilePath: null,
|
||||
);
|
||||
|
||||
when(mockConnectionCubit.state).thenReturn(InternetConnectionState(isInternetAvailable: true));
|
||||
when(mockLocalDataSource.getPendingOps()).thenAnswer((_) async => [op]);
|
||||
when(mockLocalDataSource.markSynced(any)).thenAnswer((_) async {});
|
||||
when(mockLocalDataSource.clearSyncedOps()).thenAnswer((_) async {});
|
||||
when(mockDiaryService.fillMood(diary: anyNamed('diary'), typeNo: anyNamed('typeNo'), mood: anyNamed('mood'))).thenAnswer((_) async {});
|
||||
|
||||
runner.start();
|
||||
await Future.delayed(const Duration(milliseconds: 400));
|
||||
|
||||
verify(mockLocalDataSource.markSynced('mood-id')).called(1);
|
||||
});
|
||||
|
||||
test('processQueue обрабатывает fill_time операцию', () async {
|
||||
final op = PendingOperationsTableData(
|
||||
id: 'time-id',
|
||||
type: 'fill_time',
|
||||
payload: jsonEncode({'diary_id': 1, 'type_no': 2, 'time': '12:00'}),
|
||||
status: 'PENDING',
|
||||
retryCount: 0,
|
||||
createdAt: DateTime.now().toIso8601String(),
|
||||
localFilePath: null,
|
||||
);
|
||||
|
||||
when(mockConnectionCubit.state).thenReturn(InternetConnectionState(isInternetAvailable: true));
|
||||
when(mockLocalDataSource.getPendingOps()).thenAnswer((_) async => [op]);
|
||||
when(mockLocalDataSource.markSynced(any)).thenAnswer((_) async {});
|
||||
when(mockLocalDataSource.clearSyncedOps()).thenAnswer((_) async {});
|
||||
when(mockDiaryService.fillTime(diary: anyNamed('diary'), typeNo: anyNamed('typeNo'), time: anyNamed('time'))).thenAnswer((_) async {});
|
||||
|
||||
runner.start();
|
||||
await Future.delayed(const Duration(milliseconds: 400));
|
||||
|
||||
verify(mockLocalDataSource.markSynced('time-id')).called(1);
|
||||
});
|
||||
|
||||
test('processQueue обрабатывает fill_wakeup операцию', () async {
|
||||
final op = PendingOperationsTableData(
|
||||
id: 'wake-id',
|
||||
type: 'fill_wakeup',
|
||||
payload: jsonEncode({'diary_id': 1, 'wakeup': '07:00'}),
|
||||
status: 'PENDING',
|
||||
retryCount: 0,
|
||||
createdAt: DateTime.now().toIso8601String(),
|
||||
localFilePath: null,
|
||||
);
|
||||
|
||||
when(mockConnectionCubit.state).thenReturn(InternetConnectionState(isInternetAvailable: true));
|
||||
when(mockLocalDataSource.getPendingOps()).thenAnswer((_) async => [op]);
|
||||
when(mockLocalDataSource.markSynced(any)).thenAnswer((_) async {});
|
||||
when(mockLocalDataSource.clearSyncedOps()).thenAnswer((_) async {});
|
||||
when(mockDiaryService.fillWakeup(diary: anyNamed('diary'), wakeUp: anyNamed('wakeUp'))).thenAnswer((_) async {});
|
||||
|
||||
runner.start();
|
||||
await Future.delayed(const Duration(milliseconds: 400));
|
||||
|
||||
verify(mockLocalDataSource.markSynced('wake-id')).called(1);
|
||||
});
|
||||
|
||||
test('processQueue обрабатывает fill_retire операцию', () async {
|
||||
final op = PendingOperationsTableData(
|
||||
id: 'retire-id',
|
||||
type: 'fill_retire',
|
||||
payload: jsonEncode({'diary_id': 1, 'retire': '23:00'}),
|
||||
status: 'PENDING',
|
||||
retryCount: 0,
|
||||
createdAt: DateTime.now().toIso8601String(),
|
||||
localFilePath: null,
|
||||
);
|
||||
|
||||
when(mockConnectionCubit.state).thenReturn(InternetConnectionState(isInternetAvailable: true));
|
||||
when(mockLocalDataSource.getPendingOps()).thenAnswer((_) async => [op]);
|
||||
when(mockLocalDataSource.markSynced(any)).thenAnswer((_) async {});
|
||||
when(mockLocalDataSource.clearSyncedOps()).thenAnswer((_) async {});
|
||||
when(mockDiaryService.fillRetire(diary: anyNamed('diary'), retire: anyNamed('retire'))).thenAnswer((_) async {});
|
||||
|
||||
runner.start();
|
||||
await Future.delayed(const Duration(milliseconds: 400));
|
||||
|
||||
verify(mockLocalDataSource.markSynced('retire-id')).called(1);
|
||||
});
|
||||
|
||||
test('processQueue обрабатывает delete_photo операцию', () async {
|
||||
final op = PendingOperationsTableData(
|
||||
id: 'photo-del-id',
|
||||
type: 'delete_photo',
|
||||
payload: jsonEncode({'diary_id': 1, 'type_no': 2, 'media_id': 10}),
|
||||
status: 'PENDING',
|
||||
retryCount: 0,
|
||||
createdAt: DateTime.now().toIso8601String(),
|
||||
localFilePath: null,
|
||||
);
|
||||
|
||||
when(mockConnectionCubit.state).thenReturn(InternetConnectionState(isInternetAvailable: true));
|
||||
when(mockLocalDataSource.getPendingOps()).thenAnswer((_) async => [op]);
|
||||
when(mockLocalDataSource.markSynced(any)).thenAnswer((_) async {});
|
||||
when(mockLocalDataSource.clearSyncedOps()).thenAnswer((_) async {});
|
||||
when(mockDiaryService.deletePhotoOfMeal(
|
||||
diaryId: anyNamed('diaryId'),
|
||||
typeNo: anyNamed('typeNo'),
|
||||
userAttachment: anyNamed('userAttachment'),
|
||||
)).thenAnswer((_) async {});
|
||||
|
||||
runner.start();
|
||||
await Future.delayed(const Duration(milliseconds: 400));
|
||||
|
||||
verify(mockLocalDataSource.markSynced('photo-del-id')).called(1);
|
||||
});
|
||||
|
||||
test('processQueue помечает операцию как failed при maxRetries', () async {
|
||||
final op = PendingOperationsTableData(
|
||||
id: 'max-retry-id',
|
||||
type: 'create_day',
|
||||
payload: jsonEncode({'day': '2024-01-01'}),
|
||||
status: 'PENDING',
|
||||
retryCount: 5, // >= _maxRetries
|
||||
createdAt: DateTime.now().toIso8601String(),
|
||||
localFilePath: null,
|
||||
);
|
||||
|
||||
when(mockConnectionCubit.state).thenReturn(InternetConnectionState(isInternetAvailable: true));
|
||||
when(mockLocalDataSource.getPendingOps()).thenAnswer((_) async => [op]);
|
||||
when(mockLocalDataSource.markFailed(any)).thenAnswer((_) async {});
|
||||
when(mockLocalDataSource.clearSyncedOps()).thenAnswer((_) async {});
|
||||
|
||||
runner.start();
|
||||
await Future.delayed(const Duration(milliseconds: 400));
|
||||
|
||||
verify(mockLocalDataSource.markFailed('max-retry-id')).called(1);
|
||||
});
|
||||
|
||||
test('processQueue увеличивает retryCount при OfflineOperationException retryable', () async {
|
||||
final op = PendingOperationsTableData(
|
||||
id: 'retry-id',
|
||||
type: 'create_day',
|
||||
payload: jsonEncode({'day': '2024-01-01T00:00:00+00:00'}),
|
||||
status: 'PENDING',
|
||||
retryCount: 0,
|
||||
createdAt: DateTime.now().toIso8601String(),
|
||||
localFilePath: null,
|
||||
);
|
||||
|
||||
when(mockConnectionCubit.state).thenReturn(InternetConnectionState(isInternetAvailable: true));
|
||||
when(mockLocalDataSource.getPendingOps()).thenAnswer((_) async => [op]);
|
||||
when(mockLocalDataSource.incrementRetry(any)).thenAnswer((_) async {});
|
||||
when(mockLocalDataSource.clearSyncedOps()).thenAnswer((_) async {});
|
||||
when(mockDiaryService.createDay(
|
||||
idempotencyKey: anyNamed('idempotencyKey'),
|
||||
body: anyNamed('body'),
|
||||
)).thenThrow(const OfflineOperationException(
|
||||
message: 'Server error',
|
||||
code: 'server_error',
|
||||
retryable: true,
|
||||
));
|
||||
|
||||
runner.start();
|
||||
await Future.delayed(const Duration(seconds: 2));
|
||||
|
||||
verify(mockLocalDataSource.incrementRetry('retry-id')).called(1);
|
||||
});
|
||||
|
||||
test('processQueue помечает failed при OfflineOperationException not retryable', () async {
|
||||
final op = PendingOperationsTableData(
|
||||
id: 'no-retry-id',
|
||||
type: 'create_day',
|
||||
payload: jsonEncode({'day': '2024-01-01T00:00:00+00:00'}),
|
||||
status: 'PENDING',
|
||||
retryCount: 0,
|
||||
createdAt: DateTime.now().toIso8601String(),
|
||||
localFilePath: null,
|
||||
);
|
||||
|
||||
when(mockConnectionCubit.state).thenReturn(InternetConnectionState(isInternetAvailable: true));
|
||||
when(mockLocalDataSource.getPendingOps()).thenAnswer((_) async => [op]);
|
||||
when(mockLocalDataSource.markFailed(any)).thenAnswer((_) async {});
|
||||
when(mockLocalDataSource.clearSyncedOps()).thenAnswer((_) async {});
|
||||
when(mockDiaryService.createDay(
|
||||
idempotencyKey: anyNamed('idempotencyKey'),
|
||||
body: anyNamed('body'),
|
||||
)).thenThrow(const OfflineOperationException(
|
||||
message: 'Not found',
|
||||
code: 'not_found',
|
||||
retryable: false,
|
||||
));
|
||||
|
||||
runner.start();
|
||||
await Future.delayed(const Duration(milliseconds: 400));
|
||||
|
||||
verify(mockLocalDataSource.markFailed('no-retry-id')).called(1);
|
||||
});
|
||||
|
||||
test('processQueue incrementRetry при неизвестной ошибке', () async {
|
||||
final op = PendingOperationsTableData(
|
||||
id: 'unknown-err-id',
|
||||
type: 'create_day',
|
||||
payload: jsonEncode({'day': '2024-01-01T00:00:00+00:00'}),
|
||||
status: 'PENDING',
|
||||
retryCount: 0,
|
||||
createdAt: DateTime.now().toIso8601String(),
|
||||
localFilePath: null,
|
||||
);
|
||||
|
||||
when(mockConnectionCubit.state).thenReturn(InternetConnectionState(isInternetAvailable: true));
|
||||
when(mockLocalDataSource.getPendingOps()).thenAnswer((_) async => [op]);
|
||||
when(mockLocalDataSource.incrementRetry(any)).thenAnswer((_) async {});
|
||||
when(mockLocalDataSource.clearSyncedOps()).thenAnswer((_) async {});
|
||||
when(mockDiaryService.createDay(
|
||||
idempotencyKey: anyNamed('idempotencyKey'),
|
||||
body: anyNamed('body'),
|
||||
)).thenThrow(Exception('Unknown error'));
|
||||
|
||||
runner.start();
|
||||
await Future.delayed(const Duration(milliseconds: 400));
|
||||
|
||||
verify(mockLocalDataSource.incrementRetry('unknown-err-id')).called(1);
|
||||
});
|
||||
|
||||
test('processQueue останавливается если интернет пропал', () async {
|
||||
final op1 = PendingOperationsTableData(
|
||||
id: 'op1',
|
||||
type: 'create_day',
|
||||
payload: jsonEncode({'day': '2024-01-01T00:00:00+00:00'}),
|
||||
status: 'PENDING',
|
||||
retryCount: 0,
|
||||
createdAt: DateTime.now().toIso8601String(),
|
||||
localFilePath: null,
|
||||
);
|
||||
final op2 = PendingOperationsTableData(
|
||||
id: 'op2',
|
||||
type: 'create_day',
|
||||
payload: jsonEncode({'day': '2024-01-02T00:00:00+00:00'}),
|
||||
status: 'PENDING',
|
||||
retryCount: 0,
|
||||
createdAt: DateTime.now().toIso8601String(),
|
||||
localFilePath: null,
|
||||
);
|
||||
|
||||
var callCount = 0;
|
||||
when(mockConnectionCubit.state).thenAnswer((_) {
|
||||
callCount++;
|
||||
// После первого вызова — "офлайн"
|
||||
return InternetConnectionState(isInternetAvailable: callCount <= 2);
|
||||
});
|
||||
when(mockLocalDataSource.getPendingOps()).thenAnswer((_) async => [op1, op2]);
|
||||
when(mockLocalDataSource.markSynced(any)).thenAnswer((_) async {});
|
||||
when(mockLocalDataSource.clearSyncedOps()).thenAnswer((_) async {});
|
||||
when(mockDiaryService.createDay(
|
||||
idempotencyKey: anyNamed('idempotencyKey'),
|
||||
body: anyNamed('body'),
|
||||
)).thenAnswer((_) async {});
|
||||
|
||||
runner.start();
|
||||
await Future.delayed(const Duration(milliseconds: 500));
|
||||
|
||||
// Должна быть обработана только одна операция, вторая пропущена
|
||||
verify(mockLocalDataSource.clearSyncedOps()).called(greaterThanOrEqualTo(1));
|
||||
});
|
||||
});
|
||||
}
|
||||
205
test/core/local_db/services/glossary_models_test.dart
Normal file
205
test/core/local_db/services/glossary_models_test.dart
Normal file
@@ -0,0 +1,205 @@
|
||||
import 'package:eatvio_app/core/local_db/services/glossary_dump_response.dart';
|
||||
import 'package:eatvio_app/core/local_db/services/glossary_meta_model.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
group('GlossaryDumpItem', () {
|
||||
test('fromJson parses all fields', () {
|
||||
final json = {
|
||||
'id': 1,
|
||||
'name': 'Яблоко',
|
||||
'product_group_id': 5,
|
||||
'energy_on_100gr': 52.0,
|
||||
'proteins_on_100gr': 0.3,
|
||||
'fats_on_100gr': 0.2,
|
||||
'carbohydrates_on_100gr': 14.0,
|
||||
'portion_mass_gr': 150.0,
|
||||
'energy_on_portion': 78.0,
|
||||
'proteins_on_portion': 0.45,
|
||||
'fats_on_portion': 0.3,
|
||||
'carbohydrates_on_portion': 21.0,
|
||||
'updated_at': '2024-01-01T12:00:00Z',
|
||||
};
|
||||
final item = GlossaryDumpItem.fromJson(json);
|
||||
expect(item.id, 1);
|
||||
expect(item.name, 'Яблоко');
|
||||
expect(item.productGroupId, 5);
|
||||
expect(item.energyOn100gr, 52.0);
|
||||
expect(item.proteinsOn100gr, 0.3);
|
||||
expect(item.fatsOn100gr, 0.2);
|
||||
expect(item.carbohydratesOn100gr, 14.0);
|
||||
expect(item.portionMassGr, 150.0);
|
||||
expect(item.energyOnPortion, 78.0);
|
||||
expect(item.proteinsOnPortion, 0.45);
|
||||
expect(item.fatsOnPortion, 0.3);
|
||||
expect(item.carbohydratesOnPortion, 21.0);
|
||||
expect(item.updatedAt, '2024-01-01T12:00:00Z');
|
||||
});
|
||||
|
||||
test('fromJson uses default values for portion fields', () {
|
||||
final json = {
|
||||
'id': 2,
|
||||
'name': 'Banana',
|
||||
'product_group_id': 5,
|
||||
'energy_on_100gr': 89.0,
|
||||
'proteins_on_100gr': 1.1,
|
||||
'fats_on_100gr': 0.3,
|
||||
'carbohydrates_on_100gr': 22.8,
|
||||
'updated_at': '2024-01-01T12:00:00Z',
|
||||
};
|
||||
final item = GlossaryDumpItem.fromJson(json);
|
||||
expect(item.portionMassGr, 0.0);
|
||||
expect(item.energyOnPortion, 0.0);
|
||||
expect(item.proteinsOnPortion, 0.0);
|
||||
expect(item.fatsOnPortion, 0.0);
|
||||
expect(item.carbohydratesOnPortion, 0.0);
|
||||
});
|
||||
|
||||
test('toJson serializes all fields', () {
|
||||
const item = GlossaryDumpItem(
|
||||
id: 1,
|
||||
name: 'Apple',
|
||||
productGroupId: 5,
|
||||
energyOn100gr: 52.0,
|
||||
proteinsOn100gr: 0.3,
|
||||
fatsOn100gr: 0.2,
|
||||
carbohydratesOn100gr: 14.0,
|
||||
portionMassGr: 150.0,
|
||||
energyOnPortion: 78.0,
|
||||
updatedAt: '2024-01-01T12:00:00Z',
|
||||
);
|
||||
final json = item.toJson();
|
||||
expect(json['id'], 1);
|
||||
expect(json['name'], 'Apple');
|
||||
expect(json['product_group_id'], 5);
|
||||
expect(json['energy_on_100gr'], 52.0);
|
||||
expect(json['portion_mass_gr'], 150.0);
|
||||
});
|
||||
|
||||
test('constructor works with default values', () {
|
||||
const item = GlossaryDumpItem(
|
||||
id: 1,
|
||||
name: 'Test',
|
||||
productGroupId: 1,
|
||||
energyOn100gr: 100.0,
|
||||
proteinsOn100gr: 10.0,
|
||||
fatsOn100gr: 5.0,
|
||||
carbohydratesOn100gr: 20.0,
|
||||
updatedAt: '2024-01-01',
|
||||
);
|
||||
expect(item.portionMassGr, 0.0);
|
||||
expect(item.energyOnPortion, 0.0);
|
||||
});
|
||||
});
|
||||
|
||||
group('GlossaryDumpResponse', () {
|
||||
test('fromJson parses response', () {
|
||||
final json = {
|
||||
'total': 2,
|
||||
'updated_at': '2024-01-01T12:00:00Z',
|
||||
'data': [
|
||||
{
|
||||
'id': 1,
|
||||
'name': 'Apple',
|
||||
'product_group_id': 5,
|
||||
'energy_on_100gr': 52.0,
|
||||
'proteins_on_100gr': 0.3,
|
||||
'fats_on_100gr': 0.2,
|
||||
'carbohydrates_on_100gr': 14.0,
|
||||
'updated_at': '2024-01-01T12:00:00Z',
|
||||
},
|
||||
{
|
||||
'id': 2,
|
||||
'name': 'Banana',
|
||||
'product_group_id': 5,
|
||||
'energy_on_100gr': 89.0,
|
||||
'proteins_on_100gr': 1.1,
|
||||
'fats_on_100gr': 0.3,
|
||||
'carbohydrates_on_100gr': 22.8,
|
||||
'updated_at': '2024-01-01T12:00:00Z',
|
||||
},
|
||||
],
|
||||
};
|
||||
final response = GlossaryDumpResponse.fromJson(json);
|
||||
expect(response.total, 2);
|
||||
expect(response.updatedAt, '2024-01-01T12:00:00Z');
|
||||
expect(response.data.length, 2);
|
||||
expect(response.data[0].name, 'Apple');
|
||||
expect(response.data[1].name, 'Banana');
|
||||
});
|
||||
|
||||
test('toJson serializes response', () {
|
||||
const response = GlossaryDumpResponse(
|
||||
total: 1,
|
||||
updatedAt: '2024-01-01T12:00:00Z',
|
||||
data: [
|
||||
GlossaryDumpItem(
|
||||
id: 1,
|
||||
name: 'Apple',
|
||||
productGroupId: 5,
|
||||
energyOn100gr: 52.0,
|
||||
proteinsOn100gr: 0.3,
|
||||
fatsOn100gr: 0.2,
|
||||
carbohydratesOn100gr: 14.0,
|
||||
updatedAt: '2024-01-01T12:00:00Z',
|
||||
),
|
||||
],
|
||||
);
|
||||
final json = response.toJson();
|
||||
expect(json['total'], 1);
|
||||
expect(json['updated_at'], '2024-01-01T12:00:00Z');
|
||||
expect(json['data'], isA<List>());
|
||||
expect((json['data'] as List).length, 1);
|
||||
});
|
||||
|
||||
test('handles empty data list', () {
|
||||
final json = {
|
||||
'total': 0,
|
||||
'updated_at': '2024-01-01T12:00:00Z',
|
||||
'data': [],
|
||||
};
|
||||
final response = GlossaryDumpResponse.fromJson(json);
|
||||
expect(response.total, 0);
|
||||
expect(response.data, isEmpty);
|
||||
});
|
||||
});
|
||||
|
||||
group('GlossaryMetaModel', () {
|
||||
test('fromJson parses all fields', () {
|
||||
final json = {
|
||||
'total_count': 200000,
|
||||
'last_updated_at': '2024-06-15T10:30:00Z',
|
||||
'checksum': 'abc123def456',
|
||||
};
|
||||
final meta = GlossaryMetaModel.fromJson(json);
|
||||
expect(meta.totalCount, 200000);
|
||||
expect(meta.lastUpdatedAt, '2024-06-15T10:30:00Z');
|
||||
expect(meta.checksum, 'abc123def456');
|
||||
});
|
||||
|
||||
test('toJson serializes all fields', () {
|
||||
const meta = GlossaryMetaModel(
|
||||
totalCount: 100,
|
||||
lastUpdatedAt: '2024-01-01T00:00:00Z',
|
||||
checksum: 'hash123',
|
||||
);
|
||||
final json = meta.toJson();
|
||||
expect(json['total_count'], 100);
|
||||
expect(json['last_updated_at'], '2024-01-01T00:00:00Z');
|
||||
expect(json['checksum'], 'hash123');
|
||||
});
|
||||
|
||||
test('roundtrip fromJson -> toJson', () {
|
||||
const original = GlossaryMetaModel(
|
||||
totalCount: 50,
|
||||
lastUpdatedAt: '2024-06-01T12:00:00Z',
|
||||
checksum: 'xyz',
|
||||
);
|
||||
final json = original.toJson();
|
||||
final restored = GlossaryMetaModel.fromJson(json);
|
||||
expect(restored.totalCount, original.totalCount);
|
||||
expect(restored.lastUpdatedAt, original.lastUpdatedAt);
|
||||
expect(restored.checksum, original.checksum);
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user