Skip to content

Commit

Permalink
Multiple improvements to tests suites
Browse files Browse the repository at this point in the history
  • Loading branch information
rylorin committed Mar 10, 2024
1 parent d846803 commit 39128d3
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 165 deletions.
1 change: 1 addition & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ jobs:
uses: actions/setup-node@v4
with:
node-version: 18.19.1
cache: "yarn" # caches the yarn cache folder not node_modules

- name: Prepare
run: yarn install --frozen-lockfile
Expand Down
4 changes: 2 additions & 2 deletions src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
/* eslint @typescript-eslint/no-unsafe-declaration-merging:warn */
import { EventEmitter } from "eventemitter3";
import { DurationUnit, MarketDataType, WhatToShow } from "..";
import { DurationUnit, MarketDataType, OrderStatus, WhatToShow } from "..";

import { ErrorCode } from "../common/errorCode";
import { Controller } from "../core/io/controller";
Expand Down Expand Up @@ -2582,7 +2582,7 @@ export declare interface IBApi {
event: EventName.orderStatus,
listener: (
orderId: number,
status: string,
status: OrderStatus,
filled: number,
remaining: number,
avgFillPrice: number,
Expand Down
11 changes: 8 additions & 3 deletions src/api/contract/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ import { Contract } from "./contract";
* Crypto contract.
*/
export class Crypto implements Contract {
constructor(public symbol: string) {}
constructor(
public symbol: string,
public exchange?: string,
public currency?: string,
) {
this.currency = this.currency ?? "USD";
this.exchange = this.exchange ?? "PAXOS";
}

public currency = "USD";
public exchange = "PAXOS";
public secType = SecType.CRYPTO;
}

Expand Down
2 changes: 1 addition & 1 deletion src/tests/unit/api-next-live/subscription-registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe("Subscription registry Tests", () => {

it("Twice the same event callback bug", (done) => {
// Two active subscriptions for the same Event #193
done();
done("Please fix issue #193");
return;
subscription$ = api.getOpenOrders().subscribe({
next: (data) => {
Expand Down
74 changes: 26 additions & 48 deletions src/tests/unit/api/contract-details.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
/**
* This file implements tests for the [[reqContractDetails]] API entry point.
*/
import {
ContractDetails,
EventName,
Forex,
IBApi,
Option,
OptionType,
SecType,
Stock,
} from "../../..";
import { ContractDetails, EventName, Forex, IBApi } from "../../..";
import configuration from "../../../common/configuration";
import { sample_option, sample_stock } from "../sample-data/contracts";

describe("IBApi reqContractDetails Tests", () => {
jest.setTimeout(5000);
Expand All @@ -36,22 +28,21 @@ describe("IBApi reqContractDetails Tests", () => {

test("Forex", (done) => {
const refId = 1;
const refContract = new Forex("USD", "EUR");
ib.once(EventName.nextValidId, (_reqId) => {
const contract = new Forex("USD", "EUR");
ib.reqContractDetails(refId, contract);
ib.reqContractDetails(refId, refContract);
})
.on(EventName.contractDetails, (reqId, details: ContractDetails) => {
expect(reqId).toEqual(refId);
expect(details.contract.secType).toEqual(SecType.CASH);
expect(details.contract.symbol).toEqual("EUR");
expect(details.contract.currency).toEqual("USD");
expect(details.marketName).toEqual("EUR.USD");
expect(details.contract.secType).toEqual(refContract.secType);
expect(details.contract.symbol).toEqual(refContract.symbol);
expect(details.contract.currency).toEqual(refContract.currency);
expect(details.marketName).toEqual(
`${refContract.symbol}.${refContract.currency}`,
);
})
.on(EventName.contractDetailsEnd, (reqId) => {
expect(reqId).toEqual(refId);
if (ib) ib.disconnect();
})
.on(EventName.disconnected, () => {
done();
})
.on(EventName.error, (err, code, reqId) => {
Expand All @@ -63,22 +54,18 @@ describe("IBApi reqContractDetails Tests", () => {

test("Stock", (done) => {
const refId = 2;
const refContract = sample_stock;
ib.once(EventName.nextValidId, (_reqId) => {
const contract = new Stock("SPY", "ARCA", "USD");
ib.reqContractDetails(refId, contract);
ib.reqContractDetails(refId, refContract);
})
.on(EventName.contractDetails, (reqId, details: ContractDetails) => {
expect(reqId).toEqual(refId);
expect(details.contract.secType).toEqual(SecType.STK);
expect(details.contract.symbol).toEqual("SPY");
expect(details.contract.currency).toEqual("USD");
expect(details.marketName).toEqual("SPY");
expect(details.contract.secType).toEqual(refContract.secType);
expect(details.contract.symbol).toEqual(refContract.symbol);
expect(details.contract.currency).toEqual(refContract.currency);
})
.on(EventName.contractDetailsEnd, (reqId) => {
expect(reqId).toEqual(refId);
if (ib) ib.disconnect();
})
.on(EventName.disconnected, () => {
done();
})
.on(EventName.error, (err, code, reqId) => {
Expand All @@ -90,23 +77,19 @@ describe("IBApi reqContractDetails Tests", () => {

test("Option", (done) => {
const refId = 3;
const refContract = sample_option;
ib.once(EventName.nextValidId, (_reqId) => {
const contract = new Option("SPY", "20260116", 440, OptionType.Call);
ib.reqContractDetails(refId, contract);
ib.reqContractDetails(refId, refContract);
})
.on(EventName.contractDetails, (reqId, details: ContractDetails) => {
expect(reqId).toEqual(refId);
expect(details.contract.secType).toEqual(SecType.OPT);
expect(details.contract.symbol).toEqual("SPY");
expect(details.contract.currency).toEqual("USD");
expect(details.contract.secType).toEqual(refContract.secType);
expect(details.contract.symbol).toEqual(refContract.symbol);
expect(details.contract.currency).toEqual(refContract.currency);
expect(details.contract.conId).toEqual(653318228);
expect(details.marketName).toEqual("SPY");
})
.on(EventName.contractDetailsEnd, (reqId) => {
expect(reqId).toEqual(refId);
if (ib) ib.disconnect();
})
.on(EventName.disconnected, () => {
done();
})
.on(EventName.error, (err, code, reqId) => {
Expand All @@ -120,26 +103,21 @@ describe("IBApi reqContractDetails Tests", () => {
const refId = 4;
let count = 0;

const refContract = sample_option;
refContract.strike = 0;
ib.once(EventName.nextValidId, (_reqId) => {
const contract = new Option("SPY", "20260116", 0, OptionType.Call);
ib.reqContractDetails(refId, contract);
ib.reqContractDetails(refId, refContract);
})
.on(EventName.contractDetails, (reqId, details: ContractDetails) => {
expect(reqId).toEqual(refId);
expect(details.contract.secType).toEqual(SecType.OPT);
expect(details.contract.symbol).toEqual("SPY");
expect(details.contract.currency).toEqual("USD");
expect(details.marketName).toEqual("SPY");
// console.log(details.contract);
expect(details.contract.secType).toEqual(refContract.secType);
expect(details.contract.symbol).toEqual(refContract.symbol);
expect(details.contract.currency).toEqual(refContract.currency);
count++;
})
.on(EventName.contractDetailsEnd, (reqId) => {
expect(reqId).toEqual(refId);
expect(count).toBeGreaterThanOrEqual(92);
// console.log(count);
if (ib) ib.disconnect();
})
.on(EventName.disconnected, () => {
done();
})
.on(EventName.error, (err, code, reqId) => {
Expand Down
14 changes: 7 additions & 7 deletions src/tests/unit/api/historical-data.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ describe("IBApi Historical data Tests", () => {
expect(open).toEqual(429.5);
expect(high).toEqual(429.6);
expect(low).toEqual(429.47);
expect(close).toEqual(429.52);
expect(volume).toEqual(345338);
expect(count).toEqual(1076);
expect(close).toEqual(429.51);
expect(volume).toEqual(3487.38);
expect(count).toEqual(1090);
expect(WAP).toEqual(429.532);
}
},
Expand Down Expand Up @@ -223,7 +223,7 @@ describe("IBApi Historical data Tests", () => {
expect(high).toEqual(453.67);
expect(low).toEqual(437.3);
expect(close).toEqual(450.92);
expect(volume).toEqual(277178324);
expect(volume).toEqual(2771783.24);
expect(count).toEqual(1393264);
expect(WAP).toEqual(448.476);
}
Expand Down Expand Up @@ -288,7 +288,7 @@ describe("IBApi Historical data Tests", () => {
expect(high).toEqual(453.67);
expect(low).toEqual(449.68);
expect(close).toEqual(450.92);
expect(volume).toEqual(47405890);
expect(volume).toEqual(474058.9);
expect(count).toEqual(248346);
expect(WAP).toEqual(451.3);
}
Expand Down Expand Up @@ -324,9 +324,9 @@ describe("IBApi Historical data Tests", () => {
ib.connect().reqHistoricalTicks(
refId,
contract,
"20210101-16:00:00",
"20240102-16:00:00",
null,
1000,
10,
WhatToShow.TRADES,
0,
true,
Expand Down
52 changes: 28 additions & 24 deletions src/tests/unit/api/order/cancelOrder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import {
IBApi,
Order,
OrderAction,
OrderStatus,
OrderType,
Stock,
TimeInForce,
} from "../../../..";
import configuration from "../../../../common/configuration";
import logger from "../../../../common/logger";
import { sample_etf } from "../../sample-data/contracts";

describe("CancelOrder", () => {
jest.setTimeout(20 * 1000);
Expand Down Expand Up @@ -41,48 +42,50 @@ describe("CancelOrder", () => {
test("cancelOrder", (done) => {
let refId: number;

const contract: Contract = new Stock("SPY");
const contract: Contract = sample_etf;
const order: Order = {
orderType: OrderType.LMT,
action: OrderAction.BUY,
lmtPrice: 1,
totalQuantity: 1,
// account: "DU123567",
tif: TimeInForce.GTC,
lmtPrice: 3,
totalQuantity: 3,
tif: TimeInForce.DAY,
outsideRth: false,
transmit: true,
};

let order_found = false;
let cancelling = false;
ib.once(EventName.nextValidId, (orderId: number) => {
refId = orderId;
ib.placeOrder(refId, contract, order);
})
.on(EventName.openOrder, (orderId, _contract, _order, _orderState) => {
expect(orderId).toEqual(refId);
if (!order_found && orderId === refId) {
order_found = true;
ib.cancelOrder(refId);
}
})
.on(
EventName.orderStatus,
(
orderId,
_status,
status,
_filled,
_remaining,
_avgFillPrice,
_permId?,
_parentId?,
_lastFillPrice?,
_clientId?,
_whyHeld?,
_mktCapPrice?,
_permId,
_parentId,
_lastFillPrice,
_clientId,
_whyHeld,
_mktCapPrice,
) => {
expect(orderId).toEqual(refId);
if (orderId === refId) {
if (
!cancelling &&
[OrderStatus.PreSubmitted, OrderStatus.Submitted].includes(
status as OrderStatus,
)
) {
cancelling = true;
ib.cancelOrder(refId);
}
}
},
)
.on(EventName.openOrderEnd, () => {})
.on(
EventName.error,
(
Expand All @@ -100,8 +103,9 @@ describe("CancelOrder", () => {
} else if (
code == ErrorCode.ORDER_CANCELLED &&
reqId == refId &&
order_found
cancelling
) {
logger.info(msg);
done();
} else {
done(msg);
Expand Down
Loading

0 comments on commit 39128d3

Please sign in to comment.