From 15a46656ed5956c5b448da6b414fb0ef4f39bdd6 Mon Sep 17 00:00:00 2001 From: KhaledNjim Date: Sun, 10 Nov 2024 19:13:48 +0100 Subject: [PATCH] LA-1352 added unit test for getListDataForRecipient --- .../received_share_database_manager.dart | 8 ++- .../received_share_database_manager_test.dart | 66 +++++++++++++++++++ ...ed_received_share_from_local_database.dart | 20 ++++-- 3 files changed, 85 insertions(+), 9 deletions(-) create mode 100644 data/test/local/received_share_database_manager_test.dart diff --git a/data/lib/src/local/received_share_database_manager.dart b/data/lib/src/local/received_share_database_manager.dart index db4a6d468..18e1a53b9 100644 --- a/data/lib/src/local/received_share_database_manager.dart +++ b/data/lib/src/local/received_share_database_manager.dart @@ -100,10 +100,14 @@ class ReceivedShareDatabaseManager } Future> getListDataForRecipient(String mail) async { - String todayDate = DateTime.now().toIso8601String(); + final now = DateTime.now(); + final todayDate = + DateTime(now.year, now.month, now.day, now.hour, now.minute).toIso8601String(); + final queryCondition = + '${ReceivedShareTable.MAIL_RECIPIENT} !="" AND ${ReceivedShareTable.MAIL_RECIPIENT} = ? AND ${ReceivedShareTable.EXPIRATION_DATE} >= ?'; final res = await _databaseClient.getListDataWithCondition( ReceivedShareTable.TABLE_NAME, - '${ReceivedShareTable.MAIL_RECIPIENT} !="" AND ${ReceivedShareTable.MAIL_RECIPIENT} = ? AND ${ReceivedShareTable.EXPIRATION_DATE} >= ?', + queryCondition, [mail, todayDate]); return res.isNotEmpty ? res diff --git a/data/test/local/received_share_database_manager_test.dart b/data/test/local/received_share_database_manager_test.dart new file mode 100644 index 000000000..5505a7b49 --- /dev/null +++ b/data/test/local/received_share_database_manager_test.dart @@ -0,0 +1,66 @@ +import 'package:data/data.dart'; +import 'package:data/src/local/config/received_share_table.dart'; +import 'package:data/src/local/model/received_share_cache.dart'; +import 'package:domain/domain.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:mockito/mockito.dart'; +import 'package:mockito/annotations.dart'; +import 'package:testshared/fixture/received_share_fixture.dart'; +import 'document_database_manager_test.mocks.dart'; + +@GenerateMocks([DatabaseClient]) +void main() { + late MockDatabaseClient mockDatabaseClient; + late ReceivedShareDatabaseManager receivedShareDatabaseManager; + late DateTime now; + late String todayDate; + setUp(() { + mockDatabaseClient = MockDatabaseClient(); + receivedShareDatabaseManager = + ReceivedShareDatabaseManager(mockDatabaseClient); + now = DateTime.now(); + todayDate = DateTime(now.year, now.month, now.day, now.hour, now.minute) + .toIso8601String(); + }); + + group('getListDataForRecipient', () { + test( + 'getListDataForRecipient returns filtered and mapped data for a specific recipient', + () async { + when(mockDatabaseClient.getListDataWithCondition( + ReceivedShareTable.TABLE_NAME, + '${ReceivedShareTable.MAIL_RECIPIENT} !="" AND ${ReceivedShareTable.MAIL_RECIPIENT} = ? AND ${ReceivedShareTable.EXPIRATION_DATE} >= ?', + [RECIPIENT_1.mail, todayDate], + )).thenAnswer((_) async => [ + receivedShare1.toReceivedShareCache().toJson(), + ]); + + final result = await receivedShareDatabaseManager + .getListDataForRecipient(RECIPIENT_1.mail); + + expect(result, isA>()); + expect(result.length, 1); + expect(result.first.shareId, receivedShare1.shareId); + }); + test( + 'getListDataForRecipient returns an empty list when no data matches the recipient', + () async { + when(mockDatabaseClient.getListDataWithCondition( + ReceivedShareTable.TABLE_NAME, + '${ReceivedShareTable.MAIL_RECIPIENT} !="" AND ${ReceivedShareTable.MAIL_RECIPIENT} = ? AND ${ReceivedShareTable.EXPIRATION_DATE} >= ?', + [RECIPIENT_1.mail, todayDate], + )).thenAnswer((_) async => []); + + final result = await receivedShareDatabaseManager + .getListDataForRecipient(RECIPIENT_1.mail); + expect(result, isA>()); + expect(result.isEmpty, true); + verify(mockDatabaseClient.getListDataWithCondition( + ReceivedShareTable.TABLE_NAME, + '${ReceivedShareTable.MAIL_RECIPIENT} !="" AND ${ReceivedShareTable.MAIL_RECIPIENT} = ? AND ${ReceivedShareTable.EXPIRATION_DATE} >= ?', + [RECIPIENT_1.mail, todayDate], + )).called(1); + }, + ); + }); +} diff --git a/domain/lib/src/usecases/received/remove_deleted_received_share_from_local_database.dart b/domain/lib/src/usecases/received/remove_deleted_received_share_from_local_database.dart index ebef0caa3..837692e3d 100644 --- a/domain/lib/src/usecases/received/remove_deleted_received_share_from_local_database.dart +++ b/domain/lib/src/usecases/received/remove_deleted_received_share_from_local_database.dart @@ -1,5 +1,5 @@ +import 'dart:developer'; import 'package:domain/domain.dart'; -import 'package:collection/collection.dart'; class RemoveDeletedReceivedShareFromLocalDatabaseInteractor { final ReceivedShareRepository _receivedShareRepository; @@ -9,13 +9,19 @@ class RemoveDeletedReceivedShareFromLocalDatabaseInteractor { Future execute( List receivedShares, String recipient) async { - var localReceivedShares = await _receivedShareRepository - .getAllReceivedShareOfflineByRecipient(recipient); - final receivedShareIds = receivedShares.map((received) => received.shareId).toSet(); - for (final local in localReceivedShares) { - if (!receivedShareIds.contains(local.shareId)) { - await _receivedShareRepository.disableOffline(local.shareId, local.localPath ?? ''); + try { + var localReceivedShares = await _receivedShareRepository + .getAllReceivedShareOfflineByRecipient(recipient); + final receivedShareIds = + receivedShares.map((received) => received.shareId).toSet(); + for (final local in localReceivedShares) { + if (!receivedShareIds.contains(local.shareId)) { + await _receivedShareRepository.disableOffline( + local.shareId, local.localPath ?? ''); + } } + } catch (exception) { + log('RemoveDeletedReceivedShareFromLocalDatabaseInteractor: $exception'); } } }