예제 #1
0
def test_make_trades_with_equal_stocks(offer_instances):
    """
    Ensure that function create right trade instance
    Since purchase offer and sell offer have equal quantity of stocks
    Function has to create trade instance and delete both offers
    """

    purchase_offer = offer_instances[0]
    sell_offer = offer_instances[3]
    sell_offer.quantity = purchase_offer.quantity
    sell_offer.entry_quantity = purchase_offer.entry_quantity
    sell_offer.save()

    correct_quantity = get_available_quantity_stocks(offer_id=purchase_offer.id)

    _make_trades(offer_id=purchase_offer.id)

    trade = Trade.objects.get()

    assert Trade.objects.count() == 1
    assert Offer.objects.get(id=purchase_offer.id).is_active == False
    assert Offer.objects.get(id=sell_offer.id).is_active == False

    assert trade.seller_offer == sell_offer
    assert trade.buyer_offer == purchase_offer
    assert trade.quantity == correct_quantity
    assert trade.unit_price == sell_offer.price

    assert get_available_quantity_stocks(offer_id=sell_offer.id) == 0
    assert get_available_quantity_stocks(offer_id=purchase_offer.id) == 0
예제 #2
0
def _stocks_quantity_for_trade_by_given_offers(
    sell_offer_id: int, purchase_offer_id: int
) -> int:
    """Return final stocks quantity for trading"""

    sell_stocks = get_available_quantity_stocks(offer_id=sell_offer_id)
    purchase_stocks = get_available_quantity_stocks(offer_id=purchase_offer_id)

    if purchase_stocks <= sell_stocks:
        return purchase_stocks

    return sell_stocks
예제 #3
0
def test_stocks_quantity_for_trade_by_given_offers_with_equal_quantity(offer_instances):
    """
    Ensure that function return correct stocks quantity for trade.
    Since purchase offer and sell offers have equal quantity of stocks,
    Function has to return sell offer and purchase offer quantity of stocks.
    """

    purchase_offer = offer_instances[0]
    sell_offer = offer_instances[6]

    final_quantity = _stocks_quantity_for_trade_by_given_offers(
        sell_offer_id=sell_offer.id, purchase_offer_id=purchase_offer.id
    )

    assert final_quantity == get_available_quantity_stocks(offer_id=sell_offer.id)
    assert final_quantity == get_available_quantity_stocks(offer_id=purchase_offer.id)
예제 #4
0
def _check_offer_quantity(offer_id: int) -> bool:
    """
    Checking the offer for emptiness.
    If offer has no stocks function returns True.
    Otherwise returns False.
    """

    quantity = get_available_quantity_stocks(offer_id=offer_id)

    return not bool(quantity)
예제 #5
0
def test_get_available_quantity_stocks(offer_purchase_instance):
    """
    Ensure that function return correct quantity of available stocks for now
    In purchase offer instance without current quantity
    """

    available_stocks = get_available_quantity_stocks(
        offer_id=offer_purchase_instance.id)

    assert available_stocks == 60
예제 #6
0
def test_count_current_money_quantity_in_offers(offer_instances):
    """Ensure that function return right quantity of stocks that are used in offers"""

    offer = offer_instances[0]

    quantity = _count_current_money_quantity_in_offers(user_id=offer.user.id, )

    assert quantity == get_full_price(
        sell_offer_id=offer.id,
        quantity=get_available_quantity_stocks(offer_id=offer.id),
    )
예제 #7
0
def test_get_available_quantity_stocks_with_current_quantity(
        offer_sell_instance):
    """
    Ensure that function return correct quantity of available stocks for now
    In sell offer instance with current quantity
    """

    available_stocks = get_available_quantity_stocks(
        offer_id=offer_sell_instance.id)

    assert available_stocks == 36
예제 #8
0
def _count_current_quantity_in_offers(user_id: int, item_id: int) -> int:
    """Return number of quantity that user is selling right now"""

    quantity_in_offers = 0

    for offer in Offer.objects.sell_offers().filter(user_id=user_id,
                                                    item_id=item_id):
        quantity = get_available_quantity_stocks(offer_id=offer.id)

        quantity_in_offers += quantity

    return quantity_in_offers
