-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathtest_decimal.py
More file actions
63 lines (53 loc) · 2.32 KB
/
Copy pathtest_decimal.py
File metadata and controls
63 lines (53 loc) · 2.32 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from decimal import Decimal
import pytest
from sqlobject import DecimalCol, DecimalStringCol, SQLObject, UnicodeCol
from sqlobject.tests.dbtest import setupClass, supports
########################################
# Decimal columns
########################################
try:
support_decimal_column = supports('decimalColumn')
except NameError:
# The module was imported during documentation building
pass
else:
if not support_decimal_column:
pytestmark = pytest.mark.skip("These tests require Decimal support")
class DecimalTable(SQLObject):
name = UnicodeCol(length=255)
col1 = DecimalCol(size=6, precision=4)
col2 = DecimalStringCol(size=6, precision=4)
col3 = DecimalStringCol(size=6, precision=4, quantize=True)
def test_1_decimal():
setupClass(DecimalTable)
d = DecimalTable(name='test', col1=21.12, col2='10.01', col3='10.01')
# psycopg2 returns float as Decimal
if isinstance(d.col1, Decimal):
assert d.col1 == Decimal("21.12")
else:
assert d.col1 == 21.12
assert d.col2 == Decimal("10.01")
assert DecimalTable.sqlmeta.columns['col2'].to_python(
'10.01', d._SO_validatorState) == Decimal("10.01")
assert DecimalTable.sqlmeta.columns['col2'].from_python(
'10.01', d._SO_validatorState) == "10.01"
assert d.col3 == Decimal("10.01")
assert DecimalTable.sqlmeta.columns['col3'].to_python(
'10.01', d._SO_validatorState) == Decimal("10.01")
assert DecimalTable.sqlmeta.columns['col3'].from_python(
'10.01', d._SO_validatorState) == "10.0100"
def test_2_decimal():
setupClass(DecimalTable)
d = DecimalTable(name='test', col1=Decimal("21.12"),
col2=Decimal('10.01'), col3=Decimal('10.01'))
assert d.col1 == Decimal("21.12")
assert d.col2 == Decimal("10.01")
assert DecimalTable.sqlmeta.columns['col2'].to_python(
Decimal('10.01'), d._SO_validatorState) == Decimal("10.01")
assert DecimalTable.sqlmeta.columns['col2'].from_python(
Decimal('10.01'), d._SO_validatorState) == "10.01"
assert d.col3 == Decimal("10.01")
assert DecimalTable.sqlmeta.columns['col3'].to_python(
Decimal('10.01'), d._SO_validatorState) == Decimal("10.01")
assert DecimalTable.sqlmeta.columns['col3'].from_python(
Decimal('10.01'), d._SO_validatorState) == "10.0100"