Rosetta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
prettytable_test.py
Go to the documentation of this file.
1 import unittest
2 import sys
3 sys.path.append("../src/")
4 from math import pi, e, sqrt
5 from prettytable import *
6 
7 class BuildEquivelanceTest(unittest.TestCase):
8 
9  """Make sure that building a table row-by-row and column-by-column yield the same results"""
10 
11  def setUp(self):
12 
13  # Row by row...
14  self.row = PrettyTable()
15  self.row.field_names = ["City name", "Area", "Population", "Annual Rainfall"]
16  self.row.add_row(["Adelaide",1295, 1158259, 600.5])
17  self.row.add_row(["Brisbane",5905, 1857594, 1146.4])
18  self.row.add_row(["Darwin", 112, 120900, 1714.7])
19  self.row.add_row(["Hobart", 1357, 205556, 619.5])
20  self.row.add_row(["Sydney", 2058, 4336374, 1214.8])
21  self.row.add_row(["Melbourne", 1566, 3806092, 646.9])
22  self.row.add_row(["Perth", 5386, 1554769, 869.4])
23 
24  # Column by column...
25  self.col = PrettyTable()
26  self.col.add_column("City name",["Adelaide","Brisbane","Darwin","Hobart","Sydney","Melbourne","Perth"])
27  self.col.add_column("Area", [1295, 5905, 112, 1357, 2058, 1566, 5386])
28  self.col.add_column("Population", [1158259, 1857594, 120900, 205556, 4336374, 3806092, 1554769])
29  self.col.add_column("Annual Rainfall",[600.5, 1146.4, 1714.7, 619.5, 1214.8, 646.9, 869.4])
30 
31  # A mix of both!
32  self.mix = PrettyTable()
33  self.mix.field_names = ["City name", "Area"]
34  self.mix.add_row(["Adelaide",1295])
35  self.mix.add_row(["Brisbane",5905])
36  self.mix.add_row(["Darwin", 112])
37  self.mix.add_row(["Hobart", 1357])
38  self.mix.add_row(["Sydney", 2058])
39  self.mix.add_row(["Melbourne", 1566])
40  self.mix.add_row(["Perth", 5386])
41  self.mix.add_column("Population", [1158259, 1857594, 120900, 205556, 4336374, 3806092, 1554769])
42  self.mix.add_column("Annual Rainfall",[600.5, 1146.4, 1714.7, 619.5, 1214.8, 646.9, 869.4])
43 
45 
46  self.assertEqual(self.row.get_string(), self.col.get_string())
47 
49 
50  self.assertEqual(self.row.get_string(), self.mix.get_string())
51 
53 
54  self.assertEqual(self.row.get_html_string(), self.col.get_html_string())
55 
57 
58  self.assertEqual(self.row.get_html_string(), self.mix.get_html_string())
59 
60 #class FieldnamelessTableTest(unittest.TestCase):
61 #
62 # """Make sure that building and stringing a table with no fieldnames works fine"""
63 #
64 # def setUp(self):
65 # self.x = PrettyTable()
66 # self.x.add_row(["Adelaide",1295, 1158259, 600.5])
67 # self.x.add_row(["Brisbane",5905, 1857594, 1146.4])
68 # self.x.add_row(["Darwin", 112, 120900, 1714.7])
69 # self.x.add_row(["Hobart", 1357, 205556, 619.5])
70 # self.x.add_row(["Sydney", 2058, 4336374, 1214.8])
71 # self.x.add_row(["Melbourne", 1566, 3806092, 646.9])
72 # self.x.add_row(["Perth", 5386, 1554769, 869.4])
73 #
74 # def testCanStringASCII(self):
75 # self.x.get_string()
76 #
77 # def testCanStringHTML(self):
78 # self.x.get_html_string()
79 #
80 # def testAddFieldnamesLater(self):
81 # self.x.field_names = ["City name", "Area", "Population", "Annual Rainfall"]
82 # self.x.get_string()
83 
84 class CityDataTest(unittest.TestCase):
85 
86  """Just build the Australian capital city data example table."""
87 
88  def setUp(self):
89 
90  self.x = PrettyTable(["City name", "Area", "Population", "Annual Rainfall"])
91  self.x.add_row(["Adelaide",1295, 1158259, 600.5])
92  self.x.add_row(["Brisbane",5905, 1857594, 1146.4])
93  self.x.add_row(["Darwin", 112, 120900, 1714.7])
94  self.x.add_row(["Hobart", 1357, 205556, 619.5])
95  self.x.add_row(["Sydney", 2058, 4336374, 1214.8])
96  self.x.add_row(["Melbourne", 1566, 3806092, 646.9])
97  self.x.add_row(["Perth", 5386, 1554769, 869.4])
98 
100 
101  """Make sure all options are properly overwritten by printt."""
102 
103  def testBorder(self):
104  default = self.x.get_string()
105  override = self.x.get_string(border=False)
106  self.assertTrue(default != override)
107 
108  def testHeader(self):
109  default = self.x.get_string()
110  override = self.x.get_string(header=False)
111  self.assertTrue(default != override)
112 
113  def testHrulesAll(self):
114  default = self.x.get_string()
115  override = self.x.get_string(hrules=ALL)
116  self.assertTrue(default != override)
117 
118  def testHrulesNone(self):
119 
120  default = self.x.get_string()
121  override = self.x.get_string(hrules=NONE)
122  self.assertTrue(default != override)
123 
125 
126  """Some very basic tests."""
127 
128  def testNoBlankLines(self):
129 
130  """No table should ever have blank lines in it."""
131 
132  string = self.x.get_string()
133  lines = string.split("\n")
134  self.assertTrue("" not in lines)
135 
137 
138  """All lines in a table should be of the same length."""
139 
140  string = self.x.get_string()
141  lines = string.split("\n")
142  lengths = [len(line) for line in lines]
143  lengths = set(lengths)
144  self.assertEqual(len(lengths),1)
145 
147 
148  """Run the basic tests with border = False"""
149 
150  def setUp(self):
151  BasicTests.setUp(self)
152  self.x.border = False
153 
155 
156  """Run the basic tests with header = False"""
157 
158  def setUp(self):
159  BasicTests.setUp(self)
160  self.x.header = False
161 
163 
164  """Run the basic tests with hrules = NONE"""
165 
166  def setUp(self):
167  BasicTests.setUp(self)
168  self.x.hrules = NONE
169 
171 
172  """Run the basic tests with hrules = ALL"""
173 
174  def setUp(self):
175  BasicTests.setUp(self)
176  self.x.hrules = ALL
177 
179 
180  """Run the basic tests after using set_style"""
181 
182  def setUp(self):
183  BasicTests.setUp(self)
184  self.x.set_style(MSWORD_FRIENDLY)
185 
187 
188  def setUp(self):
189  CityDataTest.setUp(self)
190 
192  y = self.x[0:2]
193  string = y.get_string()
194  assert len(string.split("\n")) == 6
195  assert "Adelaide" in string
196  assert "Brisbane" in string
197  assert "Melbourne" not in string
198  assert "Perth" not in string
199 
201  y = self.x[-2:]
202  string = y.get_string()
203  assert len(string.split("\n")) == 6
204  assert "Adelaide" not in string
205  assert "Brisbane" not in string
206  assert "Melbourne" in string
207  assert "Perth" in string
208 
210 
211  def setUp(self):
212  CityDataTest.setUp(self)
213 
214  def testSortBy(self):
215  self.x.sortby = self.x.field_names[0]
216  old = self.x.get_string()
217  for field in self.x.field_names[1:]:
218  self.x.sortby = field
219  new = self.x.get_string()
220  assert new != old
221 
222  def testReverseSort(self):
223  for field in self.x.field_names:
224  self.x.sortby = field
225  self.x.reversesort = False
226  forward = self.x.get_string()
227  self.x.reversesort = True
228  backward = self.x.get_string()
229  forward_lines = forward.split("\n")[2:] # Discard header lines
230  backward_lines = backward.split("\n")[2:]
231  backward_lines.reverse()
232  assert forward_lines == backward_lines
233 
234  def testSortKey(self):
235  # Test sorting by length of city name
236  def key(vals):
237  vals[0] = len(vals[0])
238  return vals
239  self.x.sortby = "City name"
240  self.x.sort_key = key
241  assert self.x.get_string().strip() == """+-----------+------+------------+-----------------+
242 | City name | Area | Population | Annual Rainfall |
243 +-----------+------+------------+-----------------+
244 | Perth | 5386 | 1554769 | 869.4 |
245 | Darwin | 112 | 120900 | 1714.7 |
246 | Hobart | 1357 | 205556 | 619.5 |
247 | Sydney | 2058 | 4336374 | 1214.8 |
248 | Adelaide | 1295 | 1158259 | 600.5 |
249 | Brisbane | 5905 | 1857594 | 1146.4 |
250 | Melbourne | 1566 | 3806092 | 646.9 |
251 +-----------+------+------------+-----------------+
252 """.strip()
253 
255 
256  """Run the basic tests after setting an integer format string"""
257 
258  def setUp(self):
259  BasicTests.setUp(self)
260  self.x.int_format = "04"
261 
263 
264  """Run the basic tests after setting a float format string"""
265 
266  def setUp(self):
267  BasicTests.setUp(self)
268  self.x.float_format = "6.2"
269 
270 class FloatFormatTests(unittest.TestCase):
271 
272  def setUp(self):
273  self.x = PrettyTable(["Constant", "Value"])
274  self.x.add_row(["Pi", pi])
275  self.x.add_row(["e", e])
276  self.x.add_row(["sqrt(2)", sqrt(2)])
277 
278  def testNoDecimals(self):
279  self.x.float_format = ".0"
280  self.x.caching = False
281  assert "." not in self.x.get_string()
282 
283  def testRoundTo5DP(self):
284  self.x.float_format = ".5"
285  string = self.x.get_string()
286  assert "3.14159" in string
287  assert "3.141592" not in string
288  assert "2.71828" in string
289  assert "2.718281" not in string
290  assert "2.718282" not in string
291  assert "1.41421" in string
292  assert "1.414213" not in string
293 
295  self.x.float_format = "06.2"
296  string = self.x.get_string()
297  assert "003.14" in string
298  assert "002.72" in string
299  assert "001.41" in string
300 
301 class BreakLineTests(unittest.TestCase):
303  t = PrettyTable(['Field 1', 'Field 2'])
304  t.add_row(['value 1', 'value2\nsecond line'])
305  t.add_row(['value 3', 'value4'])
306  result = t.get_string(hrules=True)
307  assert result.strip() == """
308 +---------+-------------+
309 | Field 1 | Field 2 |
310 +---------+-------------+
311 | value 1 | value2 |
312 | | second line |
313 +---------+-------------+
314 | value 3 | value4 |
315 +---------+-------------+
316 """.strip()
317 
318  t = PrettyTable(['Field 1', 'Field 2'])
319  t.add_row(['value 1', 'value2\nsecond line'])
320  t.add_row(['value 3\n\nother line', 'value4\n\n\nvalue5'])
321  result = t.get_string(hrules=True)
322  assert result.strip() == """
323 +------------+-------------+
324 | Field 1 | Field 2 |
325 +------------+-------------+
326 | value 1 | value2 |
327 | | second line |
328 +------------+-------------+
329 | value 3 | value4 |
330 | | |
331 | other line | |
332 | | value5 |
333 +------------+-------------+
334 """.strip()
335 
336  t = PrettyTable(['Field 1', 'Field 2'])
337  t.add_row(['value 1', 'value2\nsecond line'])
338  t.add_row(['value 3\n\nother line', 'value4\n\n\nvalue5'])
339  result = t.get_string()
340  assert result.strip() == """
341 +------------+-------------+
342 | Field 1 | Field 2 |
343 +------------+-------------+
344 | value 1 | value2 |
345 | | second line |
346 | value 3 | value4 |
347 | | |
348 | other line | |
349 | | value5 |
350 +------------+-------------+
351 """.strip()
352 
353  def testHtmlBreakLine(self):
354  t = PrettyTable(['Field 1', 'Field 2'])
355  t.add_row(['value 1', 'value2\nsecond line'])
356  t.add_row(['value 3', 'value4'])
357  result = t.get_html_string(hrules=True)
358  assert result.strip() == """
359 <table border="1">
360  <tr>
361  <th>Field 1</th>
362  <th>Field 2</th>
363  </tr>
364  <tr>
365  <td>value 1</td>
366  <td>value2<br />second line</td>
367  </tr>
368  <tr>
369  <td>value 3</td>
370  <td>value4</td>
371  </tr>
372 </table>
373 """.strip()
374 
375 class HtmlOutputTests(unittest.TestCase):
376  def testHtmlOutput(self):
377  t = PrettyTable(['Field 1', 'Field 2', 'Field 3'])
378  t.add_row(['value 1', 'value2', 'value3'])
379  t.add_row(['value 4', 'value5', 'value6'])
380  t.add_row(['value 7', 'value8', 'value9'])
381  result = t.get_html_string()
382  assert result.strip() == """
383 <table border="1">
384  <tr>
385  <th>Field 1</th>
386  <th>Field 2</th>
387  <th>Field 3</th>
388  </tr>
389  <tr>
390  <td>value 1</td>
391  <td>value2</td>
392  <td>value3</td>
393  </tr>
394  <tr>
395  <td>value 4</td>
396  <td>value5</td>
397  <td>value6</td>
398  </tr>
399  <tr>
400  <td>value 7</td>
401  <td>value8</td>
402  <td>value9</td>
403  </tr>
404 </table>
405 """.strip()
406 
408  t = PrettyTable(['Field 1', 'Field 2', 'Field 3'])
409  t.add_row(['value 1', 'value2', 'value3'])
410  t.add_row(['value 4', 'value5', 'value6'])
411  t.add_row(['value 7', 'value8', 'value9'])
412  result = t.get_html_string(format=True)
413  assert result.strip() == """
414 <table border="1">
415  <tr>
416  <th style="padding-left: 1em; padding-right: 1em; text-align: center">Field 1</th>
417  <th style="padding-left: 1em; padding-right: 1em; text-align: center">Field 2</th>
418  <th style="padding-left: 1em; padding-right: 1em; text-align: center">Field 3</th>
419  </tr>
420  <tr>
421  <td style="padding-left: 1em; padding-right: 1em; text-align: center">value 1</td>
422  <td style="padding-left: 1em; padding-right: 1em; text-align: center">value2</td>
423  <td style="padding-left: 1em; padding-right: 1em; text-align: center">value3</td>
424  </tr>
425  <tr>
426  <td style="padding-left: 1em; padding-right: 1em; text-align: center">value 4</td>
427  <td style="padding-left: 1em; padding-right: 1em; text-align: center">value5</td>
428  <td style="padding-left: 1em; padding-right: 1em; text-align: center">value6</td>
429  </tr>
430  <tr>
431  <td style="padding-left: 1em; padding-right: 1em; text-align: center">value 7</td>
432  <td style="padding-left: 1em; padding-right: 1em; text-align: center">value8</td>
433  <td style="padding-left: 1em; padding-right: 1em; text-align: center">value9</td>
434  </tr>
435 </table>
436 """.strip()
437 
438 if __name__ == "__main__":
439  unittest.main()
Fstring::size_type len(Fstring const &s)
Length.
Definition: Fstring.hh:2207
std::string & strip(std::string &s, std::string const &chars)
Strip Specified Characters from a string's Tails.
utility::keys::lookup::key< KeyType > const key