test_tableutils.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # -*- coding: utf-8 -*-
  2. from boltons.tableutils import Table
  3. def test_table_lists():
  4. data_lists = [['id', 'name'],
  5. [1, 'John Doe'],
  6. [2, 'Dale Simmons']]
  7. t1 = Table(data_lists)
  8. assert set(t1.headers) == set(['id', 'name'])
  9. assert len(t1) == 2
  10. assert 'John Doe' in repr(t1)
  11. T2_REF_HTML = """<table>
  12. <tr><th>id</th><td>1</td></tr>
  13. <tr><th>name</th><td>John Doe</td></tr>
  14. </table>"""
  15. T3_REF_HTML = """<table>
  16. <thead>
  17. <tr><th>id</th><th>name</th></tr>
  18. </thead>
  19. <tbody>
  20. <tr><td>1</td><td>John Doe</td></tr>
  21. <tr><td>2</td><td>Dale Simmons</td></tr>
  22. <tr><td>3</td><td>Kurt Rose</td></tr>
  23. <tr><td>4</td><td>None</td></tr>
  24. </tbody>
  25. </table>"""
  26. def test_table_dicts():
  27. data_dicts = [{'id': 1, 'name': 'John Doe'},
  28. {'id': 2, 'name': 'Dale Simmons'}]
  29. t2 = Table.from_dict(data_dicts[0])
  30. t3 = Table.from_dict(data_dicts)
  31. t3.extend([[3, 'Kurt Rose'], [4]])
  32. assert set(t2.headers) == set(['id', 'name'])
  33. assert len(t2) == 1
  34. # the sorted() stuff handles ordering differences between versions
  35. # TODO: should maybe change Table to sort the headers of dicts and such?
  36. assert sorted(t2.to_html()) == sorted(T2_REF_HTML)
  37. assert sorted(t3.to_html()) == sorted(T3_REF_HTML)
  38. assert t3.to_text()
  39. def test_table_obj():
  40. class TestType(object):
  41. def __init__(self):
  42. self.greeting = 'hi'
  43. t4 = Table.from_object(TestType())
  44. assert len(t4) == 1
  45. assert 'greeting' in t4.headers