| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from test_prettytable import CITY_DATA, CITY_DATA_HEADER |
| 4 |
|
| 5 |
from prettytable import PrettyTable, RowType |
| 6 |
|
| 7 |
|
| 8 |
class TestSorting: |
| 9 |
def test_sort_by_different_per_columns(self, city_data: PrettyTable) -> None: |
| 10 |
city_data.sortby = city_data.field_names[0] |
| 11 |
old = city_data.get_string() |
| 12 |
for field in city_data.field_names[1:]: |
| 13 |
city_data.sortby = field |
| 14 |
new = city_data.get_string() |
| 15 |
assert new != old |
| 16 |
|
| 17 |
def test_reverse_sort(self, city_data: PrettyTable) -> None: |
| 18 |
for field in city_data.field_names: |
| 19 |
city_data.sortby = field |
| 20 |
city_data.reversesort = False |
| 21 |
forward = city_data.get_string() |
| 22 |
city_data.reversesort = True |
| 23 |
backward = city_data.get_string() |
| 24 |
forward_lines = forward.split("\n")[2:] # Discard header lines |
| 25 |
backward_lines = backward.split("\n")[2:] |
| 26 |
backward_lines.reverse() |
| 27 |
assert forward_lines == backward_lines |
| 28 |
|
| 29 |
def test_sort_key(self, city_data: PrettyTable) -> None: |
| 30 |
# Test sorting by length of city name |
| 31 |
def key(vals: RowType) -> list[int]: |
| 32 |
vals[0] = len(vals[0]) |
| 33 |
return vals |
| 34 |
|
| 35 |
city_data.sortby = "City name" |
| 36 |
city_data.sort_key = key |
| 37 |
assert ( |
| 38 |
city_data.get_string().strip() |
| 39 |
== """ |
| 40 |
+-----------+------+------------+-----------------+ |
| 41 |
| City name | Area | Population | Annual Rainfall | |
| 42 |
+-----------+------+------------+-----------------+ |
| 43 |
| Perth | 5386 | 1554769 | 869.4 | |
| 44 |
| Darwin | 112 | 120900 | 1714.7 | |
| 45 |
| Hobart | 1357 | 205556 | 619.5 | |
| 46 |
| Sydney | 2058 | 4336374 | 1214.8 | |
| 47 |
| Adelaide | 1295 | 1158259 | 600.5 | |
| 48 |
| Brisbane | 5905 | 1857594 | 1146.4 | |
| 49 |
| Melbourne | 1566 | 3806092 | 646.9 | |
| 50 |
+-----------+------+------------+-----------------+ |
| 51 |
""".strip() |
| 52 |
) |
| 53 |
|
| 54 |
def test_sort_key_at_class_declaration(self) -> None: |
| 55 |
# Test sorting by length of city name |
| 56 |
def key(vals: RowType) -> list[int]: |
| 57 |
vals[0] = len(vals[0]) |
| 58 |
return vals |
| 59 |
|
| 60 |
table = PrettyTable( |
| 61 |
field_names=CITY_DATA_HEADER, |
| 62 |
sortby="City name", |
| 63 |
sort_key=key, |
| 64 |
) |
| 65 |
assert table.sort_key == key |
| 66 |
for row in CITY_DATA: |
| 67 |
table.add_row(row) |
| 68 |
assert ( |
| 69 |
"""+-----------+------+------------+-----------------+ |
| 70 |
| City name | Area | Population | Annual Rainfall | |
| 71 |
+-----------+------+------------+-----------------+ |
| 72 |
| Perth | 5386 | 1554769 | 869.4 | |
| 73 |
| Darwin | 112 | 120900 | 1714.7 | |
| 74 |
| Hobart | 1357 | 205556 | 619.5 | |
| 75 |
| Sydney | 2058 | 4336374 | 1214.8 | |
| 76 |
| Adelaide | 1295 | 1158259 | 600.5 | |
| 77 |
| Brisbane | 5905 | 1857594 | 1146.4 | |
| 78 |
| Melbourne | 1566 | 3806092 | 646.9 | |
| 79 |
+-----------+------+------------+-----------------+""" |
| 80 |
== table.get_string().strip() |
| 81 |
) |
| 82 |
|
| 83 |
def test_sort_slice(self) -> None: |
| 84 |
"""Make sure sorting and slicing interact in the expected way""" |
| 85 |
table = PrettyTable(["Foo"]) |
| 86 |
for i in range(20, 0, -1): |
| 87 |
table.add_row([i]) |
| 88 |
new_style = table.get_string(sortby="Foo", end=10) |
| 89 |
assert "10" in new_style |
| 90 |
assert "20" not in new_style |
| 91 |
oldstyle = table.get_string(sortby="Foo", end=10, oldsortslice=True) |
| 92 |
assert "10" not in oldstyle |
| 93 |
assert "20" in oldstyle |
| 94 |
|
| 95 |
def test_sortby_at_class_declaration(self) -> None: |
| 96 |
""" |
| 97 |
Fix #354 where initialization of a table with sortby fails |
| 98 |
""" |
| 99 |
table = PrettyTable( |
| 100 |
field_names=CITY_DATA_HEADER, |
| 101 |
sortby="Area", |
| 102 |
) |
| 103 |
assert table.sortby == "Area" |
| 104 |
for row in CITY_DATA: |
| 105 |
table.add_row(row) |
| 106 |
assert ( |
| 107 |
"""+-----------+------+------------+-----------------+ |
| 108 |
| City name | Area | Population | Annual Rainfall | |
| 109 |
+-----------+------+------------+-----------------+ |
| 110 |
| Darwin | 112 | 120900 | 1714.7 | |
| 111 |
| Adelaide | 1295 | 1158259 | 600.5 | |
| 112 |
| Hobart | 1357 | 205556 | 619.5 | |
| 113 |
| Melbourne | 1566 | 3806092 | 646.9 | |
| 114 |
| Sydney | 2058 | 4336374 | 1214.8 | |
| 115 |
| Perth | 5386 | 1554769 | 869.4 | |
| 116 |
| Brisbane | 5905 | 1857594 | 1146.4 | |
| 117 |
+-----------+------+------------+-----------------+""" |
| 118 |
== table.get_string().strip() |
| 119 |
) |