def test_cash_flow_filter_example_simple_predicate(self, transactions): assume(all(not math.isnan(t.value) for t in transactions)) assume(all(transaction.value > 0 for transaction in transactions)) cf = cashflow.CashFlow(transactions=transactions) assert len(cf.filter(lambda t: t.value > 0)) == len(transactions)
def test_net_value(self, transactions): assume(all(not math.isnan(t.value) for t in transactions)) cf = cashflow.CashFlow(transactions=transactions) self.assertAlmostEqual( cf.net_value, sum(t.value for t in transactions) )
def test_create_an_empty_cashflow(self): cf = cashflow.CashFlow() assert len(cf) == 0 assert list(cf.transactions) == [] assert cf.net_value == decimal.Decimal(0)
def test_seriaize_cash_flow(self, transactions): cf = cashflow.CashFlow(transactions=transactions) expected_data = {'cashflow': [t.serialize() for t in transactions]} self.assertDictEqual(cf.serialize(), expected_data) self.assertEqual( serialize_utils.json_dumps(cf.serialize()), serialize_utils.json_dumps(expected_data) )
def test_append_duplicate_transaction(self, transaction): cf = cashflow.CashFlow() cf.append(transaction) with pytest.raises(ValueError) as cm: cf.append(transaction) expected_message = ( '{} already added to the cash flow'.format(repr(transaction)) ) assert str(cm.value) == expected_message
def test_cash_flow_with_one_transaction(self, transaction): assume(not math.isnan(transaction.value)) cf = cashflow.CashFlow() cf.append(transaction) assert len(cf) == 1 assert tuple(cf.transactions) == (transaction, ) self.assertAlmostEqual( cf.net_value, sum(t.value for t in (transaction,)) )
def test_dunder_getitem(self, transactions): cf = cashflow.CashFlow() with pytest.raises(IndexError): cf[0] assert len(cf[0:100]) == 0 for transaction in transactions: cf.append(transaction) assert tuple(cf[0].transactions)[0] == transactions[0] assert tuple(cf[0:5].transactions) == transactions[0:5] assert len(tuple(cf[0:5].transactions)) == len(transactions[0:5]) assert tuple(cf[::-1].transactions) == tuple(reversed(transactions)) assert len(tuple(cf[::-1].transactions)) == len(transactions[::-1])
def test_cash_flow_filter_composition(self, transactions): assume(all(not math.isnan(t.value) for t in transactions)) assume(all(0 < t.value <= 100 for t in transactions)) cf = cashflow.CashFlow(transactions=transactions).filter( lambda t: t.value > 0 ) assert len(cf) == len(transactions) # Duplicate the filter to ensure the value keeps the same cf = cf.filter(lambda t: t.value > 0).filter(lambda t: t.value > 0) assert len(cf) == len(transactions) cf = cf.filter(lambda t: t.value == 100) transactions_values_greater_than_100 = tuple( t for t in transactions if t.value == 100 ) assert tuple(cf.transactions) == transactions_values_greater_than_100
def test_dunder_contains(self, transactions): cf = cashflow.CashFlow(transactions=transactions) self.assertIn(transactions[0], cf)
def test_dunder_iter(self, transactions): cf = cashflow.CashFlow(transactions=transactions) for transaction in cf: assert transaction in transactions
def test_dunder_len(self, transactions): cf = cashflow.CashFlow(transactions=transactions) assert len(cf) == len(transactions)