Esempio n. 1
0
def test_bid_auction_error(initdb):
    datetime_now = datetime(2000, 1, 2, 3, 4, 5, tzinfo=timezone.utc)
    datetime_end = datetime(2000, 12, 11, 10, 9, 8, tzinfo=timezone.utc)

    app = create_app({
        "MYSQL_CONNECT_KWARGS": testdb.MYSQL_CONNECT_KWARGS,
    })

    with app.app_context():
        g.datetime_now = datetime_now
        bid_result = logic.bid_auction({
            "auction_id": 1,
            "username": "******",
            "price": 100,
        })
        logic.commit()

    assert bid_result == logic.BidErrorCodes.BID_ERROR_NOT_FOUND

    with app.app_context():
        g.datetime_now = datetime_now
        auction_id, _ = logic.new_auction({
            "itemname": "foo",
            "quantity": 123,
            "username": "******",
            "datetime_end": datetime_end,
            "price_start": 456,
            "price_prompt": 789,
            "price_step_min": 111,
            "location": "anyware",
            "description": "something",
        })
        logic.commit()

    with app.app_context():
        g.datetime_now = datetime_now
        bid_result = logic.bid_auction({
            "auction_id": auction_id,
            "username": "******",
            "price": 100,
        })
        logic.commit()

    assert bid_result == logic.BidErrorCodes.BID_ERROR_ANOTHER_ONE_BIDDED_FIRST

    with app.app_context():
        g.datetime_now = datetime_end
        bid_result = logic.bid_auction({
            "auction_id": auction_id,
            "username": "******",
            "price": 1000,
        })
        logic.commit()

    assert bid_result == logic.BidErrorCodes.BID_ERROR_ALREADY_ENDED
Esempio n. 2
0
def test_bid_auction_manytimes(initdb):
    datetime_now = datetime(2000, 1, 2, 3, 4, 5, tzinfo=timezone.utc)
    datetime_end = datetime(2000, 12, 11, 10, 9, 8, tzinfo=timezone.utc)
    datetime_bid = datetime(2000, 6, 7, 8, 9, 10, tzinfo=timezone.utc)

    app = create_app({
        "MYSQL_CONNECT_KWARGS": testdb.MYSQL_CONNECT_KWARGS,
        "YAMI_PRICE_STEP_MIN": 111,
        "YAMI_PRICE_STEP_FROM_CURRENT_PRICE": True,
        "YAMI_PRICE_STEP_RULE": {
            1: 1,
        },
        "YAMI_AUTO_EXTENSION": 0,
    })

    with app.app_context():
        g.datetime_now = datetime_now
        auction_id, _ = logic.new_auction({
            "itemname": "foo",
            "quantity": 2,
            "username": "******",
            "datetime_end": datetime_end,
            "price_start": 456,
            "price_prompt": None,
            "price_step_min": 111,
            "location": "anyware",
            "description": "something",
        })
        logic.commit()

    bid_results = []
    with app.app_context():
        for i in range(5):
            g.datetime_now = datetime_bid + timedelta(days=i + 1)
            bid_results.append(
                logic.bid_auction({
                    "auction_id": auction_id,
                    "username": "******" + str(i),
                    "price": 1000 * (i + 1),
                }))
        auction, bids = logic.get_auction_info(auction_id, for_update=False)
        auctions, _ = logic.get_auction_list(1, 0, False)
        logic.commit()

    for i in range(5):
        assert bid_results[i] == logic.BidErrorCodes.BID_OK

    assert not auction["ended"]
    assert auction["datetime_update"] == datetime_bid + timedelta(days=5)
    assert auction["price_current_low"] == 4000
    assert auctions[0]["price_current_low"] == 4000
    for i in range(5):
        assert bids[i] == {
            "bid_id": 5 - i,
            "auction_id": auction_id,
            "username": "******" + str(4 - i),
            "price": 1000 * (5 - i),
            "datetime_bid": datetime_bid + timedelta(days=5 - i),
        }
