TF-129 Unit-test for mailbox_cache_client

This commit is contained in:
Dat PHAM HOANG
2021-09-30 15:50:40 +07:00
committed by Dat H. Pham
parent f152f83ca2
commit c6ee4e907b
6 changed files with 217 additions and 27 deletions
@@ -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() {
+10 -10
View File
@@ -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;
+10 -10
View File
@@ -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;