-
Notifications
You must be signed in to change notification settings - Fork 336
Expand file tree
/
Copy pathtest_formats.py
More file actions
162 lines (136 loc) · 4.4 KB
/
test_formats.py
File metadata and controls
162 lines (136 loc) · 4.4 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import doctest
import re
import datascience as ds
from datascience import formats
import os
import time
import pytest
def assert_equal(string1, string2):
string1, string2 = str(string1), str(string2)
whitespace = re.compile(r'\s')
purify = lambda s: whitespace.sub('', s)
assert purify(string1) == purify(string2), "\n%s\n!=\n%s" % (string1, string2)
def test_doctests():
results = doctest.testmod(formats, optionflags=doctest.NORMALIZE_WHITESPACE)
assert results.failed == 0
def test_default_format():
fmt = ds.default_formatter.format_value
assert_equal(fmt(1.23456789), '1.23457')
assert_equal(fmt(123456789), '123456789')
assert_equal(fmt(123456789**5), '28679718602997181072337614380936720482949')
assert_equal(fmt(123.456789**5), '2.86797e+10')
assert_equal(fmt(True), 'True')
assert_equal(fmt(False), 'False')
assert_equal(fmt('hello'), 'hello')
assert_equal(fmt((1, 2)), '(1, 2)')
def test_default_format_column():
fmtc = ds.default_formatter.format_column
description_list = ["A data scientist is someone who creates programming code and combines it with statistical knowledge to create insights from data."]
pad = fmtc("Careers", description_list)
assert_equal(pad(description_list[0]), "A data scientist is someone who creates programming code ...")
def test_number_format():
for fmt in [ds.NumberFormatter(2), ds.NumberFormatter]:
us = ['1,000', '12,000']
vs = ['1,000', '12,000.346']
t = ds.Table().with_columns('u', us, 'v', vs)
t.set_format(['u', 'v'], fmt)
assert_equal(t, """
u | v
1,000 | 1,000.00
12,000 | 12,000.35
""")
def test_currency_format():
vs = ['$60', '$162.5', '$1,625']
t = ds.Table().with_columns('num', vs, 'str', vs)
t.set_format('num', ds.CurrencyFormatter('$', int_to_float=True))
assert_equal(t, """
num | str
$60.00 | $60
$162.50 | $162.5
$1,625.00 | $1,625
""")
assert_equal(t.sort('num'), """
num | str
$60.00 | $60
$162.50 | $162.5
$1,625.00 | $1,625
""")
assert_equal(t.sort('str'), """
num | str
$1,625.00 | $1,625
$162.50 | $162.5
$60.00 | $60
""")
def test_currency_format_int():
t = ds.Table().with_column('money', [1, 2, 3])
t.set_format(['money'], ds.CurrencyFormatter)
assert_equal(t, """
money
$1
$2
$3
""")
def test_date_format():
vs = ['2015-07-01 22:39:44.900351']
t = ds.Table().with_column('time', vs)
t.set_format('time', ds.DateFormatter("%Y-%m-%d %H:%M:%S.%f"))
assert isinstance(t['time'][0], float)
@pytest.mark.skipif(
not hasattr(time, "tzset"),
reason="time.tzset not available on this platform"
)
def test_date_formatter_format_value():
formatter = formats.DateFormatter()
os.environ["TZ"] = "UTC"
if hasattr(time, "tzset"): # ✅ only call tzset if it exists
time.tzset()
assert_equal(formatter.format_value(1666264489.9004), "2022-10-20 11:14:49.900400")
def test_percent_formatter():
vs = [0.1, 0.11111, 0.199999, 10]
t = ds.Table().with_column('percent', vs)
t.set_format('percent', ds.PercentFormatter(1))
assert_equal(t, """
percent
10.0%
11.1%
20.0%
1000.0%
""")
def test_distribution_formatter():
counts = [9, 10, 18, 23]
t = ds.Table().with_column('count', counts)
t.set_format('count', ds.DistributionFormatter)
assert_equal(t, """
count
15.00%
16.67%
30.00%
38.33%
""")
counts = [0, 0, 0, 0]
t = ds.Table().with_column('count', counts)
t.set_format('count', ds.DistributionFormatter)
assert_equal(t, """
count
0.00%
0.00%
0.00%
0.00%
""")
def test_empty_init():
formatter = formats.Formatter(1, 10, "...")
assert_equal(formatter.min_width, 1)
assert_equal(formatter.max_width, 10)
assert_equal(formatter.etc, "...")
def test_function_formatter():
def _mult_ten(v):
return v * 10
func_formatter = formats.FunctionFormatter(_mult_ten)
assert func_formatter.format_value(5) == '50'
def test_distribution_formatter_negative():
"""Test that DistributionFormatter raises assertion on negative values."""
import numpy as np
counts = np.array([9, 10, -18, 23])
formatter = ds.DistributionFormatter()
with pytest.raises(AssertionError):
formatter.convert_column(counts)