finished basic change_now.dart tests

This commit is contained in:
julian 2022-08-30 14:26:43 -06:00
parent f129c13e92
commit 549e587b3b
2 changed files with 295 additions and 21 deletions

View file

@ -128,11 +128,8 @@ abstract class ChangeNow {
.add(Currency.fromJson(Map<String, dynamic>.from(json as Map)));
} catch (_) {
return ChangeNowResponse(
exception: ChangeNowException(
"Failed to serialize $json",
ChangeNowExceptionType.serializeResponseError,
),
);
exception: ChangeNowException("Failed to serialize $json",
ChangeNowExceptionType.serializeResponseError));
}
}
@ -183,11 +180,8 @@ abstract class ChangeNow {
Logging.instance.log("getPairedCurrencies exception: $e\n$s",
level: LogLevel.Error);
return ChangeNowResponse(
exception: ChangeNowException(
"Error: $jsonArray",
ChangeNowExceptionType.serializeResponseError,
),
);
exception: ChangeNowException("Error: $jsonArray",
ChangeNowExceptionType.serializeResponseError));
}
return ChangeNowResponse(value: currencies);
} catch (e, s) {
@ -389,11 +383,8 @@ abstract class ChangeNow {
FixedRateMarket.fromJson(Map<String, dynamic>.from(json as Map)));
} catch (_) {
return ChangeNowResponse(
exception: ChangeNowException(
"Failed to serialize $json",
ChangeNowExceptionType.serializeResponseError,
),
);
exception: ChangeNowException("Failed to serialize $json",
ChangeNowExceptionType.serializeResponseError));
}
}
return ChangeNowResponse(value: markets);
@ -613,11 +604,8 @@ abstract class ChangeNow {
fromTicker: stringPair[0], toTicker: stringPair[1]));
} catch (_) {
return ChangeNowResponse(
exception: ChangeNowException(
"Failed to serialize $json",
ChangeNowExceptionType.serializeResponseError,
),
);
exception: ChangeNowException("Failed to serialize $json",
ChangeNowExceptionType.serializeResponseError));
}
}
return ChangeNowResponse(value: pairs);

View file

