Debian Codegraph @

petl/test/io/test_html.py

raw
1 # -*- coding: utf-8 -*-
2 from __future__ import absolute_import, print_function, division
3
4
5 from tempfile import NamedTemporaryFile
6 import io
7 from petl.test.helpers import eq_
8
9
10 from petl.io.html import tohtml
11
12
13 def test_tohtml():
14
15 # exercise function
16 table = (('foo', 'bar'),
17 ('a', 1),
18 ('b', (1, 2)),
19 ('c', False))
20
21 f = NamedTemporaryFile(delete=False)
22 tohtml(table, f.name, encoding='ascii', lineterminator='\n')
23
24 # check what it did
25 with io.open(f.name, mode='rt', encoding='ascii', newline='') as o:
26 actual = o.read()
27 expect = (
28 u"<table class='petl'>\n"
29 u"<thead>\n"
30 u"<tr>\n"
31 u"<th>foo</th>\n"
32 u"<th>bar</th>\n"
33 u"</tr>\n"
34 u"</thead>\n"
35 u"<tbody>\n"
36 u"<tr>\n"
37 u"<td>a</td>\n"
38 u"<td style='text-align: right'>1</td>\n"
39 u"</tr>\n"
40 u"<tr>\n"
41 u"<td>b</td>\n"
42 u"<td>(1, 2)</td>\n"
43 u"</tr>\n"
44 u"<tr>\n"
45 u"<td>c</td>\n"
46 u"<td>False</td>\n"
47 u"</tr>\n"
48 u"</tbody>\n"
49 u"</table>\n"
50 )
51 eq_(expect, actual)
52
53
54 def test_tohtml_caption():
55
56 # exercise function
57 table = (('foo', 'bar'),
58 ('a', 1),
59 ('b', (1, 2)))
60 f = NamedTemporaryFile(delete=False)
61 tohtml(table, f.name, encoding='ascii', caption='my table',
62 lineterminator='\n')
63
64 # check what it did
65 with io.open(f.name, mode='rt', encoding='ascii', newline='') as o:
66 actual = o.read()
67 expect = (
68 u"<table class='petl'>\n"
69 u"<caption>my table</caption>\n"
70 u"<thead>\n"
71 u"<tr>\n"
72 u"<th>foo</th>\n"
73 u"<th>bar</th>\n"
74 u"</tr>\n"
75 u"</thead>\n"
76 u"<tbody>\n"
77 u"<tr>\n"
78 u"<td>a</td>\n"
79 u"<td style='text-align: right'>1</td>\n"
80 u"</tr>\n"
81 u"<tr>\n"
82 u"<td>b</td>\n"
83 u"<td>(1, 2)</td>\n"
84 u"</tr>\n"
85 u"</tbody>\n"
86 u"</table>\n"
87 )
88 eq_(expect, actual)
89
90
91 def test_tohtml_with_style():
92
93 # exercise function
94 table = (('foo', 'bar'),
95 ('a', 1))
96
97 f = NamedTemporaryFile(delete=False)
98 tohtml(table, f.name, encoding='ascii', lineterminator='\n',
99 tr_style='text-align: right', td_styles='text-align: center')
100
101 # check what it did
102 with io.open(f.name, mode='rt', encoding='ascii', newline='') as o:
103 actual = o.read()
104 expect = (
105 u"<table class='petl'>\n"
106 u"<thead>\n"
107 u"<tr>\n"
108 u"<th>foo</th>\n"
109 u"<th>bar</th>\n"
110 u"</tr>\n"
111 u"</thead>\n"
112 u"<tbody>\n"
113 u"<tr style='text-align: right'>\n"
114 u"<td style='text-align: center'>a</td>\n"
115 u"<td style='text-align: center'>1</td>\n"
116 u"</tr>\n"
117 u"</tbody>\n"
118 u"</table>\n"
119 )
120 eq_(expect, actual)
121
122
123 def test_tohtml_headerless():
124 table = []
125
126 f = NamedTemporaryFile(delete=False)
127 tohtml(table, f.name, encoding='ascii', lineterminator='\n')
128
129 # check what it did
130 with io.open(f.name, mode='rt', encoding='ascii', newline='') as o:
131 actual = o.read()
132 expect = (
133 u"<table class='petl'>\n"
134 u"<tbody>\n"
135 u"</tbody>\n"
136 u"</table>\n"
137 )
138 eq_(expect, actual)