From 4103da402b81b2cac31004a7379e7bd9cc3dc081 Mon Sep 17 00:00:00 2001 From: Ronan-Yann Lorin Date: Sun, 17 Nov 2024 23:00:57 +0100 Subject: [PATCH] getContractDetails test refactored --- .../get-contract-details.test.ts | 75 +++++++++++-------- src/tests/unit/api/market-data.test.ts | 2 +- src/tests/unit/api/order/issue203.test.ts | 2 +- src/tests/unit/api/order/placeOrder.test.ts | 61 +++++---------- src/tests/unit/sample-data/contracts.ts | 2 +- src/tests/unit/sample-data/orders.ts | 3 +- 6 files changed, 67 insertions(+), 78 deletions(-) diff --git a/src/tests/unit/api-next-live/get-contract-details.test.ts b/src/tests/unit/api-next-live/get-contract-details.test.ts index ddc91411..ec6f7c0d 100644 --- a/src/tests/unit/api-next-live/get-contract-details.test.ts +++ b/src/tests/unit/api-next-live/get-contract-details.test.ts @@ -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 @@ -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) => { @@ -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) => { @@ -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) => { @@ -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) => { @@ -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) => { diff --git a/src/tests/unit/api/market-data.test.ts b/src/tests/unit/api/market-data.test.ts index 87bec715..40f8c0a3 100644 --- a/src/tests/unit/api/market-data.test.ts +++ b/src/tests/unit/api/market-data.test.ts @@ -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 diff --git a/src/tests/unit/api/order/issue203.test.ts b/src/tests/unit/api/order/issue203.test.ts index 1b29acf8..b74a0f3e 100644 --- a/src/tests/unit/api/order/issue203.test.ts +++ b/src/tests/unit/api/order/issue203.test.ts @@ -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, diff --git a/src/tests/unit/api/order/placeOrder.test.ts b/src/tests/unit/api/order/placeOrder.test.ts index 9df69d71..48bb9624 100644 --- a/src/tests/unit/api/order/placeOrder.test.ts +++ b/src/tests/unit/api/order/placeOrder.test.ts @@ -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 { @@ -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); @@ -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) => { @@ -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, }; @@ -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) => { @@ -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) => { diff --git a/src/tests/unit/sample-data/contracts.ts b/src/tests/unit/sample-data/contracts.ts index 0c6a8f11..adfdb930 100644 --- a/src/tests/unit/sample-data/contracts.ts +++ b/src/tests/unit/sample-data/contracts.ts @@ -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"); diff --git a/src/tests/unit/sample-data/orders.ts b/src/tests/unit/sample-data/orders.ts index de0cd4ff..94338864 100644 --- a/src/tests/unit/sample-data/orders.ts +++ b/src/tests/unit/sample-data/orders.ts @@ -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", };