@ -5,8 +5,11 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:stackwallet/models/exchange/change_now/available_floating_rate_pair.dart';
import 'package:stackwallet/models/exchange/change_now/change_now_response.dart';
import 'package:stackwallet/models/exchange/change_now/estimated_exchange_amount.dart';
import 'package:stackwallet/models/exchange/change_now/exchange_transaction.dart';
import 'package:stackwallet/models/exchange/change_now/exchange_transaction_status.dart';
import 'package:stackwallet/services/change_now/change_now.dart';
import 'change_now_sample_data.dart';
@ -159,7 +162,7 @@ void main() {
});
test(
"getPairedCurrencies fails with ChangeNowExceptionType.serializeResponseError",
"getPairedCurrencies fails with ChangeNowExceptionType.serializeResponseError A",
() async {
final client = MockClient();
ChangeNow.client = client;
@ -261,6 +264,77 @@ void main() {
});
});
group("getEstimatedExchangeAmount", () {
test("getEstimatedExchangeAmount succeeds", () async {
final client = MockClient();
ChangeNow.client = client;
when(client.get(
Uri.parse(
"https://api.changenow.io/v1/exchange-amount/42/xmr_btc?api_key=testAPIKEY"),
headers: {'Content-Type': 'application/json'},
)).thenAnswer((realInvocation) async => Response(
'{"estimatedAmount": 58.4142873, "transactionSpeedForecast": "10-60", "warningMessage": null}',
200));
final result = await ChangeNow.getEstimatedExchangeAmount(
fromTicker: "xmr",
toTicker: "btc",
fromAmount: Decimal.fromInt(42),
apiKey: "testAPIKEY",
);
expect(result.exception, null);
expect(result.value == null, false);
expect(result.value, isA<EstimatedExchangeAmount>());
});
test(
"getEstimatedExchangeAmount fails with ChangeNowExceptionType.serializeResponseError",
() async {
final client = MockClient();
ChangeNow.client = client;
when(client.get(
Uri.parse(
"https://api.changenow.io/v1/exchange-amount/42/xmr_btc?api_key=testAPIKEY"),
headers: {'Content-Type': 'application/json'},
)).thenAnswer((realInvocation) async => Response('{"error": 42}', 200));
final result = await ChangeNow.getEstimatedExchangeAmount(
fromTicker: "xmr",
toTicker: "btc",
fromAmount: Decimal.fromInt(42),
apiKey: "testAPIKEY",
);
expect(result.exception!.type,
ChangeNowExceptionType.serializeResponseError);
expect(result.value == null, true);
});
test("getEstimatedExchangeAmount fails for any other reason", () async {
final client = MockClient();
ChangeNow.client = client;
when(client.get(
Uri.parse(
"https://api.changenow.io/v1/exchange-amount/42/xmr_btc?api_key=testAPIKEY"),
headers: {'Content-Type': 'application/json'},
)).thenAnswer((realInvocation) async => Response('', 400));
final result = await ChangeNow.getEstimatedExchangeAmount(
fromTicker: "xmr",
toTicker: "btc",
fromAmount: Decimal.fromInt(42),
apiKey: "testAPIKEY",
);
expect(result.exception!.type, ChangeNowExceptionType.generic);
expect(result.value == null, true);
});
});
group("getEstimatedFixedRateExchangeAmount", () {
test("getEstimatedFixedRateExchangeAmount succeeds", () async {
final client = MockClient();
@ -479,4 +553,216 @@ void main() {
expect(result.value == null, true);
});
});
group("createFixedRateExchangeTransaction", () {
test("createFixedRateExchangeTransaction succeeds", () async {
final client = MockClient();
ChangeNow.client = client;
when(client.post(
Uri.parse(
"https://api.changenow.io/v1/transactions/fixed-rate/testAPIKEY"),
headers: {'Content-Type': 'application/json'},
body:
'{"from":"btc","to":"eth","address":"0x57f31ad4b64095347F87eDB1675566DAfF5EC886","amount":"0.3","flow":"fixed-rate","extraId":"","userId":"","contactEmail":"","refundAddress":"","refundExtraId":"","rateId":""}',
encoding: null,
)).thenAnswer((realInvocation) async => Response(
'{"payinAddress": "33eFX2jfeWbXMSmRe9ewUUTrmSVSxZi5cj", "payoutAddress": "0x57f31ad4b64095347F87eDB1675566DAfF5EC886","payoutExtraId": "", "fromCurrency": "btc", "toCurrency": "eth", "refundAddress": "","refundExtraId": "","validUntil": "2019-09-09T14:01:04.921Z","id": "a5c73e2603f40d","amount": 62.9737711}',
200));
final result = await ChangeNow.createFixedRateExchangeTransaction(
fromTicker: "btc",
toTicker: "eth",
receivingAddress: "0x57f31ad4b64095347F87eDB1675566DAfF5EC886",
amount: Decimal.parse("0.3"),
refundAddress: "",
apiKey: "testAPIKEY",
rateId: '',
);
expect(result.exception, null);
expect(result.value == null, false);
expect(result.value, isA<ExchangeTransaction>());
});
test(
"createFixedRateExchangeTransaction fails with ChangeNowExceptionType.serializeResponseError",
() async {
final client = MockClient();
ChangeNow.client = client;
when(client.post(
Uri.parse(
"https://api.changenow.io/v1/transactions/fixed-rate/testAPIKEY"),
headers: {'Content-Type': 'application/json'},
body:
'{"from":"btc","to":"eth","address":"0x57f31ad4b64095347F87eDB1675566DAfF5EC886","amount":"0.3","flow":"fixed-rate","extraId":"","userId":"","contactEmail":"","refundAddress":"","refundExtraId":"","rateId":""}',
encoding: null,
)).thenAnswer((realInvocation) async =>
Response('{"id": "a5c73e2603f40d","amount": 62.9737711}', 200));
final result = await ChangeNow.createFixedRateExchangeTransaction(
fromTicker: "btc",
toTicker: "eth",
receivingAddress: "0x57f31ad4b64095347F87eDB1675566DAfF5EC886",
amount: Decimal.parse("0.3"),
refundAddress: "",
apiKey: "testAPIKEY",
rateId: '',
);
expect(result.exception!.type,
ChangeNowExceptionType.serializeResponseError);
expect(result.value == null, true);
});
test("createFixedRateExchangeTransaction fails for any other reason",
() async {
final client = MockClient();
ChangeNow.client = client;
when(client.post(
Uri.parse(
"https://api.changenow.io/v1/transactions/fixed-rate/testAPIKEY"),
headers: {'Content-Type': 'application/json'},
body:
'{"from": "btc","to": "eth","address": "0x57f31ad4b64095347F87eDB1675566DAfF5EC886", "amount": "1.12345","extraId": "", "userId": "","contactEmail": "","refundAddress": "", "refundExtraId": "", "rateId": "" }',
encoding: null,
)).thenAnswer((realInvocation) async => Response('', 400));
final result = await ChangeNow.createFixedRateExchangeTransaction(
fromTicker: "xmr",
toTicker: "btc",
receivingAddress: "bc1qu58svs9983e2vuyqh7gq7ratf8k5qehz5k0cn5",
amount: Decimal.parse("0.3"),
refundAddress:
"888tNkZrPN6JsEgekjMnABU4TBzc2Dt29EPAvkRxbANsAnjyPbb3iQ1YBRk1UXcdRsiKc9dhwMVgN5S9cQUiyoogDavup3H",
apiKey: "testAPIKEY",
rateId: '',
);
expect(result.exception!.type, ChangeNowExceptionType.generic);
expect(result.value == null, true);
});
});
group("getTransactionStatus", () {
test("getTransactionStatus succeeds", () async {
final client = MockClient();
ChangeNow.client = client;
when(client.get(
Uri.parse(
"https://api.changenow.io/v1/transactions/47F87eDB1675566DAfF5EC886/testAPIKEY"),
headers: {'Content-Type': 'application/json'},
)).thenAnswer((realInvocation) async => Response(
'{"status": "waiting", "payinAddress": "32Ge2ci26rj1sRGw2NjiQa9L7Xvxtgzhrj", "payoutAddress": "0x57f31ad4b64095347F87eDB1675566DAfF5EC886", "fromCurrency": "btc", "toCurrency": "eth", "id": "50727663e5d9a4", "updatedAt": "2019-08-22T14:47:49.943Z", "expectedSendAmount": 1, "expectedReceiveAmount": 52.31667, "createdAt": "2019-08-22T14:47:49.943Z", "isPartner": false}',
200));
final result = await ChangeNow.getTransactionStatus(
id: "47F87eDB1675566DAfF5EC886",
apiKey: "testAPIKEY",
);
expect(result.exception, null);
expect(result.value == null, false);
expect(result.value, isA<ExchangeTransactionStatus>());
});
test(
"getTransactionStatus fails with ChangeNowExceptionType.serializeResponseError",
() async {
final client = MockClient();
ChangeNow.client = client;
when(client.get(
Uri.parse(
"https://api.changenow.io/v1/transactions/47F87eDB1675566DAfF5EC886/testAPIKEY"),
headers: {'Content-Type': 'application/json'},
)).thenAnswer((realInvocation) async => Response('{"error": 42}', 200));
final result = await ChangeNow.getTransactionStatus(
id: "47F87eDB1675566DAfF5EC886",
apiKey: "testAPIKEY",
);
expect(result.exception!.type,
ChangeNowExceptionType.serializeResponseError);
expect(result.value == null, true);
});
test("getTransactionStatus fails for any other reason", () async {
final client = MockClient();
ChangeNow.client = client;
when(client.get(
Uri.parse(
"https://api.changenow.io/v1/transactions/47F87eDB1675566DAfF5EC886/testAPIKEY"),
headers: {'Content-Type': 'application/json'},
)).thenAnswer((realInvocation) async => Response('', 400));
final result = await ChangeNow.getTransactionStatus(
id: "47F87eDB1675566DAfF5EC886",
apiKey: "testAPIKEY",
);
expect(result.exception!.type, ChangeNowExceptionType.generic);
expect(result.value == null, true);
});
});
group("getAvailableFloatingRatePairs", () {
test("getAvailableFloatingRatePairs succeeds", () async {
final client = MockClient();
ChangeNow.client = client;
when(client.get(
Uri.parse(
"https://api.changenow.io/v1/market-info/available-pairs?includePartners=false"),
headers: {'Content-Type': 'application/json'},
)).thenAnswer((realInvocation) async =>
Response('["btc_xmr","btc_firo","btc_doge","eth_ltc"]', 200));
final result = await ChangeNow.getAvailableFloatingRatePairs();
expect(result.exception, null);
expect(result.value == null, false);
expect(result.value, isA<List<AvailableFloatingRatePair>>());
});
test(
"getAvailableFloatingRatePairs fails with ChangeNowExceptionType.serializeResponseError",
() async {
final client = MockClient();
ChangeNow.client = client;
when(client.get(
Uri.parse(
"https://api.changenow.io/v1/market-info/available-pairs?includePartners=false"),
headers: {'Content-Type': 'application/json'},
)).thenAnswer((realInvocation) async => Response('{"error": 42}', 200));
final result = await ChangeNow.getAvailableFloatingRatePairs();
expect(result.exception!.type,
ChangeNowExceptionType.serializeResponseError);
expect(result.value == null, true);
});
test("getAvailableFloatingRatePairs fails for any other reason", () async {
final client = MockClient();
ChangeNow.client = client;
when(client.get(
Uri.parse(
"https://api.changenow.io/v1/market-info/available-pairs?includePartners=false"),
headers: {'Content-Type': 'application/json'},
)).thenAnswer((realInvocation) async => Response('', 400));
final result = await ChangeNow.getAvailableFloatingRatePairs();
expect(result.exception!.type, ChangeNowExceptionType.generic);
expect(result.value == null, true);
});
});
}