Example #1
0
    def test_currency_accessor(self):
        # In the old code, accessing `myinstance.myfield_currency` would work.
        # Here we test for that and emulate the old behavior. This should
        # probably not be part of the official API and when removed, this test
        # can be removed as well.

        created = SimpleMoneyModel.objects.create(name="zero dollars",
                                                  price=Money(0))
        self.assertEqual(created.price_currency, "XXX")
        self.assertEqual(created.price.currency, "XXX")

        created = SimpleMoneyModel.objects.create(name="zero dollars",
                                                  price=Money(0, "USD"))
        self.assertEqual(created.price_currency, "USD")
        self.assertEqual(created.price.currency, "USD")

        # This actually wouldn't work in the old code without a round trip to the db
        created.price_currency = 'EUR'
        self.assertEqual(created.price_currency, "EUR")
        self.assertEqual(created.price.currency, "EUR")

        created.save()
        created = SimpleMoneyModel.objects.get(pk=created.pk)
        self.assertEqual(created.price_currency, "EUR")
        self.assertEqual(created.price.currency, "EUR")
Example #2
0
    def test_price_attribute(self):
        e = SimpleMoneyModel()
        e.price = Money(3, "BGN")
        self.assertEqual(e.price, Money(3, "BGN"))

        e.price = Money.from_string("BGN 5.0")
        self.assertEqual(e.price, Money(5, "BGN"))
Example #3
0
 def test_subtract_fail(self):
     """
     An exception is thrown when subtracting money with different currencies
     """
     eur = Money(0, Currency('eur'))
     usd = Money(0, Currency('usd'))
     with self.assertRaises(ValueError):
         eur - usd
Example #4
0
    def test_zero_edge_case(self):
        created = SimpleMoneyModel.objects.create(name="zero dollars",
                                                  price=Money(0, "USD"))
        self.assertEqual(created.price, Money(0, "USD"))

        ent = SimpleMoneyModel.objects.filter(
            price__exact=Money(0, "USD")).get()
        self.assertEqual(ent.price, Money(0, "USD"))
Example #5
0
    def test_less_or_equal_than(self):
        """
        It returns true when money amount is less or equal than given money object
        """
        money1 = Money(0, self.currency)
        money2 = Money(0, self.currency)

        self.assertTrue(money1 <= money2)
Example #6
0
    def test_greater_than(self):
        """
        It returns true when money amount is greater than given money object
        """
        money1 = Money(10, self.currency)
        money2 = Money(0, self.currency)

        self.assertTrue(money1 > money2)
Example #7
0
    def test_greater_or_equal_than(self):
        """
        It returns true when money amount is greater or equal to given money object
        """
        money1 = Money(0, self.currency)
        money2 = Money(0, self.currency)

        self.assertTrue(money1 >= money2)
Example #8
0
    def test_equals(self):
        """
        It returns true when two money objects are equal
        """
        money1 = Money(0, self.currency)
        money2 = Money(0, self.currency)

        self.assertTrue(money1 == money2)
Example #9
0
    def test_price_amount_to_string(self):
        e1 = SimpleMoneyModel(price=Money('200', 'JPY'))
        e2 = SimpleMoneyModel(price=Money('200.0', 'JPY'))

        self.assertEqual(str(e1.price), "JPY 200")

        self.assertEqual(str(e1.price.amount), "200")

        self.assertEqual(str(e2.price.amount), "200.0")
Example #10
0
    def test_subtract(self):
        """
        It subtracts two money objects
        """
        for amount in [[10, 10, 0], [0, 0, 0], [10, 20, -10]]:
            with self.subTest():
                money1 = Money(amount[0], self.currency)
                money2 = Money(amount[1], self.currency)
                difference = money1 - money2

                self.assertEqual(amount[2], difference.amount)
Example #11
0
    def test_add(self):
        """
        It adds two money objects
        """
        for amount in [[10, 10, 20], [0, 0, 0], [-10, 10, 0]]:
            with self.subTest():
                money1 = Money(amount[0], self.currency)
                money2 = Money(amount[1], self.currency)
                sum = money1 + money2

                self.assertEqual(amount[2], sum.amount)
