def test_render_alignment(io): style = TableStyle.ascii() style.set_column_alignment(1, Alignment.CENTER) style.set_column_alignment(2, Alignment.RIGHT) table = Table(style) table.set_header_row(["ISBN", "Title", "Author"]) table.add_rows([ ["99921-58-10-7", "Divine Comedy", "Dante Alighieri"], ["9971-5-0210-0", "A Tale of Two Cities", "Charles Dickens"], ["960-425-059-0", "The Lord of the Rings", "J. R. R. Tolkien"], ["80-902734-1-6", "And Then There Were None", "Agatha Christie"], ]) table.render(io) expected = """\ +---------------+--------------------------+------------------+ | ISBN | Title | Author | +---------------+--------------------------+------------------+ | 99921-58-10-7 | Divine Comedy | Dante Alighieri | | 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens | | 960-425-059-0 | The Lord of the Rings | J. R. R. Tolkien | | 80-902734-1-6 | And Then There Were None | Agatha Christie | +---------------+--------------------------+------------------+ """ assert expected == io.fetch_output()
def test_add_row_fails_if_too_missing_cells(): table = Table() table.set_header_row(["a", "b", "c"]) with pytest.raises(ValueError): table.add_row(["a", "b"])
def test_render_formatted_cells(io): table = Table(TableStyle.ascii()) table.set_header_row(["ISBN", "Title", "Author"]) table.add_rows([ ["<b>99921-58-10-7</b>", "Divine Comedy", "Dante Alighieri"], ["<b>9971-5-0210-0</b>", "A Tale of Two Cities", "Charles Dickens"], ["<b>960-425-059-0</b>", "The Lord of the Rings", "J. R. R. Tolkien"], [ "<b>80-902734-1-6</b>", "And Then There Were None", "Agatha Christie" ], ]) table.render(io) expected = """\ +---------------+--------------------------+------------------+ | ISBN | Title | Author | +---------------+--------------------------+------------------+ | 99921-58-10-7 | Divine Comedy | Dante Alighieri | | 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens | | 960-425-059-0 | The Lord of the Rings | J. R. R. Tolkien | | 80-902734-1-6 | And Then There Were None | Agatha Christie | +---------------+--------------------------+------------------+ """ assert expected == io.fetch_output()
def table(self, header=None, rows=None, style=None): """ Return a Table instance. """ if style is not None: style = self.TABLE_STYLES[style] table = Table(style) if header: table.set_header_row(header) if rows: table.set_rows(rows) return table
def table(self, header=None, rows=None, style=None): """ Return a Table instance. """ if style is not None: style = self.TABLE_STYLES[style] table = Table(style) if header: table.set_header_row(header) if rows: table.set_rows(rows) return table
def test_render_no_border(io): table = Table(TableStyle.borderless()) table.set_header_row(["ISBN", "Title", "Author"]) table.add_rows([ ["99921-58-10-7", "Divine Comedy", "Dante Alighieri"], ["9971-5-0210-0", "A Tale of Two Cities", "Charles Dickens"], ["960-425-059-0", "The Lord of the Rings", "J. R. R. Tolkien"], ["80-902734-1-6", "And Then There Were None", "Agatha Christie"], ]) table.render(io) expected = """\ ISBN Title Author ============= ======================== ================ 99921-58-10-7 Divine Comedy Dante Alighieri 9971-5-0210-0 A Tale of Two Cities Charles Dickens 960-425-059-0 The Lord of the Rings J. R. R. Tolkien 80-902734-1-6 And Then There Were None Agatha Christie """ assert expected == io.fetch_output()
def test_render_with_word_wrapping_and_indentation(io): style = TableStyle.ascii() table = Table(style) table.set_header_row(["ISBN", "Title", "Author"]) table.add_rows([ [ "99921-58-10-7", "Divine Comedy Divine Comedy Divine Comedy Divine Comedy Divine Comedy ", "Dante Alighieri", ], [ "9971-5-0210-0", "A Tale of Two Cities", "Charles Dickens Charles Dickens Charles Dickens", ], ["960-425-059-0", "The Lord of the Rings", "J. R. R. Tolkien"], ["80-902734-1-6", "And Then There Were None", "Agatha Christie"], ]) table.render(io, indentation=4) expected = """\ +---------------+-----------------------------+-------------------------+ | ISBN | Title | Author | +---------------+-----------------------------+-------------------------+ | 99921-58-10-7 | Divine Comedy Divine Comedy | Dante Alighieri | | | Divine Comedy Divine Comedy | | | | Divine Comedy | | | 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens Charles | | | | Dickens Charles Dickens | | 960-425-059-0 | The Lord of the Rings | J. R. R. Tolkien | | 80-902734-1-6 | And Then There Were None | Agatha Christie | +---------------+-----------------------------+-------------------------+ """ assert expected == io.fetch_output()
def test_render_solid_border(io): table = Table(TableStyle.solid()) table.set_header_row(["ISBN", "Title", "Author"]) table.add_rows([ ["99921-58-10-7", "Divine Comedy", "Dante Alighieri"], ["9971-5-0210-0", "A Tale of Two Cities", "Charles Dickens"], ["960-425-059-0", "The Lord of the Rings", "J. R. R. Tolkien"], ["80-902734-1-6", "And Then There Were None", "Agatha Christie"], ]) table.render(io) expected = """\ ┌───────────────┬──────────────────────────┬──────────────────┐ │ ISBN │ Title │ Author │ ├───────────────┼──────────────────────────┼──────────────────┤ │ 99921-58-10-7 │ Divine Comedy │ Dante Alighieri │ │ 9971-5-0210-0 │ A Tale of Two Cities │ Charles Dickens │ │ 960-425-059-0 │ The Lord of the Rings │ J. R. R. Tolkien │ │ 80-902734-1-6 │ And Then There Were None │ Agatha Christie │ └───────────────┴──────────────────────────┴──────────────────┘ """ assert expected == io.fetch_output()
def table(self, headers=None, rows=None): table = Table(TableStyle.solid()) table.set_header_row(headers) table.add_rows(rows) table.render(self.io())