Exemple #1
0
    def test_allocation_fail(self):
        """
        An exception is thrown when allocation with invalid ratios
        """
        money = Money(10, self.currency)

        with self.assertRaises(ValueError):
            money.allocate([])

        with self.assertRaises(ValueError):
            money.allocate([-1])
Exemple #2
0
    def test_allocation(self):
        """
        It allocates money amount to given ratios
        """
        data = [
            # amount, ratios, amount_per_ratio
            [100, [1, 1, 1], [34, 33, 33]],
            [100, [1], [100]],
            [101, [1, 1, 1], [34, 34, 33]],
            [101, [3, 7], [30, 71]],
            [101, [7, 3], [71, 30]],
            [5, [3, 7], [2, 3]],
            [5, [7, 3], [4, 1]],
            [5, [7, 3, 0], [4, 1, 0]],
            [2, [1, 1, 1], [1, 1, 0]],
            [1, [1, 1], [1, 0]],
            [-5, [7, 3], [-3, -2]],
        ]

        for amount, ratios, result in data:
            with self.subTest():
                money = Money(amount, self.currency)
                allocation = money.allocate(ratios)

                for index, money in enumerate(allocation):
                    self.assertEqual(result[index], money.amount)