Example #12
0
def gui_text():
    c = Configs()

    currency = getattr(Currency, c.money)

    update_date = datetime.datetime.now().strftime(c.date_frmt)
    update_hour = datetime.datetime.now().strftime(c.hour_frmt)

    placeholder, one_btc_value = convert_to_money(1, c.money)
    one_btc_value_frmt = Money(str(one_btc_value), currency). \
        format(c.money_frmt)

    start_replacements = {
        "title": c.title,
        "update_date": update_date,
        "update_time": update_hour,
        "btc_value": one_btc_value_frmt,
        "currency": c.money
    }

    txt = ["\n".join(c.str_title).format(**start_replacements)]

    for wallet in c.wallets:
        wallet_name = wallet["name"]
        wallet_addr = wallet['address']
        balance_btc = final_balance(wallet_addr)
        if balance_btc is not None:
            balance_money, btc_value = convert_to_money(balance_btc, c.money)
            balance_money = Money(str(balance_money),
                                  currency).format(c.money_frmt)

            wallet_replacements = {
                "btc_balance": balance_btc,
                "money_balance": balance_money,
                "wallet": wallet_name,
                "wallet_address": wallet_addr
            }

            txt.append("\n".join(
                c.str_wallet_view).format(**wallet_replacements,
                                          **start_replacements))
        else:
            wallet_replacements = {
                "wallet": wallet_name,
                "wallet_address": wallet_addr
            }

            txt.append("\n".join(
                c.str_fail_wallet_view).format(**wallet_replacements,
                                               **start_replacements))

    txt.append("\n".join(c.str_extra_content).format(**start_replacements))

    return ''.join(txt)
Example #13
0
    def test_subtract(self):
        assert Money('3.5') - Money('1.25') == Money('2.25')
        assert Money('4') - Money('5.5') == Money('-1.5')

        with pytest.raises(CurrencyMismatchError):
            Money('3.5', Currency.EUR) - Money('1.8', Currency.GBP)

        with pytest.raises(InvalidOperandError):
            Money('5.5') - 6.32

        with pytest.raises(InvalidOperandError):
            666.32 - Money('5.5')
Example #14
0
    def test_creation_parsed(self):
        result = Money('XXX -10.50')
        self.assertEqual(result.amount, Decimal("-10.50"))
        self.assertEqual(result.currency.code, 'XXX')

        result = Money('USD -11.50')
        self.assertEqual(result.amount, Decimal("-11.50"))
        self.assertEqual(result.currency.code, 'USD')

        result = Money('JPY -12.50')
        self.assertEqual(result.amount, Decimal("-12.50"))
        self.assertEqual(result.currency.code, 'JPY')
Example #15
0
    def test_not_equals(self):
        """
        It returns false when two money objects are not equal
        """
        money1 = Money(0, self.currency)
        money2 = Money(10, self.currency)

        self.assertFalse(money1 == money2)

        money1 = Money(0, Currency('EUR'))
        money2 = Money(0, Currency('USD'))

        self.assertFalse(money1 == money2)
Example #16
0
    def test_greater_than_or_equal(self):
        assert Money('3.5') >= Money('1.2')
        assert not Money('5.13') >= Money('104.2')
        assert Money('2.2') >= Money('2.2')

        with pytest.raises(CurrencyMismatchError):
            Money('3.5', Currency.EUR) >= Money('1.2', Currency.GBP)

        with pytest.raises(InvalidOperandError):
            Money('3.5') >= 1.2
Example #17
0
    def test_equal(self):
        assert Money('3.5') == Money('3.5')
        assert Money('4.0', Currency.GBP) == Money('4.0', Currency.GBP)
        assert not Money('6.9') == Money('43')

        with pytest.raises(CurrencyMismatchError):
            Money('3.5', Currency.EUR) == Money('3.5', Currency.GBP)

        with pytest.raises(InvalidOperandError):
            Money('5.5') == 5.5
Example #18
0
    def test_less_than(self):
        assert Money('1.2') < Money('3.5')
        assert not Money('104.2') < Money('5.13')
        assert not Money('2.2') < Money('2.2')

        with pytest.raises(CurrencyMismatchError):
            Money('1.2', Currency.GBP) < Money('3.5', Currency.EUR)

        with pytest.raises(InvalidOperandError):
            1.2 < Money('3.5')
