def test_cancel_order_fail():
    """
    测试取消报单但失败
    """
    openLimitPrice = 1000
    trader = Trader()

    # 尝试创建头寸
    toOrder = trader.openPosition(
        instrumentId=getDefaultInstrumentId(),
        direction='buy',
        volume=1,
        openLimitPrice=openLimitPrice
    )

    # 发起撤单请求
    order = trader.cancelOrder(toOrder.id)
    assert order.order == toOrder
    assert order.action == 'cancel'
    assert order.state == 'insert'
    assert order.finishTime is None

    # 发起撤单失败
    errorId = -1
    errorMsg = u'测试'
    trader.onCancelOrderError(order, errorId, errorMsg, toOrder)
    toOrder = ModelOrder.objects.get(id=toOrder.id)
    order = ModelOrder.objects.get(id=order.id)
    position = toOrder.position
    assert order.state == 'error'
    assert order.finishTime is not None
    assert order.errorId == errorId
    assert order.errorMsg == errorMsg
    assert toOrder.state in ('insert', 'finish')
    assert position.state in ('preopen', 'open')
def test_cancel_order_success():
    """
    测试取消报单并成功
    """
    openLimitPrice = 1000
    trader = Trader()

    # 尝试创建头寸
    toOrder = trader.openPosition(
        instrumentId=getDefaultInstrumentId(),
        direction='buy',
        volume=1,
        openLimitPrice=openLimitPrice
    )

    # 发起撤单请求
    order = trader.cancelOrder(toOrder.id)
    assert order.order == toOrder
    assert order.action == 'cancel'
    assert order.state == 'insert'
    assert order.finishTime is None

    # 发起撤单成功
    trader.onOrderCanceled(order, toOrder)
    toOrder = ModelOrder.objects.get(id=toOrder.id)
    order = ModelOrder.objects.get(id=order.id)
    position = toOrder.position
    assert order.state == 'finish'
    assert order.finishTime is not None
    assert toOrder.state == 'cancel'
    assert toOrder.finishTime is not None
    assert position.state == 'cancel'