def test_create(self):
        assert Currency(3) == Decimal('3')

        assert Currency(3.14) == Decimal('3.14')
        assert Currency(3.141) == Decimal('3.14')
        assert Currency(3.145) == Decimal('3.15')

        assert Currency('3.14') == Decimal('3.14')
        assert Currency('3.141') == Decimal('3.14')
        assert Currency('3.145') == Decimal('3.15')

        assert Currency(Decimal('3.14')) == Decimal('3.14')
        assert Currency(Decimal('3.141')) == Decimal('3.14')
        assert Currency(Decimal('3.145')) == Decimal('3.15')
Exemple #2
0
    def test_widget(self):
        form = self.CurrencyForm({'currency': Currency('52.23')})
        rendered = force_text(form['currency'])

        self.assertTrue(
            u'''<span class="input-group-addon">€</span>''' in rendered)
        # Make sure that the value does not contain the currency symbol
        self.assertTrue('''value="52.23"''' in rendered)
 def test_format(self):
     with self.settings(USE_POINTS=False, REAL_WORLD_CURRENCY_CODE='USD'):
         pi = Currency(3.14)
         assert '{}'.format(pi) == '$3.14'
         assert '{:s}'.format(pi) == '$3.14'
         assert isinstance(u'{}'.format(pi), six.text_type)
         assert '{:.1f}'.format(pi) == '3.1'
         assert '{:e}'.format(pi) == '3.14e+0'
 def test_currency_non_ascii_character(self):
     # https://github.com/oTree-org/otree-core/issues/387
     with self.settings(REAL_WORLD_CURRENCY_CODE='EUR', USE_POINTS=False):
         value = Currency(1)
         template = Template('''{{money}}''')
         ctx = Context({"money": c(1)})
         rendered = template.render(ctx)
         self.assertIn('€', rendered)
         self.assertEquals(rendered, six.text_type(value))
    def test_float_arithmetic(self):
        pi = Currency(3.14)

        assert pi + 0.2 == 3.34
        assert pi - 0.2 == 2.94
        assert pi * 0.2 == 0.63
        assert pi / 1.5 == 2.09

        # We coerse to 2 digits before operation
        assert pi + 0.005 == 3.15
        assert pi - 0.005 == 3.13
    def test_arithmetic(self):
        pi = Currency(3.14)
        e = Currency(2.78)

        # NOTE: we test this way to check that bools are returned
        assert (pi == 3.14) is True
        assert (pi == Decimal('3.14')) is True
        assert (pi == 3) is False
        assert (pi != 3) is True
        assert (Currency(3) == 3) is True

        assert (3 == pi) is False
        assert (3 != pi) is True

        assert pi + e == 5.92
        assert pi - e == 0.36

        assert -pi == -3.14
        assert +pi == 3.14
        assert abs(pi) == 3.14
        assert abs(-pi) == 3.14
Exemple #7
0
class CurrencyField(BaseCurrencyField):
    MONEY_CLASS = Currency
    auto_submit_default = Currency(0)

    def formfield(self, **kwargs):
        import otree.forms
        defaults = {
            'form_class': otree.forms.CurrencyField,
            'choices_form_class': otree.forms.CurrencyChoiceField,
        }
        defaults.update(kwargs)
        return super().formfield(**defaults)
    def test_int_arithmetic(self):
        pi = Currency(3.14)

        assert pi + 1 == 4.14
        assert pi - 1 == 2.14
        assert pi * 2 == 6.28
        assert pi / 3 == 1.05

        assert 1 + pi == 4.14
        assert 1 - pi == -2.14
        assert 2 * pi == 6.28
        assert 9 / pi == 2.87
def currency_range(first, last, increment):
    assert last >= first
    if Currency(increment) == 0:
        if settings.USE_POINTS:
            setting_name = 'POINTS_DECIMAL_PLACES'
        else:
            setting_name = 'REAL_WORLD_CURRENCY_DECIMAL_PLACES'
        raise ValueError(
            ('currency_range() step argument must not be zero. '
             'Maybe your {} setting is '
             'causing it to be rounded to 0.').format(setting_name))

    assert increment > 0  # not negative

    values = []
    current_value = Currency(first)

    while True:
        if current_value > last:
            return values
        values.append(current_value)
        current_value += increment
Exemple #10
0
 def test_str(self):
     with self.settings(USE_POINTS=False):
         with self.settings(REAL_WORLD_CURRENCY_CODE='USD'):
             # should truncate
             self.assertEqual(str(Currency(3.141)), '$3.14')
             self.assertEqual(str(Currency(3.00)), '$3.00')
         with self.settings(REAL_WORLD_CURRENCY_CODE='EUR'):
             self.assertEqual(str(Currency(3.14)), '€3.14')
             self.assertEqual(str(Currency(3.00)), '€3.00')
         with self.settings(REAL_WORLD_CURRENCY_CODE='USD',
                            REAL_WORLD_CURRENCY_DECIMAL_PLACES=3):
             self.assertEqual(str(Currency(3.141)), '$3.141')
     with self.settings(USE_POINTS=True):
         self.assertEqual(str(Currency(3)), '3 points')
         with self.settings(POINTS_DECIMAL_PLACES=2):
             self.assertEqual(str(Currency(3)), '3.00 points')
         with self.settings(POINTS_CUSTOM_NAME='tokens'):
             self.assertEqual(str(Currency(3)), '3 tokens')
