Skip to content

Commit

Permalink
tests & null safety improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-rogobete committed Jun 28, 2024
1 parent 1def6f0 commit cd2ea14
Show file tree
Hide file tree
Showing 16 changed files with 561 additions and 198 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Future<ClaimableBalanceResponse> getLatestClaimbaleBalance(
.forClaimant(sourceKeyPair.accountId)
.order(RequestBuilderOrder.DESC)
.execute();
assert(claimableBalances.records!.isNotEmpty);
assert(claimableBalances.records.isNotEmpty);

return claimableBalances.records!.first;
return claimableBalances.records.first;
}
46 changes: 23 additions & 23 deletions lib/src/responses/response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'package:http/http.dart' as http;
import 'claimable_balance_response.dart';
import 'liquidity_pool_response.dart';
import 'dart:async';
import '../util.dart';
import '../requests/request_builder.dart';
import 'effects/effect_responses.dart';
import 'operations/operation_responses.dart';
Expand Down Expand Up @@ -79,7 +78,8 @@ class Link {
return Link(json['href'], json['templated']);
}

Map<String, dynamic> toJson() => <String, dynamic>{'href': href, 'templated': templated};
Map<String, dynamic> toJson() =>
<String, dynamic>{'href': href, 'templated': templated};
}

/// Links connected to page response.
Expand Down Expand Up @@ -113,28 +113,28 @@ abstract class TypedResponse<T> {

/// Represents page of objects.
class Page<T> extends Response implements TypedResponse<Page<T>> {
List<T>? records;
// List<dynamic>? records;
List<T> records;

PageLinks? links;

late TypeToken<Page<T>> type;
TypeToken<Page<T>> type;

Page();
Page(this.records, this.links, this.type);

///The next page of results or null when there is no link for the next page of results
Future<Page<T>?> getNextPage(http.Client httpClient) async {
if (this.links!.next == null) {
if (this.links?.next == null) {
return null;
}
checkNotNull(
this.type,
"type cannot be null, is it being correctly set after the creation of this " +
this.runtimeType.toString() +
"?");
ResponseHandler<Page<T>> responseHandler = ResponseHandler<Page<T>>(this.type);
String? url = this.links!.next!.href;

return await httpClient.get(Uri.parse(url), headers: RequestBuilder.headers).then((response) {
ResponseHandler<Page<T>> responseHandler =
ResponseHandler<Page<T>>(this.type);

String url = this.links!.next!.href;

return await httpClient
.get(Uri.parse(url), headers: RequestBuilder.headers)
.then((response) {
return responseHandler.handleResponse(response);
});
}
Expand All @@ -144,16 +144,16 @@ class Page<T> extends Response implements TypedResponse<Page<T>> {
this.type = type;
}

factory Page.fromJson(Map<String, dynamic> json) => Page<T>()
factory Page.fromJson(Map<String, dynamic> json) => Page<T>(
json["_embedded"]['records'] != null
? List<T>.from(json["_embedded"]['records']
.map((e) => ResponseConverter.fromJson<T>(e) as T))
: [],
json['_links'] == null ? null : PageLinks.fromJson(json['_links']),
TypeToken<Page<T>>())
..rateLimitLimit = convertInt(json['rateLimitLimit'])
..rateLimitRemaining = convertInt(json['rateLimitRemaining'])
..rateLimitReset = convertInt(json['rateLimitReset'])
..records = json["_embedded"]['records'] != null
? List<T>.from(
json["_embedded"]['records'].map((e) => ResponseConverter.fromJson<T>(e) as T))
: null
..links = json['_links'] == null ? null : PageLinks.fromJson(json['_links'])
..setType(TypeToken<Page<T>>());
..rateLimitReset = convertInt(json['rateLimitReset']);
}

class ResponseConverter {
Expand Down
57 changes: 54 additions & 3 deletions test/account_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,20 @@ void main() {
// Find account for signer.
Page<AccountResponse> accounts = await sdk.accounts.forSigner(keyPairB.accountId).execute();
aFound = false;
for (AccountResponse? account in accounts.records!) {
for (AccountResponse? account in accounts.records) {
if (account!.accountId == keyPairA.accountId) {
aFound = true;
break;
}
}
assert(aFound);

// test operation & effects responses can be parsed
var operationsPage = await sdk.operations.forAccount(accountAId).execute();
assert(operationsPage.records.isNotEmpty);
var effectsPage = await sdk.effects.forAccount(accountAId).execute();
assert(effectsPage.records.isNotEmpty);

});

test('test find accounts for asset', () async {
Expand Down Expand Up @@ -124,12 +131,22 @@ void main() {
AccountsRequestBuilder ab = sdk.accounts.forAsset(iomAsset);
Page<AccountResponse> accounts = await ab.execute();
bool cFound = false;
for (AccountResponse? account in accounts.records!) {
if (account!.accountId == keyPairC.accountId) {
for (AccountResponse account in accounts.records) {
if (account.accountId == keyPairC.accountId) {
cFound = true;
}
}
assert(cFound);

// test operation & effects responses can be parsed
var operationsPage = await sdk.operations.forAccount(accountAId).execute();
assert(operationsPage.records.isNotEmpty);
operationsPage = await sdk.operations.forAccount(accountCId).execute();
assert(operationsPage.records.isNotEmpty);
var effectsPage = await sdk.effects.forAccount(accountAId).execute();
assert(effectsPage.records.isNotEmpty);
effectsPage = await sdk.effects.forAccount(accountCId).execute();
assert(effectsPage.records.isNotEmpty);
});

test('test account merge', () async {
Expand Down Expand Up @@ -160,6 +177,17 @@ void main() {
print(error.toString());
assert(error is ErrorResponse && error.code == 404);
});

// test operation & effects responses can be parsed
var operationsPage = await sdk.operations.forAccount(accountXId).execute();
assert(operationsPage.records.isNotEmpty);
operationsPage = await sdk.operations.forAccount(accountYId).execute();
assert(operationsPage.records.isNotEmpty);
var effectsPage = await sdk.effects.forAccount(accountXId).execute();
assert(effectsPage.records.isNotEmpty);
effectsPage = await sdk.effects.forAccount(accountYId).execute();
assert(effectsPage.records.isNotEmpty);

});

test('test account merge muxed source and destination account', () async {
Expand Down Expand Up @@ -198,6 +226,16 @@ void main() {
print(error.toString());
assert(error is ErrorResponse && error.code == 404);
});

// test operation & effects responses can be parsed
var operationsPage = await sdk.operations.forAccount(accountXId).execute();
assert(operationsPage.records.isNotEmpty);
operationsPage = await sdk.operations.forAccount(accountYId).execute();
assert(operationsPage.records.isNotEmpty);
var effectsPage = await sdk.effects.forAccount(accountXId).execute();
assert(effectsPage.records.isNotEmpty);
effectsPage = await sdk.effects.forAccount(accountYId).execute();
assert(effectsPage.records.isNotEmpty);
});

test('test bump sequence', () async {
Expand All @@ -224,6 +262,12 @@ void main() {
account = await sdk.accounts.account(accountId);

assert(startSequence + 10 == account.sequenceNumber);

// test operation & effects responses can be parsed
var operationsPage = await sdk.operations.forAccount(accountId).execute();
assert(operationsPage.records.isNotEmpty);
var effectsPage = await sdk.effects.forAccount(accountId).execute();
assert(effectsPage.records.isNotEmpty);
});

test('test manage data', () async {
Expand Down Expand Up @@ -268,6 +312,13 @@ void main() {

account = await sdk.accounts.account(accountId);
assert(!account.data.keys.contains(key));

// test operation & effects responses can be parsed
var operationsPage = await sdk.operations.forAccount(accountId).execute();
assert(operationsPage.records.isNotEmpty);
var effectsPage = await sdk.effects.forAccount(accountId).execute();
assert(effectsPage.records.isNotEmpty);

});

test('test muxed account ID (M..)', () {
Expand Down
Loading

0 comments on commit cd2ea14

Please sign in to comment.