TF-129 Unit-test for mailbox_cache_client
This commit is contained in:
committed by
Dat H. Pham
parent
f152f83ca2
commit
c6ee4e907b
@@ -5,7 +5,7 @@ abstract class HiveCacheClient<T> {
|
||||
|
||||
String get tableName;
|
||||
|
||||
Future<Box<T>> openTable();
|
||||
Future<Box<T>> openBox();
|
||||
|
||||
Future<void> insertItem(String key, T newObject);
|
||||
|
||||
@@ -26,4 +26,8 @@ abstract class HiveCacheClient<T> {
|
||||
Future<bool> isExistItem(String key);
|
||||
|
||||
Future<bool> isExistTable();
|
||||
|
||||
Future<void> deleteBox() {
|
||||
return Hive.deleteBoxFromDisk(tableName);
|
||||
}
|
||||
}
|
||||
@@ -9,14 +9,19 @@ import 'package:tmail_ui_user/features/mailbox/data/model/state_type.dart';
|
||||
|
||||
class HiveCacheConfig {
|
||||
|
||||
Future setUp() async {
|
||||
await initializeDatabase();
|
||||
Future setUp({String? cachePath}) async {
|
||||
await initializeDatabase(databasePath: cachePath);
|
||||
registerAdapter();
|
||||
}
|
||||
|
||||
Future initializeDatabase() async {
|
||||
Directory directory = await pathProvider.getApplicationDocumentsDirectory();
|
||||
Hive.init(directory.path);
|
||||
Future initializeDatabase({String? databasePath}) async {
|
||||
if (databasePath != null) {
|
||||
Hive.init(databasePath);
|
||||
} else {
|
||||
Directory directory = await pathProvider
|
||||
.getApplicationDocumentsDirectory();
|
||||
Hive.init(directory.path);
|
||||
}
|
||||
}
|
||||
|
||||
void registerAdapter() {
|
||||
|
||||
@@ -9,7 +9,7 @@ class MailboxCacheClient extends HiveCacheClient<MailboxCache> {
|
||||
String get tableName => 'MailboxCache';
|
||||
|
||||
@override
|
||||
Future<Box<MailboxCache>> openTable() {
|
||||
Future<Box<MailboxCache>> openBox() {
|
||||
return Future.sync(() async {
|
||||
if (Hive.isBoxOpen(tableName)) {
|
||||
return Hive.box<MailboxCache>(tableName);
|
||||
@@ -23,7 +23,7 @@ class MailboxCacheClient extends HiveCacheClient<MailboxCache> {
|
||||
@override
|
||||
Future<bool> isExistItem(String key) {
|
||||
return Future.sync(() async {
|
||||
final boxMailbox = await openTable();
|
||||
final boxMailbox = await openBox();
|
||||
return boxMailbox.containsKey(key);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
@@ -33,7 +33,7 @@ class MailboxCacheClient extends HiveCacheClient<MailboxCache> {
|
||||
@override
|
||||
Future<void> deleteItem(String key) {
|
||||
return Future.sync(() async {
|
||||
final boxMailbox = await openTable();
|
||||
final boxMailbox = await openBox();
|
||||
return boxMailbox.delete(key);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
@@ -43,7 +43,7 @@ class MailboxCacheClient extends HiveCacheClient<MailboxCache> {
|
||||
@override
|
||||
Future<MailboxCache?> getItem(String key) {
|
||||
return Future.sync(() async {
|
||||
final boxMailbox = await openTable();
|
||||
final boxMailbox = await openBox();
|
||||
return boxMailbox.get(key);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
@@ -53,7 +53,7 @@ class MailboxCacheClient extends HiveCacheClient<MailboxCache> {
|
||||
@override
|
||||
Future<List<MailboxCache>> getAll() {
|
||||
return Future.sync(() async {
|
||||
final boxMailbox = await openTable();
|
||||
final boxMailbox = await openBox();
|
||||
return boxMailbox.values.toList();
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
@@ -63,7 +63,7 @@ class MailboxCacheClient extends HiveCacheClient<MailboxCache> {
|
||||
@override
|
||||
Future<void> insertItem(String key, MailboxCache newObject) {
|
||||
return Future.sync(() async {
|
||||
final boxMailbox = await openTable();
|
||||
final boxMailbox = await openBox();
|
||||
boxMailbox.put(key, newObject);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
@@ -73,7 +73,7 @@ class MailboxCacheClient extends HiveCacheClient<MailboxCache> {
|
||||
@override
|
||||
Future<void> insertMultipleItem(Map<String, MailboxCache> mapObject) {
|
||||
return Future.sync(() async {
|
||||
final boxMailbox = await openTable();
|
||||
final boxMailbox = await openBox();
|
||||
boxMailbox.putAll(mapObject);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
@@ -83,7 +83,7 @@ class MailboxCacheClient extends HiveCacheClient<MailboxCache> {
|
||||
@override
|
||||
Future<void> updateItem(String key, MailboxCache newObject) {
|
||||
return Future.sync(() async {
|
||||
final boxMailbox = await openTable();
|
||||
final boxMailbox = await openBox();
|
||||
boxMailbox.put(key, newObject);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
@@ -102,7 +102,7 @@ class MailboxCacheClient extends HiveCacheClient<MailboxCache> {
|
||||
@override
|
||||
Future<void> deleteMultipleItem(List<String> listKey) {
|
||||
return Future.sync(() async {
|
||||
final boxMailbox = await openTable();
|
||||
final boxMailbox = await openBox();
|
||||
return boxMailbox.deleteAll(listKey);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
@@ -112,7 +112,7 @@ class MailboxCacheClient extends HiveCacheClient<MailboxCache> {
|
||||
@override
|
||||
Future<void> updateMultipleItem(Map<String, MailboxCache> mapObject) {
|
||||
return Future.sync(() async {
|
||||
final boxMailbox = await openTable();
|
||||
final boxMailbox = await openBox();
|
||||
boxMailbox.putAll(mapObject);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
|
||||
@@ -9,7 +9,7 @@ class StateCacheClient extends HiveCacheClient<StateCache> {
|
||||
String get tableName => 'StateCache';
|
||||
|
||||
@override
|
||||
Future<Box<StateCache>> openTable() {
|
||||
Future<Box<StateCache>> openBox() {
|
||||
return Future.sync(() async {
|
||||
if (Hive.isBoxOpen(tableName)) {
|
||||
return Hive.box<StateCache>(tableName);
|
||||
@@ -23,7 +23,7 @@ class StateCacheClient extends HiveCacheClient<StateCache> {
|
||||
@override
|
||||
Future<bool> isExistItem(String key) {
|
||||
return Future.sync(() async {
|
||||
final boxState = await openTable();
|
||||
final boxState = await openBox();
|
||||
return boxState.containsKey(key);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
@@ -33,7 +33,7 @@ class StateCacheClient extends HiveCacheClient<StateCache> {
|
||||
@override
|
||||
Future<void> deleteItem(String key) {
|
||||
return Future.sync(() async {
|
||||
final boxState = await openTable();
|
||||
final boxState = await openBox();
|
||||
return boxState.delete(key);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
@@ -43,7 +43,7 @@ class StateCacheClient extends HiveCacheClient<StateCache> {
|
||||
@override
|
||||
Future<StateCache?> getItem(String key) {
|
||||
return Future.sync(() async {
|
||||
final boxState = await openTable();
|
||||
final boxState = await openBox();
|
||||
return boxState.get(key);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
@@ -53,7 +53,7 @@ class StateCacheClient extends HiveCacheClient<StateCache> {
|
||||
@override
|
||||
Future<List<StateCache>> getAll() {
|
||||
return Future.sync(() async {
|
||||
final boxState = await openTable();
|
||||
final boxState = await openBox();
|
||||
return boxState.values.toList();
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
@@ -63,7 +63,7 @@ class StateCacheClient extends HiveCacheClient<StateCache> {
|
||||
@override
|
||||
Future<void> insertItem(String key, StateCache newObject) {
|
||||
return Future.sync(() async {
|
||||
final boxState = await openTable();
|
||||
final boxState = await openBox();
|
||||
boxState.put(key, newObject);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
@@ -73,7 +73,7 @@ class StateCacheClient extends HiveCacheClient<StateCache> {
|
||||
@override
|
||||
Future<void> insertMultipleItem(Map<String, StateCache> mapObject) {
|
||||
return Future.sync(() async {
|
||||
final boxState = await openTable();
|
||||
final boxState = await openBox();
|
||||
boxState.putAll(mapObject);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
@@ -83,7 +83,7 @@ class StateCacheClient extends HiveCacheClient<StateCache> {
|
||||
@override
|
||||
Future<void> updateItem(String key, StateCache newObject) {
|
||||
return Future.sync(() async {
|
||||
final boxState = await openTable();
|
||||
final boxState = await openBox();
|
||||
boxState.put(key, newObject);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
@@ -102,7 +102,7 @@ class StateCacheClient extends HiveCacheClient<StateCache> {
|
||||
@override
|
||||
Future<void> deleteMultipleItem(List<String> listKey) {
|
||||
return Future.sync(() async {
|
||||
final boxState = await openTable();
|
||||
final boxState = await openBox();
|
||||
boxState.deleteAll(listKey);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
@@ -112,7 +112,7 @@ class StateCacheClient extends HiveCacheClient<StateCache> {
|
||||
@override
|
||||
Future<void> updateMultipleItem(Map<String, StateCache> mapObject) {
|
||||
return Future.sync(() async {
|
||||
final boxState = await openTable();
|
||||
final boxState = await openBox();
|
||||
boxState.putAll(mapObject);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
|
||||
@@ -0,0 +1,181 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:tmail_ui_user/features/caching/config/hive_cache_config.dart';
|
||||
import 'package:tmail_ui_user/features/caching/mailbox_cache_client.dart';
|
||||
import 'package:tmail_ui_user/features/mailbox/data/extensions/mailbox_extension.dart';
|
||||
|
||||
import '../../fixtures/mailbox_fixtures.dart';
|
||||
|
||||
void main() {
|
||||
|
||||
late MailboxCacheClient _mailboxCacheClient;
|
||||
|
||||
setUpAll(() {
|
||||
HiveCacheConfig().setUp(cachePath: Directory.current.path);
|
||||
});
|
||||
|
||||
setUp(() {
|
||||
_mailboxCacheClient = MailboxCacheClient();
|
||||
});
|
||||
|
||||
group('[delete]', () {
|
||||
test('cache should delete item successfully when cache empty', () async {
|
||||
await _mailboxCacheClient.deleteItem(MailboxFixtures.inboxMailbox.id.toString());
|
||||
|
||||
final remainingItems = await _mailboxCacheClient.getAll();
|
||||
|
||||
expect(remainingItems.length, equals(0));
|
||||
});
|
||||
|
||||
test('cache should not delete item which not in the list', () async {
|
||||
await _mailboxCacheClient.insertItem(
|
||||
MailboxFixtures.inboxMailbox.id.toString(),
|
||||
MailboxFixtures.inboxMailbox.toMailboxCache());
|
||||
|
||||
await _mailboxCacheClient.insertItem(
|
||||
MailboxFixtures.sentMailbox.id.toString(),
|
||||
MailboxFixtures.sentMailbox.toMailboxCache());
|
||||
|
||||
await _mailboxCacheClient.deleteItem(MailboxFixtures.folder1.id.toString());
|
||||
|
||||
final remainingItems = await _mailboxCacheClient.getAll();
|
||||
|
||||
expect(remainingItems.length, equals(2));
|
||||
expect(
|
||||
remainingItems,
|
||||
containsAll({
|
||||
MailboxFixtures.inboxMailbox.toMailboxCache(),
|
||||
MailboxFixtures.sentMailbox.toMailboxCache()
|
||||
}));
|
||||
});
|
||||
|
||||
test('cache should delete item successfully', () async {
|
||||
await _mailboxCacheClient.insertItem(
|
||||
MailboxFixtures.inboxMailbox.id.toString(),
|
||||
MailboxFixtures.inboxMailbox.toMailboxCache());
|
||||
|
||||
await _mailboxCacheClient.insertItem(
|
||||
MailboxFixtures.sentMailbox.id.toString(),
|
||||
MailboxFixtures.sentMailbox.toMailboxCache());
|
||||
|
||||
await _mailboxCacheClient.deleteItem(MailboxFixtures.inboxMailbox.id.toString());
|
||||
|
||||
final remainingItems = await _mailboxCacheClient.getAll();
|
||||
|
||||
expect(remainingItems.length, equals(1));
|
||||
expect(remainingItems[0], equals(MailboxFixtures.sentMailbox.toMailboxCache()));
|
||||
});
|
||||
|
||||
test('cache should not delete item twice', () async {
|
||||
await _mailboxCacheClient.insertItem(
|
||||
MailboxFixtures.inboxMailbox.id.toString(),
|
||||
MailboxFixtures.inboxMailbox.toMailboxCache());
|
||||
|
||||
await _mailboxCacheClient.insertItem(
|
||||
MailboxFixtures.sentMailbox.id.toString(),
|
||||
MailboxFixtures.sentMailbox.toMailboxCache());
|
||||
|
||||
await _mailboxCacheClient.deleteItem(MailboxFixtures.inboxMailbox.id.toString());
|
||||
await _mailboxCacheClient.deleteItem(MailboxFixtures.inboxMailbox.id.toString());
|
||||
|
||||
final remainingItems = await _mailboxCacheClient.getAll();
|
||||
|
||||
expect(remainingItems.length, equals(1));
|
||||
expect(remainingItems[0], equals(MailboxFixtures.sentMailbox.toMailboxCache()));
|
||||
});
|
||||
});
|
||||
|
||||
group('[add]', () {
|
||||
test('cache should add item when cache empty', () async {
|
||||
await _mailboxCacheClient.insertItem(
|
||||
MailboxFixtures.inboxMailbox.id.toString(),
|
||||
MailboxFixtures.inboxMailbox.toMailboxCache());
|
||||
|
||||
final remainingItems = await _mailboxCacheClient.getAll();
|
||||
|
||||
expect(remainingItems.length, equals(1));
|
||||
expect(remainingItems[0], equals(MailboxFixtures.inboxMailbox.toMailboxCache()));
|
||||
});
|
||||
|
||||
test('cache should add item when cache not empty', () async {
|
||||
await _mailboxCacheClient.insertItem(
|
||||
MailboxFixtures.inboxMailbox.id.toString(),
|
||||
MailboxFixtures.inboxMailbox.toMailboxCache());
|
||||
|
||||
await _mailboxCacheClient.insertItem(
|
||||
MailboxFixtures.sentMailbox.id.toString(),
|
||||
MailboxFixtures.sentMailbox.toMailboxCache());
|
||||
|
||||
final remainingItems = await _mailboxCacheClient.getAll();
|
||||
|
||||
expect(remainingItems.length, equals(2));
|
||||
expect(
|
||||
remainingItems,
|
||||
containsAll({
|
||||
MailboxFixtures.inboxMailbox.toMailboxCache(),
|
||||
MailboxFixtures.sentMailbox.toMailboxCache()
|
||||
}));
|
||||
});
|
||||
|
||||
test('cache should not add item twice', () async {
|
||||
await _mailboxCacheClient.insertItem(
|
||||
MailboxFixtures.inboxMailbox.id.toString(),
|
||||
MailboxFixtures.inboxMailbox.toMailboxCache());
|
||||
|
||||
await _mailboxCacheClient.insertItem(
|
||||
MailboxFixtures.inboxMailbox.id.toString(),
|
||||
MailboxFixtures.inboxMailbox.toMailboxCache());
|
||||
|
||||
final remainingItems = await _mailboxCacheClient.getAll();
|
||||
|
||||
expect(remainingItems.length, equals(1));
|
||||
expect(
|
||||
remainingItems,
|
||||
containsAll({
|
||||
MailboxFixtures.inboxMailbox.toMailboxCache(),
|
||||
}));
|
||||
});
|
||||
});
|
||||
|
||||
group('[update]', () {
|
||||
test('cache should update item when update to iem which not in cache', () async {
|
||||
await _mailboxCacheClient.updateItem(
|
||||
MailboxFixtures.inboxMailbox.id.toString(),
|
||||
MailboxFixtures.inboxMailbox.toMailboxCache());
|
||||
|
||||
final remainingItems = await _mailboxCacheClient.getAll();
|
||||
|
||||
expect(remainingItems.length, equals(1));
|
||||
expect(remainingItems[0], equals(MailboxFixtures.inboxMailbox.toMailboxCache()));
|
||||
});
|
||||
|
||||
test('cache should update correctly item', () async {
|
||||
await _mailboxCacheClient.insertItem(
|
||||
MailboxFixtures.inboxMailbox.id.toString(),
|
||||
MailboxFixtures.inboxMailbox.toMailboxCache());
|
||||
|
||||
await _mailboxCacheClient.insertItem(
|
||||
MailboxFixtures.sentMailbox.id.toString(),
|
||||
MailboxFixtures.sentMailbox.toMailboxCache());
|
||||
|
||||
await _mailboxCacheClient.updateItem(
|
||||
MailboxFixtures.inboxMailbox.id.toString(),
|
||||
MailboxFixtures.folder1.toMailboxCache());
|
||||
|
||||
final remainingItems = await _mailboxCacheClient.getAll();
|
||||
|
||||
expect(remainingItems.length, equals(2));
|
||||
expect(
|
||||
remainingItems,
|
||||
containsAll({
|
||||
MailboxFixtures.sentMailbox.toMailboxCache(),
|
||||
MailboxFixtures.folder1.toMailboxCache()
|
||||
}));
|
||||
});
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
await _mailboxCacheClient.deleteBox();
|
||||
});
|
||||
}
|
||||
Vendored
+1
-1
@@ -95,4 +95,4 @@ class MailboxFixtures {
|
||||
true),
|
||||
IsSubscribed(true)
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user