class FloatFormatTests(unittest.TestCase): def setUp(self): self.x = PrettyTable(["Constant", "Value"]) self.x.add_row(["Pi", pi]) self.x.add_row(["e", e]) self.x.add_row(["sqrt(2)", sqrt(2)]) def testNoDecimals(self): self.x.float_format = ".0f" self.x.caching = False assert "." not in self.x.get_string() def testRoundTo5DP(self): self.x.float_format = ".5f" string = self.x.get_string() assert "3.14159" in string assert "3.141592" not in string assert "2.71828" in string assert "2.718281" not in string assert "2.718282" not in string assert "1.41421" in string assert "1.414213" not in string def testPadWith2Zeroes(self): self.x.float_format = "06.2f" string = self.x.get_string() assert "003.14" in string assert "002.72" in string assert "001.41" in string
def testSortSlice(self): """Make sure sorting and slicing interact in the expected way""" x = PrettyTable(["Foo"]) for i in range(20, 0, -1): x.add_row([i]) newstyle = x.get_string(sortby="Foo", end=10) assert "10" in newstyle assert "20" not in newstyle oldstyle = x.get_string(sortby="Foo", end=10, oldsortslice=True) assert "10" not in oldstyle assert "20" in oldstyle
class BuildEquivelanceTest(unittest.TestCase): """Make sure that building a table row-by-row and column-by-column yield the same results""" def setUp(self): # Row by row... self.row = PrettyTable() self.row.field_names = ["City name", "Area", "Population", "Annual Rainfall"] self.row.add_row(["Adelaide",1295, 1158259, 600.5]) self.row.add_row(["Brisbane",5905, 1857594, 1146.4]) self.row.add_row(["Darwin", 112, 120900, 1714.7]) self.row.add_row(["Hobart", 1357, 205556, 619.5]) self.row.add_row(["Sydney", 2058, 4336374, 1214.8]) self.row.add_row(["Melbourne", 1566, 3806092, 646.9]) self.row.add_row(["Perth", 5386, 1554769, 869.4]) # Column by column... self.col = PrettyTable() self.col.add_column("City name",["Adelaide","Brisbane","Darwin","Hobart","Sydney","Melbourne","Perth"]) self.col.add_column("Area", [1295, 5905, 112, 1357, 2058, 1566, 5386]) self.col.add_column("Population", [1158259, 1857594, 120900, 205556, 4336374, 3806092, 1554769]) self.col.add_column("Annual Rainfall",[600.5, 1146.4, 1714.7, 619.5, 1214.8, 646.9, 869.4]) # A mix of both! self.mix = PrettyTable() self.mix.field_names = ["City name", "Area"] self.mix.add_row(["Adelaide",1295]) self.mix.add_row(["Brisbane",5905]) self.mix.add_row(["Darwin", 112]) self.mix.add_row(["Hobart", 1357]) self.mix.add_row(["Sydney", 2058]) self.mix.add_row(["Melbourne", 1566]) self.mix.add_row(["Perth", 5386]) self.mix.add_column("Population", [1158259, 1857594, 120900, 205556, 4336374, 3806092, 1554769]) self.mix.add_column("Annual Rainfall",[600.5, 1146.4, 1714.7, 619.5, 1214.8, 646.9, 869.4]) def testRowColEquivalenceASCII(self): self.assertEqual(self.row.get_string(), self.col.get_string()) def testRowMixEquivalenceASCII(self): self.assertEqual(self.row.get_string(), self.mix.get_string()) def testRowColEquivalenceHTML(self): self.assertEqual(self.row.get_html_string(), self.col.get_html_string()) def testRowMixEquivalenceHTML(self): self.assertEqual(self.row.get_html_string(), self.mix.get_html_string())
def testAsciiBreakLine(self): t = PrettyTable(['Field 1', 'Field 2']) t.add_row(['value 1', 'value2\nsecond line']) t.add_row(['value 3', 'value4']) result = t.get_string(hrules=ALL) assert result.strip() == """ +---------+-------------+ | Field 1 | Field 2 | +---------+-------------+ | value 1 | value2 | | | second line | +---------+-------------+ | value 3 | value4 | +---------+-------------+ """.strip() t = PrettyTable(['Field 1', 'Field 2']) t.add_row(['value 1', 'value2\nsecond line']) t.add_row(['value 3\n\nother line', 'value4\n\n\nvalue5']) result = t.get_string(hrules=ALL) assert result.strip() == """ +------------+-------------+ | Field 1 | Field 2 | +------------+-------------+ | value 1 | value2 | | | second line | +------------+-------------+ | value 3 | value4 | | | | | other line | | | | value5 | +------------+-------------+ """.strip() t = PrettyTable(['Field 1', 'Field 2']) t.add_row(['value 1', 'value2\nsecond line']) t.add_row(['value 3\n\nother line', 'value4\n\n\nvalue5']) result = t.get_string() assert result.strip() == """ +------------+-------------+ | Field 1 | Field 2 | +------------+-------------+ | value 1 | value2 | | | second line | | value 3 | value4 | | | | | other line | | | | value5 | +------------+-------------+ """.strip()
class EmptyTableTests(CityDataTest): """Make sure the print_empty option works""" def setUp(self): CityDataTest.setUp(self) self.y = PrettyTable() self.y.field_names = ["City name", "Area", "Population", "Annual Rainfall"] def testPrintEmptyTrue(self): assert self.y.get_string(print_empty=True) != "" assert self.x.get_string(print_empty=True) != self.y.get_string(print_empty=True) def testPrintEmptyFalse(self): assert self.y.get_string(print_empty=False) == "" assert self.y.get_string(print_empty=False) != self.x.get_string(print_empty=False) def testInteractionWithBorder(self): assert self.y.get_string(border=False, print_empty=True) == ""