예제 #9
0
def test_count_current_money_quantity_in_offers_with_smaller_quantity(
    offer_purchase_instance, ):
    """Ensure that function return correct boolean value if user has enough quantity of stocks"""

    result = check_user_balance(
        user_id=offer_purchase_instance.user.id,
        quantity=get_available_quantity_stocks(
            offer_id=offer_purchase_instance.id),
        price=5,
    )

    assert result == True
예제 #10
0
def _count_current_money_quantity_in_offers(user_id: int) -> int:
    """Return quantity of money that user use in other offers"""

    money_in_offers = 0

    for offer in Offer.objects.purchase_offers().filter(user_id=user_id):
        quantity = get_full_price(
            sell_offer_id=offer.id,
            quantity=get_available_quantity_stocks(offer_id=offer.id),
        )

        money_in_offers += quantity

    return money_in_offers
예제 #11
0
def test_count_current_money_quantity_in_offers_with_greater_quantity(
        offer_instances):
    """Ensure that function return correct boolean value if user hasn't enough quantity of stocks"""

    offer_purchase_instance = offer_instances[0]

    result = check_user_balance(
        user_id=offer_purchase_instance.user.id,
        quantity=get_available_quantity_stocks(
            offer_id=offer_purchase_instance.id),
        price=100000,
    )

    assert result == False
예제 #12
0
def test_confirm_trade_with_greatest_purchase_stocks_quantity(offer_instances):
    """
    Ensure that function create correct trade instance and delete right offer instances
    Since purchase offer has more quantity of stocks than sell offer, function has to delete sell offer
    """

    purchase_offer = offer_instances[0]
    sell_offer = offer_instances[3]

    buyer = purchase_offer.user
    seller = sell_offer.user

    correct_quantity = get_available_quantity_stocks(offer_id=sell_offer.id)

    original_buyer_balance = buyer.balance.get().quantity
    original_seller_balance = seller.balance.get().quantity

    _confirm_trade(sell_offer_id=sell_offer.id, purchase_offer_id=purchase_offer.id)

    buyer_balance = buyer.balance.get().quantity
    buyer_inventory = buyer.inventory.get(item_id=purchase_offer.item).quantity

    seller_balance = seller.balance.get().quantity
    seller_inventory = seller.inventory.get(item_id=purchase_offer.item).quantity

    trade = Trade.objects.get()

    assert Trade.objects.count() == 1
    assert Offer.objects.get(id=purchase_offer.id).is_active == True
    assert Offer.objects.get(id=sell_offer.id).is_active == False

    assert trade.item == purchase_offer.item
    assert trade.item == sell_offer.item
    assert trade.seller == sell_offer.user
    assert trade.buyer == purchase_offer.user
    assert trade.quantity == correct_quantity
    assert trade.unit_price == sell_offer.price
    assert (
        trade.description
        == f"Trade between {sell_offer.user.username} and {purchase_offer.user.username}"
    )
    assert trade.seller_offer == sell_offer
    assert trade.buyer_offer == purchase_offer
    assert buyer_balance == original_buyer_balance - (trade.unit_price * trade.quantity)
    assert seller_balance == original_seller_balance + (
        trade.unit_price * trade.quantity
    )
    assert buyer_inventory == 1000 - trade.quantity
    assert seller_inventory == 1000 + trade.quantity
예제 #13
0
def test_stocks_quantity_for_trade_by_given_offers_with_greatest_sell_quantity(
    offer_instances,
):
    """
    Ensure that function return correct stocks quantity for trade.
    Since sell offer has more quantity of stocks than purchase offer.
    Function has to return purchase offer quantity of stocks.
    """

    purchase_offer_id = offer_instances[0].id
    sell_offer_id = offer_instances[5].id

    final_quantity = _stocks_quantity_for_trade_by_given_offers(
        sell_offer_id=sell_offer_id, purchase_offer_id=purchase_offer_id
    )

    assert final_quantity == get_available_quantity_stocks(offer_id=purchase_offer_id)
