Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-organize test files #351

Merged
merged 4 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions lib/shared/api_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,16 @@ class ApiDatabase {

// get account preferences taking into account corrupted localStorage
Map<AccountPreferences, dynamic> accountPreferences;
accountPreferences = getUpdatedAccountInfo(AccountPreferencesParams(
walletIdToSet,
preferences,
walletStorage.currentWallet.masterAccount != null
accountPreferences = getUpdatedAccountInfo(
AccountPreferencesParams(
currentWalletId: walletIdToSet,
preferences: preferences,
accountList: walletStorage.currentWallet.masterAccount != null
? {0: walletStorage.currentWallet.masterAccount!}
: walletStorage.currentWallet.externalAccounts));
: walletStorage.currentWallet.externalAccounts,
isHdWallet: walletStorage.currentWallet.masterAccount == null,
),
);

// set new current wallet and account in local storage
if (isNewWallet || isUpdatedWallet && currentWalletId != null) {
Expand Down
23 changes: 13 additions & 10 deletions lib/util/account_preferences.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import 'package:my_wit_wallet/shared/api_database.dart';
import 'package:my_wit_wallet/util/storage/database/account.dart';

enum AccountPreferences { address, addressIndex, addressList }
enum AccountPreferences { address, addressIndex, addressList, isHdWallet }

class AccountPreferencesParams {
String? currentWalletId;
Map<WalletPreferences, dynamic>? preferences;
Map<int, Account> externalAccounts;
Map<int, Account> accountList;
bool isHdWallet;
AccountPreferencesParams(
this.currentWalletId, this.preferences, this.externalAccounts);
{this.currentWalletId,
this.preferences,
required this.accountList,
required this.isHdWallet});
}

Map<AccountPreferences, dynamic> getUpdatedAccountInfo(
AccountPreferencesParams params) {
bool isHdWallet = params.externalAccounts.isNotEmpty;
bool isHdWallet = params.isHdWallet;
String addressId = isHdWallet ? "0/0" : "m";
Map<String, dynamic> addressList = {
'${params.currentWalletId}': isHdWallet ? "0/0" : "m"
Expand All @@ -27,18 +31,17 @@ Map<AccountPreferences, dynamic> getUpdatedAccountInfo(
int addressIndex =
addressId.contains("/") ? int.parse(addressId.split('/').last) : 0;

bool isAddressIdxInStorage = params.externalAccounts.length > 0 &&
(addressIndex < params.externalAccounts.length);
bool isAddressIdxInStorage = params.accountList.length > 0 &&
(addressIndex < params.accountList.length);
Map<String, dynamic> defaultAccountSettings = {
...addressList,
'${params.currentWalletId}': isHdWallet ? "0/0" : "m",
};
String? defaultAddress = params.externalAccounts[0] != null
? params.externalAccounts[0]!.address
: null;
String? defaultAddress =
params.accountList[0] != null ? params.accountList[0]!.address : null;
return {
AccountPreferences.address: isAddressIdxInStorage
? params.externalAccounts[addressIndex]!.address
? params.accountList[addressIndex]!.address
: defaultAddress,
AccountPreferences.addressIndex:
isAddressIdxInStorage ? addressIndex.toString() : "0",
Expand Down
91 changes: 0 additions & 91 deletions test/account_preferences_test.dart

This file was deleted.

131 changes: 131 additions & 0 deletions test/util/account_preferences_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import 'package:my_wit_wallet/shared/api_database.dart';
import 'package:my_wit_wallet/util/account_preferences.dart';
import 'package:my_wit_wallet/util/storage/database/account.dart';
import 'package:test/test.dart';

void main() {
Map<AccountPreferences, dynamic> accountPreferences = getUpdatedAccountInfo(
AccountPreferencesParams(
currentWalletId: '8a7cf20f',
preferences: {
WalletPreferences.addressIndex: '0/0',
WalletPreferences.addressList: {
'8a7cf20f': '0/0',
}
},
accountList: {
0: Account(
walletName: '8a7cf20f',
address: 'wit000000000000000000000000000000000000000',
path: 'M/3h/4919h/0h/0/1')
},
isHdWallet: true),
);
// With not existent index saved
Map<AccountPreferences, dynamic> accountPreferences2 =
getUpdatedAccountInfo(AccountPreferencesParams(
currentWalletId: '8a7cf20f',
preferences: {
WalletPreferences.addressIndex: '0/2',
WalletPreferences.addressList: {
'8a7cf20f': '0/2',
}
},
accountList: {
0: Account(
walletName: '8a7cf20f',
address: 'wit000000000000000000000000000000000000000',
path: 'M/3h/4919h/0h/0/1')
},
isHdWallet: true));
// With db deleted
Map<AccountPreferences, dynamic> accountPreferences3 =
getUpdatedAccountInfo(AccountPreferencesParams(
currentWalletId: '8a7cf20f',
preferences: {
WalletPreferences.addressIndex: '0/2',
WalletPreferences.addressList: {
'8a7cf20f': '0/2',
}
},
accountList: {},
isHdWallet: true));
// With deleted prefs
Map<AccountPreferences, dynamic> accountPreferences4 =
getUpdatedAccountInfo(AccountPreferencesParams(
currentWalletId: '8a7cf20f',
preferences: null,
accountList: {
0: Account(
walletName: '8a7cf20f',
address: 'wit000000000000000000000000000000000000000',
path: 'M/3h/4919h/0h/0/1')
},
isHdWallet: true));
Map<AccountPreferences, dynamic> accountPreferences5 =
getUpdatedAccountInfo(AccountPreferencesParams(
currentWalletId: '8a7cf20f',
preferences: null,
accountList: {
0: Account(
walletName: '8a7cf20f',
address: 'wit000000000000000000000000000000000000000',
path: 'M/3h/4919h/0h/0/1')
},
isHdWallet: false));
group(
'getAccountPreferences',
() => {
test(
'with correctly saved preferences',
() => {
expect(accountPreferences, {
AccountPreferences.address:
'wit000000000000000000000000000000000000000',
AccountPreferences.addressIndex: '0',
AccountPreferences.addressList: {'8a7cf20f': '0/0'}
}),
}),
test(
'with not found address index in saved preferences',
() => {
expect(accountPreferences2, {
AccountPreferences.address:
'wit000000000000000000000000000000000000000',
AccountPreferences.addressIndex: '0',
AccountPreferences.addressList: {'8a7cf20f': '0/0'}
}),
}),
test(
'with deleted db and saved preferences',
() => {
expect(accountPreferences3, {
AccountPreferences.address: null,
AccountPreferences.addressIndex: '0',
AccountPreferences.addressList: {
'8a7cf20f': '0/0',
},
}),
}),
test(
'with deleted preferences and saved db',
() => {
expect(accountPreferences4, {
AccountPreferences.address:
'wit000000000000000000000000000000000000000',
AccountPreferences.addressIndex: '0',
AccountPreferences.addressList: {'8a7cf20f': '0/0'},
}),
}),
test(
'with deleted preferences and saved db with node address',
() => {
expect(accountPreferences5, {
AccountPreferences.address:
'wit000000000000000000000000000000000000000',
AccountPreferences.addressIndex: '0',
AccountPreferences.addressList: {'8a7cf20f': 'm'},
}),
}),
});
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,27 @@ void main() {
getTransactionAddress(tolabel, inputs, outputs);
String transactionAddressFromLabel =
getTransactionAddress(fromlabel, inputs, outputs);
String transactionAddressFromSeveralAddresses =
String transactionAddressFromSeveralInputs =
getTransactionAddress(fromlabel, severalInputs, outputs);

group(
'getTransactionAddress',
() => {
test(
'with to label',
'with \'to\' label',
() => {
expect(transactionAddressToLabel, 'wit1zl7...4wv69cw'),
}),
test(
'with from label',
'with \'from\' label',
() => {
expect(transactionAddressFromLabel, 'wit1zl7...4wv69cw'),
}),
test(
'with from label from several addresses',
'with \'from\' label and several inputs',
() => {
expect(transactionAddressFromSeveralAddresses,
'Several addresses'),
expect(transactionAddressFromSeveralInputs,
'wit1zl7...4wv69cw'),
})
});
}