def test_add_half_watchlist(): (mock_bot, update, telegram_user_id) = init_telegram() from bot.db import db user = { 'telegram': { 'id': telegram_user_id } } user_id = db.users.insert(user) context = Dispatcher(bot=mock_bot, update_queue=update) add_watchlist(update, context) update = get_mock_callback_query(mock_bot, update, data='T') watchlist_letter_inputted(update, context) update = get_mock_callback_query(mock_bot, update, data='Toyota') watchlist_make_inputted(update, context) update = get_mock_callback_query(mock_bot, update, data='Aqua') watchlist_model_inputted(update, context) update = get_mock_callback_query(mock_bot, update, data='Add more details') confirm_watchlist_details(update, context) update = get_mock_callback_query(mock_bot, update, data='Add max mileage') input_car_parameters(update, context) update = get_mock_message(mock_bot, update, text='200000') watchlist_miles_inputted(update, context) update = get_mock_callback_query(mock_bot, update, data='Save watchlist') confirm_watchlist_details(update, context) watchlist = db.watchlists.find_one({'userId': user_id}) assert watchlist['make'] == 'Toyota' assert watchlist['model'] == 'Aqua' assert watchlist.get('year') == None assert watchlist['mileage']['max'] == 200000 assert watchlist.get('price') == None
def test_find_car(): (mock_bot, update, telegram_user_id) = init_telegram() client = pymongo.MongoClient(REAL_TEST_MONGODB_URI) db = client[REAL_TEST_MONGODB_DB] client.drop_database(REAL_TEST_MONGODB_DB) _set_db(db) user = { 'telegram': { 'id': telegram_user_id } } user_id = db.users.insert(user) car_1 = { **DEFAULT_CAR_ITEM } car_2 = { **DEFAULT_CAR_ITEM, 'mileage': 109207, 'price': { 'amount': 6200, 'currency': 'USD' }, 'carInfo': { 'year': 2017, 'make': 'Honda', 'model': 'Saber', 'vin': 'SOMEVIN_2' } } car_3 = { **DEFAULT_CAR_ITEM, 'mileage': 109507, 'price': { 'amount': 6000, 'currency': 'USD' }, 'carInfo': { 'year': 2011, 'make': 'Honda', 'model': 'Saber', 'vin': 'SOMEVIN_3' }, } db.cars.insert_many([car_1, car_2, car_3]) context = Dispatcher(bot=mock_bot, update_queue=update) find_car(update, context) update = get_mock_callback_query(mock_bot, update, data='H') watchlist_letter_inputted(update, context) update = get_mock_callback_query(mock_bot, update, data='Honda') watchlist_make_inputted(update, context) update = get_mock_callback_query(mock_bot, update, data='Saber') watchlist_model_inputted(update, context) update = get_mock_callback_query(mock_bot, update, data='Add more details') confirm_watchlist_details(update, context) update = get_mock_callback_query(mock_bot, update, data='Add year') input_car_parameters(update, context) update = get_mock_callback_query(mock_bot, update, data='2012') watchlist_from_year_inputted(update, context) update = get_mock_callback_query(mock_bot, update, data='2019') watchlist_to_year_inputted(update, context) update = get_mock_callback_query(mock_bot, update, data='Add more details') confirm_watchlist_details(update, context) update = get_mock_callback_query(mock_bot, update, data='Add max mileage') input_car_parameters(update, context) update = get_mock_message(mock_bot, update, text='300000') watchlist_miles_inputted(update, context) update = get_mock_callback_query(mock_bot, update, data='Add more details') confirm_watchlist_details(update, context) update = get_mock_callback_query(mock_bot, update, data='Add max price') input_car_parameters(update, context) update = get_mock_message(mock_bot, update, text='7500') watchlist_price_inputted(update, context) update = get_mock_callback_query(mock_bot, update, data='Find car') confirm_watchlist_details(update, context) update = get_mock_callback_query(mock_bot, update, data='No') confirm_watchlist_details(update, context) sent_messages = mock_bot.sent_messages assert sent_messages[-6] == { 'chat_id': telegram_user_id, 'method': 'sendMessage', 'parse_mode': 'Markdown', 'text': 'Found 2 matching cars' } assert sent_messages[-5] == { 'chat_id': telegram_user_id, 'method': 'sendMessage', 'text': '1.\n2015 Honda Saber\n96202 miles\n$5000\npostUrl' } assert sent_messages[-4] == { 'chat_id': telegram_user_id, 'method': 'sendMessage', 'text': '2.\n2017 Honda Saber\n109207 miles\n$6200\npostUrl' } assert sent_messages[-3] == { 'chat_id': telegram_user_id, 'method': 'sendMessage', 'reply_markup': json.dumps({ 'inline_keyboard': [[ {'text': 'Yes', 'callback_data': 'Yes'}, {'text': 'No', 'callback_data': 'No'} ]] }), 'text': 'Would you like to get notifications about new such cars?' } assert sent_messages[-1] == { 'chat_id': telegram_user_id, 'method': 'sendMessage', 'parse_mode': 'Markdown', 'text': 'Adding watchlist cancelled' }
def test_edit_watchlist(): (mock_bot, update, telegram_user_id) = init_telegram() from bot.db import db user = { 'telegram': { 'id': telegram_user_id } } user_id = db.users.insert(user) watchlist = { 'userId': user_id, 'make': 'Honda', 'model': 'Saber', 'year': { 'min': 2004, 'max': 2009 }, 'mileage': { 'max': 300000 }, 'price': { 'max': 3500 } } watchlist_id = db.watchlists.insert(watchlist) context = Dispatcher(bot=mock_bot, update_queue=update) edit_watchlist(update, context) update = get_mock_callback_query(mock_bot, update, data=str(watchlist_id)) watchlist_selected_for_edit(update, context) update = get_mock_callback_query(mock_bot, update, data='T') watchlist_letter_inputted(update, context) update = get_mock_callback_query(mock_bot, update, data='Toyota') watchlist_make_inputted(update, context) update = get_mock_callback_query(mock_bot, update, data='Aqua') watchlist_model_inputted(update, context) update = get_mock_callback_query(mock_bot, update, data='Add more details') confirm_watchlist_details(update, context) update = get_mock_callback_query(mock_bot, update, data='Add year') input_car_parameters(update, context) update = get_mock_callback_query(mock_bot, update, data='2010') watchlist_from_year_inputted(update, context) update = get_mock_callback_query(mock_bot, update, data='2020') watchlist_to_year_inputted(update, context) update = get_mock_callback_query(mock_bot, update, data='Add more details') confirm_watchlist_details(update, context) update = get_mock_callback_query(mock_bot, update, data='Add max mileage') input_car_parameters(update, context) update = get_mock_message(mock_bot, update, text='200000') watchlist_miles_inputted(update, context) update = get_mock_callback_query(mock_bot, update, data='Add more details') confirm_watchlist_details(update, context) update = get_mock_callback_query(mock_bot, update, data='Add max price') input_car_parameters(update, context) update = get_mock_message(mock_bot, update, text='15500') watchlist_price_inputted(update, context) update = get_mock_callback_query(mock_bot, update, data='Save watchlist') confirm_watchlist_details(update, context) old_watchlist = db.watchlists.find_one({'userId': user_id, 'make': 'Honda'}) watchlist = db.watchlists.find_one({'_id': watchlist_id}) assert old_watchlist == None assert watchlist['make'] == 'Toyota' assert watchlist['model'] == 'Aqua' assert watchlist['year'] == { 'min': 2010, 'max': 2020 } assert watchlist['mileage']['max'] == 200000 assert watchlist['price']['max'] == 15500