def test_clubbed_json(self): clubbed_json = _to_ordered_dict({"sample": [ {"a":1, "b":2, "c":3}, {"a":5, "b":6, "c":7} ] }) result = convert(clubbed_json) clubbed_table = "<table><tr><th>sample</th><td><table><tr><th>a</th><th>b</th><th>c</th></tr><"\ "tr><td>1</td><td>2</td><td>3</td></tr><tr><td>5</td><td>6</td><td>7</td></tr>"\ "</table></td></tr></table>" self.assertEqual(result, clubbed_table)
def test_nested_left_to_right(self): result = convert(self.nested_json, build_direction="LEFT_TO_RIGHT") nested_table = "<table><tr><th>menu</th><td><table><tr><th>id</th><td>file</td></tr><tr><th>me"\ "nuitem</th><td><table><tr><th>onclick</th><th>value</th></tr><tr><td>CreateNew"\ "Doc()</td><td>New</td></tr><tr><td>OpenDoc()</td><td>Open</td></tr><tr><td>Clo"\ "seDoc()</td><td>Close</td></tr></table></td></tr><tr><th>value</th><td>File</t"\ "d></tr></table></td></tr></table>" self.assertEqual(result, nested_table)
def test_nested_left_to_right(self): result = convert(self.nested_json, build_direction="LEFT_TO_RIGHT") nested_table = "<table><tr><th>menu</th><td><table><tr><th>id</th><td>file</td></tr><tr><th>me"\ "nuitem</th><td><table><tr><th>onclick</th><th>value</th></tr><tr><td>CreateNew"\ "Doc()</td><td>New</td></tr><tr><td>OpenDoc()</td><td>Open</td></tr><tr><td>Clo"\ "seDoc()</td><td>Close</td></tr></table></td></tr><tr><th>value</th><td>File</t"\ "d></tr></table></td></tr></table>" self.assertEqual(result, nested_table)
def test_nested_top_to_bottom(self): result = convert(self.nested_json, build_direction="TOP_TO_BOTTOM") nested_table = "<table><tr><th>menu</th></tr><tr><td><table><tr><th>id</th><th>menuitem</th><t"\ "h>value</th></tr><tr><td>file</td><td><table><tr><th>onclick</th><th>value</th"\ "></tr><tr><td>CreateNewDoc()</td><td>New</td></tr><tr><td>OpenDoc()</td><td>Op"\ "en</td></tr><tr><td>CloseDoc()</td><td>Close</td></tr></table></td><td>File</t"\ "d></tr></table></td></tr></table>" self.assertEqual(result, nested_table)
def test_nested_top_to_bottom(self): result = convert(self.nested_json, build_direction="TOP_TO_BOTTOM") nested_table = "<table><tr><th>menu</th></tr><tr><td><table><tr><th>id</th><th>menuitem</th><t"\ "h>value</th></tr><tr><td>file</td><td><table><tr><th>onclick</th><th>value</th"\ "></tr><tr><td>CreateNewDoc()</td><td>New</td></tr><tr><td>OpenDoc()</td><td>Op"\ "en</td></tr><tr><td>CloseDoc()</td><td>Close</td></tr></table></td><td>File</t"\ "d></tr></table></td></tr></table>" self.assertEqual(result, nested_table)
def show_data(): shark_db = get_db() all_data = {} result_cursor = shark_db.find_all('stocks').sort([ ('key', pymongo.DESCENDING) ]).limit(7) for doc in result_cursor: doc.pop('_id') all_data[doc['key']] = doc return json2table.convert(all_data, "LEFT_TO_RIGHT", {'border': 1})
def test_clubbed_json(self): clubbed_json = _to_ordered_dict( {"sample": [{ "a": 1, "b": 2, "c": 3 }, { "a": 5, "b": 6, "c": 7 }]}) result = convert(clubbed_json) clubbed_table = "<table><tr><th>sample</th><td><table><tr><th>a</th><th>b</th><th>c</th></tr><"\ "tr><td>1</td><td>2</td><td>3</td></tr><tr><td>5</td><td>6</td><td>7</td></tr>"\ "</table></td></tr></table>" self.assertEqual(result, clubbed_table)
def test_invalid_build_direction(self): with self.assertRaises(ValueError) as context: convert(None, build_direction=None) self.assertTrue("Invalid build direction" in context.exception)
def test_build_direction_top_to_bottom(self): result = convert(self.simple_json, build_direction="TOP_TO_BOTTOM") simple_table = "<table><tr><th>key</th></tr><tr><td>value</td></tr></table>" self.assertEqual(result, simple_table)
def test_invalid_table_attributes(self): with self.assertRaises(TypeError) as context: convert(None, table_attributes=0) self.assertTrue( "Table attributes must be either" in context.exception)
def test_simple(self): result = convert(self.simple_json) simple_table = "<table><tr><th>key</th><td>value</td></tr></table>" self.assertEqual(result, simple_table)
def test_custom_table_attributes(self): result = convert({}, table_attributes=self.custom_table_attributes) self.assertTrue("border=\"1\"" in result)
def test_invalid_table_attributes(self): with self.assertRaises(TypeError) as context: convert(None, table_attributes=0) self.assertTrue("Table attributes must be either" in context.exception)
def test_invalid_json(self): with self.assertRaises(AttributeError) as context: convert(None)
def test_invalid_build_direction(self): with self.assertRaises(ValueError) as context: convert(None, build_direction=None) self.assertTrue("Invalid build direction" in context.exception)
def test_invalid_json(self): with self.assertRaises(AttributeError) as context: convert(None)
def test_build_direction_top_to_bottom(self): result = convert(self.simple_json, build_direction="TOP_TO_BOTTOM") simple_table = "<table><tr><th>key</th></tr><tr><td>value</td></tr></table>" self.assertEqual(result, simple_table)
def test_custom_table_attributes(self): result = convert({}, table_attributes=self.custom_table_attributes) self.assertTrue("border=\"1\"" in result)
def test_simple(self): result = convert(self.simple_json) simple_table = "<table><tr><th>key</th><td>value</td></tr></table>" self.assertEqual(result, simple_table)