fix manager tests and update fakecoinservice

This commit is contained in:
julian 2023-01-19 11:55:40 -06:00
parent 686c897515
commit e248a6fd77
2 changed files with 57 additions and 117 deletions

View file

@ -1,19 +1,11 @@
import 'package:decimal/decimal.dart';
import 'package:stackwallet/models/balance.dart';
import 'package:stackwallet/models/isar/models/blockchain_data/transaction.dart';
import 'package:stackwallet/models/isar/models/blockchain_data/utxo.dart';
import 'package:stackwallet/models/paymint/fee_object_model.dart';
import 'package:stackwallet/models/paymint/transactions_model.dart';
import 'package:stackwallet/models/paymint/utxo_model.dart';
import 'package:stackwallet/services/coins/coin_service.dart';
import 'package:stackwallet/utilities/enums/coin_enum.dart';
class FakeCoinServiceAPI extends CoinServiceAPI {
@override
// TODO: implement allOwnAddresses
Future<List<String>> get allOwnAddresses => throw UnimplementedError();
@override
// TODO: implement balanceMinusMaxFee
Future<Decimal> get balanceMinusMaxFee => throw UnimplementedError();
@override
// TODO: implement currentReceivingAddress
Future<String> get currentReceivingAddress => throw UnimplementedError();
@ -32,24 +24,12 @@ class FakeCoinServiceAPI extends CoinServiceAPI {
// TODO: implement mnemonic
Future<List<String>> get mnemonic => throw UnimplementedError();
@override
// TODO: implement pendingBalance
Future<Decimal> get pendingBalance => throw UnimplementedError();
@override
Future<void> refresh() {
// TODO: implement refresh
throw UnimplementedError();
}
@override
// TODO: implement totalBalance
Future<Decimal> get totalBalance => throw UnimplementedError();
@override
// TODO: implement transactionData
Future<TransactionData> get transactionData => throw UnimplementedError();
@override
bool validateAddress(String address) {
// TODO: implement validateAddress
@ -71,10 +51,6 @@ class FakeCoinServiceAPI extends CoinServiceAPI {
throw UnimplementedError();
}
@override
// TODO: implement unspentOutputs
Future<List<UtxoObject>> get unspentOutputs => throw UnimplementedError();
@override
bool get isFavorite => throw UnimplementedError();
@ -84,10 +60,6 @@ class FakeCoinServiceAPI extends CoinServiceAPI {
@override
late bool shouldAutoSync;
@override
// TODO: implement availableBalance
Future<Decimal> get availableBalance => throw UnimplementedError();
@override
// TODO: implement coin
Coin get coin => throw UnimplementedError();
@ -162,15 +134,6 @@ class FakeCoinServiceAPI extends CoinServiceAPI {
throw UnimplementedError();
}
@override
Future<String> send(
{required String toAddress,
required int amount,
Map<String, String> args = const {}}) {
// TODO: implement send
throw UnimplementedError();
}
@override
Future<bool> testNetworkConnection() {
// TODO: implement testNetworkConnection
@ -188,4 +151,20 @@ class FakeCoinServiceAPI extends CoinServiceAPI {
// TODO: implement updateSentCachedTxData
throw UnimplementedError();
}
@override
// TODO: implement balance
Balance get balance => throw UnimplementedError();
@override
// TODO: implement storedChainHeight
int get storedChainHeight => throw UnimplementedError();
@override
// TODO: implement transactions
Future<List<Transaction>> get transactions => throw UnimplementedError();
@override
// TODO: implement utxos
Future<List<UTXO>> get utxos => throw UnimplementedError();
}

View file

@ -1,15 +1,15 @@
import 'package:decimal/decimal.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:stackwallet/electrumx_rpc/electrumx.dart';
import 'package:stackwallet/models/models.dart';
import 'package:stackwallet/models/balance.dart';
import 'package:stackwallet/models/isar/models/isar_models.dart';
import 'package:stackwallet/models/paymint/fee_object_model.dart';
import 'package:stackwallet/services/coins/coin_service.dart';
import 'package:stackwallet/services/coins/firo/firo_wallet.dart';
import 'package:stackwallet/services/coins/manager.dart';
import 'package:stackwallet/utilities/enums/coin_enum.dart';
import 'firo/sample_data/transaction_data_samples.dart';
import 'manager_test.mocks.dart';
@GenerateMocks([FiroWallet, ElectrumX])
@ -28,30 +28,6 @@ void main() {
expect(manager.coin, Coin.firo);
});
group("send", () {
test("successful send", () async {
final CoinServiceAPI wallet = MockFiroWallet();
when(wallet.send(toAddress: "some address", amount: 1987634))
.thenAnswer((_) async => "some txid");
final manager = Manager(wallet);
expect(await manager.send(toAddress: "some address", amount: 1987634),
"some txid");
});
test("failed send", () {
final CoinServiceAPI wallet = MockFiroWallet();
when(wallet.send(toAddress: "some address", amount: 1987634))
.thenThrow(Exception("Tx failed!"));
final manager = Manager(wallet);
expect(() => manager.send(toAddress: "some address", amount: 1987634),
throwsA(isA<Exception>()));
});
});
test("fees", () async {
final CoinServiceAPI wallet = MockFiroWallet();
when(wallet.fees).thenAnswer((_) async => FeeObject(
@ -98,68 +74,53 @@ void main() {
group("get balances", () {
test("balance", () async {
final CoinServiceAPI wallet = MockFiroWallet();
when(wallet.availableBalance).thenAnswer((_) async => Decimal.ten);
when(wallet.balance).thenAnswer(
(_) => Balance(
coin: Coin.firo,
total: 10,
spendable: 1,
blockedTotal: 0,
pendingSpendable: 9,
),
);
final manager = Manager(wallet);
expect(await manager.availableBalance, Decimal.ten);
expect(manager.balance.coin, Coin.firo);
expect(manager.balance.total, 10);
expect(manager.balance.spendable, 1);
expect(manager.balance.blockedTotal, 0);
expect(manager.balance.pendingSpendable, 9);
});
});
test("pendingBalance", () async {
test("transactions", () async {
final CoinServiceAPI wallet = MockFiroWallet();
when(wallet.pendingBalance).thenAnswer((_) async => Decimal.fromInt(23));
final tx = Transaction(
walletId: "walletId",
txid: "txid",
timestamp: 6,
type: TransactionType.incoming,
subType: TransactionSubType.mint,
amount: 123,
fee: 3,
height: 123,
isCancelled: false,
isLelantus: true,
slateId: null,
otherData: null,
);
when(wallet.transactions).thenAnswer((_) async => [
tx,
]);
final manager = Manager(wallet);
expect(await manager.pendingBalance, Decimal.fromInt(23));
});
final result = await manager.transactions;
test("totalBalance", () async {
final wallet = MockFiroWallet();
when(wallet.totalBalance).thenAnswer((_) async => Decimal.fromInt(2));
expect(result.length, 1);
final manager = Manager(wallet);
expect(await manager.totalBalance, Decimal.fromInt(2));
});
test("balanceMinusMaxFee", () async {
final CoinServiceAPI wallet = MockFiroWallet();
when(wallet.balanceMinusMaxFee).thenAnswer((_) async => Decimal.one);
final manager = Manager(wallet);
expect(await manager.balanceMinusMaxFee, Decimal.one);
});
});
test("allOwnAddresses", () async {
final CoinServiceAPI wallet = MockFiroWallet();
when(wallet.allOwnAddresses)
.thenAnswer((_) async => ["address1", "address2", "address3"]);
final manager = Manager(wallet);
expect(await manager.allOwnAddresses, ["address1", "address2", "address3"]);
});
test("transactionData", () async {
final CoinServiceAPI wallet = MockFiroWallet();
when(wallet.transactionData)
.thenAnswer((_) async => TransactionData.fromJson(dateTimeChunksJson));
final manager = Manager(wallet);
final expectedMap =
TransactionData.fromJson(dateTimeChunksJson).getAllTransactions();
final result = (await manager.transactionData).getAllTransactions();
expect(result.length, expectedMap.length);
for (int i = 0; i < expectedMap.length; i++) {
final resultTxid = result.keys.toList(growable: false)[i];
expect(result[resultTxid].toString(), expectedMap[resultTxid].toString());
}
expect(result.first, tx);
});
test("refresh", () async {