Debian Codegraph @

petl/test/util/test_vis.py

raw
1 from __future__ import absolute_import, print_function, division
2
3
4 import logging
5
6
7 from petl.test.helpers import eq_
8 import petl as etl
9 from petl.util.vis import look, see, lookstr
10
11
12 logger = logging.getLogger(__name__)
13 debug = logger.debug
14
15
16 def test_look():
17
18 table = (('foo', 'bar'), ('a', 1), ('b', 2))
19 actual = repr(look(table))
20 expect = """+-----+-----+
21 | foo | bar |
22 +=====+=====+
23 | 'a' | 1 |
24 +-----+-----+
25 | 'b' | 2 |
26 +-----+-----+
27 """
28 eq_(expect, actual)
29
30
31 def test_look_irregular_rows():
32
33 table = (('foo', 'bar'), ('a',), ('b', 2, True))
34 actual = repr(look(table))
35 expect = """+-----+-----+------+
36 | foo | bar | |
37 +=====+=====+======+
38 | 'a' | | |
39 +-----+-----+------+
40 | 'b' | 2 | True |
41 +-----+-----+------+
42 """
43 eq_(expect, actual)
44
45
46 def test_look_index_header():
47
48 table = (('foo', 'bar'), ('a', 1), ('b', 2))
49 actual = repr(look(table, index_header=True))
50 expect = """+-------+-------+
51 | 0|foo | 1|bar |
52 +=======+=======+
53 | 'a' | 1 |
54 +-------+-------+
55 | 'b' | 2 |
56 +-------+-------+
57 """
58 eq_(expect, actual)
59
60
61 def test_look_bool():
62
63 table = (('foo', 'bar'), ('a', True), ('b', False))
64 actual = repr(look(table))
65 expect = """+-----+-------+
66 | foo | bar |
67 +=====+=======+
68 | 'a' | True |
69 +-----+-------+
70 | 'b' | False |
71 +-----+-------+
72 """
73 eq_(expect, actual)
74
75
76 def test_look_truncate():
77
78 table = (('foo', 'bar'), ('abcd', 1234), ('bcde', 2345))
79
80 actual = repr(look(table, truncate=3))
81 expect = """+-----+-----+
82 | foo | bar |
83 +=====+=====+
84 | 'ab | 123 |
85 +-----+-----+
86 | 'bc | 234 |
87 +-----+-----+
88 """
89 eq_(expect, actual)
90
91 actual = repr(look(table, truncate=3, vrepr=str))
92 expect = """+-----+-----+
93 | foo | bar |
94 +=====+=====+
95 | abc | 123 |
96 +-----+-----+
97 | bcd | 234 |
98 +-----+-----+
99 """
100 eq_(expect, actual)
101
102
103 def test_look_width():
104
105 table = (('foo', 'bar'), ('a', 1), ('b', 2))
106 actual = repr(look(table, width=10))
107 expect = ("+-----+---\n"
108 "| foo | ba\n"
109 "+=====+===\n"
110 "| 'a' | \n"
111 "+-----+---\n"
112 "| 'b' | \n"
113 "+-----+---\n")
114 eq_(expect, actual)
115
116
117 def test_look_style_simple():
118 table = (('foo', 'bar'), ('a', 1), ('b', 2))
119 actual = repr(look(table, style='simple'))
120 expect = """=== ===
121 foo bar
122 === ===
123 'a' 1
124 'b' 2
125 === ===
126 """
127 eq_(expect, actual)
128 etl.config.look_style = 'simple'
129 actual = repr(look(table))
130 eq_(expect, actual)
131 etl.config.look_style = 'grid'
132
133
134 def test_look_style_minimal():
135 table = (('foo', 'bar'), ('a', 1), ('b', 2))
136 actual = repr(look(table, style='minimal'))
137 expect = """foo bar
138 'a' 1
139 'b' 2
140 """
141 eq_(expect, actual)
142 etl.config.look_style = 'minimal'
143 actual = repr(look(table))
144 eq_(expect, actual)
145 etl.config.look_style = 'grid'
146
147
148 def test_see():
149
150 table = (('foo', 'bar'), ('a', 1), ('b', 2))
151 actual = repr(see(table))
152 expect = """foo: 'a', 'b'
153 bar: 1, 2
154 """
155 eq_(expect, actual)
156
157
158 def test_see_index_header():
159
160 table = (('foo', 'bar'), ('a', 1), ('b', 2))
161 actual = repr(see(table, index_header=True))
162 expect = """0|foo: 'a', 'b'
163 1|bar: 1, 2
164 """
165 eq_(expect, actual)
166
167
168 def test_see_duplicateheader():
169
170 table = (('foo', 'bar', 'foo'), ('a', 1, 'a_prime'), ('b', 2, 'b_prime'))
171 actual = repr(see(table))
172 expect = """foo: 'a', 'b'
173 bar: 1, 2
174 foo: 'a_prime', 'b_prime'
175 """
176 eq_(expect, actual)
177
178
179 def test_lookstr():
180
181 table = (('foo', 'bar'), ('a', 1), ('b', 2))
182 actual = repr(lookstr(table))
183 expect = """+-----+-----+
184 | foo | bar |
185 +=====+=====+
186 | a | 1 |
187 +-----+-----+
188 | b | 2 |
189 +-----+-----+
190 """
191 eq_(expect, actual)
192
193
194 def test_look_headerless():
195 table = []
196 actual = repr(look(table))
197 expect = ""
198 eq_(expect, actual)