<commit_before>from __future__ import unicode_literals
import babel

from decimal import Decimal, ROUND_UP
from unittest import TestCase
from wtforms.form import _unset_value
from wtforms import Form
from wtforms.fields.core import DecimalField


class DummyPostData(dict):
    def getlist(self, key):
        v = self[key]
        if not isinstance(v, (list, tuple)):
            v = [v]
        return v


class TestLocaleDecimal(TestCase):
    class F(Form):
        LOCALES = ['hi_IN', 'en_US']
        a = DecimalField(use_locale=True)

    def _format_test(self, expected, val, LOCALES=_unset_value):
        form = self.F(LOCALES=LOCALES, a=Decimal(val))
        self.assertEqual(form.a._value(), expected)

    def test_typeerror(self):
        def build(**kw):
            form = self.F()
            f = DecimalField(
                use_locale=True,
                _form=form,
                _name='a',
                _translations=form._get_translations(),
                **kw
            )
            print repr(f)

        self.assertRaises(TypeError, build, places=2)
        self.assertRaises(TypeError, build, rounding=ROUND_UP)

    def test_formatting(self):
        val = Decimal('123456.789')
        neg = Decimal('-5.2')
        self._format_test('1,23,456.789', val)
        self._format_test('-12,52,378.2', '-1252378.2')
        self._format_test('123,456.789', val, ['en_US'])
        self._format_test('-5.2', neg, ['en_US'])
        self._format_test('123.456,789', val, ['es_ES'])
        self._format_test('123.456,789', val, ['de_DE'])
        self._format_test('-5,2', neg, ['de_DE'])
        self._format_test("-12'345.2", '-12345.2', ['de_CH'])

    def _parse_test(self, raw_val, expected, LOCALES=_unset_value):
        form = self.F(DummyPostData(a=raw_val), LOCALES=LOCALES)
        if not form.validate():
            raise AssertionError(
                'Expected value %r to parse as a decimal, instead got %r' % (
                    raw_val, form.a.errors,
                )
            )
        self.assertEqual(form.a.data, expected)

    def _fail_parse(self, raw_val, expected_error, LOCALES=_unset_value):
        form = self.F(DummyPostData(a=raw_val), LOCALES=LOCALES)
        assert not form.validate()<commit_msg>Fix tests in python3<commit_after>from __future__ import unicode_literals
import babel

from decimal import Decimal, ROUND_UP
from unittest import TestCase
from wtforms.form import _unset_value
from wtforms import Form
from wtforms.fields.core import DecimalField


class DummyPostData(dict):
    def getlist(self, key):
        v = self[key]
        if not isinstance(v, (list, tuple)):
            v = [v]
        return v


class TestLocaleDecimal(TestCase):
    class F(Form):
        LOCALES = ['hi_IN', 'en_US']
        a = DecimalField(use_locale=True)

    def _format_test(self, expected, val, LOCALES=_unset_value):
        form = self.F(LOCALES=LOCALES, a=Decimal(val))
        self.assertEqual(form.a._value(), expected)

    def test_typeerror(self):
        def build(**kw):
            form = self.F()
            DecimalField(
                use_locale=True,
                _form=form,
                _name='a',
                _translations=form._get_translations(),
                **kw
            )

        self.assertRaises(TypeError, build, places=2)
        self.assertRaises(TypeError, build, rounding=ROUND_UP)

    def test_formatting(self):
        val = Decimal('123456.789')
        neg = Decimal('-5.2')
        self._format_test('1,23,456.789', val)
        self._format_test('-12,52,378.2', '-1252378.2')
        self._format_test('123,456.789', val, ['en_US'])
        self._format_test('-5.2', neg, ['en_US'])
        self._format_test('123.456,789', val, ['es_ES'])
        self._format_test('123.456,789', val, ['de_DE'])
        self._format_test('-5,2', neg, ['de_DE'])
        self._format_test("-12'345.2", '-12345.2', ['de_CH'])

    def _parse_test(self, raw_val, expected, LOCALES=_unset_value):
        form = self.F(DummyPostData(a=raw_val), LOCALES=LOCALES)
        if not form.validate():
            raise AssertionError(
                'Expected value %r to parse as a decimal, instead got %r' % (
                    raw_val, form.a.errors,
                )
            )
        self.assertEqual(form.a.data, expected)

    def _fail_parse(self, raw_val, expected_error, LOCALES=_unset_value):
        form = self.F(DummyPostData(a=raw_val), LOCALES=LOCALES)
        assert not form.validate()
{'fraction_non_alphanumeric': 0.12933893433561794, 'fraction_numerical': 0.035595843466725625, 'mean_word_length': 2.4272727272727272, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3586946', 'n_tokens_mistral': 1581, 'n_tokens_neox': 1462, 'n_words': 299}