コード例 #1
0
    def test_compute_residual(self):

        # Try with two accounts.
        residual = interpolate.compute_residual([
            P(None, "Assets:Bank:Checking", "105.50", "USD"),
            P(None, "Assets:Bank:Checking", "-194.50", "USD"),
            ])
        self.assertEqual(inventory.from_string("-89 USD"), residual.units())

        # Try with more accounts.
        residual = interpolate.compute_residual([
            P(None, "Assets:Bank:Checking", "105.50", "USD"),
            P(None, "Assets:Bank:Checking", "-194.50", "USD"),
            P(None, "Assets:Bank:Investing", "5", "AAPL"),
            P(None, "Assets:Bank:Savings", "89.00", "USD"),
            ])
        self.assertEqual(inventory.from_string("5 AAPL"), residual.units())
コード例 #2
0
    def test_get_incomplete_postings_pathological(self):
        meta = data.new_metadata(__file__, 0)

        # Test with no entries.
        entry = data.Transaction(meta, None, None, None, None, data.EMPTY_SET,
                                 data.EMPTY_SET, [])
        new_postings, has_inserted, errors, _, __ = booking_simple.get_incomplete_postings(
            entry, self.OPTIONS_MAP)
        self.assertFalse(has_inserted)
        self.assertEqual(0, len(new_postings))
        self.assertEqual(0, len(errors))

        # Test with only a single leg (and check that it does not balance).
        entry = data.Transaction(
            meta, None, None, None, None, data.EMPTY_SET, data.EMPTY_SET, [
                P(None, "Assets:Bank:Checking", "105.50", "USD"),
            ])
        (new_postings, has_inserted, errors, residual,
         tolerances) = booking_simple.get_incomplete_postings(
             entry, self.OPTIONS_MAP)
        self.assertFalse(has_inserted)
        self.assertEqual(1, len(new_postings))
        self.assertEqual(1 if ERRORS_ON_RESIDUAL else 0, len(errors))
        self.assertIsInstance(tolerances, dict)

        # Test with two legs that balance.
        entry = data.Transaction(
            meta, None, None, None, None, data.EMPTY_SET, data.EMPTY_SET, [
                P(None, "Assets:Bank:Checking", "105.50", "USD"),
                P(None, "Assets:Bank:Savings", "-105.50", "USD"),
            ])
        new_postings, has_inserted, errors, _, __ = booking_simple.get_incomplete_postings(
            entry, self.OPTIONS_MAP)
        self.assertFalse(has_inserted)
        self.assertEqual(2, len(new_postings))
        self.assertEqual(0, len(errors))

        # Test with two legs that do not balance.
        entry = data.Transaction(
            meta, None, None, None, None, data.EMPTY_SET, data.EMPTY_SET, [
                P(None, "Assets:Bank:Checking", "105.50", "USD"),
                P(None, "Assets:Bank:Savings", "-115.50", "USD"),
            ])
        new_postings, has_inserted, errors, _, __ = booking_simple.get_incomplete_postings(
            entry, self.OPTIONS_MAP)
        self.assertFalse(has_inserted)
        self.assertEqual(2, len(new_postings))
        self.assertEqual(1 if ERRORS_ON_RESIDUAL else 0, len(errors))

        # Test with only one auto-posting.
        entry = data.Transaction(
            meta, None, None, None, None, data.EMPTY_SET, data.EMPTY_SET, [
                P(None, "Assets:Bank:Checking", None, None),
            ])
        new_postings, has_inserted, errors, _, __ = booking_simple.get_incomplete_postings(
            entry, self.OPTIONS_MAP)
        self.assertFalse(has_inserted)
        self.assertEqual(0, len(new_postings))
        self.assertEqual(1, len(errors))

        # Test with an auto-posting where there is no residual.
        entry = data.Transaction(
            meta, None, None, None, None, data.EMPTY_SET, data.EMPTY_SET, [
                P(None, "Assets:Bank:Checking", "105.50", "USD"),
                P(None, "Assets:Bank:Savings", "-105.50", "USD"),
                P(None, "Assets:Bank:Balancing", None, None),
            ])
        new_postings, has_inserted, errors, _, __ = booking_simple.get_incomplete_postings(
            entry, self.OPTIONS_MAP)
        self.assertTrue(has_inserted)
        self.assertEqual(3, len(new_postings))
        self.assertEqual(1, len(errors))

        # Test with too many empty postings.
        entry = data.Transaction(
            meta, None, None, None, None, data.EMPTY_SET, data.EMPTY_SET, [
                P(None, "Assets:Bank:Checking", "105.50", "USD"),
                P(None, "Assets:Bank:Savings", "-106.50", "USD"),
                P(None, "Assets:Bank:BalancingA", None, None),
                P(None, "Assets:Bank:BalancingB", None, None),
            ])
        new_postings, has_inserted, errors, _, __ = booking_simple.get_incomplete_postings(
            entry, self.OPTIONS_MAP)
        self.assertTrue(has_inserted)
        self.assertEqual(3, len(new_postings))
        self.assertEqual(1, len(errors))