Skip to content

Commit

Permalink
getContractDetails test refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
rylorin committed Nov 17, 2024
1 parent ba623ad commit 4103da4
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 78 deletions.
75 changes: 45 additions & 30 deletions src/tests/unit/api-next-live/get-contract-details.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import { IBApiNext, IBApiNextError } from "../../..";
import {
sample_bond,
sample_crypto,
sample_future,
sample_option,
sample_stock,
} from "../sample-data/contracts";

describe("ApiNext: getContractDetails()", () => {
jest.setTimeout(10 * 1000);
jest.setTimeout(5_000);

const clientId = Math.floor(Math.random() * 32766) + 1; // ensure unique client

Expand Down Expand Up @@ -51,15 +52,14 @@ describe("ApiNext: getContractDetails()", () => {
});

test("Stock contract details", (done) => {
const ref_contract = sample_stock;

api
.getContractDetails(sample_stock)
.getContractDetails(ref_contract)
.then((result) => {
// console.log(result);
expect(result.length).toBeGreaterThan(0);
if (result.length) {
expect(result[0].contract.symbol).toEqual(sample_stock.symbol);
expect(result[0].contract.secType).toEqual(sample_stock.secType);
}
expect(result[0].contract.symbol).toEqual(ref_contract.symbol);
expect(result[0].contract.secType).toEqual(ref_contract.secType);
done();
})
.catch((err: IBApiNextError) => {
Expand All @@ -70,15 +70,32 @@ describe("ApiNext: getContractDetails()", () => {
});

test("Future contract details", (done) => {
const ref_contract = sample_future;

api
.getContractDetails(sample_crypto)
.getContractDetails(ref_contract)
.then((result) => {
// console.log(result);
expect(result.length).toBeGreaterThan(0);
if (result.length) {
expect(result[0].contract.symbol).toEqual(sample_crypto.symbol);
expect(result[0].contract.secType).toEqual(sample_crypto.secType);
}
expect(result[0].contract.symbol).toEqual(ref_contract.symbol);
expect(result[0].contract.secType).toEqual(ref_contract.secType);
done();
})
.catch((err: IBApiNextError) => {
done(
`getContractDetails failed with '${err.error.message}' (Error #${err.code})`,
);
});
});

test("Crypto contract details", (done) => {
const ref_contract = sample_crypto;

api
.getContractDetails(ref_contract)
.then((result) => {
expect(result.length).toBeGreaterThan(0);
expect(result[0].contract.symbol).toEqual(ref_contract.symbol);
expect(result[0].contract.secType).toEqual(ref_contract.secType);
done();
})
.catch((err: IBApiNextError) => {
Expand All @@ -89,15 +106,14 @@ describe("ApiNext: getContractDetails()", () => {
});

test("Option contract details", (done) => {
const ref_contract = sample_option;

api
.getContractDetails(sample_option)
.getContractDetails(ref_contract)
.then((result) => {
// console.log(result);
expect(result.length).toBeGreaterThan(0);
if (result.length) {
expect(result[0].contract.symbol).toEqual(sample_option.symbol);
expect(result[0].contract.secType).toEqual(sample_option.secType);
}
expect(result[0].contract.symbol).toEqual(ref_contract.symbol);
expect(result[0].contract.secType).toEqual(ref_contract.secType);
done();
})
.catch((err: IBApiNextError) => {
Expand All @@ -108,14 +124,14 @@ describe("ApiNext: getContractDetails()", () => {
});

test("Bond contract details", (done) => {
const ref_contract = sample_bond;

api
.getContractDetails(sample_bond)
.getContractDetails(ref_contract)
.then((result) => {
// console.log(result);
expect(result.length).toBeGreaterThan(0);
if (result.length) {
expect(result[0].contract.secType).toEqual(sample_bond.secType);
}
// expect(result[0].contract.symbol).toEqual(ref_contract.symbol);
expect(result[0].contract.secType).toEqual(ref_contract.secType);
done();
})
.catch((err: IBApiNextError) => {
Expand All @@ -126,15 +142,14 @@ describe("ApiNext: getContractDetails()", () => {
});

test("Crypto contract details", (done) => {
const ref_contract = sample_crypto;

api
.getContractDetails(sample_crypto)
.getContractDetails(ref_contract)
.then((result) => {
// console.log(result);
expect(result.length).toBeGreaterThan(0);
if (result.length) {
expect(result[0].contract.symbol).toEqual(sample_crypto.symbol);
expect(result[0].contract.secType).toEqual(sample_crypto.secType);
}
expect(result[0].contract.symbol).toEqual(ref_contract.symbol);
expect(result[0].contract.secType).toEqual(ref_contract.secType);
done();
})
.catch((err: IBApiNextError) => {
Expand Down
2 changes: 1 addition & 1 deletion src/tests/unit/api/market-data.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
} from "../sample-data/contracts";

describe("IBApi Market data Tests", () => {
jest.setTimeout(15 * 1000);
jest.setTimeout(10_000);

let ib: IBApi;
const clientId = Math.floor(Math.random() * 32766) + 1; // ensure unique client
Expand Down
2 changes: 1 addition & 1 deletion src/tests/unit/api/order/issue203.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ describe("Issue #203", () => {
const refOrder: Order = {
orderType: OrderType.LMT,
action: OrderAction.BUY,
lmtPrice: 5400,
lmtPrice: 2400,
orderId,
totalQuantity: 1,
transmit: true,
Expand Down
61 changes: 17 additions & 44 deletions src/tests/unit/api/order/placeOrder.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
/**
* This file implement test code for the placeOrder function .
*/
import {
Contract,
ErrorCode,
EventName,
IBApi,
Order,
OrderAction,
OrderType,
TimeInForce,
} from "../../../..";
import { Contract, ErrorCode, EventName, IBApi, Order } from "../../../..";
import configuration from "../../../../common/configuration";
import logger from "../../../../common/logger";
import {
Expand All @@ -19,6 +10,7 @@ import {
sample_option,
sample_stock,
} from "../../sample-data/contracts";
import { sample_order } from "../../sample-data/orders";

describe("Place Orders", () => {
jest.setTimeout(20 * 1000);
Expand All @@ -45,15 +37,7 @@ describe("Place Orders", () => {
let refId: number;

const refContract: Contract = sample_stock;
const refOrder: Order = {
orderType: OrderType.LMT,
action: OrderAction.BUY,
lmtPrice: 1,
orderId: refId,
totalQuantity: 2,
tif: TimeInForce.DAY,
transmit: true,
};
const refOrder: Order = sample_order;

let isSuccess = false;
ib.once(EventName.nextValidId, (orderId: number) => {
Expand Down Expand Up @@ -98,14 +82,19 @@ describe("Place Orders", () => {
let refId: number;

const refContract: Contract = sample_etf;
// const refOrder: Order = {
// orderType: OrderType.LMT,
// action: OrderAction.BUY,
// lmtPrice: 1,
// orderId: refId,
// totalQuantity: 2,
// tif: TimeInForce.DAY,
// transmit: true,
// whatIf: true,
// };
const refOrder: Order = {
orderType: OrderType.LMT,
action: OrderAction.BUY,
lmtPrice: 1,
orderId: refId,
totalQuantity: 2,
tif: TimeInForce.DAY,
transmit: true,
...sample_order,
goodAfterTime: undefined,
whatIf: true,
};

Expand Down Expand Up @@ -145,15 +134,7 @@ describe("Place Orders", () => {
let refId: number;

const refContract: Contract = sample_crypto;
const refOrder: Order = {
orderType: OrderType.LMT,
action: OrderAction.BUY,
lmtPrice: 1,
orderId: refId,
totalQuantity: 2,
tif: TimeInForce.DAY,
transmit: true,
};
const refOrder: Order = sample_order;

let isSuccess = false;
ib.once(EventName.nextValidId, (orderId: number) => {
Expand Down Expand Up @@ -198,15 +179,7 @@ describe("Place Orders", () => {
let refId: number;

const refContract: Contract = sample_option;
const refOrder: Order = {
orderType: OrderType.LMT,
action: OrderAction.BUY,
lmtPrice: 1,
orderId: refId,
totalQuantity: 2,
tif: TimeInForce.DAY,
transmit: true,
};
const refOrder: Order = sample_order;

let isSuccess = false;
ib.once(EventName.nextValidId, (orderId: number) => {
Expand Down
2 changes: 1 addition & 1 deletion src/tests/unit/sample-data/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import Crypto from "../../../api/contract/crypto";

export const sample_stock: Contract = new Stock("AAPL");
export const sample_etf: Contract = new Stock("SPY");
export const sample_bond: Contract = new Bond("US279158AE95");
export const sample_bond: Contract = new Bond("US064159KJ44");
export const sample_index: Contract = new Index("ES", "USD");
export const sample_dax_index: Contract = new Index("DAX", "EUR", "EUREX");
export const sample_crypto: Contract = new Crypto("ETH");
Expand Down
3 changes: 2 additions & 1 deletion src/tests/unit/sample-data/orders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import { Order, OrderAction, OrderType, TimeInForce } from "../../..";
export const sample_order: Order = {
orderType: OrderType.LMT,
action: OrderAction.BUY,
lmtPrice: 0.01,
lmtPrice: 1,
totalQuantity: 1,
conditionsIgnoreRth: true,
conditionsCancelOrder: false,
tif: TimeInForce.DAY,
transmit: true,
goodAfterTime: "20300101-01:01:01",
};

0 comments on commit 4103da4

Please sign in to comment.