Exemple #1
0
async def test_chat_service_set_min_price_greater_than_max(
        amocker, chat_factory, chat_service: services.ChatService
):
    chat = chat_factory(min_price=datatypes.Price('700'), max_price=datatypes.Price('1000'))
    chat_service.get_or_create = amocker.CoroutineMock(return_value=chat)

    min_price = datatypes.Price('1001')
    with pytest.raises(ValueError):
        await chat_service.set_min_price(chat_id=chat.id, price=min_price)

    assert chat_service.get_or_create.called
    assert not chat_service.db_adapter.update_chat.called
Exemple #2
0
 def wrapper(**kwargs):
     from app import datatypes, entities
     defaults = {
         'title': faker.sentence(),
         'price': datatypes.Price(faker.pydecimal(positive=True)),
         'url': faker.url(),
         'created_at': faker.unix_time(),
     }
     defaults.update(kwargs)
     return entities.Property(**defaults)
async def test_sqlite_db_adapter_update_chat(
        chat_factory, sqlite_db_adapter: adapters.SqliteDBAdapter):
    min_price = datatypes.Price(700)
    chat = chat_factory(min_price=min_price)
    await sqlite_db_adapter.create_chat(chat_id=chat.id)

    await sqlite_db_adapter.update_chat(chat)

    updated_chat = await sqlite_db_adapter.get_chat(chat_id=chat.id)
    assert updated_chat.min_price == min_price
    assert updated_chat.max_price is None
Exemple #4
0
async def test_send_service_start_sending(amocker, async_gen_mock, property_factory, send_service):
    real_property = property_factory(
        title='Renovated house', price=datatypes.Price('700'), url='https://example.com/1'
    )
    send_service.get_updates = async_gen_mock(return_value=real_property)

    await send_service.start_sending()

    assert send_service.db_adapter.select_chats.called
    assert send_service.bot_adapter.broadcast.called
    assert send_service.bot_adapter.broadcast.call_args == amocker.call(
        chats=[], text=f'[Renovated house €700]({real_property.telegram_link})'
    )
Exemple #5
0
async def test_chat_service_set_max_price(
        amocker, chat_factory, chat_service: services.ChatService
):
    chat = chat_factory()
    chat_service.get_or_create = amocker.CoroutineMock(return_value=chat)

    max_price = datatypes.Price('700.0')
    await chat_service.set_max_price(chat_id=chat.id, price=max_price)

    assert chat_service.get_or_create.called
    assert chat_service.db_adapter.update_chat.called

    chat.max_price = max_price
    assert chat_service.db_adapter.update_chat.call_args == amocker.call(chat)
async def test_sqlite_db_adapter_select_chats(
        sqlite_db_adapter: adapters.SqliteDBAdapter):
    price = datatypes.Price(700)

    await sqlite_db_adapter.connect.execute('''
        INSERT INTO
            chat (id, min_price, max_price)
        VALUES
            (1, 550.0, 700.0),
            (2, 700.0, 1000.0),
            (3, 400.0, 600.0),
            (4, 800.0, 1300.0),
            (5, null, 1000.0),
            (6, 550.0, null),
            (7, null, null)
    ''')

    chats = await sqlite_db_adapter.select_chats(interested_in_price=price)
    chat_id_list = [chat.id for chat in chats]
    assert chat_id_list == [1, 2, 5, 6, 7]
async def test_sqlite_db_adapter_row_to_chat(
        chat_factory, sqlite_db_adapter: adapters.SqliteDBAdapter):
    chat = chat_factory(max_price=datatypes.Price(700))
    row = {'id': chat.id, 'min_price': None, 'max_price': 700}
    assert sqlite_db_adapter.row_to_chat(row) == chat
def test_bazaraki_parser_get_item_price(bazaraki_item):
    parser = parsers.BazarakiParser()
    assert parser.get_item_price(bazaraki_item) == datatypes.Price(3500)
Exemple #9
0
def test_price():
    price = datatypes.Price()
    assert price == 0
Exemple #10
0
def test_negative_price():
    with pytest.raises(ValueError):
        datatypes.Price('-1')
Exemple #11
0
import pytest

from app import datatypes


def test_price():
    price = datatypes.Price()
    assert price == 0


def test_negative_price():
    with pytest.raises(ValueError):
        datatypes.Price('-1')


@pytest.mark.parametrize(['given', 'expected'],
                         [(None, None), (700.0, datatypes.Price('700.0'))])
def test_price_from_optional(given, expected):
    assert datatypes.Price.from_optional(given) == expected