Esempio n. 3
0
def test_bid_auction_extension(quantity, prompt, extended, initdb):
    datetime_now = datetime(2000, 1, 2, 3, 4, 5, tzinfo=timezone.utc)
    datetime_end = datetime(2000, 12, 11, 10, 9, 8, tzinfo=timezone.utc)
    yami_auto_extension = 3600

    app = create_app({
        "MYSQL_CONNECT_KWARGS": testdb.MYSQL_CONNECT_KWARGS,
        "YAMI_PRICE_STEP_MIN": 111,
        "YAMI_PRICE_STEP_FROM_CURRENT_PRICE": True,
        "YAMI_PRICE_STEP_RULE": {
            1: 1,
        },
        "YAMI_AUTO_EXTENSION": yami_auto_extension,
    })

    with app.app_context():
        g.datetime_now = datetime_now
        auction_id, _ = logic.new_auction({
            "itemname": "foo",
            "quantity": quantity,
            "username": "******",
            "datetime_end": datetime_end,
            "price_start": 456,
            "price_prompt": 1000,
            "price_step_min": 111,
            "location": "anyware",
            "description": "something",
        })
        logic.commit()

    with app.app_context():
        g.datetime_now = datetime_end - timedelta(minutes=30)
        logic.bid_auction({
            "auction_id": auction_id,
            "username": "******",
            "price": 1000 if prompt else 500,
        })
        auction, _ = logic.get_auction_info(auction_id, for_update=False)
        logic.commit()

    assert auction["datetime_end"] == datetime_end + (
        -timedelta(minutes=30) +
        timedelta(seconds=yami_auto_extension) if extended else timedelta())
Esempio n. 4
0
def test_bid_auction_another_one_bidded_first(monkeypatch):
    datetime_now = datetime(2000, 1, 2, 3, 4, 5, tzinfo=timezone.utc)
    #datetime_now_naive = datetime_now.replace(tzinfo=None)

    app = create_app({})
    newbid = {
        "auction_id": 42,
        "price": 100,
    }

    get_auction_info_mock = mock.MagicMock()
    get_auction_info_mock.return_value = ({
        "auction_id":
        42,
        "quantity":
        3,
        "price_start":
        10,
        "price_prompt":
        None,
        "price_step_min":
        100,
        "ended":
        False,
        "datetime_end":
        datetime_now - timedelta(days=1),
        "price_bid_min":
        200,
    }, [{
        "bid_id": 3,
        "price": 100,
    }, {
        "bid_id": 2,
        "price": 50,
    }, {
        "bid_id": 1,
        "price": 10,
    }])
    monkeypatch.setattr(logic, "get_auction_info", get_auction_info_mock)

    with app.app_context():
        g.datetime_now = datetime_now
        g.db = None
        result = logic.bid_auction(newbid)

    get_auction_info_mock.mock_calls == [
        mock.call(42, for_update=True),
    ]

    assert result == logic.BidErrorCodes.BID_ERROR_ANOTHER_ONE_BIDDED_FIRST
Esempio n. 5
0
def test_bid_auction_notfound(monkeypatch):
    datetime_now = datetime(2000, 1, 2, 3, 4, 5, tzinfo=timezone.utc)
    #datetime_now_naive = datetime_now.replace(tzinfo=None)

    app = create_app({})
    newbid = {
        "auction_id": 42,
    }

    get_auction_info_mock = mock.MagicMock()
    get_auction_info_mock.return_value = (None, None)
    monkeypatch.setattr(logic, "get_auction_info", get_auction_info_mock)

    with app.app_context():
        g.datetime_now = datetime_now
        g.db = None
        result = logic.bid_auction(newbid)

    get_auction_info_mock.mock_calls == [
        mock.call(42, for_update=True),
    ]

    assert result == logic.BidErrorCodes.BID_ERROR_NOT_FOUND
Esempio n. 6
0
def test_bid_auction_prompt(initdb):
    datetime_now = datetime(2000, 1, 2, 3, 4, 5, tzinfo=timezone.utc)
    datetime_end = datetime(2000, 12, 11, 10, 9, 8, tzinfo=timezone.utc)
    datetime_bid = datetime(2000, 6, 7, 8, 9, 10, tzinfo=timezone.utc)

    app = create_app({
        "MYSQL_CONNECT_KWARGS": testdb.MYSQL_CONNECT_KWARGS,
        "YAMI_PRICE_STEP_MIN": 111,
        "YAMI_PRICE_STEP_FROM_CURRENT_PRICE": True,
        "YAMI_PRICE_STEP_RULE": {
            1: 1,
        }
    })

    with app.app_context():
        g.datetime_now = datetime_now
        auction_id, _ = logic.new_auction({
            "itemname": "foo",
            "quantity": 1,
            "username": "******",
            "datetime_end": datetime_end,
            "price_start": 456,
            "price_prompt": 789,
            "price_step_min": 111,
            "location": "anyware",
            "description": "something",
        })
        logic.commit()

    with app.app_context():
        g.datetime_now = datetime_bid
        bid_result = logic.bid_auction({
            "auction_id": auction_id,
            "username": "******",
            "price": 789,
        })
        auction, bids = logic.get_auction_info(auction_id, for_update=False)
        logic.commit()

    assert bid_result == logic.BidErrorCodes.BID_OK

    assert auction["ended"]
    assert auction["datetime_update"] == datetime_bid
    assert bids == [{
        "bid_id": 1,
        "auction_id": auction_id,
        "username": "******",
        "price": 789,
        "datetime_bid": datetime_bid,
    }]

    with app.app_context():
        g.datetime_now = datetime_bid
        bid_result = logic.bid_auction({
            "auction_id": auction_id,
            "username": "******",
            "price": 1000,
        })
        logic.commit()

    assert bid_result == logic.BidErrorCodes.BID_ERROR_ALREADY_ENDED
