forked from eatvio/eatvio_app
feat(diary): tests
This commit is contained in:
@@ -1,93 +1,102 @@
|
||||
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/features/glossary/models/glossary_model.dart';
|
||||
import 'package:eatvio_app/features/glossary/repositories/glossary_repository.dart';
|
||||
import 'package:eatvio_app/features/glossary/services/glossary_service.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
import 'package:mockito/annotations.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
|
||||
class MockGlossaryService extends Mock implements GlossaryService {}
|
||||
class MockInternetConnectionCubit extends Mock implements InternetConnectionCubit {}
|
||||
class MockDiaryLocalDataSource extends Mock implements DiaryLocalDataSource {}
|
||||
@GenerateNiceMocks([
|
||||
MockSpec<GlossaryService>(),
|
||||
MockSpec<DiaryLocalDataSource>(),
|
||||
MockSpec<InternetConnectionCubit>(),
|
||||
])
|
||||
import 'glossary_repository_test.mocks.dart';
|
||||
|
||||
void main() {
|
||||
late GlossaryRepository repository;
|
||||
late MockGlossaryService mockService;
|
||||
late MockDiaryLocalDataSource mockLocalDataSource;
|
||||
late MockInternetConnectionCubit mockConnectionCubit;
|
||||
|
||||
setUp(() {
|
||||
mockService = MockGlossaryService();
|
||||
mockLocalDataSource = MockDiaryLocalDataSource();
|
||||
mockConnectionCubit = MockInternetConnectionCubit();
|
||||
repository = GlossaryRepository(
|
||||
glossaryService: mockService,
|
||||
localDataSource: mockLocalDataSource,
|
||||
connectionCubit: mockConnectionCubit,
|
||||
);
|
||||
});
|
||||
|
||||
group('GlossaryRepository', () {
|
||||
late GlossaryRepository repository;
|
||||
late MockGlossaryService mockService;
|
||||
late MockInternetConnectionCubit mockConnectionCubit;
|
||||
late MockDiaryLocalDataSource mockLocalDataSource;
|
||||
test('offline → searches local cache', () async {
|
||||
when(mockConnectionCubit.state).thenReturn(InternetConnectionState(isInternetAvailable: false));
|
||||
when(mockLocalDataSource.searchGlossary(any)).thenAnswer((_) async => [
|
||||
const GlossaryCacheTableData(
|
||||
id: 1,
|
||||
name: 'Яблоко',
|
||||
productGroupId: 5,
|
||||
energyOn100gr: 52.0,
|
||||
proteinsOn100gr: 0.3,
|
||||
fatsOn100gr: 0.2,
|
||||
carbohydratesOn100gr: 14.0,
|
||||
portionMassGr: 150.0,
|
||||
energyOnPortion: 78.0,
|
||||
proteinsOnPortion: 0.45,
|
||||
fatsOnPortion: 0.3,
|
||||
carbohydratesOnPortion: 21.0,
|
||||
updatedAt: '2024-01-01',
|
||||
),
|
||||
]);
|
||||
|
||||
setUp(() {
|
||||
mockService = MockGlossaryService();
|
||||
mockConnectionCubit = MockInternetConnectionCubit();
|
||||
mockLocalDataSource = MockDiaryLocalDataSource();
|
||||
when(() => mockConnectionCubit.state).thenReturn(
|
||||
InternetConnectionState(isInternetAvailable: true),
|
||||
);
|
||||
repository = GlossaryRepository(
|
||||
glossaryService: mockService,
|
||||
localDataSource: mockLocalDataSource,
|
||||
connectionCubit: mockConnectionCubit,
|
||||
);
|
||||
final result = await repository.getGlossaryListByQuery('яблоко');
|
||||
expect(result.length, 1);
|
||||
expect(result[0].name, 'Яблоко');
|
||||
expect(result[0].energyOn100Gr, 52.0);
|
||||
expect(result[0].grInPortion, 150);
|
||||
verifyNever(mockService.getGlossaryByQuery(query: anyNamed('query')));
|
||||
});
|
||||
|
||||
test('getGlossaryListByQuery - успешный запрос', () async {
|
||||
const query = 'test';
|
||||
final mockGlossaries = <GlossaryModel>[];
|
||||
final mockPagination = GlossaryPaginationModel(glossariesList: mockGlossaries);
|
||||
test('online → uses network service', () async {
|
||||
when(mockConnectionCubit.state).thenReturn(InternetConnectionState(isInternetAvailable: true));
|
||||
when(mockService.getGlossaryByQuery(query: anyNamed('query'))).thenAnswer(
|
||||
(_) async => const GlossaryPaginationModel(
|
||||
glossariesList: [
|
||||
GlossaryModel(
|
||||
id: 1,
|
||||
name: 'Яблоко',
|
||||
grInPortion: 150,
|
||||
energyOn100Gr: 52.0,
|
||||
proteinsOn100Gr: 0.3,
|
||||
fatsOn100Gr: 0.2,
|
||||
carbohydratesOn100Gr: 14.0,
|
||||
energyOnPortion: 78.0,
|
||||
proteinsOnPortion: 0.45,
|
||||
fatsOnPortion: 0.3,
|
||||
carbohydratesOnPortion: 21.0,
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
when(() => mockService.getGlossaryByQuery(query: query))
|
||||
.thenAnswer((_) async => mockPagination);
|
||||
|
||||
final result = await repository.getGlossaryListByQuery(query);
|
||||
|
||||
expect(result, mockGlossaries);
|
||||
verify(() => mockService.getGlossaryByQuery(query: query)).called(1);
|
||||
final result = await repository.getGlossaryListByQuery('яблоко');
|
||||
expect(result.length, 1);
|
||||
expect(result[0].name, 'Яблоко');
|
||||
});
|
||||
|
||||
test('getGlossaryListByQuery - пробрасывает ошибку', () async {
|
||||
const query = 'test';
|
||||
test('online with network error → falls back to local cache', () async {
|
||||
when(mockConnectionCubit.state).thenReturn(InternetConnectionState(isInternetAvailable: true));
|
||||
when(mockService.getGlossaryByQuery(query: anyNamed('query'))).thenThrow(Exception('Network error'));
|
||||
when(mockLocalDataSource.searchGlossary(any)).thenAnswer((_) async => []);
|
||||
|
||||
when(() => mockService.getGlossaryByQuery(query: query))
|
||||
.thenThrow(Exception('Error'));
|
||||
when(() => mockLocalDataSource.searchGlossary(query))
|
||||
.thenThrow(Exception('Local error'));
|
||||
|
||||
expect(() => repository.getGlossaryListByQuery(query), throwsException);
|
||||
final result = await repository.getGlossaryListByQuery('яблоко');
|
||||
expect(result, isEmpty);
|
||||
verify(mockLocalDataSource.searchGlossary('яблоко')).called(1);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user