File: test_decimal.py

package info (click to toggle)
wtforms 3.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,064 kB
  • sloc: python: 5,264; makefile: 27; sh: 17
file content (43 lines) | stat: -rw-r--r-- 1,282 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from decimal import Decimal
from decimal import ROUND_DOWN
from decimal import ROUND_UP

from tests.common import DummyPostData
from wtforms.fields import DecimalField
from wtforms.form import Form


def make_form(name="F", **fields):
    return type(str(name), (Form,), fields)


def test_decimal_field():
    F = make_form(a=DecimalField())
    form = F(DummyPostData(a="2.1"))
    assert form.a.data == Decimal("2.1")
    assert form.a._value() == "2.1"
    form.a.raw_data = None
    assert form.a._value() == "2.10"
    assert form.validate()
    form = F(DummyPostData(a="2,1"), a=Decimal(5))
    assert form.a.data is None
    assert form.a.raw_data == ["2,1"]
    assert not form.validate()
    form = F(DummyPostData(a="asdf"), a=Decimal(".21"))
    assert form.a._value() == "asdf"
    assert not form.validate()


def test_quantize():
    F = make_form(
        a=DecimalField(places=3, rounding=ROUND_UP), b=DecimalField(places=None)
    )
    form = F(a=Decimal("3.1415926535"))
    assert form.a._value() == "3.142"
    form.a.rounding = ROUND_DOWN
    assert form.a._value() == "3.141"
    assert form.b._value() == ""
    form = F(a=3.14159265, b=72)
    assert form.a._value() == "3.142"
    assert isinstance(form.a.data, float)
    assert form.b._value() == "72"