Skip to content

Commit 944cc9f

Browse files
committed
Added account http unit tests. Code simplification
Improved exception handling when the Observable error is in the code, not the http response.
1 parent 3114a2a commit 944cc9f

File tree

4 files changed

+281
-76
lines changed

4 files changed

+281
-76
lines changed

src/infrastructure/AccountHttp.ts

Lines changed: 42 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { from as observableFrom, Observable, throwError } from 'rxjs';
18-
import { catchError, map } from 'rxjs/operators';
19-
import { AccountInfoDTO, AccountRoutesApi } from 'symbol-openapi-typescript-node-client';
17+
import { Observable } from 'rxjs';
18+
import { AccountIds, AccountInfoDTO, AccountRoutesApi } from 'symbol-openapi-typescript-node-client';
2019
import { AccountInfo } from '../model/account/AccountInfo';
2120
import { ActivityBucket } from '../model/account/ActivityBucket';
2221
import { Address } from '../model/account/Address';
@@ -58,25 +57,18 @@ export class AccountHttp extends Http implements AccountRepository {
5857
* @returns Observable<AccountInfo>
5958
*/
6059
public getAccountInfo(address: Address): Observable<AccountInfo> {
61-
return observableFrom(this.accountRoutesApi.getAccountInfo(address.plain())).pipe(
62-
map(({body}) => this.toAccountInfo(body)),
63-
catchError((error) => throwError(this.errorHandling(error))),
64-
);
60+
return this.call(this.accountRoutesApi.getAccountInfo(address.plain()), (body) => this.toAccountInfo(body));
6561
}
62+
6663
/**
6764
* Gets AccountsInfo for different accounts.
6865
* @param addresses List of Address
6966
* @returns Observable<AccountInfo[]>
7067
*/
7168
public getAccountsInfo(addresses: Address[]): Observable<AccountInfo[]> {
72-
const accountIdsBody = {
73-
addresses: addresses.map((address) => address.plain()),
74-
};
75-
return observableFrom(
76-
this.accountRoutesApi.getAccountsInfo(accountIdsBody)).pipe(
77-
map(({body}) => body.map(this.toAccountInfo)),
78-
catchError((error) => throwError(this.errorHandling(error))),
79-
);
69+
const accountIds = new AccountIds();
70+
accountIds.addresses = addresses.map((address) => address.plain());
71+
return this.call(this.accountRoutesApi.getAccountsInfo(accountIds), (body) => body.map(this.toAccountInfo));
8072
}
8173

8274
/**
@@ -122,17 +114,13 @@ export class AccountHttp extends Http implements AccountRepository {
122114
public getAccountTransactions(address: Address,
123115
queryParams?: QueryParams,
124116
transactionFilter?: TransactionFilter): Observable<Transaction[]> {
125-
return observableFrom(
126-
this.accountRoutesApi.getAccountConfirmedTransactions(address.plain(),
127-
this.queryParams(queryParams).pageSize,
128-
this.queryParams(queryParams).id,
129-
this.queryParams(queryParams).ordering,
130-
this.transactionFilter(transactionFilter).type)).pipe(
131-
map(({body}) => body.map((transactionDTO) => {
132-
return CreateTransactionFromDTO(transactionDTO);
133-
})),
134-
catchError((error) => throwError(this.errorHandling(error))),
135-
);
117+
118+
return this.call(this.accountRoutesApi.getAccountConfirmedTransactions(address.plain(),
119+
this.queryParams(queryParams).pageSize,
120+
this.queryParams(queryParams).id,
121+
this.queryParams(queryParams).ordering,
122+
this.transactionFilter(transactionFilter).type),
123+
(body) => body.map((transactionDTO) => CreateTransactionFromDTO(transactionDTO)));
136124
}
137125

138126
/**
@@ -145,18 +133,13 @@ export class AccountHttp extends Http implements AccountRepository {
145133
*/
146134
public getAccountIncomingTransactions(address: Address,
147135
queryParams?: QueryParams,
148-
transactionFilter?: TransactionFilter): Observable <Transaction[]> {
149-
return observableFrom(
150-
this.accountRoutesApi.getAccountIncomingTransactions(address.plain(),
151-
this.queryParams(queryParams).pageSize,
152-
this.queryParams(queryParams).id,
153-
this.queryParams(queryParams).ordering),
154-
this.transactionFilter(transactionFilter).type).pipe(
155-
map(({body}) => body.map((transactionDTO) => {
156-
return CreateTransactionFromDTO(transactionDTO);
157-
})),
158-
catchError((error) => throwError(this.errorHandling(error))),
159-
);
136+
transactionFilter?: TransactionFilter): Observable<Transaction[]> {
137+
return this.call(this.accountRoutesApi.getAccountIncomingTransactions(address.plain(),
138+
this.queryParams(queryParams).pageSize,
139+
this.queryParams(queryParams).id,
140+
this.queryParams(queryParams).ordering,
141+
this.transactionFilter(transactionFilter).type),
142+
(body) => body.map((transactionDTO) => CreateTransactionFromDTO(transactionDTO)));
160143
}
161144

162145
/**
@@ -169,18 +152,13 @@ export class AccountHttp extends Http implements AccountRepository {
169152
*/
170153
public getAccountOutgoingTransactions(address: Address,
171154
queryParams?: QueryParams,
172-
transactionFilter?: TransactionFilter): Observable <Transaction[]> {
173-
return observableFrom(
174-
this.accountRoutesApi.getAccountOutgoingTransactions(address.plain(),
175-
this.queryParams(queryParams).pageSize,
176-
this.queryParams(queryParams).id,
177-
this.queryParams(queryParams).ordering),
178-
this.transactionFilter(transactionFilter).type).pipe(
179-
map(({body}) => body.map((transactionDTO) => {
180-
return CreateTransactionFromDTO(transactionDTO);
181-
})),
182-
catchError((error) => throwError(this.errorHandling(error))),
183-
);
155+
transactionFilter?: TransactionFilter): Observable<Transaction[]> {
156+
return this.call(this.accountRoutesApi.getAccountOutgoingTransactions(address.plain(),
157+
this.queryParams(queryParams).pageSize,
158+
this.queryParams(queryParams).id,
159+
this.queryParams(queryParams).ordering,
160+
this.transactionFilter(transactionFilter).type),
161+
(body) => body.map((transactionDTO) => CreateTransactionFromDTO(transactionDTO)));
184162
}
185163

186164
/**
@@ -194,18 +172,13 @@ export class AccountHttp extends Http implements AccountRepository {
194172
*/
195173
public getAccountUnconfirmedTransactions(address: Address,
196174
queryParams?: QueryParams,
197-
transactionFilter?: TransactionFilter): Observable <Transaction[]> {
198-
return observableFrom(
199-
this.accountRoutesApi.getAccountUnconfirmedTransactions(address.plain(),
200-
this.queryParams(queryParams).pageSize,
201-
this.queryParams(queryParams).id,
202-
this.queryParams(queryParams).ordering),
203-
this.transactionFilter(transactionFilter).type).pipe(
204-
map(({body}) => body.map((transactionDTO) => {
205-
return CreateTransactionFromDTO(transactionDTO);
206-
})),
207-
catchError((error) => throwError(this.errorHandling(error))),
208-
);
175+
transactionFilter?: TransactionFilter): Observable<Transaction[]> {
176+
return this.call(this.accountRoutesApi.getAccountUnconfirmedTransactions(address.plain(),
177+
this.queryParams(queryParams).pageSize,
178+
this.queryParams(queryParams).id,
179+
this.queryParams(queryParams).ordering,
180+
this.transactionFilter(transactionFilter).type),
181+
(body) => body.map((transactionDTO) => CreateTransactionFromDTO(transactionDTO)));
209182
}
210183

211184
/**
@@ -218,17 +191,12 @@ export class AccountHttp extends Http implements AccountRepository {
218191
*/
219192
public getAccountPartialTransactions(address: Address,
220193
queryParams?: QueryParams,
221-
transactionFilter?: TransactionFilter): Observable <AggregateTransaction[]> {
222-
return observableFrom(
223-
this.accountRoutesApi.getAccountPartialTransactions(address.plain(),
224-
this.queryParams(queryParams).pageSize,
225-
this.queryParams(queryParams).id,
226-
this.queryParams(queryParams).ordering),
227-
this.transactionFilter(transactionFilter).type).pipe(
228-
map(({body}) => body.map((transactionDTO) => {
229-
return CreateTransactionFromDTO(transactionDTO) as AggregateTransaction;
230-
})),
231-
catchError((error) => throwError(this.errorHandling(error))),
232-
);
194+
transactionFilter?: TransactionFilter): Observable<AggregateTransaction[]> {
195+
return this.call(this.accountRoutesApi.getAccountPartialTransactions(address.plain(),
196+
this.queryParams(queryParams).pageSize,
197+
this.queryParams(queryParams).id,
198+
this.queryParams(queryParams).ordering,
199+
this.transactionFilter(transactionFilter).type),
200+
(body) => body.map((transactionDTO) => CreateTransactionFromDTO(transactionDTO) as AggregateTransaction));
233201
}
234202
}

src/infrastructure/Http.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export abstract class Http {
7979
if (error.code && error.address && error.code === 'ECONNREFUSED') {
8080
return new Error(`Cannot reach node: ${error.address}:${error.port}`);
8181
}
82-
return new Error(error);
82+
return error;
8383
}
8484

8585
/**

0 commit comments

Comments
 (0)