Esempio n. 7
0
def test_bid_auction_ok(quantity, enddelta, price_prompt, price_bidded, ended,
                        extend, monkeypatch):
    datetime_now = datetime(2000, 1, 2, 3, 4, 5, tzinfo=timezone.utc)
    #datetime_now_naive = datetime_now.replace(tzinfo=None)

    app = create_app({
        "YAMI_AUTO_EXTENSION": 300,
    })
    newbid = {
        "auction_id": 42,
        "price": 250,
        "username": "******",
    }

    get_auction_info_mock = mock.MagicMock()
    get_auction_info_mock.return_value = ({
        "auction_id":
        42,
        "quantity":
        quantity,
        "price_start":
        10,
        "price_prompt":
        price_prompt,
        "price_step_min":
        100,
        "ended":
        False,
        "datetime_end":
        datetime_now + timedelta(seconds=enddelta),
        "price_bid_min":
        200,
    }, [{
        "bid_id": 3,
        "price": 100,
    }, {
        "bid_id": 2,
        "price": 50,
    }, {
        "bid_id": 1,
        "price": 10,
    }])
    monkeypatch.setattr(logic, "get_auction_info", get_auction_info_mock)

    end_auction_mock = mock.MagicMock()
    monkeypatch.setattr(logic, "end_auction", end_auction_mock)

    cursor_mock1 = create_cursor_mock()
    cursor_mock1.fetchone.return_value = (4, )
    cursor_mock2 = create_cursor_mock()
    cursor_mock3 = create_cursor_mock()
    db_mock = mock.MagicMock()
    db_side_effect = [cursor_mock1, cursor_mock3]
    if extend:
        db_side_effect.insert(1, cursor_mock2)
    db_mock.cursor.side_effect = db_side_effect

    with app.app_context():
        g.datetime_now = datetime_now
        g.db = db_mock
        result = logic.bid_auction(newbid)

    assert get_auction_info_mock.mock_calls == [
        mock.call(42, for_update=True),
    ]

    assert end_auction_mock.mock_calls == ([
        mock.call({
            "auction_id": 42,
            "quantity": quantity,
            "price_start": 10,
            "price_prompt": price_prompt,
            "price_step_min": 100,
            "ended": True,
            "datetime_end": datetime_now + timedelta(seconds=enddelta),
            "price_bid_min": 200,
        })
    ] if ended else [])

    assert db_mock.cursor.mock_calls == ([
        mock.call(),
        mock.call(),
        mock.call(),
    ] if extend else [
        mock.call(),
        mock.call(),
    ])

    assert cursor_mock1.mock_calls == [
        mock.call.execute(
            "INSERT INTO t_bid (auction_id, username, price, datetime_bid) VALUES (%s, %s, %s, %s)",
            (42, "bidder", price_bidded, datetime_now)),
        mock.call.execute("SELECT LAST_INSERT_ID() FROM t_bid"),
        mock.call.fetchone(),
        mock.call.close(),
    ]

    assert cursor_mock2.mock_calls == ([
        mock.call.execute(
            "UPDATE t_auction SET datetime_end = %s WHERE auction_id = %s",
            (datetime_now + timedelta(seconds=300), 42)),
        mock.call.close(),
    ] if extend else [])

    assert cursor_mock3.mock_calls == [
        mock.call.execute(
            "UPDATE t_auction SET datetime_update = %s WHERE auction_id = %s",
            (datetime_now, 42)),
        mock.call.close(),
    ]

    assert result == logic.BidErrorCodes.BID_OK
    assert newbid["bid_id"] == 4
    assert newbid["price"] == price_bidded
    assert newbid["datetime_bid"] == datetime_now