def test_sync(self): mock_sess = Mock() with patch('%s.inspect' % pbm) as mock_inspect: type(mock_inspect.return_value).session = mock_sess b1 = Budget(name='my_budg') b2 = Budget(name='my_budg2') b3 = Budget(name='my_budg3') t = Transaction(budget_amounts={ b1: Decimal('10.00'), b2: Decimal('90.00') }) mock_sess.reset_mock() t.set_budget_amounts({b1: Decimal('40.00'), b3: Decimal('60.00')}) assert len(t.budget_transactions) == 3 for bt in t.budget_transactions: assert isinstance(bt, BudgetTransaction) assert bt.transaction == t assert {bt.budget: bt.amount for bt in t.budget_transactions} == { b1: Decimal('40.00'), b2: Decimal('90.00'), b3: Decimal('60.00') } assert len(mock_sess.mock_calls) == 1 assert mock_sess.mock_calls[0][0] == 'delete' assert mock_sess.mock_calls[0][1][0].budget == b2 assert mock_sess.mock_calls[0][1][0].amount == Decimal('90.00')
def _budgets_preshot(self): logger.info('budgets preshot - DB update') conn = engine.connect() data_sess = scoped_session( sessionmaker(autocommit=False, autoflush=False, bind=conn) ) pp = BiweeklyPayPeriod.period_for_date(dtnow(), data_sess).previous data_sess.add(Budget( name='Budget3', is_periodic=True, starting_balance=Decimal('0') )) data_sess.add(Budget( name='Budget4', is_periodic=True, starting_balance=Decimal('0') )) data_sess.add(Budget( name='Budget5', is_periodic=True, starting_balance=Decimal('250') )) data_sess.flush() data_sess.commit() for i in range(0, 10): self._add_transactions(data_sess, pp) pp = pp.previous data_sess.close() conn.close() logger.info('budgets preshot - DB update done; click') self.get('/budgets') sleep(10) logger.info('budgets preshot - executing script') self.browser.execute_script( "$('#budget-per-period-chart').find('.morris-hover').show()" ) sleep(2) logger.info('budgets preshot done')
def submit(self, data): """ Handle form submission; create or update models in the DB. Raises an Exception for any errors. :param data: submitted form data :type data: dict :return: message describing changes to DB (i.e. link to created record) :rtype: str """ if 'id' in data and data['id'].strip() != '': # updating an existing budget budget = db_session.query(Budget).get(int(data['id'])) if budget is None: raise RuntimeError("Error: no Budget with ID %s" % data['id']) action = 'updating Budget ' + data['id'] else: budget = Budget() action = 'creating new Budget' budget.name = data['name'].strip() budget.description = data['description'].strip() budget.is_periodic = data['is_periodic'] if data['is_periodic'] is True: budget.starting_balance = Decimal(data['starting_balance']) else: budget.current_balance = Decimal(data['current_balance']) budget.is_active = data['is_active'] budget.is_income = data['is_income'] budget.omit_from_graphs = data['omit_from_graphs'] logger.info('%s: %s', action, budget.as_dict) db_session.add(budget) db_session.commit() return 'Successfully saved Budget %d in database.' % budget.id
def test_2_add_budgets(self, testdb): testdb.add( Budget(name='1Standing', is_periodic=False, description='1Standing', current_balance=987.65)) testdb.add( Budget(name='2Income', is_periodic=True, description='2Income', starting_balance=123.45, is_income=True)) testdb.add( Budget(name='3Income', is_periodic=True, description='2Income', starting_balance=0.0, is_income=True)) testdb.add( Budget(name='4Periodic', is_periodic=True, description='4Periodic', starting_balance=500.00)) testdb.add( Budget(name='5Periodic', is_periodic=True, description='5Periodic', starting_balance=100.00)) testdb.flush() testdb.commit()
def test_12_add_budgets(self, testdb): testdb.add( Budget(name='1Standing', is_periodic=False, description='1Standing', current_balance=Decimal('987.65'))) testdb.add( Budget(name='2Income', is_periodic=True, description='2Income', starting_balance=Decimal('123.45'), is_income=True)) testdb.add( Budget(name='3Income', is_periodic=True, description='2Income', starting_balance=Decimal('0.0'), is_income=True)) testdb.add( Budget(name='4Periodic', is_periodic=True, description='4Periodic', starting_balance=Decimal('500.00'))) testdb.add( Budget(name='5Periodic', is_periodic=True, description='5Periodic', starting_balance=Decimal('100.00'))) testdb.flush() testdb.commit()
def _payperiods_preshot(self): logger.info('payperiods preshot') # BEGIN DB update conn = engine.connect() data_sess = scoped_session( sessionmaker(autocommit=False, autoflush=False, bind=conn) ) pp = BiweeklyPayPeriod.period_for_date(dtnow(), data_sess).previous data_sess.add(Budget( name='Budget3', is_periodic=True, starting_balance=Decimal('0') )) data_sess.add(Budget( name='Budget4', is_periodic=True, starting_balance=Decimal('0') )) data_sess.flush() data_sess.commit() budgets = list(data_sess.query(Budget).filter( Budget.is_active.__eq__(True), Budget.is_income.__eq__(False), Budget.is_periodic.__eq__(True) ).all()) for i in range(0, 12): # payperiods mult = choice([-1, 1]) target = 2011.67 + (mult * uniform(0, 500)) total = 0 count = 0 while total < target: count += 1 amt = uniform(0, target * 0.2) if total + amt > target: amt = target - total total += amt amt = Decimal(Decimal(amt).quantize( Decimal('.001'), rounding=ROUND_HALF_UP )) data_sess.add(Transaction( account_id=1, budgeted_amount=amt, date=pp.start_date + timedelta(days=1), description='Transaction %d.%d' % (i, count), budget_amounts={ choice(budgets): amt } )) data_sess.flush() data_sess.commit() pp = pp.next data_sess.close() conn.close() # END DB update self.get('/payperiods') sleep(1)
def test_add_one(self): mock_sess = Mock() with patch('%s.inspect' % pbm) as mock_inspect: type(mock_inspect.return_value).session = mock_sess b1 = Budget(name='my_budg') t = Transaction(budget_amounts={b1: Decimal('10.00')}) assert len(t.budget_transactions) == 1 assert t.budget_transactions[0].transaction == t assert t.budget_transactions[0].budget == b1 assert t.budget_transactions[0].amount == Decimal('10.00') assert mock_sess.mock_calls == []
def test_none_but_existing(self): mock_sess = Mock() with patch('%s.inspect' % pbm) as mock_inspect: type(mock_inspect.return_value).session = mock_sess b1 = Budget(name='my_budg') b2 = Budget(name='my_budg2') t = Transaction(budget_amounts={ b1: Decimal('10.00'), b2: Decimal('90.00') }) assert len(t.budget_transactions) == 2 for bt in t.budget_transactions: assert isinstance(bt, BudgetTransaction) assert bt.transaction == t assert {bt.budget: bt.amount for bt in t.budget_transactions} == { b1: Decimal('10.00'), b2: Decimal('90.00') } mock_sess.reset_mock() with pytest.raises(AssertionError): t.set_budget_amounts({}) assert mock_sess.mock_calls == []
def test_add_three(self): mock_sess = Mock() with patch('%s.inspect' % pbm) as mock_inspect: type(mock_inspect.return_value).session = mock_sess b1 = Budget(name='my_budg') b2 = Budget(name='my_budg2') b3 = Budget(name='my_budg3') t = Transaction(budget_amounts={ b1: Decimal('50.00'), b2: Decimal('10.00'), b3: Decimal('40.00') }) assert len(t.budget_transactions) == 3 for bt in t.budget_transactions: assert isinstance(bt, BudgetTransaction) assert bt.transaction == t assert {bt.budget: bt.amount for bt in t.budget_transactions} == { b1: Decimal('50.00'), b2: Decimal('10.00'), b3: Decimal('40.00') } assert mock_sess.mock_calls == []
def test_80_issue201_setup(self, testdb): budg = Budget(name='6Periodic', is_periodic=True, description='6Periodic', starting_balance=Decimal('0.00')) testdb.add(budg) acct = testdb.query(Account).get(1) testdb.add( ScheduledTransaction(amount=Decimal('2.00'), description='B6 ST4', account=acct, budget=budg, day_of_month=11)) testdb.flush() testdb.commit()
def test_01_add(self, testdb): a = Account(description='First Bank Account', name='BankOne', ofx_cat_memo_to_name=True, ofxgetter_config_json='{"foo": "bar"}', vault_creds_path='secret/foo/bar/BankOne', acct_type=AcctType.Bank) testdb.add(a) a.set_balance(overall_date=datetime(2017, 4, 10, 12, 0, 0, tzinfo=UTC), ledger=1.0, ledger_date=datetime(2017, 4, 10, 12, 0, 0, tzinfo=UTC)) b = Budget(name='1Income', is_periodic=True, description='1Income', starting_balance=0.0, is_income=True) testdb.add(b) testdb.flush() testdb.commit()