def test_run_external(cur, user, user2, tr_external): try: # fixed all data cur.connection.commit() tr_external.run() assert tr_external.get_status() == SUCCESS inv_from_new = Invoice.find_by_id(tr_external.invoice_id_from) inv_to_new = Invoice.find_by_id(tr_external.invoice_id_to) user_invoice_usd = user.invoices.get(USD, None) context = Context(prec=1, rounding=ROUND_DOWN) com = context.create_decimal_from_float(COMMISSION) assert inv_from_new.balance == ( user_invoice_usd.balance - (tr_external.amount + tr_external.amount * com)).quantize(TWOPLACES) user_invoice_eur = user2.invoices.get(EUR, None) assert inv_to_new.balance == (user_invoice_eur.balance + tr_external.convert_amount(user_invoice_usd, user_invoice_eur)).quantize( TWOPLACES) finally: with get_db_cursor(commit=True) as cur: cur.execute(f'''DELETE FROM public.transaction WHERE id={tr_external.id}::int;''') cur.execute(f'''DELETE FROM public.invoice WHERE user_id={user.id}::int;''') cur.execute(f'''DELETE FROM public.invoice WHERE user_id={user2.id}::int;''') cur.execute(f'''DELETE FROM public.user WHERE id={user.id}::int;''') cur.execute(f'''DELETE FROM public.user WHERE id={user2.id}::int;''')
def run(self): """ run transaction with special logic """ tr = Transaction.find_by_id(self.id) if not tr: raise Exception('Transaction had to fix (created in db) ') if tr.get_status() != IN_PROCESS: raise Exception( f'Transaction completed yet with status {tr.get_status()}') if tr != self: raise Exception("Transaction object not synchronize with db") with get_db_cursor(commit=False) as cur: try: invoice_from = Invoice.find_by_id(self.invoice_id_from) invoice_to = Invoice.find_by_id(tr.invoice_id_to) if invoice_from.user_id == invoice_to.user_id: self.__move_internal(invoice_from, invoice_to, cur) else: self.__move_external(invoice_from, invoice_to, cur) self.__update_status(SUCCESS, cur) cur.connection.commit() return self except Exception as err: cur.connection.rollback() self.canceled() raise err
def __create(self, cur): created_at, u_id = self.__insert(cur) self.id = u_id self.created_at = created_at invoice_usd = Invoice.new_empty(user_id=u_id, currency=USD, balance=Decimal(100)).create(cur) invoice_eur = Invoice.new_empty(user_id=u_id, currency=EUR).create(cur) invoice_cny = Invoice.new_empty(user_id=u_id, currency=CNY).create(cur) self.invoices = {USD: invoice_usd, EUR: invoice_eur, CNY: invoice_cny}
def test_run_inner(cur, user, tr_inner): try: # fixed all data cur.connection.commit() tr_inner.run() assert tr_inner.get_status() == SUCCESS inv_from_new = Invoice.find_by_id(tr_inner.invoice_id_from) inv_to_new = Invoice.find_by_id(tr_inner.invoice_id_to) user_invoice_usd = user.invoices.get(USD, None) assert inv_from_new.balance == user_invoice_usd.balance - tr_inner.amount user_invoice_eur = user.invoices.get(EUR, None) assert inv_to_new.balance == user_invoice_eur.balance + tr_inner.convert_amount(user_invoice_usd, user_invoice_eur) finally: with get_db_cursor(commit=True) as cur: cur.execute(f'''DELETE FROM public.transaction WHERE id={tr_inner.id}::int;''') cur.execute(f'''DELETE FROM public.invoice WHERE user_id={user.id}::int;''') cur.execute(f'''DELETE FROM public.user WHERE id={user.id}::int;''')
def test_create(cur, user): invoice = Invoice.new_empty(user_id=user.id, currency=USD, balance=Decimal(500)).create(cur=cur) cur.execute( f'''SELECT id,user_id,currency_id,balance,created_at,updated_at FROM public.invoice where id = {invoice.id}''' ) id, user_id, currency_id, balance, created_at, updated_at = cur.fetchone() assert id == invoice.id, "Bad invoice id in saved object" assert user_id == invoice.user_id, "Bad invoice user_id in saved object" assert balance == invoice.balance, "Bad invoice balance in saved object" assert created_at == invoice.created_at, "Bad invoice created_at in saved object" assert updated_at == invoice.updated_at, "Bad invoice updated_at in saved object" assert currency_id == invoice.currency.id, "Bad invoice currency_id in saved object"
def __init__(self, u_id=0, name='', email='', password='', created_at=None, invoice_ids=()): self.id = u_id self.name = name self.invoices = {EUR: None, USD: None, CNY: None} self.email = email self.password = password self.created_at = created_at if invoice_ids: for item in invoice_ids: inv = Invoice.find_by_id(item) self.invoices[inv.currency.name] = inv
def test_find_by_id(cur, user): invoice = user.invoices.get(USD, None) invoice_new = Invoice.find_by_id(invoice.id, cur) assert invoice == invoice_new