File: test_vis.py

package info (click to toggle)
python-petl 1.7.17-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,224 kB
  • sloc: python: 22,617; makefile: 109; xml: 9
file content (198 lines) | stat: -rw-r--r-- 3,832 bytes parent folder | download | duplicates (2)
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
from __future__ import absolute_import, print_function, division


import logging


from petl.test.helpers import eq_
import petl as etl
from petl.util.vis import look, see, lookstr


logger = logging.getLogger(__name__)
debug = logger.debug


def test_look():

    table = (('foo', 'bar'), ('a', 1), ('b', 2))
    actual = repr(look(table))
    expect = """+-----+-----+
| foo | bar |
+=====+=====+
| 'a' |   1 |
+-----+-----+
| 'b' |   2 |
+-----+-----+
"""
    eq_(expect, actual)


def test_look_irregular_rows():

    table = (('foo', 'bar'), ('a',), ('b', 2, True))
    actual = repr(look(table))
    expect = """+-----+-----+------+
| foo | bar |      |
+=====+=====+======+
| 'a' |     |      |
+-----+-----+------+
| 'b' |   2 | True |
+-----+-----+------+
"""
    eq_(expect, actual)


def test_look_index_header():

    table = (('foo', 'bar'), ('a', 1), ('b', 2))
    actual = repr(look(table, index_header=True))
    expect = """+-------+-------+
| 0|foo | 1|bar |
+=======+=======+
| 'a'   |     1 |
+-------+-------+
| 'b'   |     2 |
+-------+-------+
"""
    eq_(expect, actual)


def test_look_bool():

    table = (('foo', 'bar'), ('a', True), ('b', False))
    actual = repr(look(table))
    expect = """+-----+-------+
| foo | bar   |
+=====+=======+
| 'a' | True  |
+-----+-------+
| 'b' | False |
+-----+-------+
"""
    eq_(expect, actual)


def test_look_truncate():

    table = (('foo', 'bar'), ('abcd', 1234), ('bcde', 2345))

    actual = repr(look(table, truncate=3))
    expect = """+-----+-----+
| foo | bar |
+=====+=====+
| 'ab | 123 |
+-----+-----+
| 'bc | 234 |
+-----+-----+
"""
    eq_(expect, actual)

    actual = repr(look(table, truncate=3, vrepr=str))
    expect = """+-----+-----+
| foo | bar |
+=====+=====+
| abc | 123 |
+-----+-----+
| bcd | 234 |
+-----+-----+
"""
    eq_(expect, actual)


def test_look_width():

    table = (('foo', 'bar'), ('a', 1), ('b', 2))
    actual = repr(look(table, width=10))
    expect = ("+-----+---\n"
              "| foo | ba\n"
              "+=====+===\n"
              "| 'a' |   \n"
              "+-----+---\n"
              "| 'b' |   \n"
              "+-----+---\n")
    eq_(expect, actual)


def test_look_style_simple():
    table = (('foo', 'bar'), ('a', 1), ('b', 2))
    actual = repr(look(table, style='simple'))
    expect = """===  ===
foo  bar
===  ===
'a'    1
'b'    2
===  ===
"""
    eq_(expect, actual)
    etl.config.look_style = 'simple'
    actual = repr(look(table))
    eq_(expect, actual)
    etl.config.look_style = 'grid'


def test_look_style_minimal():
    table = (('foo', 'bar'), ('a', 1), ('b', 2))
    actual = repr(look(table, style='minimal'))
    expect = """foo  bar
'a'    1
'b'    2
"""
    eq_(expect, actual)
    etl.config.look_style = 'minimal'
    actual = repr(look(table))
    eq_(expect, actual)
    etl.config.look_style = 'grid'


def test_see():

    table = (('foo', 'bar'), ('a', 1), ('b', 2))
    actual = repr(see(table))
    expect = """foo: 'a', 'b'
bar: 1, 2
"""
    eq_(expect, actual)


def test_see_index_header():

    table = (('foo', 'bar'), ('a', 1), ('b', 2))
    actual = repr(see(table, index_header=True))
    expect = """0|foo: 'a', 'b'
1|bar: 1, 2
"""
    eq_(expect, actual)


def test_see_duplicateheader():

    table = (('foo', 'bar', 'foo'), ('a', 1, 'a_prime'), ('b', 2, 'b_prime'))
    actual = repr(see(table))
    expect = """foo: 'a', 'b'
bar: 1, 2
foo: 'a_prime', 'b_prime'
"""
    eq_(expect, actual)


def test_lookstr():

    table = (('foo', 'bar'), ('a', 1), ('b', 2))
    actual = repr(lookstr(table))
    expect = """+-----+-----+
| foo | bar |
+=====+=====+
| a   |   1 |
+-----+-----+
| b   |   2 |
+-----+-----+
"""
    eq_(expect, actual)


def test_look_headerless():
    table = []
    actual = repr(look(table))
    expect = ""
    eq_(expect, actual)