Fix duplicated selected AccountCache

This commit is contained in:
Dat PHAM HOANG
2024-06-21 23:23:22 +07:00
committed by Dat H. Pham
parent 4a245e06b8
commit 885a386068
3 changed files with 344 additions and 1 deletions
@@ -35,8 +35,8 @@ class AccountCacheManager {
.removeDuplicated()
.whereNot((account) => account.accountId == newAccountCache.accountId)
.toList();
await _accountCacheClient.clearAllData();
if (newAllAccounts.isNotEmpty) {
await _accountCacheClient.clearAllData();
await _accountCacheClient.updateMultipleItem(newAllAccounts.toMap());
}
}
@@ -0,0 +1,234 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:jmap_dart_client/jmap/account_id.dart';
import 'package:jmap_dart_client/jmap/core/id.dart';
import 'package:jmap_dart_client/jmap/core/user_name.dart';
import 'package:model/model.dart';
import 'package:tmail_ui_user/features/caching/clients/account_cache_client.dart';
import 'package:tmail_ui_user/features/login/data/local/account_cache_manager.dart';
import 'package:tmail_ui_user/features/login/data/model/account_cache.dart';
import 'memory_account_cache_client.dart';
void main() {
late AccountCacheManager accountCacheManager;
late AccountCacheClient accountCacheClient;
group('setCurrentAccount for the same accountId', () {
setUp(() {
accountCacheClient = MemoryAccountCacheClient();
accountCacheManager = AccountCacheManager(accountCacheClient);
});
test('WHEN cache have no account \n'
'AND another account is selected \n'
'THEN cache will have only new account', () async {
// arrange
final personalAccount3 = PersonalAccount(
'834191067',
AuthenticationType.oidc,
isSelected: true,
accountId: AccountId(Id(
'ae08b34da40b48f30ec0b94db05675894262fbc5c2e278644f9517aaf25e8246')),
apiUrl: 'https://jmap.domain.com/oidc/jmap',
userName: UserName('username@domain.com'),
);
// act
await accountCacheManager.setCurrentAccount(personalAccount3);
// assert
final allAccounts = await accountCacheClient.getAll();
expect(allAccounts.length, 1);
final result = await accountCacheManager.getCurrentAccount();
expect(result, personalAccount3);
});
test('WHEN cache have one selected account \n'
'AND another account which have same accountId is selected \n'
'THEN cache will have only new account', () async {
// arrange
final account1 = AccountCache(
'253956617',
'oidc',
isSelected: true,
accountId:
'ae08b34da40b48f30ec0b94db05675894262fbc5c2e278644f9517aaf25e8246',
apiUrl: 'https://jmap.domain.com/oidc/jmap',
userName: 'username@domain.com',
);
final personalAccount3 = PersonalAccount(
'834191067',
AuthenticationType.oidc,
isSelected: true,
accountId: AccountId(Id(
'ae08b34da40b48f30ec0b94db05675894262fbc5c2e278644f9517aaf25e8246')),
apiUrl: 'https://jmap.domain.com/oidc/jmap',
userName: UserName('username@domain.com'),
);
accountCacheClient.insertMultipleItem({
account1.id: account1,
});
// act
await accountCacheManager.setCurrentAccount(personalAccount3);
// assert
final allAccounts = await accountCacheClient.getAll();
expect(allAccounts.length, 1);
final result = await accountCacheManager.getCurrentAccount();
expect(result, personalAccount3);
});
test('WHEN cache have two selected account \n'
'AND another account which have the same accountId is selected \n'
'THEN cache will have only new account', () async {
// arrange
final account1 = AccountCache(
'253956617',
'oidc',
isSelected: true,
accountId:
'ae08b34da40b48f30ec0b94db05675894262fbc5c2e278644f9517aaf25e8246',
apiUrl: 'https://jmap.domain.com/oidc/jmap',
userName: 'username@domain.com',
);
final account2 = AccountCache(
'60734964',
'oidc',
isSelected: true,
accountId:
'ae08b34da40b48f30ec0b94db05675894262fbc5c2e278644f9517aaf25e8246',
apiUrl: 'https://jmap.domain.com/oidc/jmap',
userName: 'username@domain.com',
);
final personalAccount3 = PersonalAccount(
'834191067',
AuthenticationType.oidc,
isSelected: true,
accountId: AccountId(Id(
'ae08b34da40b48f30ec0b94db05675894262fbc5c2e278644f9517aaf25e8246')),
apiUrl: 'https://jmap.domain.com/oidc/jmap',
userName: UserName('username@domain.com'),
);
accountCacheClient.insertMultipleItem({
account1.id: account1,
account2.id: account2,
});
// act
await accountCacheManager.setCurrentAccount(personalAccount3);
// assert
final allAccounts = await accountCacheClient.getAll();
expect(allAccounts.length, 1);
final result = await accountCacheManager.getCurrentAccount();
expect(result, personalAccount3);
});
});
group('setCurrentAccount for different accountId', () {
setUp(() {
accountCacheClient = MemoryAccountCacheClient();
accountCacheManager = AccountCacheManager(accountCacheClient);
});
test('WHEN cache have one selected account \n'
'AND another account is selected \n'
'THEN cache will update with new selection', () async {
// arrange
final account1 = AccountCache(
'253956617',
'oidc',
isSelected: true,
accountId:
'5c2e278644f9517aaf25e8246ae08b34da40b48f30ec0b94db05675894262fbc',
apiUrl: 'https://jmap.domain.com/oidc/jmap',
userName: 'name@domain.com',
);
final personalAccount3 = PersonalAccount(
'834191067',
AuthenticationType.oidc,
isSelected: true,
accountId: AccountId(Id(
'ae08b34da40b48f30ec0b94db05675894262fbc5c2e278644f9517aaf25e8246')),
apiUrl: 'https://jmap.domain.com/oidc/jmap',
userName: UserName('username@domain.com'),
);
accountCacheClient.insertItem(account1.id, account1);
// act
await accountCacheManager.setCurrentAccount(personalAccount3);
// assert
final allAccounts = await accountCacheClient.getAll();
expect(allAccounts.length, 2);
final oldSelected = await accountCacheClient.getItem(account1.id);
expect(oldSelected!.isSelected, false);
final newSelected = await accountCacheManager.getCurrentAccount();
expect(newSelected, personalAccount3);
expect(newSelected.isSelected, true);
});
test('WHEN cache have two selected account \n'
'AND another account is selected \n'
'THEN cache will update with new selected account', () async {
// arrange
final account1 = AccountCache(
'253956617',
'oidc',
isSelected: true,
accountId:
'f30ec0b94db05675894262fbc5c2e27ae08b34da40b488644f9517aaf25e8246',
apiUrl: 'https://jmap.domain.com/oidc/jmap',
userName: 'usernameA@domain.com',
);
final account2 = AccountCache(
'60734964',
'oidc',
isSelected: true,
accountId:
'4db05675894262fbc5c2e27ae08b34da40b48f30ec0b98644f9517aaf25e8246',
apiUrl: 'https://jmap.domain.com/oidc/jmap',
userName: 'usernameB@domain.com',
);
final personalAccount3 = PersonalAccount(
'834191067',
AuthenticationType.oidc,
isSelected: true,
accountId: AccountId(Id(
'5675894262fbc5c2e278644f9517aaf25e8246ae08b34da40b48f30ec0b94db0')),
apiUrl: 'https://jmap.domain.com/oidc/jmap',
userName: UserName('usernameC@domain.com'),
);
accountCacheClient.insertMultipleItem({
account1.id: account1,
account2.id: account2,
});
// act
await accountCacheManager.setCurrentAccount(personalAccount3);
// assert
final allAccounts = await accountCacheClient.getAll();
expect(allAccounts.length, 3);
final oldOne = await accountCacheClient.getItem(account1.id);
expect(oldOne!.isSelected, false);
final oldTwo = await accountCacheClient.getItem(account2.id);
expect(oldTwo!.isSelected, false);
final newSelected = await accountCacheManager.getCurrentAccount();
expect(newSelected, personalAccount3);
expect(newSelected.isSelected, true);
});
});
}
@@ -0,0 +1,109 @@
import 'package:hive/hive.dart';
import 'package:tmail_ui_user/features/caching/clients/account_cache_client.dart';
import 'package:tmail_ui_user/features/login/data/model/account_cache.dart';
class MemoryAccountCacheClient implements AccountCacheClient {
final Map<String, AccountCache> _cache = {};
@override
Future<void> clearAllData() {
_cache.clear();
return Future.value();
}
@override
Future<void> clearAllDataContainKey(String nestedKey) {
_cache.removeWhere((key, value) => key == nestedKey);
return Future.value();
}
@override
Future<void> closeBox() {
return Future.value();
}
@override
Future<void> deleteBox() {
return Future.value();
}
@override
Future<void> deleteItem(String key) {
_cache.remove(key);
return Future.value();
}
@override
Future<void> deleteMultipleItem(List<String> listKey) {
_cache.removeWhere((key, value) => listKey.contains(key));
return Future.value();
}
@override
bool get encryption => false;
@override
Future<List<AccountCache>> getAll() {
return Future.value(_cache.values.toList());
}
@override
Future<AccountCache?> getItem(String key, {bool needToReopen = false}) {
return Future.value(_cache[key]);
}
@override
Future<List<AccountCache>> getListByNestedKey(String nestedKey) {
return Future.value(
_cache.values.where((account) => account.id == nestedKey).toList());
}
@override
Future<List<AccountCache>> getValuesByListKey(List<String> listKeys) {
return Future.value(_cache.values
.where((account) => listKeys.contains(account.id))
.toList());
}
@override
Future<void> insertItem(String key, AccountCache newObject) {
_cache[key] = newObject;
return Future.value();
}
@override
Future<void> insertMultipleItem(Map<String, AccountCache> mapObject) {
_cache.addAll(mapObject);
return Future.value();
}
@override
Future<bool> isExistItem(String key) {
return Future.value(_cache.containsKey(key));
}
@override
Future<Box<AccountCache>> openBox() {
throw UnimplementedError();
}
@override
Future<Box<AccountCache>> openBoxEncryption() {
throw UnimplementedError();
}
@override
String get tableName => 'AccountCache';
@override
Future<void> updateItem(String key, AccountCache newObject) {
_cache[key] = newObject;
return Future.value();
}
@override
Future<void> updateMultipleItem(Map<String, AccountCache> mapObject) {
_cache.addAll(mapObject);
return Future.value();
}
}