stack_wallet/test/cached_electrumx_test.dart

172 lines
5.1 KiB
Dart
Raw Normal View History

2022-08-26 08:11:35 +00:00
import 'package:flutter_test/flutter_test.dart';
import 'package:hive_test/hive_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
2023-11-14 20:31:53 +00:00
import 'package:stackwallet/electrumx_rpc/cached_electrumx_client.dart';
import 'package:stackwallet/electrumx_rpc/electrumx_client.dart';
2022-08-26 08:11:35 +00:00
import 'package:stackwallet/utilities/enums/coin_enum.dart';
import 'package:stackwallet/utilities/prefs.dart';
import 'cached_electrumx_test.mocks.dart';
2022-09-08 20:21:26 +00:00
// import 'sample_data/get_anonymity_set_sample_data.dart';
2022-08-26 08:11:35 +00:00
2023-11-14 20:31:53 +00:00
@GenerateMocks([ElectrumXClient, Prefs])
2022-08-26 08:11:35 +00:00
void main() {
group("tests using mock hive", () {
setUp(() async {
await setUpTestHive();
// await Hive.openBox<dynamic>(
// DB.instance.boxNameUsedSerialsCache(coin: Coin.firo));
// await Hive.openBox<dynamic>(DB.instance.boxNameSetCache(coin: Coin.firo));
// await Hive.openBox<dynamic>(DB.instance.boxNameTxCache(coin: Coin.firo));
2022-08-26 08:11:35 +00:00
});
group("getAnonymitySet", () {
2022-09-08 20:21:26 +00:00
// test("empty set cache call", () async {
// final client = MockElectrumX();
// when(
// client.getAnonymitySet(
// groupId: "1",
// blockhash: "",
// ),
// ).thenAnswer(
// (_) async => GetAnonymitySetSampleData.data,
// );
//
// final cachedClient = CachedElectrumX(
// electrumXClient: client,
// port: 0,
// failovers: [],
// server: '',
// useSSL: true,
// prefs: Prefs.instance);
//
// final result = await cachedClient.getAnonymitySet(
// groupId: "1",
// coin: Coin.firo,
// );
//
// final expected =
// Map<String, dynamic>.from(GetAnonymitySetSampleData.data);
// expected["setId"] = "1";
//
// expect(result, expected);
// });
2022-08-26 08:11:35 +00:00
//
// test("use and update set cache call", () async {
// final storedData = Map.from(GetAnonymitySetSampleData.initialData);
// storedData["setId"] = "1";
// final box = await Hive.openBox('Some coinName_anonymitySetCache');
// await box.put("1", storedData);
//
// final client = MockElectrumX();
// when(
// client.getAnonymitySet(
// groupId: "1",
// blockhash: GetAnonymitySetSampleData.initialData["blockHash"],
// ),
// ).thenAnswer(
// (_) async => GetAnonymitySetSampleData.followUpData,
// );
//
// final cachedClient = CCachedElectrumX(
// electrumXClient: client,
// port: 0,
// failovers: [],
// server: '',
// useSSL: true,
// prefs: Prefs.instance);
//
// final result = await cachedClient.getAnonymitySet(
// groupId: "1",
// coinName: "Some coinName",
// callOutSideMainIsolate: true,
// );
//
// final expected = Map.from(GetAnonymitySetSampleData.finalData as Map);
// expected["setId"] = "1";
//
// expect(result, expected);
// fail("This test needs updating");
// });
2022-09-08 20:21:26 +00:00
// test("getAnonymitySet throws", () async {
// final client = MockElectrumX();
// when(
// client.getAnonymitySet(
// groupId: "1",
// blockhash: "",
// ),
// ).thenThrow(Exception());
//
// final cachedClient = CachedElectrumX(
// electrumXClient: client,
// port: 0,
// failovers: [],
// server: '',
// useSSL: true,
// prefs: Prefs.instance);
//
// expect(
// () async => await cachedClient.getAnonymitySet(
// groupId: "1",
// coin: Coin.firo,
// ),
// throwsA(isA<Exception>()));
// });
2022-08-26 08:11:35 +00:00
});
test("getTransaction throws", () async {
final client = MockElectrumX();
when(
client.getTransaction(
txHash: "some hash",
),
).thenThrow(Exception());
2023-11-14 20:31:53 +00:00
final cachedClient = CachedElectrumXClient(
2023-05-26 23:45:14 +00:00
electrumXClient: client,
);
2022-08-26 08:11:35 +00:00
expect(
() async => await cachedClient.getTransaction(
txHash: "some hash",
coin: Coin.firo,
),
throwsA(isA<Exception>()));
});
test("clearSharedTransactionCache", () async {
2023-11-14 20:31:53 +00:00
final cachedClient = CachedElectrumXClient(
2023-05-26 23:45:14 +00:00
electrumXClient: MockElectrumX(),
);
2022-08-26 08:11:35 +00:00
bool didThrow = false;
try {
await cachedClient.clearSharedTransactionCache(coin: Coin.firo);
} catch (_) {
didThrow = true;
}
expect(didThrow, false);
});
tearDown(() async {
await tearDownTestHive();
});
});
test(".from factory", () {
final node = ElectrumXNode(
address: "some address",
port: 1,
name: "some name",
id: "some ID",
useSSL: true,
);
2023-11-14 20:31:53 +00:00
final client = CachedElectrumXClient.from(electrumXClient: MockElectrumX());
2022-08-26 08:11:35 +00:00
2023-11-14 20:31:53 +00:00
expect(client, isA<CachedElectrumXClient>());
2022-08-26 08:11:35 +00:00
});
}