Skip to content

Commit

Permalink
LA-1352 added unit test for getListDataForRecipient
Browse files Browse the repository at this point in the history
  • Loading branch information
KhaledNjim committed Nov 14, 2024
1 parent 93e1695 commit 7bd656d
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 9 deletions.
8 changes: 6 additions & 2 deletions data/lib/src/local/received_share_database_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,14 @@ class ReceivedShareDatabaseManager
}

Future<List<ReceivedShare>> 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
Expand Down
66 changes: 66 additions & 0 deletions data/test/local/received_share_database_manager_test.dart
Original file line number Diff line number Diff line change
@@ -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<List<ReceivedShare>>());
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<List<ReceivedShare>>());
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);
},
);
});
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'dart:developer';
import 'package:domain/domain.dart';
import 'package:collection/collection.dart';

class RemoveDeletedReceivedShareFromLocalDatabaseInteractor {
final ReceivedShareRepository _receivedShareRepository;
Expand All @@ -9,13 +9,19 @@ class RemoveDeletedReceivedShareFromLocalDatabaseInteractor {

Future<void> execute(
List<ReceivedShare> 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');
}
}
}

0 comments on commit 7bd656d

Please sign in to comment.