Ejemplo n.º 1
0
    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')
Ejemplo n.º 2
0
 def test_none(self):
     mock_sess = Mock()
     with patch('%s.inspect' % pbm) as mock_inspect:
         type(mock_inspect.return_value).session = mock_sess
         t = Transaction()
         with pytest.raises(AssertionError):
             t.set_budget_amounts({})
     assert mock_sess.mock_calls == []
Ejemplo n.º 3
0
    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
            trans = db_session.query(Transaction).get(int(data['id']))
            if trans is None:
                raise RuntimeError("Error: no Transaction with ID "
                                   "%s" % data['id'])
            if trans.reconcile is not None:
                raise RuntimeError(
                    "Transaction %d is already reconciled; cannot be edited."
                    "" % trans.id)
            action = 'updating Transaction ' + data['id']
        else:
            trans = Transaction()
            action = 'creating new Transaction'
        trans.description = data['description'].strip()
        trans.date = datetime.strptime(data['date'], '%Y-%m-%d').date()
        trans.account_id = int(data['account'])
        trans.notes = data['notes'].strip()
        budg_amts = {}
        for bid, budg_amt in data['budgets'].items():
            budg = db_session.query(Budget).get(int(bid))
            budg_amts[budg] = Decimal(budg_amt)
        trans.set_budget_amounts(budg_amts)
        logger.info('%s: %s', action, trans.as_dict)
        db_session.add(trans)
        db_session.commit()
        return {
            'success_message':
            'Successfully saved Transaction %d  in database.'
            '' % trans.id,
            'success':
            True,
            'trans_id':
            trans.id
        }
Ejemplo n.º 4
0
 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 == []