From e248a6fd77ab1bc99b9a191089779905cb59663a Mon Sep 17 00:00:00 2001 From: julian Date: Thu, 19 Jan 2023 11:55:40 -0600 Subject: [PATCH] fix manager tests and update fakecoinservice --- .../services/coins/fake_coin_service_api.dart | 59 +++------ test/services/coins/manager_test.dart | 115 ++++++------------ 2 files changed, 57 insertions(+), 117 deletions(-) diff --git a/test/services/coins/fake_coin_service_api.dart b/test/services/coins/fake_coin_service_api.dart index c5f300c16..37f47fa8e 100644 --- a/test/services/coins/fake_coin_service_api.dart +++ b/test/services/coins/fake_coin_service_api.dart @@ -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> get allOwnAddresses => throw UnimplementedError(); - - @override - // TODO: implement balanceMinusMaxFee - Future get balanceMinusMaxFee => throw UnimplementedError(); - @override // TODO: implement currentReceivingAddress Future get currentReceivingAddress => throw UnimplementedError(); @@ -32,24 +24,12 @@ class FakeCoinServiceAPI extends CoinServiceAPI { // TODO: implement mnemonic Future> get mnemonic => throw UnimplementedError(); - @override - // TODO: implement pendingBalance - Future get pendingBalance => throw UnimplementedError(); - @override Future refresh() { // TODO: implement refresh throw UnimplementedError(); } - @override - // TODO: implement totalBalance - Future get totalBalance => throw UnimplementedError(); - - @override - // TODO: implement transactionData - Future 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> 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 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 send( - {required String toAddress, - required int amount, - Map args = const {}}) { - // TODO: implement send - throw UnimplementedError(); - } - @override Future 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> get transactions => throw UnimplementedError(); + + @override + // TODO: implement utxos + Future> get utxos => throw UnimplementedError(); } diff --git a/test/services/coins/manager_test.dart b/test/services/coins/manager_test.dart index 60cab37c0..e3cd7b917 100644 --- a/test/services/coins/manager_test.dart +++ b/test/services/coins/manager_test.dart @@ -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())); - }); - }); - 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); - }); - - test("pendingBalance", () async { - final CoinServiceAPI wallet = MockFiroWallet(); - when(wallet.pendingBalance).thenAnswer((_) async => Decimal.fromInt(23)); - - final manager = Manager(wallet); - - expect(await manager.pendingBalance, Decimal.fromInt(23)); - }); - - test("totalBalance", () async { - final wallet = MockFiroWallet(); - when(wallet.totalBalance).thenAnswer((_) async => Decimal.fromInt(2)); - - 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); + 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("allOwnAddresses", () async { + test("transactions", () async { final CoinServiceAPI wallet = MockFiroWallet(); - when(wallet.allOwnAddresses) - .thenAnswer((_) async => ["address1", "address2", "address3"]); + 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.allOwnAddresses, ["address1", "address2", "address3"]); - }); + final result = await manager.transactions; - test("transactionData", () async { - final CoinServiceAPI wallet = MockFiroWallet(); - when(wallet.transactionData) - .thenAnswer((_) async => TransactionData.fromJson(dateTimeChunksJson)); + expect(result.length, 1); - 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 {