def callback_query_handler( self, update: Update, context: CallbackContext, ) -> NoReturn: data = update.callback_query.data event_token = context.user_data[States.CURRENT_EVENT_TOKEN] event = self._splitwise.get_event_info(event_token) user_id = update.effective_user.id if data == menu_items.SHOW_DEBTS: lenders_info, debtors_info = self._splitwise.get_final_transactions( event_token) if user_id in debtors_info: update.callback_query.edit_message_text( 'Вы должны: \n' + str(debtors_info[user_id]) + f'\n\n {event.name}\nВыберите пункт меню:') elif user_id in lenders_info: update.callback_query.edit_message_text( 'Вам должны: \n' + str(lenders_info[user_id]) + f'\n\n {event.name}\nВыберите пункт меню:') else: update.callback_query.edit_message_text( 'Вы никому не должны и вам никто не должен!!!' + f'\n\n {event.name}\nВыберите пункт меню:') update.callback_query.edit_message_reply_markup( reply_markup=buttons.get_event_commands_keyboard()) update.callback_query.answer() return States.EVENT_ACTIONS elif data == menu_items.ADD_EXPENSE: event_token = context.user_data[States.CURRENT_EVENT_TOKEN] try: self._splitwise.get_event_info(event_token) except KeyError: update.effective_chat.send_message( 'Мероприятия с таким токеном не существует. Повторите создание траты:' ) return ConversationHandler.END expense = Expense() expense.event_token = event_token expense.lender_id = user_id context.user_data[States.EXPENSE] = expense update.callback_query.edit_message_text( 'Введи название траты или нажми \'Отмена\'', reply_markup=buttons.get_cancel_button()) return States.EXPENSE_NAME elif data == menu_items.CANCEL: del context.user_data[States.CURRENT_EVENT_TOKEN] update.callback_query.edit_message_text('Меню:') update.callback_query.edit_message_reply_markup( reply_markup=buttons.get_menu_keyboard()) update.callback_query.answer() return ConversationHandler.END
def add_expense( self, expense: Expense, ) -> int: """ Adds expense to database Fields 'name', 'sum', 'lender_id', 'event_token' are required Fields 'id', 'datetime' will be ignored @return: expense_id """ expense.id = None # will be filled after expense being added to db expense.datetime = datetime.now() return self.conn.save_expense_info(expense)
def test_event_isolation(app): for user in USERS: app.add_new_user(user) for i in range(2): app.create_event(event_name=EVENTS[i].name, event_token=EVENTS[i].token, user_id=USERS[0].id) expense_id = app.add_expense( Expense( name='expense', sum=100 * len(USERS), lender_id=USERS[0].id, event_token=EVENTS[0].token, )) for user in USERS[1:]: app.add_debt( Debt( expense_id=expense_id, lender_id=USERS[0].id, debtor_id=user.id, sum=100, )) lenders_info, debtors_info = app.get_final_transactions(EVENTS[1].token) assert lenders_info == dict() assert debtors_info == dict()
def test_circular_transactions(app): for user in USERS: app.add_new_user(user) event_token = app.create_event(user_id=USERS[0].id, event_name='Test event') for user in USERS[1:]: app.add_user_to_event(user_id=user.id, event_token=event_token) for i in range(len(USERS)): expense_id = app.add_expense( Expense( name=f'expense #{i + 1}', sum=100, lender_id=USERS[i].id, event_token=event_token, )) app.add_debt( Debt( expense_id=expense_id, lender_id=USERS[i].id, debtor_id=USERS[(i + 1) % len(USERS)].id, sum=100, )) lenders_info, debtors_info = app.get_final_transactions(event_token) assert lenders_info == dict() assert debtors_info == dict()
def test_final_transactions(app): for user in USERS: app.add_new_user(user) event_token = app.create_event(user_id=USERS[0].id, event_name='Test event') expense_id = app.add_expense( Expense( name='first expense', sum=100 * len(USERS), lender_id=USERS[0].id, event_token=event_token, )) for user in USERS[1:]: app.add_user_to_event(user_id=user.id, event_token=event_token) app.add_debt( Debt( expense_id=expense_id, lender_id=USERS[0].id, debtor_id=user.id, sum=100, )) lenders_info, debtors_info = app.get_final_transactions(event_token) assert list(lenders_info.keys()) == [USERS[0].id] assert sorted(lenders_info[USERS[0].id]) == sorted([(user.name, 100) for user in USERS[1:]]) assert sorted(list(debtors_info.keys())) == sorted( [user.id for user in USERS[1:]]) for _, debt_list in debtors_info.items(): assert debt_list == [(USERS[0].name, 100)]
def get_expense_info( self, expense_id: int, ) -> Expense: cursor = self.conn.cursor() expense = cursor.execute('SELECT * FROM expenses WHERE id = ?', (expense_id, )).fetchone() return Expense( id=expense[0], name=expense[1], sum=expense[2], lender_id=expense[3], event_token=expense[4], datetime=expense[5], )
def get_event_expenses( self, event_token: str, ) -> List[Expense]: cursor = self.conn.cursor() expenses = cursor.execute( 'SELECT * FROM expenses WHERE event_token = ?', (event_token, )) return [ Expense( id=item[0], name=item[1], sum=item[2], lender_id=item[3], event_token=item[4], datetime=item[5], ) for item in expenses.fetchall() ]