| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
from prettytable import PrettyTable, from_mediawiki |
| 6 |
|
| 7 |
|
| 8 |
class TestMediaWikiOutput: |
| 9 |
def test_mediawiki_output(self, helper_table: PrettyTable) -> None: |
| 10 |
assert ( |
| 11 |
helper_table.get_mediawiki_string(header=True).strip() |
| 12 |
== """ |
| 13 |
{| class="wikitable" |
| 14 |
|- |
| 15 |
! !! Field 1 !! Field 2 !! Field 3 |
| 16 |
|- |
| 17 |
| 1 || value 1 || value2 || value3 |
| 18 |
|- |
| 19 |
| 4 || value 4 || value5 || value6 |
| 20 |
|- |
| 21 |
| 7 || value 7 || value8 || value9 |
| 22 |
|} |
| 23 |
""".strip() |
| 24 |
) |
| 25 |
|
| 26 |
def test_mediawiki_output_without_header(self, helper_table: PrettyTable) -> None: |
| 27 |
assert ( |
| 28 |
helper_table.get_mediawiki_string(header=False).strip() |
| 29 |
== """ |
| 30 |
{| class="wikitable" |
| 31 |
|- |
| 32 |
| 1 || value 1 || value2 || value3 |
| 33 |
|- |
| 34 |
| 4 || value 4 || value5 || value6 |
| 35 |
|- |
| 36 |
| 7 || value 7 || value8 || value9 |
| 37 |
|} |
| 38 |
""".strip() |
| 39 |
) |
| 40 |
|
| 41 |
def test_mediawiki_output_with_caption(self, helper_table: PrettyTable) -> None: |
| 42 |
assert ( |
| 43 |
helper_table.get_mediawiki_string( |
| 44 |
title="Optional caption", header=True |
| 45 |
).strip() |
| 46 |
== """ |
| 47 |
{| class="wikitable" |
| 48 |
|+ Optional caption |
| 49 |
|- |
| 50 |
! !! Field 1 !! Field 2 !! Field 3 |
| 51 |
|- |
| 52 |
| 1 || value 1 || value2 || value3 |
| 53 |
|- |
| 54 |
| 4 || value 4 || value5 || value6 |
| 55 |
|- |
| 56 |
| 7 || value 7 || value8 || value9 |
| 57 |
|} |
| 58 |
""".strip() |
| 59 |
) |
| 60 |
|
| 61 |
def test_mediawiki_output_with_attributes(self, helper_table: PrettyTable) -> None: |
| 62 |
assert ( |
| 63 |
helper_table.get_mediawiki_string( |
| 64 |
attributes={"class": "mytable", "id": "table1"}, header=True |
| 65 |
).strip() |
| 66 |
== """ |
| 67 |
{| class="mytable" id="table1" |
| 68 |
|- |
| 69 |
! !! Field 1 !! Field 2 !! Field 3 |
| 70 |
|- |
| 71 |
| 1 || value 1 || value2 || value3 |
| 72 |
|- |
| 73 |
| 4 || value 4 || value5 || value6 |
| 74 |
|- |
| 75 |
| 7 || value 7 || value8 || value9 |
| 76 |
|} |
| 77 |
""".strip() |
| 78 |
) |
| 79 |
|
| 80 |
def test_mediawiki_output_with_fields_option( |
| 81 |
self, helper_table: PrettyTable |
| 82 |
) -> None: |
| 83 |
assert ( |
| 84 |
helper_table.get_mediawiki_string( |
| 85 |
fields=["Field 1", "Field 3"], header=True |
| 86 |
).strip() |
| 87 |
== """ |
| 88 |
{| class="wikitable" |
| 89 |
|- |
| 90 |
! Field 1 !! Field 3 |
| 91 |
|- |
| 92 |
| value 1 || value3 |
| 93 |
|- |
| 94 |
| value 4 || value6 |
| 95 |
|- |
| 96 |
| value 7 || value9 |
| 97 |
|} |
| 98 |
""".strip() |
| 99 |
) |
| 100 |
|
| 101 |
|
| 102 |
class TestMediaWikiConstructor: |
| 103 |
def test_mediawiki_and_back(self, city_data: PrettyTable) -> None: |
| 104 |
mediawiki_string = city_data.get_mediawiki_string() |
| 105 |
new_table = from_mediawiki(mediawiki_string) |
| 106 |
assert new_table.get_string() == city_data.get_string() |
| 107 |
|
| 108 |
def test_from_mediawiki_ignores_non_table_text( |
| 109 |
self, city_data: PrettyTable |
| 110 |
) -> None: |
| 111 |
wiki_table = city_data.get_mediawiki_string() |
| 112 |
wiki_text = f"Before table text.\n{wiki_table}\nAfter table text." |
| 113 |
table = from_mediawiki(wiki_text) |
| 114 |
output = table.get_string() |
| 115 |
|
| 116 |
for header in city_data.field_names: |
| 117 |
assert header in output |
| 118 |
for row in city_data._rows: |
| 119 |
for cell in row: |
| 120 |
assert str(cell) in output |
| 121 |
assert "Before table text." not in output |
| 122 |
assert "After table text." not in output |
| 123 |
|
| 124 |
def test_from_mediawiki_ignores_caption(self) -> None: |
| 125 |
wiki_text = """ |
| 126 |
{| class="wikitable" |
| 127 |
|+ Optional caption |
| 128 |
|- |
| 129 |
! Field 1 !! Field 2 |
| 130 |
|- |
| 131 |
| value 1 || value2 |
| 132 |
|- |
| 133 |
| value 4 || value5 |
| 134 |
|} |
| 135 |
""" |
| 136 |
table = from_mediawiki(wiki_text) |
| 137 |
output = table.get_string() |
| 138 |
assert "Optional caption" not in output |
| 139 |
assert "value 1" in output |
| 140 |
|
| 141 |
def test_from_mediawiki_no_header(self) -> None: |
| 142 |
wiki_text = """ |
| 143 |
{| class="wikitable" |
| 144 |
|- |
| 145 |
| value 1 || value2 |
| 146 |
|- |
| 147 |
| value 4 || value5 |
| 148 |
|- |
| 149 |
| value 7 || value8 |
| 150 |
|} |
| 151 |
""" |
| 152 |
with pytest.raises( |
| 153 |
ValueError, match="No valid header found in the MediaWiki table." |
| 154 |
): |
| 155 |
from_mediawiki(wiki_text) |
| 156 |
|
| 157 |
def test_from_mediawiki_row_length_mismatch(self) -> None: |
| 158 |
wiki_text = """ |
| 159 |
{| class="wikitable" |
| 160 |
|- |
| 161 |
! Field 1 !! Field 2 |
| 162 |
|- |
| 163 |
| value 1 || value2 || value3 |
| 164 |
|- |
| 165 |
| value 4 || value5 || value6 |
| 166 |
|} |
| 167 |
""" |
| 168 |
with pytest.raises( |
| 169 |
ValueError, match="Row length mismatch between header and body." |
| 170 |
): |
| 171 |
from_mediawiki(wiki_text) |