Example #19
0
    def test_not_equal(self):
        assert Money('3.5') != Money('46.44')
        assert Money('4.0', Currency.GBP) != Money('12.01', Currency.GBP)
        assert not Money('6.9') != Money('6.9')

        with pytest.raises(CurrencyMismatchError):
            Money('3.5', Currency.EUR) != Money('23', Currency.GBP)

        with pytest.raises(InvalidOperandError):
            Money('5.5') != 666.32
Example #20
0
    def test_creation(self):
        """
        We should be able to create a money object with inputs
        similar to a Decimal type
        """
        result = Money(10, 'USD')
        self.assertEqual(result.amount, 10)

        result = Money(-10, 'USD')
        self.assertEqual(result.amount, Decimal("-10"))

        result = Money(Decimal("10"), 'USD')
        self.assertEqual(result.amount, Decimal("10"))

        result = Money(Decimal("-10"), 'USD')
        self.assertEqual(result.amount, Decimal("-10"))

        result = Money('10.50', 'USD')
        self.assertEqual(result.amount, Decimal("10.50"))

        result = Money('-10.50', 'USD')
        self.assertEqual(result.amount, Decimal("-10.50"))

        result = Money(u'10.50', u'USD')
        self.assertEqual(result.amount, Decimal("10.50"))

        result = Money(u'-10.50', u'USD')
        self.assertEqual(result.amount, Decimal("-10.50"))
Example #21
0
    def test_assign(self):
        price = Money(100, "USD")
        ent = SimpleMoneyModel(name='test',
                               price=price.amount,
                               price_currency=price.currency)
        ent.save()
        self.assertEqual(ent.price, Money(100, "USD"))

        ent.price = Money(10, "USD")
        ent.save()
        self.assertEqual(ent.price, Money(10, "USD"))

        ent_same = SimpleMoneyModel.objects.get(pk=ent.id)
        self.assertEqual(ent_same.price, Money(10, "USD"))
Example #22
0
class MoneyModelDefaultMoneyUSD(models.Model):
    name = models.CharField(max_length=100)
    price = fields.MoneyField(max_digits=12,
                              decimal_places=3,
                              default=Money("123.45", "USD"))
    zero = fields.MoneyField(max_digits=12,
                             decimal_places=3,
                             default=Money("0", "USD"))

    def __str__(self):
        return self.name + " " + str(self.price)

    class Meta:
        app_label = 'tests'
Example #23
0
 def compress(self, data_list):
     """
     Take the two values from the request and return a single data value
     """
     if data_list:
         return Money(*data_list)
     return None
Example #24
0
 def test_create_money(self):
     """
     Create new money object
     """
     for amount in [0, 10, 1000, -100]:
         with self.subTest():
             money = Money(amount, self.currency)
Example #25
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)
Example #26
0
def model_from_db_view(request, amount='0', currency='XXX'):
    # db roundtrip
    instance = SimpleMoneyModel.objects.create(price=Money(amount, currency))
    instance = SimpleMoneyModel.objects.get(pk=instance.pk)

    money = instance.price
    return render_to_response('view.html', {'money': money})
Example #27
0
    def test_return_currency_object(self):
        """
        It returns currecy object
        """
        money = Money(10, self.currency)
        currency = money.currency

        self.assertIsInstance(currency, Currency)
Example #28
0
    def test_creation_unspecified_amount(self):
        """
        Same thing as above but with the unspecified 'xxx' currency
        """

        result = Money(currency='USD')
        self.assertEqual(result.amount, 0)
        self.assertEqual(result.currency.code, 'USD')
Example #29
0
class ParametrizedDefaultAsMoneyModel(models.Model):
    """ The simplest possible declaration with a Money object """
    value = fields.MoneyField(max_digits=12,
                              decimal_places=3,
                              default=Money(100, 'JPY'))

    def expected_value(self):
        return Money('100', 'JPY')
Example #30
0
def cents_to_brl(value):
    if value:
        value = int(value)
        # valor com centavos em float
        value = float(value) / 100
        m = Money(value, "BRL")
        return m.format("pt_BR", "ยค #,##0.00")
    return None