| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import datetime as dt |
| 4 |
import sqlite3 |
| 5 |
from collections.abc import Generator |
| 6 |
from math import e, pi, sqrt |
| 7 |
from typing import Any |
| 8 |
|
| 9 |
import pytest |
| 10 |
from pytest_lazy_fixtures import lf |
| 11 |
|
| 12 |
import prettytable |
| 13 |
from prettytable import ( |
| 14 |
HRuleStyle, |
| 15 |
PrettyTable, |
| 16 |
RowType, |
| 17 |
TableStyle, |
| 18 |
VRuleStyle, |
| 19 |
from_db_cursor, |
| 20 |
) |
| 21 |
|
| 22 |
|
| 23 |
def test_version() -> None: |
| 24 |
assert isinstance(prettytable.__version__, str) |
| 25 |
assert prettytable.__version__[0].isdigit() |
| 26 |
assert prettytable.__version__.count(".") >= 2 |
| 27 |
assert prettytable.__version__[-1].isdigit() |
| 28 |
|
| 29 |
|
| 30 |
# Australian capital city data example table |
| 31 |
CITY_DATA_HEADER = ["City name", "Area", "Population", "Annual Rainfall"] |
| 32 |
CITY_DATA = [ |
| 33 |
["Adelaide", 1295, 1158259, 600.5], |
| 34 |
["Brisbane", 5905, 1857594, 1146.4], |
| 35 |
["Darwin", 112, 120900, 1714.7], |
| 36 |
["Hobart", 1357, 205556, 619.5], |
| 37 |
["Sydney", 2058, 4336374, 1214.8], |
| 38 |
["Melbourne", 1566, 3806092, 646.9], |
| 39 |
["Perth", 5386, 1554769, 869.4], |
| 40 |
] |
| 41 |
|
| 42 |
|
| 43 |
class TestNoneOption: |
| 44 |
def test_none_char_valid_option(self) -> None: |
| 45 |
PrettyTable(["Field 1", "Field 2", "Field 3"], none_format="") |
| 46 |
|
| 47 |
def test_none_char_invalid_option(self) -> None: |
| 48 |
with pytest.raises(TypeError) as exc: |
| 49 |
PrettyTable(["Field 1", "Field 2", "Field 3"], none_format=2) |
| 50 |
assert "must be a string" in str(exc.value) |
| 51 |
|
| 52 |
def test_no_value_replace_none(self) -> None: |
| 53 |
table = PrettyTable(["Field 1", "Field 2", "Field 3"]) |
| 54 |
table.add_row(["value 1", None, "value 2"]) |
| 55 |
assert ( |
| 56 |
table.get_string().strip() |
| 57 |
== """ |
| 58 |
+---------+---------+---------+ |
| 59 |
| Field 1 | Field 2 | Field 3 | |
| 60 |
+---------+---------+---------+ |
| 61 |
| value 1 | None | value 2 | |
| 62 |
+---------+---------+---------+ |
| 63 |
""".strip() |
| 64 |
) |
| 65 |
|
| 66 |
def test_no_value_replace_none_with_default_field_names(self) -> None: |
| 67 |
table = PrettyTable() |
| 68 |
table.add_row(["value 1", "None", "value 2"]) |
| 69 |
assert ( |
| 70 |
table.get_string().strip() |
| 71 |
== """ |
| 72 |
+---------+---------+---------+ |
| 73 |
| Field 1 | Field 2 | Field 3 | |
| 74 |
+---------+---------+---------+ |
| 75 |
| value 1 | None | value 2 | |
| 76 |
+---------+---------+---------+ |
| 77 |
""".strip() |
| 78 |
) |
| 79 |
|
| 80 |
def test_replace_none_all(self) -> None: |
| 81 |
table = PrettyTable( |
| 82 |
["Field 1", "Field 2", "Field 3", "Field 4"], none_format="N/A" |
| 83 |
) |
| 84 |
table.add_row(["value 1", None, "None", ""]) |
| 85 |
assert ( |
| 86 |
table.get_string().strip() |
| 87 |
== """ |
| 88 |
+---------+---------+---------+---------+ |
| 89 |
| Field 1 | Field 2 | Field 3 | Field 4 | |
| 90 |
+---------+---------+---------+---------+ |
| 91 |
| value 1 | N/A | N/A | | |
| 92 |
+---------+---------+---------+---------+ |
| 93 |
""".strip() |
| 94 |
) |
| 95 |
|
| 96 |
def test_replace_none_by_col(self) -> None: |
| 97 |
table = PrettyTable(["Field 1", "Field 2", "Field 3"]) |
| 98 |
table.none_format["Field 2"] = "N/A" |
| 99 |
table.none_format["Field 3"] = "" |
| 100 |
table.add_row(["value 1", None, None]) |
| 101 |
assert ( |
| 102 |
table.get_string().strip() |
| 103 |
== """ |
| 104 |
+---------+---------+---------+ |
| 105 |
| Field 1 | Field 2 | Field 3 | |
| 106 |
+---------+---------+---------+ |
| 107 |
| value 1 | N/A | | |
| 108 |
+---------+---------+---------+ |
| 109 |
""".strip() |
| 110 |
) |
| 111 |
|
| 112 |
def test_replace_none_recompute_width(self) -> None: |
| 113 |
table = PrettyTable() |
| 114 |
table.add_row([None]) |
| 115 |
table.none_format = "0123456789" |
| 116 |
assert ( |
| 117 |
table.get_string().strip() |
| 118 |
== """ |
| 119 |
+------------+ |
| 120 |
| Field 1 | |
| 121 |
+------------+ |
| 122 |
| 0123456789 | |
| 123 |
+------------+ |
| 124 |
""".strip() |
| 125 |
) |
| 126 |
|
| 127 |
def test_replace_none_maintain_width_on_recompute(self) -> None: |
| 128 |
table = PrettyTable() |
| 129 |
table.add_row(["Hello"]) |
| 130 |
table.none_format = "0123456789" |
| 131 |
assert ( |
| 132 |
table.get_string().strip() |
| 133 |
== """ |
| 134 |
+---------+ |
| 135 |
| Field 1 | |
| 136 |
+---------+ |
| 137 |
| Hello | |
| 138 |
+---------+ |
| 139 |
""".strip() |
| 140 |
) |
| 141 |
|
| 142 |
def test_replace_none_recompute_width_multi_column(self) -> None: |
| 143 |
table = PrettyTable() |
| 144 |
table.add_row(["Hello", None, "World"]) |
| 145 |
table.none_format = "0123456789" |
| 146 |
assert ( |
| 147 |
table.get_string().strip() |
| 148 |
== """ |
| 149 |
+---------+------------+---------+ |
| 150 |
| Field 1 | Field 2 | Field 3 | |
| 151 |
+---------+------------+---------+ |
| 152 |
| Hello | 0123456789 | World | |
| 153 |
+---------+------------+---------+ |
| 154 |
""".strip() |
| 155 |
) |
| 156 |
|
| 157 |
|
| 158 |
class TestBuildEquivalence: |
| 159 |
"""Make sure that building a table row-by-row and column-by-column yield the same |
| 160 |
results""" |
| 161 |
|
| 162 |
@pytest.mark.parametrize( |
| 163 |
["left_hand", "right_hand"], |
| 164 |
[ |
| 165 |
( |
| 166 |
lf("row_prettytable"), |
| 167 |
lf("col_prettytable"), |
| 168 |
), |
| 169 |
( |
| 170 |
lf("row_prettytable"), |
| 171 |
lf("mix_prettytable"), |
| 172 |
), |
| 173 |
], |
| 174 |
) |
| 175 |
def test_equivalence_ascii( |
| 176 |
self, left_hand: PrettyTable, right_hand: PrettyTable |
| 177 |
) -> None: |
| 178 |
assert left_hand.get_string() == right_hand.get_string() |
| 179 |
|
| 180 |
@pytest.mark.parametrize( |
| 181 |
["left_hand", "right_hand"], |
| 182 |
[ |
| 183 |
( |
| 184 |
lf("row_prettytable"), |
| 185 |
lf("col_prettytable"), |
| 186 |
), |
| 187 |
( |
| 188 |
lf("row_prettytable"), |
| 189 |
lf("mix_prettytable"), |
| 190 |
), |
| 191 |
], |
| 192 |
) |
| 193 |
def test_equivalence_html( |
| 194 |
self, left_hand: PrettyTable, right_hand: PrettyTable |
| 195 |
) -> None: |
| 196 |
assert left_hand.get_html_string() == right_hand.get_html_string() |
| 197 |
|
| 198 |
@pytest.mark.parametrize( |
| 199 |
["left_hand", "right_hand"], |
| 200 |
[ |
| 201 |
( |
| 202 |
lf("row_prettytable"), |
| 203 |
lf("col_prettytable"), |
| 204 |
), |
| 205 |
( |
| 206 |
lf("row_prettytable"), |
| 207 |
lf("mix_prettytable"), |
| 208 |
), |
| 209 |
], |
| 210 |
) |
| 211 |
def test_equivalence_latex( |
| 212 |
self, left_hand: PrettyTable, right_hand: PrettyTable |
| 213 |
) -> None: |
| 214 |
assert left_hand.get_latex_string() == right_hand.get_latex_string() |
| 215 |
|
| 216 |
@pytest.mark.parametrize( |
| 217 |
["left_hand", "right_hand"], |
| 218 |
[ |
| 219 |
( |
| 220 |
lf("row_prettytable"), |
| 221 |
lf("col_prettytable"), |
| 222 |
), |
| 223 |
( |
| 224 |
lf("row_prettytable"), |
| 225 |
lf("mix_prettytable"), |
| 226 |
), |
| 227 |
], |
| 228 |
) |
| 229 |
def test_equivalence_mediawiki( |
| 230 |
self, left_hand: PrettyTable, right_hand: PrettyTable |
| 231 |
) -> None: |
| 232 |
assert left_hand.get_mediawiki_string() == right_hand.get_mediawiki_string() |
| 233 |
|
| 234 |
|
| 235 |
class TestDelete: |
| 236 |
def test_delete_column(self, col_prettytable: PrettyTable) -> None: |
| 237 |
col_prettytable.del_column("Area") |
| 238 |
|
| 239 |
assert ( |
| 240 |
col_prettytable.get_string() |
| 241 |
== """+-----------+------------+-----------------+ |
| 242 |
| City name | Population | Annual Rainfall | |
| 243 |
+-----------+------------+-----------------+ |
| 244 |
| Adelaide | 1158259 | 600.5 | |
| 245 |
| Brisbane | 1857594 | 1146.4 | |
| 246 |
| Darwin | 120900 | 1714.7 | |
| 247 |
| Hobart | 205556 | 619.5 | |
| 248 |
| Sydney | 4336374 | 1214.8 | |
| 249 |
| Melbourne | 3806092 | 646.9 | |
| 250 |
| Perth | 1554769 | 869.4 | |
| 251 |
+-----------+------------+-----------------+""" |
| 252 |
) |
| 253 |
|
| 254 |
def test_delete_illegal_column_raises_error( |
| 255 |
self, col_prettytable: PrettyTable |
| 256 |
) -> None: |
| 257 |
with pytest.raises(ValueError): |
| 258 |
col_prettytable.del_column("City not-a-name") |
| 259 |
|
| 260 |
def test_delete_row(self, city_data: PrettyTable) -> None: |
| 261 |
city_data.del_row(2) |
| 262 |
|
| 263 |
assert ( |
| 264 |
city_data.get_string() |
| 265 |
== """+-----------+------+------------+-----------------+ |
| 266 |
| City name | Area | Population | Annual Rainfall | |
| 267 |
+-----------+------+------------+-----------------+ |
| 268 |
| Adelaide | 1295 | 1158259 | 600.5 | |
| 269 |
| Brisbane | 5905 | 1857594 | 1146.4 | |
| 270 |
| Hobart | 1357 | 205556 | 619.5 | |
| 271 |
| Sydney | 2058 | 4336374 | 1214.8 | |
| 272 |
| Melbourne | 1566 | 3806092 | 646.9 | |
| 273 |
| Perth | 5386 | 1554769 | 869.4 | |
| 274 |
+-----------+------+------------+-----------------+""" |
| 275 |
) |
| 276 |
|
| 277 |
def test_delete_row_unavailable(self, city_data: PrettyTable) -> None: |
| 278 |
with pytest.raises(IndexError): |
| 279 |
city_data.del_row(10) |
| 280 |
|
| 281 |
|
| 282 |
class TestFieldNameLessTable: |
| 283 |
"""Make sure that building and stringing a table with no fieldnames works fine""" |
| 284 |
|
| 285 |
def test_can_string_ascii(self, field_name_less_table: PrettyTable) -> None: |
| 286 |
output = field_name_less_table.get_string() |
| 287 |
assert "| Field 1 | Field 2 | Field 3 | Field 4 |" in output |
| 288 |
assert "| Adelaide | 1295 | 1158259 | 600.5 |" in output |
| 289 |
|
| 290 |
def test_can_string_html(self, field_name_less_table: PrettyTable) -> None: |
| 291 |
output = field_name_less_table.get_html_string() |
| 292 |
assert "<th>Field 1</th>" in output |
| 293 |
assert "<td>Adelaide</td>" in output |
| 294 |
|
| 295 |
def test_can_string_latex(self, field_name_less_table: PrettyTable) -> None: |
| 296 |
output = field_name_less_table.get_latex_string() |
| 297 |
assert "Field 1 & Field 2 & Field 3 & Field 4 \\\\" in output |
| 298 |
assert "Adelaide & 1295 & 1158259 & 600.5 \\\\" in output |
| 299 |
|
| 300 |
def test_can_string_mediawiki(self, field_name_less_table: PrettyTable) -> None: |
| 301 |
output = field_name_less_table.get_mediawiki_string(header=True) |
| 302 |
assert "! Field 1 !! Field 2 !! Field 3 !! Field 4" in output |
| 303 |
assert "| Adelaide || 1295 || 1158259 || 600.5" in output |
| 304 |
|
| 305 |
def test_add_field_names_later(self, field_name_less_table: PrettyTable) -> None: |
| 306 |
field_name_less_table.field_names = CITY_DATA_HEADER |
| 307 |
assert ( |
| 308 |
"City name | Area | Population | Annual Rainfall" |
| 309 |
in field_name_less_table.get_string() |
| 310 |
) |
| 311 |
|
| 312 |
|
| 313 |
@pytest.fixture(scope="function") |
| 314 |
def aligned_before_table() -> PrettyTable: |
| 315 |
table = PrettyTable() |
| 316 |
table.align = "r" |
| 317 |
table.field_names = CITY_DATA_HEADER |
| 318 |
for row in CITY_DATA: |
| 319 |
table.add_row(row) |
| 320 |
return table |
| 321 |
|
| 322 |
|
| 323 |
@pytest.fixture(scope="function") |
| 324 |
def aligned_after_table() -> PrettyTable: |
| 325 |
table = PrettyTable() |
| 326 |
table.field_names = CITY_DATA_HEADER |
| 327 |
for row in CITY_DATA: |
| 328 |
table.add_row(row) |
| 329 |
table.align = "r" |
| 330 |
return table |
| 331 |
|
| 332 |
|
| 333 |
class TestAlignment: |
| 334 |
"""Make sure alignment works regardless of when it was set""" |
| 335 |
|
| 336 |
def test_aligned_ascii( |
| 337 |
self, aligned_before_table: PrettyTable, aligned_after_table: PrettyTable |
| 338 |
) -> None: |
| 339 |
assert aligned_before_table.get_string() == aligned_after_table.get_string() |
| 340 |
|
| 341 |
def test_aligned_html( |
| 342 |
self, aligned_before_table: PrettyTable, aligned_after_table: PrettyTable |
| 343 |
) -> None: |
| 344 |
assert ( |
| 345 |
aligned_before_table.get_html_string() |
| 346 |
== aligned_after_table.get_html_string() |
| 347 |
) |
| 348 |
|
| 349 |
def test_aligned_latex( |
| 350 |
self, aligned_before_table: PrettyTable, aligned_after_table: PrettyTable |
| 351 |
) -> None: |
| 352 |
assert ( |
| 353 |
aligned_before_table.get_latex_string() |
| 354 |
== aligned_after_table.get_latex_string() |
| 355 |
) |
| 356 |
|
| 357 |
def test_aligned_mediawiki( |
| 358 |
self, aligned_before_table: PrettyTable, aligned_after_table: PrettyTable |
| 359 |
) -> None: |
| 360 |
assert aligned_before_table.get_mediawiki_string( |
| 361 |
header=True |
| 362 |
) == aligned_after_table.get_mediawiki_string(header=True) |
| 363 |
|
| 364 |
|
| 365 |
class TestOptionOverride: |
| 366 |
"""Make sure all options are properly overwritten by get_string.""" |
| 367 |
|
| 368 |
def test_border(self, city_data: PrettyTable) -> None: |
| 369 |
assert city_data.get_string() != city_data.get_string(border=False) |
| 370 |
|
| 371 |
def test_header(self, city_data: PrettyTable) -> None: |
| 372 |
assert city_data.get_string() != city_data.get_string(header=False) |
| 373 |
|
| 374 |
def test_hrules_all(self, city_data: PrettyTable) -> None: |
| 375 |
assert city_data.get_string() != city_data.get_string(hrules=HRuleStyle.ALL) |
| 376 |
|
| 377 |
def test_hrules_none(self, city_data: PrettyTable) -> None: |
| 378 |
assert city_data.get_string() != city_data.get_string(hrules=HRuleStyle.NONE) |
| 379 |
|
| 380 |
|
| 381 |
class TestOptionAttribute: |
| 382 |
"""Make sure all options which have an attribute interface work as they should. |
| 383 |
Also make sure option settings are copied correctly when a table is cloned by |
| 384 |
slicing.""" |
| 385 |
|
| 386 |
def test_set_for_all_columns(self, city_data: PrettyTable) -> None: |
| 387 |
city_data.field_names = sorted(city_data.field_names) |
| 388 |
city_data.align = "l" |
| 389 |
city_data.max_width = 10 |
| 390 |
city_data.start = 2 |
| 391 |
city_data.end = 4 |
| 392 |
city_data.sortby = "Area" |
| 393 |
city_data.reversesort = True |
| 394 |
city_data.header = True |
| 395 |
city_data.border = False |
| 396 |
city_data.hrules = HRuleStyle.ALL |
| 397 |
city_data.int_format = "4" |
| 398 |
city_data.float_format = "2.2" |
| 399 |
city_data.padding_width = 2 |
| 400 |
city_data.left_padding_width = 2 |
| 401 |
city_data.right_padding_width = 2 |
| 402 |
city_data.vertical_char = "!" |
| 403 |
city_data.horizontal_char = "~" |
| 404 |
city_data.junction_char = "*" |
| 405 |
city_data.top_junction_char = "@" |
| 406 |
city_data.bottom_junction_char = "#" |
| 407 |
city_data.right_junction_char = "$" |
| 408 |
city_data.left_junction_char = "%" |
| 409 |
city_data.top_right_junction_char = "^" |
| 410 |
city_data.top_left_junction_char = "&" |
| 411 |
city_data.bottom_right_junction_char = "(" |
| 412 |
city_data.bottom_left_junction_char = ")" |
| 413 |
city_data.format = True |
| 414 |
city_data.attributes = {"class": "prettytable"} |
| 415 |
assert city_data.get_string() == city_data[:].get_string() |
| 416 |
|
| 417 |
def test_set_for_one_column(self, city_data: PrettyTable) -> None: |
| 418 |
city_data.align["Rainfall"] = "l" |
| 419 |
city_data.max_width["Name"] = 10 |
| 420 |
city_data.int_format["Population"] = "4" |
| 421 |
city_data.float_format["Area"] = "2.2" |
| 422 |
assert city_data.get_string() == city_data[:].get_string() |
| 423 |
|
| 424 |
def test_preserve_internal_border(self) -> None: |
| 425 |
table = PrettyTable(preserve_internal_border=True) |
| 426 |
assert table.preserve_internal_border is True |
| 427 |
|
| 428 |
def test_internal_border_preserved(self, helper_table: PrettyTable) -> None: |
| 429 |
helper_table.border = False |
| 430 |
helper_table.preserve_internal_border = True |
| 431 |
|
| 432 |
assert ( |
| 433 |
helper_table.get_string().strip() |
| 434 |
== """ |
| 435 |
| Field 1 | Field 2 | Field 3 |
| 436 |
---+---------+---------+--------- |
| 437 |
1 | value 1 | value2 | value3 |
| 438 |
4 | value 4 | value5 | value6 |
| 439 |
7 | value 7 | value8 | value9 |
| 440 |
""".strip() # noqa: W291 |
| 441 |
) |
| 442 |
|
| 443 |
|
| 444 |
@pytest.fixture(scope="module") |
| 445 |
def db_cursor() -> Generator[sqlite3.Cursor]: |
| 446 |
conn = sqlite3.connect(":memory:") |
| 447 |
cur = conn.cursor() |
| 448 |
yield cur |
| 449 |
cur.close() |
| 450 |
conn.close() |
| 451 |
|
| 452 |
|
| 453 |
@pytest.fixture(scope="module") |
| 454 |
def init_db(db_cursor: sqlite3.Cursor) -> Generator[Any]: |
| 455 |
db_cursor.execute( |
| 456 |
"CREATE TABLE cities " |
| 457 |
"(name TEXT, area INTEGER, population INTEGER, rainfall REAL)" |
| 458 |
) |
| 459 |
for row in CITY_DATA: |
| 460 |
db_cursor.execute(f"INSERT INTO cities VALUES {tuple(row)}") |
| 461 |
yield |
| 462 |
db_cursor.execute("DROP TABLE cities") |
| 463 |
|
| 464 |
|
| 465 |
class TestBasic: |
| 466 |
"""Some very basic tests.""" |
| 467 |
|
| 468 |
def test_table_rows(self, city_data: PrettyTable) -> None: |
| 469 |
rows = city_data.rows |
| 470 |
assert len(rows) == 7 |
| 471 |
assert rows[0] == CITY_DATA[0] |
| 472 |
|
| 473 |
def test_add_rows(self, city_data: PrettyTable) -> None: |
| 474 |
"""A table created with multiple add_row calls is the same as one created |
| 475 |
with a single add_rows |
| 476 |
""" |
| 477 |
table = PrettyTable(CITY_DATA_HEADER) |
| 478 |
table.add_rows(CITY_DATA) |
| 479 |
assert str(city_data) == str(table) |
| 480 |
|
| 481 |
table.add_rows([]) |
| 482 |
assert str(city_data) == str(table) |
| 483 |
|
| 484 |
def _test_no_blank_lines(self, table: PrettyTable) -> None: |
| 485 |
string = table.get_string() |
| 486 |
lines = string.split("\n") |
| 487 |
assert "" not in lines |
| 488 |
|
| 489 |
def _test_all_length_equal(self, table: PrettyTable) -> None: |
| 490 |
string = table.get_string() |
| 491 |
lines = string.split("\n") |
| 492 |
lengths = {len(line) for line in lines} |
| 493 |
assert len(lengths) == 1 |
| 494 |
|
| 495 |
def test_no_blank_lines(self, city_data: PrettyTable) -> None: |
| 496 |
"""No table should ever have blank lines in it.""" |
| 497 |
self._test_no_blank_lines(city_data) |
| 498 |
|
| 499 |
def test_all_lengths_equal(self, city_data: PrettyTable) -> None: |
| 500 |
"""All lines in a table should be of the same length.""" |
| 501 |
self._test_all_length_equal(city_data) |
| 502 |
|
| 503 |
def test_no_blank_lines_with_title(self, city_data: PrettyTable) -> None: |
| 504 |
"""No table should ever have blank lines in it.""" |
| 505 |
city_data.title = "My table" |
| 506 |
self._test_no_blank_lines(city_data) |
| 507 |
|
| 508 |
def test_all_lengths_equal_with_title(self, city_data: PrettyTable) -> None: |
| 509 |
"""All lines in a table should be of the same length.""" |
| 510 |
city_data.title = "My table" |
| 511 |
self._test_all_length_equal(city_data) |
| 512 |
|
| 513 |
def test_all_lengths_equal_with_long_title(self, city_data: PrettyTable) -> None: |
| 514 |
"""All lines in a table should be of the same length, even with a long title.""" |
| 515 |
city_data.title = "My table (75 characters wide) " + "=" * 45 |
| 516 |
self._test_all_length_equal(city_data) |
| 517 |
|
| 518 |
def test_no_blank_lines_without_border(self, city_data: PrettyTable) -> None: |
| 519 |
"""No table should ever have blank lines in it.""" |
| 520 |
city_data.border = False |
| 521 |
self._test_no_blank_lines(city_data) |
| 522 |
|
| 523 |
def test_all_lengths_equal_without_border(self, city_data: PrettyTable) -> None: |
| 524 |
"""All lines in a table should be of the same length.""" |
| 525 |
city_data.border = False |
| 526 |
self._test_all_length_equal(city_data) |
| 527 |
|
| 528 |
def test_no_blank_lines_without_header(self, city_data: PrettyTable) -> None: |
| 529 |
"""No table should ever have blank lines in it.""" |
| 530 |
city_data.header = False |
| 531 |
self._test_no_blank_lines(city_data) |
| 532 |
|
| 533 |
def test_all_lengths_equal_without_header(self, city_data: PrettyTable) -> None: |
| 534 |
"""All lines in a table should be of the same length.""" |
| 535 |
city_data.header = False |
| 536 |
self._test_all_length_equal(city_data) |
| 537 |
|
| 538 |
def test_no_blank_lines_with_hrules_none(self, city_data: PrettyTable) -> None: |
| 539 |
"""No table should ever have blank lines in it.""" |
| 540 |
city_data.hrules = HRuleStyle.NONE |
| 541 |
self._test_no_blank_lines(city_data) |
| 542 |
|
| 543 |
def test_all_lengths_equal_with_hrules_none(self, city_data: PrettyTable) -> None: |
| 544 |
"""All lines in a table should be of the same length.""" |
| 545 |
city_data.hrules = HRuleStyle.NONE |
| 546 |
self._test_all_length_equal(city_data) |
| 547 |
|
| 548 |
def test_no_blank_lines_with_hrules_all(self, city_data: PrettyTable) -> None: |
| 549 |
"""No table should ever have blank lines in it.""" |
| 550 |
city_data.hrules = HRuleStyle.ALL |
| 551 |
self._test_no_blank_lines(city_data) |
| 552 |
|
| 553 |
def test_all_lengths_equal_with_hrules_all(self, city_data: PrettyTable) -> None: |
| 554 |
"""All lines in a table should be of the same length.""" |
| 555 |
city_data.hrules = HRuleStyle.ALL |
| 556 |
self._test_all_length_equal(city_data) |
| 557 |
|
| 558 |
def test_no_blank_lines_with_style_msword(self, city_data: PrettyTable) -> None: |
| 559 |
"""No table should ever have blank lines in it.""" |
| 560 |
city_data.set_style(TableStyle.MSWORD_FRIENDLY) |
| 561 |
self._test_no_blank_lines(city_data) |
| 562 |
|
| 563 |
def test_all_lengths_equal_with_style_msword(self, city_data: PrettyTable) -> None: |
| 564 |
"""All lines in a table should be of the same length.""" |
| 565 |
city_data.set_style(TableStyle.MSWORD_FRIENDLY) |
| 566 |
self._test_all_length_equal(city_data) |
| 567 |
|
| 568 |
def test_no_blank_lines_with_int_format(self, city_data: PrettyTable) -> None: |
| 569 |
"""No table should ever have blank lines in it.""" |
| 570 |
city_data.int_format = "04" |
| 571 |
self._test_no_blank_lines(city_data) |
| 572 |
|
| 573 |
def test_all_lengths_equal_with_int_format(self, city_data: PrettyTable) -> None: |
| 574 |
"""All lines in a table should be of the same length.""" |
| 575 |
city_data.int_format = "04" |
| 576 |
self._test_all_length_equal(city_data) |
| 577 |
|
| 578 |
def test_no_blank_lines_with_float_format(self, city_data: PrettyTable) -> None: |
| 579 |
"""No table should ever have blank lines in it.""" |
| 580 |
city_data.float_format = "6.2f" |
| 581 |
self._test_no_blank_lines(city_data) |
| 582 |
|
| 583 |
def test_all_lengths_equal_with_float_format(self, city_data: PrettyTable) -> None: |
| 584 |
"""All lines in a table should be of the same length.""" |
| 585 |
city_data.float_format = "6.2f" |
| 586 |
self._test_all_length_equal(city_data) |
| 587 |
|
| 588 |
def test_no_blank_lines_from_csv(self, city_data_from_csv: PrettyTable) -> None: |
| 589 |
"""No table should ever have blank lines in it.""" |
| 590 |
self._test_no_blank_lines(city_data_from_csv) |
| 591 |
|
| 592 |
def test_all_lengths_equal_from_csv(self, city_data_from_csv: PrettyTable) -> None: |
| 593 |
"""All lines in a table should be of the same length.""" |
| 594 |
self._test_all_length_equal(city_data_from_csv) |
| 595 |
|
| 596 |
def test_no_blank_lines_from_mediawiki( |
| 597 |
self, city_data_from_mediawiki: PrettyTable |
| 598 |
) -> None: |
| 599 |
"""No table should ever have blank lines in it.""" |
| 600 |
self._test_no_blank_lines(city_data_from_mediawiki) |
| 601 |
|
| 602 |
def test_all_lengths_equal_from_mediawiki( |
| 603 |
self, city_data_from_mediawiki: PrettyTable |
| 604 |
) -> None: |
| 605 |
"""All lines in a table should be of the same length.""" |
| 606 |
self._test_all_length_equal(city_data_from_mediawiki) |
| 607 |
|
| 608 |
def test_rowcount(self, city_data: PrettyTable) -> None: |
| 609 |
assert city_data.rowcount == 7 |
| 610 |
|
| 611 |
def test_colcount(self, city_data: PrettyTable) -> None: |
| 612 |
assert city_data.colcount == 4 |
| 613 |
|
| 614 |
def test_getitem(self, city_data: PrettyTable) -> None: |
| 615 |
assert ( |
| 616 |
city_data[1].get_string() |
| 617 |
== """+-----------+------+------------+-----------------+ |
| 618 |
| City name | Area | Population | Annual Rainfall | |
| 619 |
+-----------+------+------------+-----------------+ |
| 620 |
| Brisbane | 5905 | 1857594 | 1146.4 | |
| 621 |
+-----------+------+------------+-----------------+""" |
| 622 |
) |
| 623 |
|
| 624 |
def test_invalid_getitem(self, city_data: PrettyTable) -> None: |
| 625 |
with pytest.raises(IndexError): |
| 626 |
assert city_data[10] |
| 627 |
|
| 628 |
@pytest.mark.usefixtures("init_db") |
| 629 |
def test_no_blank_lines_from_db(self, db_cursor: sqlite3.Cursor) -> None: |
| 630 |
"""No table should ever have blank lines in it.""" |
| 631 |
db_cursor.execute("SELECT * FROM cities") |
| 632 |
table = from_db_cursor(db_cursor) |
| 633 |
assert table is not None |
| 634 |
self._test_no_blank_lines(table) |
| 635 |
|
| 636 |
@pytest.mark.usefixtures("init_db") |
| 637 |
def test_all_lengths_equal_from_db(self, db_cursor: sqlite3.Cursor) -> None: |
| 638 |
"""No table should ever have blank lines in it.""" |
| 639 |
db_cursor.execute("SELECT * FROM cities") |
| 640 |
table = from_db_cursor(db_cursor) |
| 641 |
assert table is not None |
| 642 |
self._test_all_length_equal(table) |
| 643 |
|
| 644 |
|
| 645 |
class TestEmptyTable: |
| 646 |
"""Make sure the print_empty option works""" |
| 647 |
|
| 648 |
def test_print_empty_true(self, city_data: PrettyTable) -> None: |
| 649 |
table = PrettyTable() |
| 650 |
table.field_names = CITY_DATA_HEADER |
| 651 |
|
| 652 |
assert table.get_string(print_empty=True) != "" |
| 653 |
assert table.get_string(print_empty=True) != city_data.get_string( |
| 654 |
print_empty=True |
| 655 |
) |
| 656 |
|
| 657 |
def test_print_empty_false(self, city_data: PrettyTable) -> None: |
| 658 |
table = PrettyTable() |
| 659 |
table.field_names = CITY_DATA_HEADER |
| 660 |
|
| 661 |
assert table.get_string(print_empty=False) == "" |
| 662 |
assert table.get_string(print_empty=False) != city_data.get_string( |
| 663 |
print_empty=False |
| 664 |
) |
| 665 |
|
| 666 |
def test_interaction_with_border(self) -> None: |
| 667 |
table = PrettyTable() |
| 668 |
table.field_names = CITY_DATA_HEADER |
| 669 |
|
| 670 |
assert table.get_string(border=False, print_empty=True) == "" |
| 671 |
|
| 672 |
|
| 673 |
class TestSlicing: |
| 674 |
def test_slice_all(self, city_data: PrettyTable) -> None: |
| 675 |
table = city_data[:] |
| 676 |
assert city_data.get_string() == table.get_string() |
| 677 |
|
| 678 |
def test_slice_first_two_rows(self, city_data: PrettyTable) -> None: |
| 679 |
table = city_data[0:2] |
| 680 |
string = table.get_string() |
| 681 |
assert len(string.split("\n")) == 6 |
| 682 |
for row_index in (0, 1): |
| 683 |
city = CITY_DATA[row_index][0] |
| 684 |
assert isinstance(city, str) |
| 685 |
assert city in string |
| 686 |
for row_index in (2, 3, 4, 5, 6): |
| 687 |
city = CITY_DATA[row_index][0] |
| 688 |
assert isinstance(city, str) |
| 689 |
assert city not in string |
| 690 |
|
| 691 |
def test_slice_last_two_rows(self, city_data: PrettyTable) -> None: |
| 692 |
table = city_data[-2:] |
| 693 |
string = table.get_string() |
| 694 |
assert len(string.split("\n")) == 6 |
| 695 |
for row_index in (0, 1, 2, 3, 4): |
| 696 |
city = CITY_DATA[row_index][0] |
| 697 |
assert isinstance(city, str) |
| 698 |
assert city not in string |
| 699 |
for row_index in (5, 6): |
| 700 |
city = CITY_DATA[row_index][0] |
| 701 |
assert isinstance(city, str) |
| 702 |
assert city in string |
| 703 |
|
| 704 |
|
| 705 |
class TestRowFilter: |
| 706 |
EXPECTED_RESULT = """+-----------+------+------------+-----------------+ |
| 707 |
| City name | Area | Population | Annual Rainfall | |
| 708 |
+-----------+------+------------+-----------------+ |
| 709 |
| Adelaide | 1295 | 1158259 | 600.5 | |
| 710 |
| Brisbane | 5905 | 1857594 | 1146.4 | |
| 711 |
| Sydney | 2058 | 4336374 | 1214.8 | |
| 712 |
| Melbourne | 1566 | 3806092 | 646.9 | |
| 713 |
| Perth | 5386 | 1554769 | 869.4 | |
| 714 |
+-----------+------+------------+-----------------+""" |
| 715 |
|
| 716 |
def filter_function(self, vals: RowType) -> bool: |
| 717 |
return vals[2] > 999999 |
| 718 |
|
| 719 |
def test_row_filter(self, city_data: PrettyTable) -> None: |
| 720 |
city_data.row_filter = self.filter_function |
| 721 |
assert city_data.row_filter == self.filter_function |
| 722 |
assert self.EXPECTED_RESULT == city_data.get_string() |
| 723 |
|
| 724 |
def test_row_filter_at_class_declaration(self) -> None: |
| 725 |
table = PrettyTable( |
| 726 |
field_names=CITY_DATA_HEADER, |
| 727 |
row_filter=self.filter_function, |
| 728 |
) |
| 729 |
for row in CITY_DATA: |
| 730 |
table.add_row(row) |
| 731 |
assert table.row_filter == self.filter_function |
| 732 |
assert self.EXPECTED_RESULT == table.get_string().strip() |
| 733 |
|
| 734 |
|
| 735 |
@pytest.fixture(scope="function") |
| 736 |
def float_pt() -> PrettyTable: |
| 737 |
table = PrettyTable(["Constant", "Value"]) |
| 738 |
table.add_row(["Pi", pi]) |
| 739 |
table.add_row(["e", e]) |
| 740 |
table.add_row(["sqrt(2)", sqrt(2)]) |
| 741 |
return table |
| 742 |
|
| 743 |
|
| 744 |
class TestFloatFormat: |
| 745 |
def test_no_decimals(self, float_pt: PrettyTable) -> None: |
| 746 |
float_pt.float_format = ".0f" |
| 747 |
assert "." not in float_pt.get_string() |
| 748 |
|
| 749 |
def test_round_to_5dp(self, float_pt: PrettyTable) -> None: |
| 750 |
float_pt.float_format = ".5f" |
| 751 |
string = float_pt.get_string() |
| 752 |
assert "3.14159" in string |
| 753 |
assert "3.141592" not in string |
| 754 |
assert "2.71828" in string |
| 755 |
assert "2.718281" not in string |
| 756 |
assert "2.718282" not in string |
| 757 |
assert "1.41421" in string |
| 758 |
assert "1.414213" not in string |
| 759 |
|
| 760 |
def test_pad_with_2zeroes(self, float_pt: PrettyTable) -> None: |
| 761 |
float_pt.float_format = "06.2f" |
| 762 |
string = float_pt.get_string() |
| 763 |
assert "003.14" in string |
| 764 |
assert "002.72" in string |
| 765 |
assert "001.41" in string |
| 766 |
|
| 767 |
|
| 768 |
class TestColumnFormattingfromDict: |
| 769 |
def test_set_align_format(self, city_data: PrettyTable) -> None: |
| 770 |
city_data.align = {"Annual Rainfall": "r"} |
| 771 |
assert ( |
| 772 |
city_data.get_string() |
| 773 |
== """ |
| 774 |
+-----------+------+------------+-----------------+ |
| 775 |
| City name | Area | Population | Annual Rainfall | |
| 776 |
+-----------+------+------------+-----------------+ |
| 777 |
| Adelaide | 1295 | 1158259 | 600.5 | |
| 778 |
| Brisbane | 5905 | 1857594 | 1146.4 | |
| 779 |
| Darwin | 112 | 120900 | 1714.7 | |
| 780 |
| Hobart | 1357 | 205556 | 619.5 | |
| 781 |
| Sydney | 2058 | 4336374 | 1214.8 | |
| 782 |
| Melbourne | 1566 | 3806092 | 646.9 | |
| 783 |
| Perth | 5386 | 1554769 | 869.4 | |
| 784 |
+-----------+------+------------+-----------------+ |
| 785 |
""".strip() |
| 786 |
) |
| 787 |
|
| 788 |
def test_set_valign_format(self, city_data: PrettyTable) -> None: |
| 789 |
table = PrettyTable( |
| 790 |
["Field 1", "Field 2", "Field 3", "Field 4", "Field 5", "Field 6"], |
| 791 |
) |
| 792 |
table.valign = {"Field 1": "m"} |
| 793 |
table.max_width = {"Field 2": 20, "Field 4": 10, "Field 6": 10} |
| 794 |
table.add_row( |
| 795 |
[ |
| 796 |
"Lorem", |
| 797 |
"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam ", |
| 798 |
"ipsum", |
| 799 |
"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam ", |
| 800 |
"dolor", |
| 801 |
"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam ", |
| 802 |
] |
| 803 |
) |
| 804 |
|
| 805 |
assert ( |
| 806 |
table.get_string() |
| 807 |
== """ |
| 808 |
+---------+----------------------+---------+------------+---------+------------+ |
| 809 |
| Field 1 | Field 2 | Field 3 | Field 4 | Field 5 | Field 6 | |
| 810 |
+---------+----------------------+---------+------------+---------+------------+ |
| 811 |
| | Lorem ipsum dolor | ipsum | Lorem | dolor | Lorem | |
| 812 |
| | sit amet, consetetur | | ipsum | | ipsum | |
| 813 |
| | sadipscing elitr, | | dolor sit | | dolor sit | |
| 814 |
| Lorem | sed diam | | amet, | | amet, | |
| 815 |
| | | | consetetur | | consetetur | |
| 816 |
| | | | sadipscing | | sadipscing | |
| 817 |
| | | | elitr, sed | | elitr, sed | |
| 818 |
| | | | diam | | diam | |
| 819 |
+---------+----------------------+---------+------------+---------+------------+ |
| 820 |
""".strip() |
| 821 |
) |
| 822 |
|
| 823 |
def test_max_width( |
| 824 |
self, |
| 825 |
) -> None: |
| 826 |
table = PrettyTable( |
| 827 |
["Field 1", "Field 2", "Field 3", "Field 4", "Field 5", "Field 6"], |
| 828 |
) |
| 829 |
table.max_width = {"Field 2": 20, "Field 4": 10, "Field 6": 10} |
| 830 |
table.add_row( |
| 831 |
[ |
| 832 |
"Lorem", |
| 833 |
"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam ", |
| 834 |
"ipsum", |
| 835 |
"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam ", |
| 836 |
"dolor", |
| 837 |
"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam ", |
| 838 |
] |
| 839 |
) |
| 840 |
|
| 841 |
assert ( |
| 842 |
table.get_string() |
| 843 |
== """ |
| 844 |
+---------+----------------------+---------+------------+---------+------------+ |
| 845 |
| Field 1 | Field 2 | Field 3 | Field 4 | Field 5 | Field 6 | |
| 846 |
+---------+----------------------+---------+------------+---------+------------+ |
| 847 |
| Lorem | Lorem ipsum dolor | ipsum | Lorem | dolor | Lorem | |
| 848 |
| | sit amet, consetetur | | ipsum | | ipsum | |
| 849 |
| | sadipscing elitr, | | dolor sit | | dolor sit | |
| 850 |
| | sed diam | | amet, | | amet, | |
| 851 |
| | | | consetetur | | consetetur | |
| 852 |
| | | | sadipscing | | sadipscing | |
| 853 |
| | | | elitr, sed | | elitr, sed | |
| 854 |
| | | | diam | | diam | |
| 855 |
+---------+----------------------+---------+------------+---------+------------+ |
| 856 |
""".strip() |
| 857 |
) |
| 858 |
|
| 859 |
def test_min_width(self, city_data: PrettyTable) -> None: |
| 860 |
city_data.min_width = { |
| 861 |
"City name": 20, |
| 862 |
"Area": 10, |
| 863 |
"Population": 20, |
| 864 |
"Annual Rainfall": 20, |
| 865 |
} |
| 866 |
assert ( |
| 867 |
city_data.get_string() |
| 868 |
== """ |
| 869 |
+----------------------+------------+----------------------+----------------------+ |
| 870 |
| City name | Area | Population | Annual Rainfall | |
| 871 |
+----------------------+------------+----------------------+----------------------+ |
| 872 |
| Adelaide | 1295 | 1158259 | 600.5 | |
| 873 |
| Brisbane | 5905 | 1857594 | 1146.4 | |
| 874 |
| Darwin | 112 | 120900 | 1714.7 | |
| 875 |
| Hobart | 1357 | 205556 | 619.5 | |
| 876 |
| Sydney | 2058 | 4336374 | 1214.8 | |
| 877 |
| Melbourne | 1566 | 3806092 | 646.9 | |
| 878 |
| Perth | 5386 | 1554769 | 869.4 | |
| 879 |
+----------------------+------------+----------------------+----------------------+ |
| 880 |
""".strip() |
| 881 |
) |
| 882 |
|
| 883 |
def test_set_int_format(self, city_data: PrettyTable) -> None: |
| 884 |
city_data.int_format = {"Population": "20"} |
| 885 |
assert ( |
| 886 |
city_data.get_string() |
| 887 |
== """ |
| 888 |
+-----------+------+----------------------+-----------------+ |
| 889 |
| City name | Area | Population | Annual Rainfall | |
| 890 |
+-----------+------+----------------------+-----------------+ |
| 891 |
| Adelaide | 1295 | 1158259 | 600.5 | |
| 892 |
| Brisbane | 5905 | 1857594 | 1146.4 | |
| 893 |
| Darwin | 112 | 120900 | 1714.7 | |
| 894 |
| Hobart | 1357 | 205556 | 619.5 | |
| 895 |
| Sydney | 2058 | 4336374 | 1214.8 | |
| 896 |
| Melbourne | 1566 | 3806092 | 646.9 | |
| 897 |
| Perth | 5386 | 1554769 | 869.4 | |
| 898 |
+-----------+------+----------------------+-----------------+ |
| 899 |
""".strip() |
| 900 |
) |
| 901 |
|
| 902 |
def test_set_float_format(self, city_data: PrettyTable) -> None: |
| 903 |
city_data.float_format = {"Annual Rainfall": "4.2"} |
| 904 |
assert ( |
| 905 |
city_data.get_string() |
| 906 |
== """ |
| 907 |
+-----------+------+------------+-----------------+ |
| 908 |
| City name | Area | Population | Annual Rainfall | |
| 909 |
+-----------+------+------------+-----------------+ |
| 910 |
| Adelaide | 1295 | 1158259 | 600.50 | |
| 911 |
| Brisbane | 5905 | 1857594 | 1146.40 | |
| 912 |
| Darwin | 112 | 120900 | 1714.70 | |
| 913 |
| Hobart | 1357 | 205556 | 619.50 | |
| 914 |
| Sydney | 2058 | 4336374 | 1214.80 | |
| 915 |
| Melbourne | 1566 | 3806092 | 646.90 | |
| 916 |
| Perth | 5386 | 1554769 | 869.40 | |
| 917 |
+-----------+------+------------+-----------------+ |
| 918 |
""".strip() |
| 919 |
) |
| 920 |
|
| 921 |
def test_set_custom_format(self, city_data: PrettyTable) -> None: |
| 922 |
city_data.custom_format = {"Annual Rainfall": lambda f, v: f"{v:.2f}"} |
| 923 |
assert ( |
| 924 |
city_data.get_string() |
| 925 |
== """ |
| 926 |
+-----------+------+------------+-----------------+ |
| 927 |
| City name | Area | Population | Annual Rainfall | |
| 928 |
+-----------+------+------------+-----------------+ |
| 929 |
| Adelaide | 1295 | 1158259 | 600.50 | |
| 930 |
| Brisbane | 5905 | 1857594 | 1146.40 | |
| 931 |
| Darwin | 112 | 120900 | 1714.70 | |
| 932 |
| Hobart | 1357 | 205556 | 619.50 | |
| 933 |
| Sydney | 2058 | 4336374 | 1214.80 | |
| 934 |
| Melbourne | 1566 | 3806092 | 646.90 | |
| 935 |
| Perth | 5386 | 1554769 | 869.40 | |
| 936 |
+-----------+------+------------+-----------------+ |
| 937 |
""".strip() |
| 938 |
) |
| 939 |
|
| 940 |
def test_set_none_format(self, city_data: PrettyTable) -> None: |
| 941 |
city_data.clear_rows() |
| 942 |
city_data.add_row([None, None, None, None]) |
| 943 |
city_data.none_format = {"Annual Rainfall": "N/A"} |
| 944 |
assert ( |
| 945 |
city_data.get_string() |
| 946 |
== """ |
| 947 |
+-----------+------+------------+-----------------+ |
| 948 |
| City name | Area | Population | Annual Rainfall | |
| 949 |
+-----------+------+------------+-----------------+ |
| 950 |
| None | None | None | N/A | |
| 951 |
+-----------+------+------------+-----------------+ |
| 952 |
""".strip() |
| 953 |
) |
| 954 |
|
| 955 |
|
| 956 |
class TestBreakLine: |
| 957 |
@pytest.mark.parametrize( |
| 958 |
["rows", "hrule", "expected_result"], |
| 959 |
[ |
| 960 |
( |
| 961 |
[["value 1", "value2\nsecond line"], ["value 3", "value4"]], |
| 962 |
HRuleStyle.ALL, |
| 963 |
""" |
| 964 |
+---------+-------------+ |
| 965 |
| Field 1 | Field 2 | |
| 966 |
+---------+-------------+ |
| 967 |
| value 1 | value2 | |
| 968 |
| | second line | |
| 969 |
+---------+-------------+ |
| 970 |
| value 3 | value4 | |
| 971 |
+---------+-------------+ |
| 972 |
""", |
| 973 |
), |
| 974 |
( |
| 975 |
[ |
| 976 |
["value 1", "value2\nsecond line"], |
| 977 |
["value 3\n\nother line", "value4\n\n\nvalue5"], |
| 978 |
], |
| 979 |
HRuleStyle.ALL, |
| 980 |
""" |
| 981 |
+------------+-------------+ |
| 982 |
| Field 1 | Field 2 | |
| 983 |
+------------+-------------+ |
| 984 |
| value 1 | value2 | |
| 985 |
| | second line | |
| 986 |
+------------+-------------+ |
| 987 |
| value 3 | value4 | |
| 988 |
| | | |
| 989 |
| other line | | |
| 990 |
| | value5 | |
| 991 |
+------------+-------------+ |
| 992 |
""", |
| 993 |
), |
| 994 |
( |
| 995 |
[ |
| 996 |
["value 1", "value2\nsecond line"], |
| 997 |
["value 3\n\nother line", "value4\n\n\nvalue5"], |
| 998 |
], |
| 999 |
HRuleStyle.FRAME, |
| 1000 |
""" |
| 1001 |
+------------+-------------+ |
| 1002 |
| Field 1 | Field 2 | |
| 1003 |
+------------+-------------+ |
| 1004 |
| value 1 | value2 | |
| 1005 |
| | second line | |
| 1006 |
| value 3 | value4 | |
| 1007 |
| | | |
| 1008 |
| other line | | |
| 1009 |
| | value5 | |
| 1010 |
+------------+-------------+ |
| 1011 |
""", |
| 1012 |
), |
| 1013 |
], |
| 1014 |
) |
| 1015 |
def test_break_line_ascii( |
| 1016 |
self, rows: list[list[Any]], hrule: int, expected_result: str |
| 1017 |
) -> None: |
| 1018 |
table = PrettyTable(["Field 1", "Field 2"]) |
| 1019 |
for row in rows: |
| 1020 |
table.add_row(row) |
| 1021 |
result = table.get_string(hrules=hrule) |
| 1022 |
assert result.strip() == expected_result.strip() |
| 1023 |
|
| 1024 |
|
| 1025 |
class TestFromDB: |
| 1026 |
@pytest.mark.usefixtures("init_db") |
| 1027 |
def test_non_select_cursor(self, db_cursor: sqlite3.Cursor) -> None: |
| 1028 |
db_cursor.execute(f"INSERT INTO cities VALUES {tuple(CITY_DATA[0])}") |
| 1029 |
assert from_db_cursor(db_cursor) is None |
| 1030 |
|
| 1031 |
|
| 1032 |
class TestCsvOutput: |
| 1033 |
def test_csv_output(self, helper_table: PrettyTable) -> None: |
| 1034 |
assert helper_table.get_csv_string(delimiter="\t", header=False) == ( |
| 1035 |
"1\tvalue 1\tvalue2\tvalue3\r\n" |
| 1036 |
"4\tvalue 4\tvalue5\tvalue6\r\n" |
| 1037 |
"7\tvalue 7\tvalue8\tvalue9\r\n" |
| 1038 |
) |
| 1039 |
assert helper_table.get_csv_string() == ( |
| 1040 |
",Field 1,Field 2,Field 3\r\n" |
| 1041 |
"1,value 1,value2,value3\r\n" |
| 1042 |
"4,value 4,value5,value6\r\n" |
| 1043 |
"7,value 7,value8,value9\r\n" |
| 1044 |
) |
| 1045 |
options = {"fields": ["Field 1", "Field 3"]} |
| 1046 |
assert helper_table.get_csv_string(**options) == ( |
| 1047 |
"Field 1,Field 3\r\n" |
| 1048 |
"value 1,value3\r\n" |
| 1049 |
"value 4,value6\r\n" |
| 1050 |
"value 7,value9\r\n" |
| 1051 |
) |
| 1052 |
|
| 1053 |
|
| 1054 |
def test_paginate(city_data: PrettyTable) -> None: |
| 1055 |
expected_page_1 = """ |
| 1056 |
+-----------+------+------------+-----------------+ |
| 1057 |
| City name | Area | Population | Annual Rainfall | |
| 1058 |
+-----------+------+------------+-----------------+ |
| 1059 |
| Adelaide | 1295 | 1158259 | 600.5 | |
| 1060 |
| Brisbane | 5905 | 1857594 | 1146.4 | |
| 1061 |
| Darwin | 112 | 120900 | 1714.7 | |
| 1062 |
| Hobart | 1357 | 205556 | 619.5 | |
| 1063 |
+-----------+------+------------+-----------------+""".strip() |
| 1064 |
expected_page_2 = """ |
| 1065 |
+-----------+------+------------+-----------------+ |
| 1066 |
| City name | Area | Population | Annual Rainfall | |
| 1067 |
+-----------+------+------------+-----------------+ |
| 1068 |
| Sydney | 2058 | 4336374 | 1214.8 | |
| 1069 |
| Melbourne | 1566 | 3806092 | 646.9 | |
| 1070 |
| Perth | 5386 | 1554769 | 869.4 | |
| 1071 |
+-----------+------+------------+-----------------+""".strip() |
| 1072 |
|
| 1073 |
paginated = city_data.paginate(page_length=4).strip() |
| 1074 |
assert paginated.startswith(expected_page_1) |
| 1075 |
assert "\f" in paginated |
| 1076 |
assert paginated.endswith(expected_page_2) |
| 1077 |
|
| 1078 |
paginated = city_data.paginate(page_length=4, line_break="\n") |
| 1079 |
assert "\f" not in paginated |
| 1080 |
assert "\n" in paginated |
| 1081 |
|
| 1082 |
|
| 1083 |
def test_autoindex(city_data: PrettyTable) -> None: |
| 1084 |
"""Testing that a table with a custom index row is |
| 1085 |
equal to the one produced by the function |
| 1086 |
.add_autoindex() |
| 1087 |
""" |
| 1088 |
city_data.field_names = CITY_DATA_HEADER |
| 1089 |
city_data.add_autoindex(fieldname="Test") |
| 1090 |
|
| 1091 |
table2 = PrettyTable() |
| 1092 |
table2.field_names = ["Test"] + CITY_DATA_HEADER |
| 1093 |
for idx, row in enumerate(CITY_DATA): |
| 1094 |
table2.add_row([idx + 1] + row) |
| 1095 |
|
| 1096 |
assert str(city_data) == str(table2) |
| 1097 |
|
| 1098 |
|
| 1099 |
@pytest.fixture(scope="function") |
| 1100 |
def unpadded_pt() -> PrettyTable: |
| 1101 |
table = PrettyTable(header=False, padding_width=0) |
| 1102 |
table.add_row(list("abc")) |
| 1103 |
table.add_row(list("def")) |
| 1104 |
table.add_row(list("g..")) |
| 1105 |
return table |
| 1106 |
|
| 1107 |
|
| 1108 |
class TestUnpaddedTable: |
| 1109 |
def test_unbordered(self, unpadded_pt: PrettyTable) -> None: |
| 1110 |
unpadded_pt.border = False |
| 1111 |
result = unpadded_pt.get_string() |
| 1112 |
expected = """ |
| 1113 |
abc |
| 1114 |
def |
| 1115 |
g.. |
| 1116 |
""" |
| 1117 |
assert result.strip() == expected.strip() |
| 1118 |
|
| 1119 |
def test_bordered(self, unpadded_pt: PrettyTable) -> None: |
| 1120 |
unpadded_pt.border = True |
| 1121 |
result = unpadded_pt.get_string() |
| 1122 |
expected = """ |
| 1123 |
+-+-+-+ |
| 1124 |
|a|b|c| |
| 1125 |
|d|e|f| |
| 1126 |
|g|.|.| |
| 1127 |
+-+-+-+ |
| 1128 |
""" |
| 1129 |
assert result.strip() == expected.strip() |
| 1130 |
|
| 1131 |
|
| 1132 |
class TestCustomFormatter: |
| 1133 |
def test_init_custom_format_is_empty(self) -> None: |
| 1134 |
table = PrettyTable() |
| 1135 |
assert table.custom_format == {} |
| 1136 |
|
| 1137 |
def test_init_custom_format_set_value(self) -> None: |
| 1138 |
table = PrettyTable( |
| 1139 |
custom_format={"col1": (lambda col_name, value: f"{value:.2}")} |
| 1140 |
) |
| 1141 |
assert len(table.custom_format) == 1 |
| 1142 |
|
| 1143 |
def test_init_custom_format_throw_error_is_not_callable(self) -> None: |
| 1144 |
with pytest.raises(ValueError) as e: |
| 1145 |
PrettyTable(custom_format={"col1": "{:.2}"}) |
| 1146 |
|
| 1147 |
assert "Invalid value for custom_format.col1. Must be a function." in str( |
| 1148 |
e.value |
| 1149 |
) |
| 1150 |
|
| 1151 |
def test_can_set_custom_format_from_property_setter(self) -> None: |
| 1152 |
table = PrettyTable() |
| 1153 |
table.custom_format = {"col1": (lambda col_name, value: f"{value:.2}")} |
| 1154 |
assert len(table.custom_format) == 1 |
| 1155 |
|
| 1156 |
def test_set_custom_format_to_none_set_empty_dict(self) -> None: |
| 1157 |
table = PrettyTable() |
| 1158 |
table.custom_format = None |
| 1159 |
assert isinstance(table.custom_format, dict) |
| 1160 |
|
| 1161 |
def test_set_custom_format_invalid_type_throw_error(self) -> None: |
| 1162 |
table = PrettyTable() |
| 1163 |
with pytest.raises(TypeError) as e: |
| 1164 |
table.custom_format = "Some String" # type: ignore[assignment] |
| 1165 |
assert "The custom_format property need to be a dictionary or callable" in str( |
| 1166 |
e.value |
| 1167 |
) |
| 1168 |
|
| 1169 |
def test_use_custom_formatter_for_int(self, city_data: PrettyTable) -> None: |
| 1170 |
city_data.custom_format["Annual Rainfall"] = lambda n, v: f"{v:.2f}" |
| 1171 |
assert ( |
| 1172 |
city_data.get_string().strip() |
| 1173 |
== """ |
| 1174 |
+-----------+------+------------+-----------------+ |
| 1175 |
| City name | Area | Population | Annual Rainfall | |
| 1176 |
+-----------+------+------------+-----------------+ |
| 1177 |
| Adelaide | 1295 | 1158259 | 600.50 | |
| 1178 |
| Brisbane | 5905 | 1857594 | 1146.40 | |
| 1179 |
| Darwin | 112 | 120900 | 1714.70 | |
| 1180 |
| Hobart | 1357 | 205556 | 619.50 | |
| 1181 |
| Sydney | 2058 | 4336374 | 1214.80 | |
| 1182 |
| Melbourne | 1566 | 3806092 | 646.90 | |
| 1183 |
| Perth | 5386 | 1554769 | 869.40 | |
| 1184 |
+-----------+------+------------+-----------------+ |
| 1185 |
""".strip() |
| 1186 |
) |
| 1187 |
|
| 1188 |
def test_custom_format_multi_type(self) -> None: |
| 1189 |
table = PrettyTable(["col_date", "col_str", "col_float", "col_int"]) |
| 1190 |
table.add_row([dt.date(2021, 1, 1), "January", 12345.12345, 12345678]) |
| 1191 |
table.add_row([dt.date(2021, 2, 1), "February", 54321.12345, 87654321]) |
| 1192 |
table.custom_format["col_date"] = lambda f, v: v.strftime("%d %b %Y") |
| 1193 |
table.custom_format["col_float"] = lambda f, v: f"{v:.3f}" |
| 1194 |
table.custom_format["col_int"] = lambda f, v: f"{v:,}" |
| 1195 |
assert ( |
| 1196 |
table.get_string().strip() |
| 1197 |
== """ |
| 1198 |
+-------------+----------+-----------+------------+ |
| 1199 |
| col_date | col_str | col_float | col_int | |
| 1200 |
+-------------+----------+-----------+------------+ |
| 1201 |
| 01 Jan 2021 | January | 12345.123 | 12,345,678 | |
| 1202 |
| 01 Feb 2021 | February | 54321.123 | 87,654,321 | |
| 1203 |
+-------------+----------+-----------+------------+ |
| 1204 |
""".strip() |
| 1205 |
) |
| 1206 |
|
| 1207 |
def test_custom_format_multi_type_using_on_function(self) -> None: |
| 1208 |
table = PrettyTable(["col_date", "col_str", "col_float", "col_int"]) |
| 1209 |
table.add_row([dt.date(2021, 1, 1), "January", 12345.12345, 12345678]) |
| 1210 |
table.add_row([dt.date(2021, 2, 1), "February", 54321.12345, 87654321]) |
| 1211 |
|
| 1212 |
def my_format(col: str, value: Any) -> str: |
| 1213 |
if col == "col_date": |
| 1214 |
return value.strftime("%d %b %Y") |
| 1215 |
if col == "col_float": |
| 1216 |
return f"{value:.3f}" |
| 1217 |
if col == "col_int": |
| 1218 |
return f"{value:,}" |
| 1219 |
return str(value) |
| 1220 |
|
| 1221 |
table.custom_format = my_format |
| 1222 |
assert ( |
| 1223 |
table.get_string().strip() |
| 1224 |
== """ |
| 1225 |
+-------------+----------+-----------+------------+ |
| 1226 |
| col_date | col_str | col_float | col_int | |
| 1227 |
+-------------+----------+-----------+------------+ |
| 1228 |
| 01 Jan 2021 | January | 12345.123 | 12,345,678 | |
| 1229 |
| 01 Feb 2021 | February | 54321.123 | 87,654,321 | |
| 1230 |
+-------------+----------+-----------+------------+ |
| 1231 |
""".strip() |
| 1232 |
) |
| 1233 |
|
| 1234 |
|
| 1235 |
class TestRepr: |
| 1236 |
def test_default_repr(self, row_prettytable: PrettyTable) -> None: |
| 1237 |
assert row_prettytable.__str__() == row_prettytable.__repr__() |
| 1238 |
|
| 1239 |
def test_jupyter_repr(self, row_prettytable: PrettyTable) -> None: |
| 1240 |
assert row_prettytable._repr_html_() == row_prettytable.get_html_string() |
| 1241 |
|
| 1242 |
|
| 1243 |
class TestBreakOnHyphens: |
| 1244 |
row = [ |
| 1245 |
"bluedevil breeze breeze-gtk eos-bash-shared glib2 " |
| 1246 |
"kactivitymanagerd kde-cli-tools kde-gtk-config kdecoration" |
| 1247 |
] |
| 1248 |
EXPECTED_TRUE = """+------------------------------------------+ |
| 1249 |
| Field 1 | |
| 1250 |
+------------------------------------------+ |
| 1251 |
| bluedevil breeze breeze-gtk eos-bash- | |
| 1252 |
| shared glib2 kactivitymanagerd kde-cli- | |
| 1253 |
| tools kde-gtk-config kdecoration | |
| 1254 |
+------------------------------------------+""" |
| 1255 |
EXPECTED_FALSE = """+------------------------------------------+ |
| 1256 |
| Field 1 | |
| 1257 |
+------------------------------------------+ |
| 1258 |
| bluedevil breeze breeze-gtk | |
| 1259 |
| eos-bash-shared glib2 kactivitymanagerd | |
| 1260 |
| kde-cli-tools kde-gtk-config kdecoration | |
| 1261 |
+------------------------------------------+""" |
| 1262 |
|
| 1263 |
def test_break_on_hyphens(self) -> None: |
| 1264 |
table = PrettyTable(max_width=40) |
| 1265 |
table.break_on_hyphens = False |
| 1266 |
assert not table.break_on_hyphens |
| 1267 |
table.add_row(self.row) |
| 1268 |
assert table.get_string().strip() == self.EXPECTED_FALSE |
| 1269 |
|
| 1270 |
def test_break_on_hyphens_on_init(self) -> None: |
| 1271 |
table = PrettyTable(max_width=40, break_on_hyphens=False) |
| 1272 |
assert not table._break_on_hyphens |
| 1273 |
assert not table.break_on_hyphens |
| 1274 |
table.add_row(self.row) |
| 1275 |
assert table.get_string().strip() == self.EXPECTED_FALSE |
| 1276 |
|
| 1277 |
def test_break_on_hyphens_default(self) -> None: |
| 1278 |
table = PrettyTable(max_width=40) |
| 1279 |
assert table.break_on_hyphens |
| 1280 |
table.add_row(self.row) |
| 1281 |
assert table.get_string().strip() == self.EXPECTED_TRUE |
| 1282 |
|
| 1283 |
|
| 1284 |
class TestWidth: |
| 1285 |
colored = "\033[31mC\033[32mO\033[31mL\033[32mO\033[31mR\033[32mE\033[31mD\033[0m" |
| 1286 |
|
| 1287 |
def test_color(self) -> None: |
| 1288 |
table = PrettyTable(["Field 1", "Field 2"]) |
| 1289 |
table.add_row([self.colored, self.colored]) |
| 1290 |
table.add_row(["nothing", "neither"]) |
| 1291 |
result = table.get_string() |
| 1292 |
assert ( |
| 1293 |
result.strip() |
| 1294 |
== f""" |
| 1295 |
+---------+---------+ |
| 1296 |
| Field 1 | Field 2 | |
| 1297 |
+---------+---------+ |
| 1298 |
| {self.colored} | {self.colored} | |
| 1299 |
| nothing | neither | |
| 1300 |
+---------+---------+ |
| 1301 |
""".strip() |
| 1302 |
) |
| 1303 |
|
| 1304 |
def test_reset(self) -> None: |
| 1305 |
table = PrettyTable(["Field 1", "Field 2"]) |
| 1306 |
table.add_row(["abc def\033(B", "\033[31mabc def\033[m"]) |
| 1307 |
table.add_row(["nothing", "neither"]) |
| 1308 |
result = table.get_string() |
| 1309 |
assert ( |
| 1310 |
result.strip() |
| 1311 |
== """ |
| 1312 |
+---------+---------+ |
| 1313 |
| Field 1 | Field 2 | |
| 1314 |
+---------+---------+ |
| 1315 |
| abc def\033(B | \033[31mabc def\033[m | |
| 1316 |
| nothing | neither | |
| 1317 |
+---------+---------+ |
| 1318 |
""".strip() |
| 1319 |
) |
| 1320 |
|
| 1321 |
@pytest.mark.parametrize( |
| 1322 |
"loops, fields, desired_width, border, internal_border", |
| 1323 |
[ |
| 1324 |
(15, ["Test table"], 20, True, False), |
| 1325 |
(16, ["Test table"], 21, True, False), |
| 1326 |
(18, ["Test table", "Test table 2"], 40, True, False), |
| 1327 |
(19, ["Test table", "Test table 2"], 41, True, False), |
| 1328 |
(21, ["Test table", "Test col 2", "Test col 3"], 50, True, False), |
| 1329 |
(22, ["Test table", "Test col 2", "Test col 3"], 51, True, False), |
| 1330 |
(19, ["Test table"], 20, False, False), |
| 1331 |
(20, ["Test table"], 21, False, False), |
| 1332 |
(25, ["Test table", "Test table 2"], 40, False, False), |
| 1333 |
(26, ["Test table", "Test table 2"], 41, False, False), |
| 1334 |
(25, ["Test table", "Test col 2", "Test col 3"], 50, False, False), |
| 1335 |
(26, ["Test table", "Test col 2", "Test col 3"], 51, False, False), |
| 1336 |
(18, ["Test table"], 20, False, True), |
| 1337 |
(19, ["Test table"], 21, False, True), |
| 1338 |
(23, ["Test table", "Test table 2"], 40, False, True), |
| 1339 |
(24, ["Test table", "Test table 2"], 41, False, True), |
| 1340 |
(22, ["Test table", "Test col 2", "Test col 3"], 50, False, True), |
| 1341 |
(23, ["Test table", "Test col 2", "Test col 3"], 51, False, True), |
| 1342 |
], |
| 1343 |
) |
| 1344 |
def test_min_table_width( |
| 1345 |
self, |
| 1346 |
loops: int, |
| 1347 |
fields: list[str], |
| 1348 |
desired_width: int, |
| 1349 |
border: bool, |
| 1350 |
internal_border: bool, |
| 1351 |
) -> None: |
| 1352 |
for col_width in range(loops): |
| 1353 |
x = prettytable.PrettyTable() |
| 1354 |
x.border = border |
| 1355 |
x.preserve_internal_border = internal_border |
| 1356 |
x.field_names = fields |
| 1357 |
x.add_row(["X" * col_width] + ["" for _ in range(len(fields) - 1)]) |
| 1358 |
x.min_table_width = desired_width |
| 1359 |
t = x.get_string() |
| 1360 |
if border is False and internal_border is False: |
| 1361 |
assert [len(x) for x in t.split("\n")] == [desired_width, desired_width] |
| 1362 |
elif border is False and internal_border is True: |
| 1363 |
assert [len(x) for x in t.split("\n")] == [ |
| 1364 |
desired_width, |
| 1365 |
desired_width - 1, |
| 1366 |
desired_width, |
| 1367 |
] |
| 1368 |
else: |
| 1369 |
assert [len(x) for x in t.split("\n")] == [ |
| 1370 |
desired_width, |
| 1371 |
desired_width, |
| 1372 |
desired_width, |
| 1373 |
desired_width, |
| 1374 |
desired_width, |
| 1375 |
] |
| 1376 |
|
| 1377 |
def test_max_table_width(self) -> None: |
| 1378 |
table = PrettyTable() |
| 1379 |
table.max_table_width = 5 |
| 1380 |
table.add_row([0]) |
| 1381 |
|
| 1382 |
# FIXME: Table is wider than table.max_table_width |
| 1383 |
assert ( |
| 1384 |
table.get_string().strip() |
| 1385 |
== """ |
| 1386 |
+----+ |
| 1387 |
| Fi | |
| 1388 |
+----+ |
| 1389 |
| 0 | |
| 1390 |
+----+ |
| 1391 |
""".strip() |
| 1392 |
) |
| 1393 |
|
| 1394 |
def test_max_table_width_wide(self) -> None: |
| 1395 |
table = PrettyTable() |
| 1396 |
table.max_table_width = 52 |
| 1397 |
table.add_row( |
| 1398 |
[ |
| 1399 |
0, |
| 1400 |
0, |
| 1401 |
0, |
| 1402 |
0, |
| 1403 |
0, |
| 1404 |
"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam " |
| 1405 |
"nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam " |
| 1406 |
"erat, sed diam voluptua", |
| 1407 |
] |
| 1408 |
) |
| 1409 |
|
| 1410 |
assert ( |
| 1411 |
table.get_string().strip() |
| 1412 |
== """ |
| 1413 |
+---+---+---+---+---+------------------------------+ |
| 1414 |
| F | F | F | F | F | Field 6 | |
| 1415 |
+---+---+---+---+---+------------------------------+ |
| 1416 |
| 0 | 0 | 0 | 0 | 0 | Lorem ipsum dolor sit amet, | |
| 1417 |
| | | | | | consetetur sadipscing elitr, | |
| 1418 |
| | | | | | sed diam nonumy eirmod | |
| 1419 |
| | | | | | tempor invidunt ut labore et | |
| 1420 |
| | | | | | dolore magna aliquyam erat, | |
| 1421 |
| | | | | | sed diam voluptua | |
| 1422 |
+---+---+---+---+---+------------------------------+""".strip() |
| 1423 |
) |
| 1424 |
|
| 1425 |
def test_max_table_width_wide2(self) -> None: |
| 1426 |
table = PrettyTable() |
| 1427 |
table.max_table_width = 70 |
| 1428 |
table.add_row( |
| 1429 |
[ |
| 1430 |
"Lorem", |
| 1431 |
"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam ", |
| 1432 |
"ipsum", |
| 1433 |
"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam ", |
| 1434 |
"dolor", |
| 1435 |
"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam ", |
| 1436 |
] |
| 1437 |
) |
| 1438 |
|
| 1439 |
assert ( |
| 1440 |
table.get_string().strip() |
| 1441 |
== """ |
| 1442 |
+---+-----------------+---+-----------------+---+-----------------+ |
| 1443 |
| F | Field 2 | F | Field 4 | F | Field 6 | |
| 1444 |
+---+-----------------+---+-----------------+---+-----------------+ |
| 1445 |
| L | Lorem ipsum | i | Lorem ipsum | d | Lorem ipsum | |
| 1446 |
| o | dolor sit amet, | p | dolor sit amet, | o | dolor sit amet, | |
| 1447 |
| r | consetetur | s | consetetur | l | consetetur | |
| 1448 |
| e | sadipscing | u | sadipscing | o | sadipscing | |
| 1449 |
| m | elitr, sed diam | m | elitr, sed diam | r | elitr, sed diam | |
| 1450 |
+---+-----------------+---+-----------------+---+-----------------+""".strip() |
| 1451 |
) |
| 1452 |
|
| 1453 |
@pytest.mark.parametrize("set_width_parameter", [True, False]) |
| 1454 |
def test_table_max_width_wo_header_width(self, set_width_parameter: bool) -> None: |
| 1455 |
headers = [ |
| 1456 |
"A Field Name", |
| 1457 |
"B Field Name", |
| 1458 |
"D Field Name", |
| 1459 |
"E Field Name", |
| 1460 |
"F Field Name", |
| 1461 |
"G Field Name", |
| 1462 |
"H Field Name", |
| 1463 |
"I Field Name", |
| 1464 |
"J Field Name", |
| 1465 |
"K Field Name", |
| 1466 |
"L Field Name", |
| 1467 |
"M Field Name", |
| 1468 |
] |
| 1469 |
row = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] |
| 1470 |
expected = """+---+---+---+---+---+---+---+---+---+---+----+----+ |
| 1471 |
| A | B | D | E | F | G | H | I | J | K | L | M | |
| 1472 |
+---+---+---+---+---+---+---+---+---+---+----+----+ |
| 1473 |
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | |
| 1474 |
+---+---+---+---+---+---+---+---+---+---+----+----+""" |
| 1475 |
|
| 1476 |
if set_width_parameter: |
| 1477 |
table = PrettyTable(headers, use_header_width=False) |
| 1478 |
else: |
| 1479 |
table = PrettyTable(headers) |
| 1480 |
table.use_header_width = False |
| 1481 |
table.add_row(row) |
| 1482 |
|
| 1483 |
assert table.get_string() == expected |
| 1484 |
|
| 1485 |
def test_table_width_on_init_wo_columns(self) -> None: |
| 1486 |
"""See also #272""" |
| 1487 |
table = PrettyTable(max_width=10) |
| 1488 |
table.add_row( |
| 1489 |
[ |
| 1490 |
"Lorem", |
| 1491 |
"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam ", |
| 1492 |
"ipsum", |
| 1493 |
"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam ", |
| 1494 |
"dolor", |
| 1495 |
"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam ", |
| 1496 |
] |
| 1497 |
) |
| 1498 |
|
| 1499 |
assert ( |
| 1500 |
table.get_string().strip() |
| 1501 |
== """ |
| 1502 |
+---------+------------+---------+------------+---------+------------+ |
| 1503 |
| Field 1 | Field 2 | Field 3 | Field 4 | Field 5 | Field 6 | |
| 1504 |
+---------+------------+---------+------------+---------+------------+ |
| 1505 |
| Lorem | Lorem | ipsum | Lorem | dolor | Lorem | |
| 1506 |
| | ipsum | | ipsum | | ipsum | |
| 1507 |
| | dolor sit | | dolor sit | | dolor sit | |
| 1508 |
| | amet, | | amet, | | amet, | |
| 1509 |
| | consetetur | | consetetur | | consetetur | |
| 1510 |
| | sadipscing | | sadipscing | | sadipscing | |
| 1511 |
| | elitr, sed | | elitr, sed | | elitr, sed | |
| 1512 |
| | diam | | diam | | diam | |
| 1513 |
+---------+------------+---------+------------+---------+------------+""".strip() |
| 1514 |
) |
| 1515 |
|
| 1516 |
def test_table_width_on_init_with_columns(self) -> None: |
| 1517 |
"""See also #272""" |
| 1518 |
table = PrettyTable( |
| 1519 |
["Field 1", "Field 2", "Field 3", "Field 4", "Field 5", "Field 6"], |
| 1520 |
max_width=10, |
| 1521 |
) |
| 1522 |
table.add_row( |
| 1523 |
[ |
| 1524 |
"Lorem", |
| 1525 |
"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam ", |
| 1526 |
"ipsum", |
| 1527 |
"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam ", |
| 1528 |
"dolor", |
| 1529 |
"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam ", |
| 1530 |
] |
| 1531 |
) |
| 1532 |
|
| 1533 |
assert ( |
| 1534 |
table.get_string().strip() |
| 1535 |
== """ |
| 1536 |
+---------+------------+---------+------------+---------+------------+ |
| 1537 |
| Field 1 | Field 2 | Field 3 | Field 4 | Field 5 | Field 6 | |
| 1538 |
+---------+------------+---------+------------+---------+------------+ |
| 1539 |
| Lorem | Lorem | ipsum | Lorem | dolor | Lorem | |
| 1540 |
| | ipsum | | ipsum | | ipsum | |
| 1541 |
| | dolor sit | | dolor sit | | dolor sit | |
| 1542 |
| | amet, | | amet, | | amet, | |
| 1543 |
| | consetetur | | consetetur | | consetetur | |
| 1544 |
| | sadipscing | | sadipscing | | sadipscing | |
| 1545 |
| | elitr, sed | | elitr, sed | | elitr, sed | |
| 1546 |
| | diam | | diam | | diam | |
| 1547 |
+---------+------------+---------+------------+---------+------------+""".strip() |
| 1548 |
) |
| 1549 |
|
| 1550 |
def test_table_minwidth_on_init_with_columns(self) -> None: |
| 1551 |
table = PrettyTable(["Field 1", "Field 2"], min_width=20) |
| 1552 |
table.add_row( |
| 1553 |
[ |
| 1554 |
"Lorem", |
| 1555 |
"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam ", |
| 1556 |
] |
| 1557 |
) |
| 1558 |
|
| 1559 |
assert ( |
| 1560 |
table.get_string().strip() |
| 1561 |
== """+----------------------+--------------------------------------------------------------------+ |
| 1562 |
| Field 1 | Field 2 | |
| 1563 |
+----------------------+--------------------------------------------------------------------+ |
| 1564 |
| Lorem | Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam | |
| 1565 |
+----------------------+--------------------------------------------------------------------+""" # noqa: E501 |
| 1566 |
) |
| 1567 |
|
| 1568 |
def test_table_min_max_width_on_init_with_columns(self) -> None: |
| 1569 |
table = PrettyTable(["Field 1", "Field 2"], min_width=20, max_width=40) |
| 1570 |
table.add_row( |
| 1571 |
[ |
| 1572 |
"Lorem", |
| 1573 |
"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam ", |
| 1574 |
] |
| 1575 |
) |
| 1576 |
|
| 1577 |
assert ( |
| 1578 |
table.get_string().strip() |
| 1579 |
== """+----------------------+------------------------------------------+ |
| 1580 |
| Field 1 | Field 2 | |
| 1581 |
+----------------------+------------------------------------------+ |
| 1582 |
| Lorem | Lorem ipsum dolor sit amet, consetetur | |
| 1583 |
| | sadipscing elitr, sed diam | |
| 1584 |
+----------------------+------------------------------------------+""" |
| 1585 |
) |
| 1586 |
|
| 1587 |
def test_table_float_formatting_on_init_wo_columns(self) -> None: |
| 1588 |
"""See also #243""" |
| 1589 |
table = prettytable.PrettyTable(float_format="10.2") |
| 1590 |
table.field_names = ["Metric", "Initial sol.", "Best sol."] |
| 1591 |
table.add_rows([["foo", 1.0 / 3.0, 1.0 / 3.0]]) |
| 1592 |
|
| 1593 |
assert ( |
| 1594 |
table.get_string().strip() |
| 1595 |
== """ |
| 1596 |
+--------+--------------+------------+ |
| 1597 |
| Metric | Initial sol. | Best sol. | |
| 1598 |
+--------+--------------+------------+ |
| 1599 |
| foo | 0.33 | 0.33 | |
| 1600 |
+--------+--------------+------------+""".strip() |
| 1601 |
) |
| 1602 |
|
| 1603 |
def test_max_table_width_wide_vrules_frame(self) -> None: |
| 1604 |
table = PrettyTable() |
| 1605 |
table.max_table_width = 52 |
| 1606 |
table.vrules = VRuleStyle.FRAME |
| 1607 |
table.add_row( |
| 1608 |
[ |
| 1609 |
0, |
| 1610 |
0, |
| 1611 |
0, |
| 1612 |
0, |
| 1613 |
0, |
| 1614 |
"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam " |
| 1615 |
"nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam " |
| 1616 |
"erat, sed diam voluptua", |
| 1617 |
] |
| 1618 |
) |
| 1619 |
|
| 1620 |
assert ( |
| 1621 |
table.get_string().strip() |
| 1622 |
== """ |
| 1623 |
+--------------------------------------------------+ |
| 1624 |
| F F F F F Field 6 | |
| 1625 |
+--------------------------------------------------+ |
| 1626 |
| 0 0 0 0 0 Lorem ipsum dolor sit amet, | |
| 1627 |
| consetetur sadipscing elitr, | |
| 1628 |
| sed diam nonumy eirmod | |
| 1629 |
| tempor invidunt ut labore et | |
| 1630 |
| dolore magna aliquyam erat, | |
| 1631 |
| sed diam voluptua | |
| 1632 |
+--------------------------------------------------+""".strip() |
| 1633 |
) |
| 1634 |
|
| 1635 |
def test_max_table_width_wide_vrules_none(self) -> None: |
| 1636 |
table = PrettyTable() |
| 1637 |
table.max_table_width = 52 |
| 1638 |
table.vrules = VRuleStyle.NONE |
| 1639 |
table.add_row( |
| 1640 |
[ |
| 1641 |
0, |
| 1642 |
0, |
| 1643 |
0, |
| 1644 |
0, |
| 1645 |
0, |
| 1646 |
"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam " |
| 1647 |
"nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam " |
| 1648 |
"erat, sed diam voluptua", |
| 1649 |
] |
| 1650 |
) |
| 1651 |
|
| 1652 |
assert ( |
| 1653 |
table.get_string().strip() |
| 1654 |
== """ |
| 1655 |
---------------------------------------------------- |
| 1656 |
F F F F F Field 6 |
| 1657 |
---------------------------------------------------- |
| 1658 |
0 0 0 0 0 Lorem ipsum dolor sit amet, |
| 1659 |
consetetur sadipscing elitr, |
| 1660 |
sed diam nonumy eirmod |
| 1661 |
tempor invidunt ut labore et |
| 1662 |
dolore magna aliquyam erat, |
| 1663 |
sed diam voluptua |
| 1664 |
----------------------------------------------------""".strip() # noqa: W291 |
| 1665 |
) |
| 1666 |
|
| 1667 |
|
| 1668 |
class TestFields: |
| 1669 |
def test_fields_at_class_declaration(self) -> None: |
| 1670 |
table = PrettyTable( |
| 1671 |
field_names=CITY_DATA_HEADER, |
| 1672 |
fields=["City name", "Annual Rainfall"], |
| 1673 |
) |
| 1674 |
for row in CITY_DATA: |
| 1675 |
table.add_row(row) |
| 1676 |
assert ( |
| 1677 |
"""+-----------+-----------------+ |
| 1678 |
| City name | Annual Rainfall | |
| 1679 |
+-----------+-----------------+ |
| 1680 |
| Adelaide | 600.5 | |
| 1681 |
| Brisbane | 1146.4 | |
| 1682 |
| Darwin | 1714.7 | |
| 1683 |
| Hobart | 619.5 | |
| 1684 |
| Sydney | 1214.8 | |
| 1685 |
| Melbourne | 646.9 | |
| 1686 |
| Perth | 869.4 | |
| 1687 |
+-----------+-----------------+""" |
| 1688 |
== table.get_string().strip() |
| 1689 |
) |
| 1690 |
|
| 1691 |
def test_fields(self) -> None: |
| 1692 |
table = PrettyTable() |
| 1693 |
table.field_names = CITY_DATA_HEADER |
| 1694 |
table.fields = ["City name", "Annual Rainfall"] |
| 1695 |
for row in CITY_DATA: |
| 1696 |
table.add_row(row) |
| 1697 |
assert ( |
| 1698 |
"""+-----------+-----------------+ |
| 1699 |
| City name | Annual Rainfall | |
| 1700 |
+-----------+-----------------+ |
| 1701 |
| Adelaide | 600.5 | |
| 1702 |
| Brisbane | 1146.4 | |
| 1703 |
| Darwin | 1714.7 | |
| 1704 |
| Hobart | 619.5 | |
| 1705 |
| Sydney | 1214.8 | |
| 1706 |
| Melbourne | 646.9 | |
| 1707 |
| Perth | 869.4 | |
| 1708 |
+-----------+-----------------+""" |
| 1709 |
== table.get_string().strip() |
| 1710 |
) |
| 1711 |
|
| 1712 |
|
| 1713 |
class TestGeneralOutput: |
| 1714 |
def test_copy(self, helper_table: PrettyTable) -> None: |
| 1715 |
t_copy = helper_table.copy() |
| 1716 |
assert helper_table.get_string() == t_copy.get_string() |
| 1717 |
|
| 1718 |
def test_text(self, helper_table: PrettyTable) -> None: |
| 1719 |
assert helper_table.get_formatted_string("text") == helper_table.get_string() |
| 1720 |
# test with default arg, too |
| 1721 |
assert helper_table.get_formatted_string() == helper_table.get_string() |
| 1722 |
# args passed through |
| 1723 |
assert helper_table.get_formatted_string( |
| 1724 |
border=False |
| 1725 |
) == helper_table.get_string(border=False) |
| 1726 |
|
| 1727 |
def test_csv(self, helper_table: PrettyTable) -> None: |
| 1728 |
assert helper_table.get_formatted_string("csv") == helper_table.get_csv_string() |
| 1729 |
# args passed through |
| 1730 |
assert helper_table.get_formatted_string( |
| 1731 |
"csv", border=False |
| 1732 |
) == helper_table.get_csv_string(border=False) |
| 1733 |
|
| 1734 |
def test_json(self, helper_table: PrettyTable) -> None: |
| 1735 |
assert ( |
| 1736 |
helper_table.get_formatted_string("json") == helper_table.get_json_string() |
| 1737 |
) |
| 1738 |
# args passed through |
| 1739 |
assert helper_table.get_formatted_string( |
| 1740 |
"json", border=False |
| 1741 |
) == helper_table.get_json_string(border=False) |
| 1742 |
|
| 1743 |
def test_html(self, helper_table: PrettyTable) -> None: |
| 1744 |
assert ( |
| 1745 |
helper_table.get_formatted_string("html") == helper_table.get_html_string() |
| 1746 |
) |
| 1747 |
# args passed through |
| 1748 |
assert helper_table.get_formatted_string( |
| 1749 |
"html", border=False |
| 1750 |
) == helper_table.get_html_string(border=False) |
| 1751 |
|
| 1752 |
def test_latex(self, helper_table: PrettyTable) -> None: |
| 1753 |
assert ( |
| 1754 |
helper_table.get_formatted_string("latex") |
| 1755 |
== helper_table.get_latex_string() |
| 1756 |
) |
| 1757 |
# args passed through |
| 1758 |
assert helper_table.get_formatted_string( |
| 1759 |
"latex", border=False |
| 1760 |
) == helper_table.get_latex_string(border=False) |
| 1761 |
|
| 1762 |
def test_mediawiki(self, helper_table: PrettyTable) -> None: |
| 1763 |
assert helper_table.get_formatted_string( |
| 1764 |
"mediawiki", border=False |
| 1765 |
) == helper_table.get_mediawiki_string(border=False) |
| 1766 |
|
| 1767 |
def test_invalid(self, helper_table: PrettyTable) -> None: |
| 1768 |
with pytest.raises(ValueError): |
| 1769 |
helper_table.get_formatted_string("pdf") |
| 1770 |
|
| 1771 |
|
| 1772 |
class TestDeprecations: |
| 1773 |
@pytest.mark.parametrize( |
| 1774 |
"module_name", |
| 1775 |
[ |
| 1776 |
"prettytable", |
| 1777 |
"prettytable.prettytable", |
| 1778 |
], |
| 1779 |
) |
| 1780 |
@pytest.mark.parametrize( |
| 1781 |
"name", |
| 1782 |
[ |
| 1783 |
"FRAME", |
| 1784 |
"ALL", |
| 1785 |
"NONE", |
| 1786 |
"HEADER", |
| 1787 |
], |
| 1788 |
) |
| 1789 |
def test_hrule_constant_deprecations(self, module_name: str, name: str) -> None: |
| 1790 |
with pytest.deprecated_call(match=f"the '{name}' constant is deprecated"): |
| 1791 |
exec(f"from {module_name} import {name}") |
| 1792 |
|
| 1793 |
@pytest.mark.parametrize( |
| 1794 |
"module_name", |
| 1795 |
[ |
| 1796 |
"prettytable", |
| 1797 |
"prettytable.prettytable", |
| 1798 |
], |
| 1799 |
) |
| 1800 |
@pytest.mark.parametrize( |
| 1801 |
"name", |
| 1802 |
[ |
| 1803 |
"DEFAULT", |
| 1804 |
"MSWORD_FRIENDLY", |
| 1805 |
"PLAIN_COLUMNS", |
| 1806 |
"MARKDOWN", |
| 1807 |
"ORGMODE", |
| 1808 |
"DOUBLE_BORDER", |
| 1809 |
"SINGLE_BORDER", |
| 1810 |
"RANDOM", |
| 1811 |
], |
| 1812 |
) |
| 1813 |
def test_table_style_constant_deprecations( |
| 1814 |
self, module_name: str, name: str |
| 1815 |
) -> None: |
| 1816 |
with pytest.deprecated_call(match=f"the '{name}' constant is deprecated"): |
| 1817 |
exec(f"from {module_name} import {name}") |