예제 #14
0
def test_create_trade_with_equal_quantity_stocks(offer_instances):
    """
    Ensure that function correctly create Trade instance by the given offers
    """

    purchase_offer = offer_instances[0]
    sell_offer = offer_instances[6]

    buyer = purchase_offer.user
    seller = sell_offer.user

    original_buyer_balance = buyer.balance.get().quantity
    original_seller_balance = seller.balance.get().quantity

    correct_quantity = get_available_quantity_stocks(offer_id=sell_offer.id)

    _create_trade(sell_offer_id=sell_offer.id, purchase_offer_id=purchase_offer.id)

    trade = Trade.objects.get()

    buyer_balance = buyer.balance.get().quantity
    buyer_inventory = buyer.inventory.get(item_id=purchase_offer.item).quantity

    seller_balance = seller.balance.get().quantity
    seller_inventory = seller.inventory.get(item_id=purchase_offer.item).quantity

    assert trade.item == purchase_offer.item
    assert trade.item == sell_offer.item
    assert trade.seller == sell_offer.user
    assert trade.buyer == purchase_offer.user
    assert trade.quantity == correct_quantity
    assert trade.unit_price == sell_offer.price
    assert (
        trade.description
        == f"Trade between {sell_offer.user.username} and {purchase_offer.user.username}"
    )
    assert trade.seller_offer == sell_offer
    assert trade.buyer_offer == purchase_offer
    assert buyer_balance == original_buyer_balance - (trade.unit_price * trade.quantity)
    assert seller_balance == original_seller_balance + (
        trade.unit_price * trade.quantity
    )
    assert buyer_inventory == 1000 - trade.quantity
    assert seller_inventory == 1000 + trade.quantity
예제 #15
0
def test_make_trades_with_greatest_purchase_stocks(offer_instances):
    """
    Ensure that function create right trade instances
    Since purchase offer has more quantity of stocks than one sell offer
    Function has to create 2 trade instances with different sell offer instances
    And delete purchase offer and first sell offer
    """

    purchase_offer = offer_instances[0]
    sell_offer_1 = offer_instances[3]
    sell_offer_2 = offer_instances[5]

    current_purchase_quantity = get_available_quantity_stocks(
        offer_id=purchase_offer.id
    )
    current_sell_quantity_1 = get_available_quantity_stocks(offer_id=sell_offer_1.id)
    current_sell_quantity_2 = get_available_quantity_stocks(offer_id=sell_offer_2.id)

    correct_quantity_1 = current_sell_quantity_1
    correct_quantity_2 = current_purchase_quantity - correct_quantity_1

    _make_trades(offer_id=purchase_offer.id)

    trade_1 = Trade.objects.first()
    trade_2 = Trade.objects.last()

    assert Trade.objects.count() == 2
    assert Offer.objects.get(id=purchase_offer.id).is_active == False
    assert Offer.objects.get(id=sell_offer_1.id).is_active == False
    assert Offer.objects.get(id=sell_offer_2.id).is_active == True

    assert trade_1.seller_offer == sell_offer_1
    assert trade_1.buyer_offer == purchase_offer
    assert trade_1.quantity == correct_quantity_1
    assert trade_1.unit_price == sell_offer_1.price
    assert get_available_quantity_stocks(offer_id=sell_offer_1.id) == 0

    assert trade_2.seller_offer == sell_offer_2
    assert trade_2.buyer_offer == purchase_offer
    assert trade_2.quantity == correct_quantity_2
    assert trade_2.unit_price == sell_offer_2.price
    assert (
        get_available_quantity_stocks(offer_id=sell_offer_2.id)
        == current_sell_quantity_2 - trade_2.quantity
    )
    assert get_available_quantity_stocks(offer_id=purchase_offer.id) == 0