Exemple #11
0
def get_choices_field(fa, is_currency=False, is_boolean=False):
    if is_boolean:
        fa.setdefault('choices', [(True, gettext('Yes')),
                                  (False, gettext('No'))])
        fa['coerce'] = bool_from_form_value
    if 'choices' in fa:
        if is_currency:
            before = fa['choices']
            if isinstance(before[0], (list, tuple)):
                after = [(to_dec(v), label) for (v, label) in before]
            else:
                after = [(to_dec(v), Currency(v)) for v in before]
            fa['choices'] = after
            fa['coerce'] = Currency

        widget = fa.pop('widget', None)
        if widget:
            widget = type(widget)
        return {
            widgets.RadioSelect: fields.RadioField,
            widgets.RadioSelectHorizontal: fields.RadioFieldHorizontal,
            widgets.TextInput: fields.StringField,
            None: fields.DropdownField,
        }[widget](**fa)
Exemple #12
0
 def test_precision(self):
     assert Currency(1000) * 1.001 == 1001
     assert Currency('1.001') * 1000 == 1000
Exemple #13
0
def currency_filter(val):
    return Currency(val)
Exemple #14
0
class CurrencyType(BaseCurrencyType):
    MONEY_CLASS = Currency


class RealWorldCurrencyType(BaseCurrencyType):
    MONEY_CLASS = RealWorldCurrency


AUTO_SUBMIT_DEFAULTS = {
    st.Boolean: False,
    st.Integer: 0,
    st.Float: 0,
    st.String: '',
    st.Text: '',
    RealWorldCurrencyType: Currency(0),
    CurrencyType: RealWorldCurrency(0),
}


def wrap_column(coltype,
                *,
                initial=None,
                null=True,
                **form_props) -> OTreeColumn:
    if 'default' in form_props:
        raise Exception(
            "Don't use default= in model fields. Use initial= instead.")
    col = OTreeColumn(coltype, default=initial, nullable=null)
    col.form_props = form_props
Exemple #15
0
 def test_pow(self):
     m = Currency(2)
     assert m**7 == 128
     assert pow(m, 7, 3) == 2
Exemple #16
0
 def test_higher_precision(self):
     with self.settings(USE_POINTS=False,
                        REAL_WORLD_CURRENCY_DECIMAL_PLACES=3):
         assert Currency('1.001') * 1000 == 1001
Exemple #17
0
 def test_pickle(self):
     pi = Currency(3.14)
     assert pickle.loads(pickle.dumps(pi)) == pi
     assert pickle.loads(pickle.dumps(pi, -1)) == pi
Exemple #18
0
 def test_copy(self):
     pi = Currency(3.14)
     assert pi == copy.copy(pi)
     assert pi == copy.deepcopy(pi)
Exemple #19
0
 def test_hashing(self):
     x = {Currency(3): 1}
Exemple #20
0
 def process_formdata(self, valuelist):
     if valuelist and valuelist[0]:
         data = Currency(valuelist[0])
     else:
         data = None
     self.data = data
Exemple #21
0
class CurrencyField(BaseCurrencyField):
    MONEY_CLASS = Currency
    auto_submit_default = Currency(0)
Exemple #22
0
 def test_arithmetic_returns_money_instance(self):
     assert type(Currency(3) + 2) is Currency
     assert type(3 + Currency(2)) is Currency
     assert type(Currency(3) - 2) is Currency
     assert type(3 - Currency(2)) is Currency
     assert type(Currency(3) * 2) is Currency
     assert type(3 * Currency(2)) is Currency
     assert type(floordiv(Currency(3), 2)) is Currency
     assert type(floordiv(3, Currency(2))) is Currency
     assert type(truediv(Currency(3), 2)) is Currency
     assert type(truediv(3, Currency(2))) is Currency
     assert type(Currency(3)**2) is Currency
     assert type(2**Currency(3)) is Currency
     assert type(+Currency(2)) is Currency
     assert type(-Currency(2)) is Currency
     assert type(abs(Currency(2))) is Currency
Exemple #23
0
def get_payoff_plus_participation_fee(session, participant_values_dict):
    payoff = Currency(participant_values_dict['payoff'])
    return session._get_payoff_plus_participation_fee(payoff)
Exemple #24
0
 def test_repr(self):
     pi = Currency('3.14')
     assert repr(pi) == 'Currency(3.14)'
Exemple #25
0
def c(val):
    return Currency(val)
Exemple #26
0
class CurrencyFieldTestModel(otree.db.models.Model):
    currency_with_default_value_zero = models.CurrencyField(
        initial=Currency(0), min=0)