test_prettytable.py 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351
  1. from __future__ import annotations
  2. import datetime as dt
  3. import io
  4. import random
  5. import sqlite3
  6. from math import e, pi, sqrt
  7. from typing import Any
  8. import pytest
  9. from pytest_lazy_fixtures import lf
  10. import prettytable
  11. from prettytable import (
  12. ALL,
  13. DEFAULT,
  14. DOUBLE_BORDER,
  15. FRAME,
  16. HEADER,
  17. MARKDOWN,
  18. MSWORD_FRIENDLY,
  19. NONE,
  20. ORGMODE,
  21. PLAIN_COLUMNS,
  22. RANDOM,
  23. SINGLE_BORDER,
  24. PrettyTable,
  25. from_csv,
  26. from_db_cursor,
  27. from_html,
  28. from_html_one,
  29. from_json,
  30. )
  31. def test_version() -> None:
  32. assert isinstance(prettytable.__version__, str)
  33. assert prettytable.__version__[0].isdigit()
  34. assert prettytable.__version__.count(".") >= 2
  35. assert prettytable.__version__[-1].isdigit()
  36. def helper_table(rows: int = 3) -> PrettyTable:
  37. table = PrettyTable(["", "Field 1", "Field 2", "Field 3"])
  38. v = 1
  39. for row in range(rows):
  40. # Some have spaces, some not, to help test padding columns of different widths
  41. table.add_row([v, f"value {v}", f"value{v+1}", f"value{v+2}"])
  42. v += 3
  43. return table
  44. @pytest.fixture
  45. def row_prettytable() -> PrettyTable:
  46. # Row by row...
  47. table = PrettyTable()
  48. table.field_names = ["City name", "Area", "Population", "Annual Rainfall"]
  49. table.add_row(["Adelaide", 1295, 1158259, 600.5])
  50. table.add_row(["Brisbane", 5905, 1857594, 1146.4])
  51. table.add_row(["Darwin", 112, 120900, 1714.7])
  52. table.add_row(["Hobart", 1357, 205556, 619.5])
  53. table.add_row(["Sydney", 2058, 4336374, 1214.8])
  54. table.add_row(["Melbourne", 1566, 3806092, 646.9])
  55. table.add_row(["Perth", 5386, 1554769, 869.4])
  56. return table
  57. @pytest.fixture
  58. def col_prettytable() -> PrettyTable:
  59. # Column by column...
  60. table = PrettyTable()
  61. table.add_column(
  62. "City name",
  63. ["Adelaide", "Brisbane", "Darwin", "Hobart", "Sydney", "Melbourne", "Perth"],
  64. )
  65. table.add_column("Area", [1295, 5905, 112, 1357, 2058, 1566, 5386])
  66. table.add_column(
  67. "Population", [1158259, 1857594, 120900, 205556, 4336374, 3806092, 1554769]
  68. )
  69. table.add_column(
  70. "Annual Rainfall", [600.5, 1146.4, 1714.7, 619.5, 1214.8, 646.9, 869.4]
  71. )
  72. return table
  73. @pytest.fixture
  74. def mix_prettytable() -> PrettyTable:
  75. # A mix of both!
  76. table = PrettyTable()
  77. table.field_names = ["City name", "Area"]
  78. table.add_row(["Adelaide", 1295])
  79. table.add_row(["Brisbane", 5905])
  80. table.add_row(["Darwin", 112])
  81. table.add_row(["Hobart", 1357])
  82. table.add_row(["Sydney", 2058])
  83. table.add_row(["Melbourne", 1566])
  84. table.add_row(["Perth", 5386])
  85. table.add_column(
  86. "Population", [1158259, 1857594, 120900, 205556, 4336374, 3806092, 1554769]
  87. )
  88. table.add_column(
  89. "Annual Rainfall", [600.5, 1146.4, 1714.7, 619.5, 1214.8, 646.9, 869.4]
  90. )
  91. return table
  92. class TestNoneOption:
  93. def test_none_char_valid_option(self) -> None:
  94. PrettyTable(["Field 1", "Field 2", "Field 3"], none_format="")
  95. def test_none_char_invalid_option(self) -> None:
  96. with pytest.raises(TypeError) as exc:
  97. PrettyTable(["Field 1", "Field 2", "Field 3"], none_format=2)
  98. assert "must be a string" in str(exc.value)
  99. def test_no_value_replace_none(self) -> None:
  100. table = PrettyTable(["Field 1", "Field 2", "Field 3"])
  101. table.add_row(["value 1", None, "value 2"])
  102. assert (
  103. table.get_string().strip()
  104. == """
  105. +---------+---------+---------+
  106. | Field 1 | Field 2 | Field 3 |
  107. +---------+---------+---------+
  108. | value 1 | None | value 2 |
  109. +---------+---------+---------+
  110. """.strip()
  111. )
  112. def test_no_value_replace_none_with_default_field_names(self) -> None:
  113. table = PrettyTable()
  114. table.add_row(["value 1", "None", "value 2"])
  115. assert (
  116. table.get_string().strip()
  117. == """
  118. +---------+---------+---------+
  119. | Field 1 | Field 2 | Field 3 |
  120. +---------+---------+---------+
  121. | value 1 | None | value 2 |
  122. +---------+---------+---------+
  123. """.strip()
  124. )
  125. def test_replace_none_all(self) -> None:
  126. table = PrettyTable(["Field 1", "Field 2", "Field 3"], none_format="N/A")
  127. table.add_row(["value 1", None, "None"])
  128. assert (
  129. table.get_string().strip()
  130. == """
  131. +---------+---------+---------+
  132. | Field 1 | Field 2 | Field 3 |
  133. +---------+---------+---------+
  134. | value 1 | N/A | N/A |
  135. +---------+---------+---------+
  136. """.strip()
  137. )
  138. def test_replace_none_by_col(self) -> None:
  139. table = PrettyTable(["Field 1", "Field 2", "Field 3"])
  140. table.none_format["Field 2"] = "N/A"
  141. table.none_format["Field 3"] = ""
  142. table.add_row(["value 1", None, None])
  143. assert (
  144. table.get_string().strip()
  145. == """
  146. +---------+---------+---------+
  147. | Field 1 | Field 2 | Field 3 |
  148. +---------+---------+---------+
  149. | value 1 | N/A | |
  150. +---------+---------+---------+
  151. """.strip()
  152. )
  153. def test_replace_none_recompute_width(self) -> None:
  154. table = PrettyTable()
  155. table.add_row([None])
  156. table.none_format = "0123456789"
  157. assert (
  158. table.get_string().strip()
  159. == """
  160. +------------+
  161. | Field 1 |
  162. +------------+
  163. | 0123456789 |
  164. +------------+
  165. """.strip()
  166. )
  167. def test_replace_none_maintain_width_on_recompute(self) -> None:
  168. table = PrettyTable()
  169. table.add_row(["Hello"])
  170. table.none_format = "0123456789"
  171. assert (
  172. table.get_string().strip()
  173. == """
  174. +---------+
  175. | Field 1 |
  176. +---------+
  177. | Hello |
  178. +---------+
  179. """.strip()
  180. )
  181. def test_replace_none_recompute_width_multi_column(self) -> None:
  182. table = PrettyTable()
  183. table.add_row(["Hello", None, "World"])
  184. table.none_format = "0123456789"
  185. assert (
  186. table.get_string().strip()
  187. == """
  188. +---------+------------+---------+
  189. | Field 1 | Field 2 | Field 3 |
  190. +---------+------------+---------+
  191. | Hello | 0123456789 | World |
  192. +---------+------------+---------+
  193. """.strip()
  194. )
  195. class TestBuildEquivalence:
  196. """Make sure that building a table row-by-row and column-by-column yield the same
  197. results"""
  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_ascii(
  212. self, left_hand: PrettyTable, right_hand: PrettyTable
  213. ) -> None:
  214. assert left_hand.get_string() == right_hand.get_string()
  215. @pytest.mark.parametrize(
  216. ["left_hand", "right_hand"],
  217. [
  218. (
  219. lf("row_prettytable"),
  220. lf("col_prettytable"),
  221. ),
  222. (
  223. lf("row_prettytable"),
  224. lf("mix_prettytable"),
  225. ),
  226. ],
  227. )
  228. def test_equivalence_html(
  229. self, left_hand: PrettyTable, right_hand: PrettyTable
  230. ) -> None:
  231. assert left_hand.get_html_string() == right_hand.get_html_string()
  232. @pytest.mark.parametrize(
  233. ["left_hand", "right_hand"],
  234. [
  235. (
  236. lf("row_prettytable"),
  237. lf("col_prettytable"),
  238. ),
  239. (
  240. lf("row_prettytable"),
  241. lf("mix_prettytable"),
  242. ),
  243. ],
  244. )
  245. def test_equivalence_latex(
  246. self, left_hand: PrettyTable, right_hand: PrettyTable
  247. ) -> None:
  248. assert left_hand.get_latex_string() == right_hand.get_latex_string()
  249. class TestDeleteColumn:
  250. def test_delete_column(self) -> None:
  251. table = PrettyTable()
  252. table.add_column("City name", ["Adelaide", "Brisbane", "Darwin"])
  253. table.add_column("Area", [1295, 5905, 112])
  254. table.add_column("Population", [1158259, 1857594, 120900])
  255. table.del_column("Area")
  256. without_row = PrettyTable()
  257. without_row.add_column("City name", ["Adelaide", "Brisbane", "Darwin"])
  258. without_row.add_column("Population", [1158259, 1857594, 120900])
  259. assert table.get_string() == without_row.get_string()
  260. def test_delete_illegal_column_raises_error(self) -> None:
  261. table = PrettyTable()
  262. table.add_column("City name", ["Adelaide", "Brisbane", "Darwin"])
  263. with pytest.raises(ValueError):
  264. table.del_column("City not-a-name")
  265. @pytest.fixture(scope="function")
  266. def field_name_less_table() -> PrettyTable:
  267. table = PrettyTable()
  268. table.add_row(["Adelaide", 1295, 1158259, 600.5])
  269. table.add_row(["Brisbane", 5905, 1857594, 1146.4])
  270. table.add_row(["Darwin", 112, 120900, 1714.7])
  271. table.add_row(["Hobart", 1357, 205556, 619.5])
  272. table.add_row(["Sydney", 2058, 4336374, 1214.8])
  273. table.add_row(["Melbourne", 1566, 3806092, 646.9])
  274. table.add_row(["Perth", 5386, 1554769, 869.4])
  275. return table
  276. class TestFieldNameLessTable:
  277. """Make sure that building and stringing a table with no fieldnames works fine"""
  278. def test_can_string_ascii(self, field_name_less_table: prettytable) -> None:
  279. output = field_name_less_table.get_string()
  280. assert "| Field 1 | Field 2 | Field 3 | Field 4 |" in output
  281. assert "| Adelaide | 1295 | 1158259 | 600.5 |" in output
  282. def test_can_string_html(self, field_name_less_table: prettytable) -> None:
  283. output = field_name_less_table.get_html_string()
  284. assert "<th>Field 1</th>" in output
  285. assert "<td>Adelaide</td>" in output
  286. def test_can_string_latex(self, field_name_less_table: prettytable) -> None:
  287. output = field_name_less_table.get_latex_string()
  288. assert "Field 1 & Field 2 & Field 3 & Field 4 \\\\" in output
  289. assert "Adelaide & 1295 & 1158259 & 600.5 \\\\" in output
  290. def test_add_field_names_later(self, field_name_less_table: prettytable) -> None:
  291. field_name_less_table.field_names = [
  292. "City name",
  293. "Area",
  294. "Population",
  295. "Annual Rainfall",
  296. ]
  297. assert (
  298. "City name | Area | Population | Annual Rainfall"
  299. in field_name_less_table.get_string()
  300. )
  301. @pytest.fixture(scope="function")
  302. def aligned_before_table() -> PrettyTable:
  303. table = PrettyTable()
  304. table.align = "r"
  305. table.field_names = ["City name", "Area", "Population", "Annual Rainfall"]
  306. table.add_row(["Adelaide", 1295, 1158259, 600.5])
  307. table.add_row(["Brisbane", 5905, 1857594, 1146.4])
  308. table.add_row(["Darwin", 112, 120900, 1714.7])
  309. table.add_row(["Hobart", 1357, 205556, 619.5])
  310. table.add_row(["Sydney", 2058, 4336374, 1214.8])
  311. table.add_row(["Melbourne", 1566, 3806092, 646.9])
  312. table.add_row(["Perth", 5386, 1554769, 869.4])
  313. return table
  314. @pytest.fixture(scope="function")
  315. def aligned_after_table() -> PrettyTable:
  316. table = PrettyTable()
  317. table.field_names = ["City name", "Area", "Population", "Annual Rainfall"]
  318. table.add_row(["Adelaide", 1295, 1158259, 600.5])
  319. table.add_row(["Brisbane", 5905, 1857594, 1146.4])
  320. table.add_row(["Darwin", 112, 120900, 1714.7])
  321. table.add_row(["Hobart", 1357, 205556, 619.5])
  322. table.add_row(["Sydney", 2058, 4336374, 1214.8])
  323. table.add_row(["Melbourne", 1566, 3806092, 646.9])
  324. table.add_row(["Perth", 5386, 1554769, 869.4])
  325. table.align = "r"
  326. return table
  327. class TestAlignment:
  328. """Make sure alignment works regardless of when it was set"""
  329. def test_aligned_ascii(
  330. self, aligned_before_table: prettytable, aligned_after_table: prettytable
  331. ) -> None:
  332. before = aligned_before_table.get_string()
  333. after = aligned_after_table.get_string()
  334. assert before == after
  335. def test_aligned_html(
  336. self, aligned_before_table: prettytable, aligned_after_table: prettytable
  337. ) -> None:
  338. before = aligned_before_table.get_html_string()
  339. after = aligned_after_table.get_html_string()
  340. assert before == after
  341. def test_aligned_latex(
  342. self, aligned_before_table: prettytable, aligned_after_table: prettytable
  343. ) -> None:
  344. before = aligned_before_table.get_latex_string()
  345. after = aligned_after_table.get_latex_string()
  346. assert before == after
  347. @pytest.fixture(scope="function")
  348. def city_data_prettytable() -> PrettyTable:
  349. """Just build the Australian capital city data example table."""
  350. table = PrettyTable(["City name", "Area", "Population", "Annual Rainfall"])
  351. table.add_row(["Adelaide", 1295, 1158259, 600.5])
  352. table.add_row(["Brisbane", 5905, 1857594, 1146.4])
  353. table.add_row(["Darwin", 112, 120900, 1714.7])
  354. table.add_row(["Hobart", 1357, 205556, 619.5])
  355. table.add_row(["Sydney", 2058, 4336374, 1214.8])
  356. table.add_row(["Melbourne", 1566, 3806092, 646.9])
  357. table.add_row(["Perth", 5386, 1554769, 869.4])
  358. return table
  359. @pytest.fixture(scope="function")
  360. def city_data_from_csv() -> PrettyTable:
  361. csv_string = """City name, Area, Population, Annual Rainfall
  362. Sydney, 2058, 4336374, 1214.8
  363. Melbourne, 1566, 3806092, 646.9
  364. Brisbane, 5905, 1857594, 1146.4
  365. Perth, 5386, 1554769, 869.4
  366. Adelaide, 1295, 1158259, 600.5
  367. Hobart, 1357, 205556, 619.5
  368. Darwin, 0112, 120900, 1714.7"""
  369. csv_fp = io.StringIO(csv_string)
  370. return from_csv(csv_fp)
  371. class TestOptionOverride:
  372. """Make sure all options are properly overwritten by get_string."""
  373. def test_border(self, city_data_prettytable: prettytable) -> None:
  374. default = city_data_prettytable.get_string()
  375. override = city_data_prettytable.get_string(border=False)
  376. assert default != override
  377. def test_header(self, city_data_prettytable) -> None:
  378. default = city_data_prettytable.get_string()
  379. override = city_data_prettytable.get_string(header=False)
  380. assert default != override
  381. def test_hrules_all(self, city_data_prettytable) -> None:
  382. default = city_data_prettytable.get_string()
  383. override = city_data_prettytable.get_string(hrules=ALL)
  384. assert default != override
  385. def test_hrules_none(self, city_data_prettytable) -> None:
  386. default = city_data_prettytable.get_string()
  387. override = city_data_prettytable.get_string(hrules=NONE)
  388. assert default != override
  389. class TestOptionAttribute:
  390. """Make sure all options which have an attribute interface work as they should.
  391. Also make sure option settings are copied correctly when a table is cloned by
  392. slicing."""
  393. def test_set_for_all_columns(self, city_data_prettytable) -> None:
  394. city_data_prettytable.field_names = sorted(city_data_prettytable.field_names)
  395. city_data_prettytable.align = "l"
  396. city_data_prettytable.max_width = 10
  397. city_data_prettytable.start = 2
  398. city_data_prettytable.end = 4
  399. city_data_prettytable.sortby = "Area"
  400. city_data_prettytable.reversesort = True
  401. city_data_prettytable.header = True
  402. city_data_prettytable.border = False
  403. city_data_prettytable.hrules = True
  404. city_data_prettytable.int_format = "4"
  405. city_data_prettytable.float_format = "2.2"
  406. city_data_prettytable.padding_width = 2
  407. city_data_prettytable.left_padding_width = 2
  408. city_data_prettytable.right_padding_width = 2
  409. city_data_prettytable.vertical_char = "!"
  410. city_data_prettytable.horizontal_char = "~"
  411. city_data_prettytable.junction_char = "*"
  412. city_data_prettytable.top_junction_char = "@"
  413. city_data_prettytable.bottom_junction_char = "#"
  414. city_data_prettytable.right_junction_char = "$"
  415. city_data_prettytable.left_junction_char = "%"
  416. city_data_prettytable.top_right_junction_char = "^"
  417. city_data_prettytable.top_left_junction_char = "&"
  418. city_data_prettytable.bottom_right_junction_char = "("
  419. city_data_prettytable.bottom_left_junction_char = ")"
  420. city_data_prettytable.format = True
  421. city_data_prettytable.attributes = {"class": "prettytable"}
  422. assert (
  423. city_data_prettytable.get_string() == city_data_prettytable[:].get_string()
  424. )
  425. def test_set_for_one_column(self, city_data_prettytable) -> None:
  426. city_data_prettytable.align["Rainfall"] = "l"
  427. city_data_prettytable.max_width["Name"] = 10
  428. city_data_prettytable.int_format["Population"] = "4"
  429. city_data_prettytable.float_format["Area"] = "2.2"
  430. assert (
  431. city_data_prettytable.get_string() == city_data_prettytable[:].get_string()
  432. )
  433. def test_preserve_internal_border(self) -> None:
  434. table = PrettyTable(preserve_internal_border=True)
  435. assert table.preserve_internal_border is True
  436. @pytest.fixture(scope="module")
  437. def db_cursor():
  438. conn = sqlite3.connect(":memory:")
  439. cur = conn.cursor()
  440. yield cur
  441. cur.close()
  442. conn.close()
  443. @pytest.fixture(scope="module")
  444. def init_db(db_cursor):
  445. db_cursor.execute(
  446. "CREATE TABLE cities "
  447. "(name TEXT, area INTEGER, population INTEGER, rainfall REAL)"
  448. )
  449. db_cursor.execute('INSERT INTO cities VALUES ("Adelaide", 1295, 1158259, 600.5)')
  450. db_cursor.execute('INSERT INTO cities VALUES ("Brisbane", 5905, 1857594, 1146.4)')
  451. db_cursor.execute('INSERT INTO cities VALUES ("Darwin", 112, 120900, 1714.7)')
  452. db_cursor.execute('INSERT INTO cities VALUES ("Hobart", 1357, 205556, 619.5)')
  453. db_cursor.execute('INSERT INTO cities VALUES ("Sydney", 2058, 4336374, 1214.8)')
  454. db_cursor.execute('INSERT INTO cities VALUES ("Melbourne", 1566, 3806092, 646.9)')
  455. db_cursor.execute('INSERT INTO cities VALUES ("Perth", 5386, 1554769, 869.4)')
  456. yield
  457. db_cursor.execute("DROP TABLE cities")
  458. class TestBasic:
  459. """Some very basic tests."""
  460. def test_table_rows(self, city_data_prettytable: PrettyTable) -> None:
  461. rows = city_data_prettytable.rows
  462. assert len(rows) == 7
  463. assert rows[0] == ["Adelaide", 1295, 1158259, 600.5]
  464. def _test_no_blank_lines(self, table: prettytable) -> None:
  465. string = table.get_string()
  466. lines = string.split("\n")
  467. assert "" not in lines
  468. def _test_all_length_equal(self, table: prettytable) -> None:
  469. string = table.get_string()
  470. lines = string.split("\n")
  471. lengths = [len(line) for line in lines]
  472. lengths = set(lengths)
  473. assert len(lengths) == 1
  474. def test_no_blank_lines(self, city_data_prettytable) -> None:
  475. """No table should ever have blank lines in it."""
  476. self._test_no_blank_lines(city_data_prettytable)
  477. def test_all_lengths_equal(self, city_data_prettytable) -> None:
  478. """All lines in a table should be of the same length."""
  479. self._test_all_length_equal(city_data_prettytable)
  480. def test_no_blank_lines_with_title(
  481. self, city_data_prettytable: PrettyTable
  482. ) -> None:
  483. """No table should ever have blank lines in it."""
  484. city_data_prettytable.title = "My table"
  485. self._test_no_blank_lines(city_data_prettytable)
  486. def test_all_lengths_equal_with_title(
  487. self, city_data_prettytable: PrettyTable
  488. ) -> None:
  489. """All lines in a table should be of the same length."""
  490. city_data_prettytable.title = "My table"
  491. self._test_all_length_equal(city_data_prettytable)
  492. def test_all_lengths_equal_with_long_title(
  493. self, city_data_prettytable: PrettyTable
  494. ) -> None:
  495. """All lines in a table should be of the same length, even with a long title."""
  496. city_data_prettytable.title = "My table (75 characters wide) " + "=" * 45
  497. self._test_all_length_equal(city_data_prettytable)
  498. def test_no_blank_lines_without_border(
  499. self, city_data_prettytable: PrettyTable
  500. ) -> None:
  501. """No table should ever have blank lines in it."""
  502. city_data_prettytable.border = False
  503. self._test_no_blank_lines(city_data_prettytable)
  504. def test_all_lengths_equal_without_border(
  505. self, city_data_prettytable: PrettyTable
  506. ) -> None:
  507. """All lines in a table should be of the same length."""
  508. city_data_prettytable.border = False
  509. self._test_all_length_equal(city_data_prettytable)
  510. def test_no_blank_lines_without_header(
  511. self, city_data_prettytable: PrettyTable
  512. ) -> None:
  513. """No table should ever have blank lines in it."""
  514. city_data_prettytable.header = False
  515. self._test_no_blank_lines(city_data_prettytable)
  516. def test_all_lengths_equal_without_header(
  517. self, city_data_prettytable: PrettyTable
  518. ) -> None:
  519. """All lines in a table should be of the same length."""
  520. city_data_prettytable.header = False
  521. self._test_all_length_equal(city_data_prettytable)
  522. def test_no_blank_lines_with_hrules_none(
  523. self, city_data_prettytable: PrettyTable
  524. ) -> None:
  525. """No table should ever have blank lines in it."""
  526. city_data_prettytable.hrules = NONE
  527. self._test_no_blank_lines(city_data_prettytable)
  528. def test_all_lengths_equal_with_hrules_none(
  529. self, city_data_prettytable: PrettyTable
  530. ) -> None:
  531. """All lines in a table should be of the same length."""
  532. city_data_prettytable.hrules = NONE
  533. self._test_all_length_equal(city_data_prettytable)
  534. def test_no_blank_lines_with_hrules_all(
  535. self, city_data_prettytable: PrettyTable
  536. ) -> None:
  537. """No table should ever have blank lines in it."""
  538. city_data_prettytable.hrules = ALL
  539. self._test_no_blank_lines(city_data_prettytable)
  540. def test_all_lengths_equal_with_hrules_all(
  541. self, city_data_prettytable: PrettyTable
  542. ) -> None:
  543. """All lines in a table should be of the same length."""
  544. city_data_prettytable.hrules = ALL
  545. self._test_all_length_equal(city_data_prettytable)
  546. def test_no_blank_lines_with_style_msword(
  547. self, city_data_prettytable: PrettyTable
  548. ) -> None:
  549. """No table should ever have blank lines in it."""
  550. city_data_prettytable.set_style(MSWORD_FRIENDLY)
  551. self._test_no_blank_lines(city_data_prettytable)
  552. def test_all_lengths_equal_with_style_msword(
  553. self, city_data_prettytable: PrettyTable
  554. ) -> None:
  555. """All lines in a table should be of the same length."""
  556. city_data_prettytable.set_style(MSWORD_FRIENDLY)
  557. self._test_all_length_equal(city_data_prettytable)
  558. def test_no_blank_lines_with_int_format(
  559. self, city_data_prettytable: PrettyTable
  560. ) -> None:
  561. """No table should ever have blank lines in it."""
  562. city_data_prettytable.int_format = "04"
  563. self._test_no_blank_lines(city_data_prettytable)
  564. def test_all_lengths_equal_with_int_format(
  565. self, city_data_prettytable: PrettyTable
  566. ) -> None:
  567. """All lines in a table should be of the same length."""
  568. city_data_prettytable.int_format = "04"
  569. self._test_all_length_equal(city_data_prettytable)
  570. def test_no_blank_lines_with_float_format(
  571. self, city_data_prettytable: PrettyTable
  572. ) -> None:
  573. """No table should ever have blank lines in it."""
  574. city_data_prettytable.float_format = "6.2f"
  575. self._test_no_blank_lines(city_data_prettytable)
  576. def test_all_lengths_equal_with_float_format(
  577. self, city_data_prettytable: PrettyTable
  578. ) -> None:
  579. """All lines in a table should be of the same length."""
  580. city_data_prettytable.float_format = "6.2f"
  581. self._test_all_length_equal(city_data_prettytable)
  582. def test_no_blank_lines_from_csv(self, city_data_from_csv: PrettyTable) -> None:
  583. """No table should ever have blank lines in it."""
  584. self._test_no_blank_lines(city_data_from_csv)
  585. def test_all_lengths_equal_from_csv(self, city_data_from_csv: PrettyTable) -> None:
  586. """All lines in a table should be of the same length."""
  587. self._test_all_length_equal(city_data_from_csv)
  588. @pytest.mark.usefixtures("init_db")
  589. def test_no_blank_lines_from_db(self, db_cursor) -> None:
  590. """No table should ever have blank lines in it."""
  591. db_cursor.execute("SELECT * FROM cities")
  592. pt = from_db_cursor(db_cursor)
  593. self._test_no_blank_lines(pt)
  594. @pytest.mark.usefixtures("init_db")
  595. def test_all_lengths_equal_from_db(self, db_cursor) -> None:
  596. """No table should ever have blank lines in it."""
  597. db_cursor.execute("SELECT * FROM cities")
  598. pt = from_db_cursor(db_cursor)
  599. self._test_all_length_equal(pt)
  600. class TestEmptyTable:
  601. """Make sure the print_empty option works"""
  602. def test_print_empty_true(self, city_data_prettytable: PrettyTable) -> None:
  603. table = PrettyTable()
  604. table.field_names = ["City name", "Area", "Population", "Annual Rainfall"]
  605. assert table.get_string(print_empty=True) != ""
  606. assert table.get_string(print_empty=True) != city_data_prettytable.get_string(
  607. print_empty=True
  608. )
  609. def test_print_empty_false(self, city_data_prettytable: PrettyTable) -> None:
  610. table = PrettyTable()
  611. table.field_names = ["City name", "Area", "Population", "Annual Rainfall"]
  612. assert table.get_string(print_empty=False) == ""
  613. assert table.get_string(print_empty=False) != city_data_prettytable.get_string(
  614. print_empty=False
  615. )
  616. def test_interaction_with_border(self) -> None:
  617. table = PrettyTable()
  618. table.field_names = ["City name", "Area", "Population", "Annual Rainfall"]
  619. assert table.get_string(border=False, print_empty=True) == ""
  620. class TestSlicing:
  621. def test_slice_all(self, city_data_prettytable: PrettyTable) -> None:
  622. table = city_data_prettytable[:]
  623. assert city_data_prettytable.get_string() == table.get_string()
  624. def test_slice_first_two_rows(self, city_data_prettytable: PrettyTable) -> None:
  625. table = city_data_prettytable[0:2]
  626. string = table.get_string()
  627. assert len(string.split("\n")) == 6
  628. assert "Adelaide" in string
  629. assert "Brisbane" in string
  630. assert "Melbourne" not in string
  631. assert "Perth" not in string
  632. def test_slice_last_two_rows(self, city_data_prettytable: PrettyTable) -> None:
  633. table = city_data_prettytable[-2:]
  634. string = table.get_string()
  635. assert len(string.split("\n")) == 6
  636. assert "Adelaide" not in string
  637. assert "Brisbane" not in string
  638. assert "Melbourne" in string
  639. assert "Perth" in string
  640. class TestSorting:
  641. def test_sort_by_different_per_columns(
  642. self, city_data_prettytable: PrettyTable
  643. ) -> None:
  644. city_data_prettytable.sortby = city_data_prettytable.field_names[0]
  645. old = city_data_prettytable.get_string()
  646. for field in city_data_prettytable.field_names[1:]:
  647. city_data_prettytable.sortby = field
  648. new = city_data_prettytable.get_string()
  649. assert new != old
  650. def test_reverse_sort(self, city_data_prettytable: PrettyTable) -> None:
  651. for field in city_data_prettytable.field_names:
  652. city_data_prettytable.sortby = field
  653. city_data_prettytable.reversesort = False
  654. forward = city_data_prettytable.get_string()
  655. city_data_prettytable.reversesort = True
  656. backward = city_data_prettytable.get_string()
  657. forward_lines = forward.split("\n")[2:] # Discard header lines
  658. backward_lines = backward.split("\n")[2:]
  659. backward_lines.reverse()
  660. assert forward_lines == backward_lines
  661. def test_sort_key(self, city_data_prettytable: PrettyTable) -> None:
  662. # Test sorting by length of city name
  663. def key(vals):
  664. vals[0] = len(vals[0])
  665. return vals
  666. city_data_prettytable.sortby = "City name"
  667. city_data_prettytable.sort_key = key
  668. assert (
  669. city_data_prettytable.get_string().strip()
  670. == """
  671. +-----------+------+------------+-----------------+
  672. | City name | Area | Population | Annual Rainfall |
  673. +-----------+------+------------+-----------------+
  674. | Perth | 5386 | 1554769 | 869.4 |
  675. | Darwin | 112 | 120900 | 1714.7 |
  676. | Hobart | 1357 | 205556 | 619.5 |
  677. | Sydney | 2058 | 4336374 | 1214.8 |
  678. | Adelaide | 1295 | 1158259 | 600.5 |
  679. | Brisbane | 5905 | 1857594 | 1146.4 |
  680. | Melbourne | 1566 | 3806092 | 646.9 |
  681. +-----------+------+------------+-----------------+
  682. """.strip()
  683. )
  684. def test_sort_slice(self) -> None:
  685. """Make sure sorting and slicing interact in the expected way"""
  686. table = PrettyTable(["Foo"])
  687. for i in range(20, 0, -1):
  688. table.add_row([i])
  689. new_style = table.get_string(sortby="Foo", end=10)
  690. assert "10" in new_style
  691. assert "20" not in new_style
  692. oldstyle = table.get_string(sortby="Foo", end=10, oldsortslice=True)
  693. assert "10" not in oldstyle
  694. assert "20" in oldstyle
  695. @pytest.fixture(scope="function")
  696. def float_pt() -> PrettyTable:
  697. table = PrettyTable(["Constant", "Value"])
  698. table.add_row(["Pi", pi])
  699. table.add_row(["e", e])
  700. table.add_row(["sqrt(2)", sqrt(2)])
  701. return table
  702. class TestFloatFormat:
  703. def test_no_decimals(self, float_pt: PrettyTable) -> None:
  704. float_pt.float_format = ".0f"
  705. float_pt.caching = False
  706. assert "." not in float_pt.get_string()
  707. def test_round_to_5dp(self, float_pt: PrettyTable) -> None:
  708. float_pt.float_format = ".5f"
  709. string = float_pt.get_string()
  710. assert "3.14159" in string
  711. assert "3.141592" not in string
  712. assert "2.71828" in string
  713. assert "2.718281" not in string
  714. assert "2.718282" not in string
  715. assert "1.41421" in string
  716. assert "1.414213" not in string
  717. def test_pad_with_2zeroes(self, float_pt: PrettyTable) -> None:
  718. float_pt.float_format = "06.2f"
  719. string = float_pt.get_string()
  720. assert "003.14" in string
  721. assert "002.72" in string
  722. assert "001.41" in string
  723. class TestBreakLine:
  724. @pytest.mark.parametrize(
  725. ["rows", "hrule", "expected_result"],
  726. [
  727. (
  728. [["value 1", "value2\nsecond line"], ["value 3", "value4"]],
  729. ALL,
  730. """
  731. +---------+-------------+
  732. | Field 1 | Field 2 |
  733. +---------+-------------+
  734. | value 1 | value2 |
  735. | | second line |
  736. +---------+-------------+
  737. | value 3 | value4 |
  738. +---------+-------------+
  739. """,
  740. ),
  741. (
  742. [
  743. ["value 1", "value2\nsecond line"],
  744. ["value 3\n\nother line", "value4\n\n\nvalue5"],
  745. ],
  746. ALL,
  747. """
  748. +------------+-------------+
  749. | Field 1 | Field 2 |
  750. +------------+-------------+
  751. | value 1 | value2 |
  752. | | second line |
  753. +------------+-------------+
  754. | value 3 | value4 |
  755. | | |
  756. | other line | |
  757. | | value5 |
  758. +------------+-------------+
  759. """,
  760. ),
  761. (
  762. [
  763. ["value 1", "value2\nsecond line"],
  764. ["value 3\n\nother line", "value4\n\n\nvalue5"],
  765. ],
  766. FRAME,
  767. """
  768. +------------+-------------+
  769. | Field 1 | Field 2 |
  770. +------------+-------------+
  771. | value 1 | value2 |
  772. | | second line |
  773. | value 3 | value4 |
  774. | | |
  775. | other line | |
  776. | | value5 |
  777. +------------+-------------+
  778. """,
  779. ),
  780. ],
  781. )
  782. def test_break_line_ascii(
  783. self, rows: list[list[Any]], hrule: int, expected_result: str
  784. ) -> None:
  785. table = PrettyTable(["Field 1", "Field 2"])
  786. for row in rows:
  787. table.add_row(row)
  788. result = table.get_string(hrules=hrule)
  789. assert result.strip() == expected_result.strip()
  790. def test_break_line_html(self) -> None:
  791. table = PrettyTable(["Field 1", "Field 2"])
  792. table.add_row(["value 1", "value2\nsecond line"])
  793. table.add_row(["value 3", "value4"])
  794. result = table.get_html_string(hrules=ALL)
  795. assert (
  796. result.strip()
  797. == """
  798. <table>
  799. <thead>
  800. <tr>
  801. <th>Field 1</th>
  802. <th>Field 2</th>
  803. </tr>
  804. </thead>
  805. <tbody>
  806. <tr>
  807. <td>value 1</td>
  808. <td>value2<br>second line</td>
  809. </tr>
  810. <tr>
  811. <td>value 3</td>
  812. <td>value4</td>
  813. </tr>
  814. </tbody>
  815. </table>
  816. """.strip()
  817. )
  818. class TestAnsiWidth:
  819. colored = "\033[31mC\033[32mO\033[31mL\033[32mO\033[31mR\033[32mE\033[31mD\033[0m"
  820. def test_color(self) -> None:
  821. table = PrettyTable(["Field 1", "Field 2"])
  822. table.add_row([self.colored, self.colored])
  823. table.add_row(["nothing", "neither"])
  824. result = table.get_string()
  825. assert (
  826. result.strip()
  827. == f"""
  828. +---------+---------+
  829. | Field 1 | Field 2 |
  830. +---------+---------+
  831. | {self.colored} | {self.colored} |
  832. | nothing | neither |
  833. +---------+---------+
  834. """.strip()
  835. )
  836. def test_reset(self) -> None:
  837. table = PrettyTable(["Field 1", "Field 2"])
  838. table.add_row(["abc def\033(B", "\033[31mabc def\033[m"])
  839. table.add_row(["nothing", "neither"])
  840. result = table.get_string()
  841. assert (
  842. result.strip()
  843. == """
  844. +---------+---------+
  845. | Field 1 | Field 2 |
  846. +---------+---------+
  847. | abc def\033(B | \033[31mabc def\033[m |
  848. | nothing | neither |
  849. +---------+---------+
  850. """.strip()
  851. )
  852. class TestFromDB:
  853. @pytest.mark.usefixtures("init_db")
  854. def test_non_select_cursor(self, db_cursor) -> None:
  855. db_cursor.execute(
  856. 'INSERT INTO cities VALUES ("Adelaide", 1295, 1158259, 600.5)'
  857. )
  858. assert from_db_cursor(db_cursor) is None
  859. class TestJSONOutput:
  860. def test_json_output(self) -> None:
  861. t = helper_table()
  862. result = t.get_json_string()
  863. assert (
  864. result.strip()
  865. == """
  866. [
  867. [
  868. "",
  869. "Field 1",
  870. "Field 2",
  871. "Field 3"
  872. ],
  873. {
  874. "": 1,
  875. "Field 1": "value 1",
  876. "Field 2": "value2",
  877. "Field 3": "value3"
  878. },
  879. {
  880. "": 4,
  881. "Field 1": "value 4",
  882. "Field 2": "value5",
  883. "Field 3": "value6"
  884. },
  885. {
  886. "": 7,
  887. "Field 1": "value 7",
  888. "Field 2": "value8",
  889. "Field 3": "value9"
  890. }
  891. ]""".strip()
  892. )
  893. def test_json_output_options(self) -> None:
  894. t = helper_table()
  895. result = t.get_json_string(header=False, indent=None, separators=(",", ":"))
  896. assert (
  897. result
  898. == """[{"":1,"Field 1":"value 1","Field 2":"value2","Field 3":"value3"},"""
  899. """{"":4,"Field 1":"value 4","Field 2":"value5","Field 3":"value6"},"""
  900. """{"":7,"Field 1":"value 7","Field 2":"value8","Field 3":"value9"}]"""
  901. )
  902. class TestHtmlOutput:
  903. def test_html_output(self) -> None:
  904. t = helper_table()
  905. result = t.get_html_string()
  906. assert (
  907. result.strip()
  908. == """
  909. <table>
  910. <thead>
  911. <tr>
  912. <th></th>
  913. <th>Field 1</th>
  914. <th>Field 2</th>
  915. <th>Field 3</th>
  916. </tr>
  917. </thead>
  918. <tbody>
  919. <tr>
  920. <td>1</td>
  921. <td>value 1</td>
  922. <td>value2</td>
  923. <td>value3</td>
  924. </tr>
  925. <tr>
  926. <td>4</td>
  927. <td>value 4</td>
  928. <td>value5</td>
  929. <td>value6</td>
  930. </tr>
  931. <tr>
  932. <td>7</td>
  933. <td>value 7</td>
  934. <td>value8</td>
  935. <td>value9</td>
  936. </tr>
  937. </tbody>
  938. </table>
  939. """.strip()
  940. )
  941. def test_html_output_formatted(self) -> None:
  942. t = helper_table()
  943. result = t.get_html_string(format=True)
  944. assert (
  945. result.strip()
  946. == """
  947. <table frame="box" rules="cols">
  948. <thead>
  949. <tr>
  950. <th style="padding-left: 1em; padding-right: 1em; text-align: center"></th>
  951. <th style="padding-left: 1em; padding-right: 1em; text-align: center">Field 1</th>
  952. <th style="padding-left: 1em; padding-right: 1em; text-align: center">Field 2</th>
  953. <th style="padding-left: 1em; padding-right: 1em; text-align: center">Field 3</th>
  954. </tr>
  955. </thead>
  956. <tbody>
  957. <tr>
  958. <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">1</td>
  959. <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value 1</td>
  960. <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value2</td>
  961. <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value3</td>
  962. </tr>
  963. <tr>
  964. <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">4</td>
  965. <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value 4</td>
  966. <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value5</td>
  967. <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value6</td>
  968. </tr>
  969. <tr>
  970. <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">7</td>
  971. <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value 7</td>
  972. <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value8</td>
  973. <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value9</td>
  974. </tr>
  975. </tbody>
  976. </table>
  977. """.strip() # noqa: E501
  978. )
  979. def test_html_output_with_title(self) -> None:
  980. t = helper_table()
  981. t.title = "Title & Title"
  982. result = t.get_html_string(attributes={"bgcolor": "red", "a<b": "1<2"})
  983. assert (
  984. result.strip()
  985. == """
  986. <table bgcolor="red" a&lt;b="1&lt;2">
  987. <caption>Title &amp; Title</caption>
  988. <thead>
  989. <tr>
  990. <th></th>
  991. <th>Field 1</th>
  992. <th>Field 2</th>
  993. <th>Field 3</th>
  994. </tr>
  995. </thead>
  996. <tbody>
  997. <tr>
  998. <td>1</td>
  999. <td>value 1</td>
  1000. <td>value2</td>
  1001. <td>value3</td>
  1002. </tr>
  1003. <tr>
  1004. <td>4</td>
  1005. <td>value 4</td>
  1006. <td>value5</td>
  1007. <td>value6</td>
  1008. </tr>
  1009. <tr>
  1010. <td>7</td>
  1011. <td>value 7</td>
  1012. <td>value8</td>
  1013. <td>value9</td>
  1014. </tr>
  1015. </tbody>
  1016. </table>
  1017. """.strip()
  1018. )
  1019. def test_html_output_formatted_with_title(self) -> None:
  1020. t = helper_table()
  1021. t.title = "Title & Title"
  1022. result = t.get_html_string(
  1023. attributes={"bgcolor": "red", "a<b": "1<2"}, format=True
  1024. )
  1025. assert (
  1026. result.strip()
  1027. == """
  1028. <table frame="box" rules="cols" bgcolor="red" a&lt;b="1&lt;2">
  1029. <caption>Title &amp; Title</caption>
  1030. <thead>
  1031. <tr>
  1032. <th style="padding-left: 1em; padding-right: 1em; text-align: center"></th>
  1033. <th style="padding-left: 1em; padding-right: 1em; text-align: center">Field 1</th>
  1034. <th style="padding-left: 1em; padding-right: 1em; text-align: center">Field 2</th>
  1035. <th style="padding-left: 1em; padding-right: 1em; text-align: center">Field 3</th>
  1036. </tr>
  1037. </thead>
  1038. <tbody>
  1039. <tr>
  1040. <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">1</td>
  1041. <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value 1</td>
  1042. <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value2</td>
  1043. <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value3</td>
  1044. </tr>
  1045. <tr>
  1046. <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">4</td>
  1047. <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value 4</td>
  1048. <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value5</td>
  1049. <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value6</td>
  1050. </tr>
  1051. <tr>
  1052. <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">7</td>
  1053. <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value 7</td>
  1054. <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value8</td>
  1055. <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value9</td>
  1056. </tr>
  1057. </tbody>
  1058. </table>
  1059. """.strip() # noqa: E501
  1060. )
  1061. class TestPositionalJunctions:
  1062. """Verify different cases for positional-junction characters"""
  1063. def test_default(self, city_data_prettytable: PrettyTable) -> None:
  1064. city_data_prettytable.set_style(DOUBLE_BORDER)
  1065. assert (
  1066. city_data_prettytable.get_string().strip()
  1067. == """
  1068. ╔═══════════╦══════╦════════════╦═════════════════╗
  1069. ║ City name ║ Area ║ Population ║ Annual Rainfall ║
  1070. ╠═══════════╬══════╬════════════╬═════════════════╣
  1071. ║ Adelaide ║ 1295 ║ 1158259 ║ 600.5 ║
  1072. ║ Brisbane ║ 5905 ║ 1857594 ║ 1146.4 ║
  1073. ║ Darwin ║ 112 ║ 120900 ║ 1714.7 ║
  1074. ║ Hobart ║ 1357 ║ 205556 ║ 619.5 ║
  1075. ║ Sydney ║ 2058 ║ 4336374 ║ 1214.8 ║
  1076. ║ Melbourne ║ 1566 ║ 3806092 ║ 646.9 ║
  1077. ║ Perth ║ 5386 ║ 1554769 ║ 869.4 ║
  1078. ╚═══════════╩══════╩════════════╩═════════════════╝""".strip()
  1079. )
  1080. def test_no_header(self, city_data_prettytable: PrettyTable) -> None:
  1081. city_data_prettytable.set_style(DOUBLE_BORDER)
  1082. city_data_prettytable.header = False
  1083. assert (
  1084. city_data_prettytable.get_string().strip()
  1085. == """
  1086. ╔═══════════╦══════╦═════════╦════════╗
  1087. ║ Adelaide ║ 1295 ║ 1158259 ║ 600.5 ║
  1088. ║ Brisbane ║ 5905 ║ 1857594 ║ 1146.4 ║
  1089. ║ Darwin ║ 112 ║ 120900 ║ 1714.7 ║
  1090. ║ Hobart ║ 1357 ║ 205556 ║ 619.5 ║
  1091. ║ Sydney ║ 2058 ║ 4336374 ║ 1214.8 ║
  1092. ║ Melbourne ║ 1566 ║ 3806092 ║ 646.9 ║
  1093. ║ Perth ║ 5386 ║ 1554769 ║ 869.4 ║
  1094. ╚═══════════╩══════╩═════════╩════════╝""".strip()
  1095. )
  1096. def test_with_title(self, city_data_prettytable: PrettyTable) -> None:
  1097. city_data_prettytable.set_style(DOUBLE_BORDER)
  1098. city_data_prettytable.title = "Title"
  1099. assert (
  1100. city_data_prettytable.get_string().strip()
  1101. == """
  1102. ╔═════════════════════════════════════════════════╗
  1103. ║ Title ║
  1104. ╠═══════════╦══════╦════════════╦═════════════════╣
  1105. ║ City name ║ Area ║ Population ║ Annual Rainfall ║
  1106. ╠═══════════╬══════╬════════════╬═════════════════╣
  1107. ║ Adelaide ║ 1295 ║ 1158259 ║ 600.5 ║
  1108. ║ Brisbane ║ 5905 ║ 1857594 ║ 1146.4 ║
  1109. ║ Darwin ║ 112 ║ 120900 ║ 1714.7 ║
  1110. ║ Hobart ║ 1357 ║ 205556 ║ 619.5 ║
  1111. ║ Sydney ║ 2058 ║ 4336374 ║ 1214.8 ║
  1112. ║ Melbourne ║ 1566 ║ 3806092 ║ 646.9 ║
  1113. ║ Perth ║ 5386 ║ 1554769 ║ 869.4 ║
  1114. ╚═══════════╩══════╩════════════╩═════════════════╝""".strip()
  1115. )
  1116. def test_with_title_no_header(self, city_data_prettytable: PrettyTable) -> None:
  1117. city_data_prettytable.set_style(DOUBLE_BORDER)
  1118. city_data_prettytable.title = "Title"
  1119. city_data_prettytable.header = False
  1120. assert (
  1121. city_data_prettytable.get_string().strip()
  1122. == """
  1123. ╔═════════════════════════════════════╗
  1124. ║ Title ║
  1125. ╠═══════════╦══════╦═════════╦════════╣
  1126. ║ Adelaide ║ 1295 ║ 1158259 ║ 600.5 ║
  1127. ║ Brisbane ║ 5905 ║ 1857594 ║ 1146.4 ║
  1128. ║ Darwin ║ 112 ║ 120900 ║ 1714.7 ║
  1129. ║ Hobart ║ 1357 ║ 205556 ║ 619.5 ║
  1130. ║ Sydney ║ 2058 ║ 4336374 ║ 1214.8 ║
  1131. ║ Melbourne ║ 1566 ║ 3806092 ║ 646.9 ║
  1132. ║ Perth ║ 5386 ║ 1554769 ║ 869.4 ║
  1133. ╚═══════════╩══════╩═════════╩════════╝""".strip()
  1134. )
  1135. def test_hrule_all(self, city_data_prettytable: PrettyTable) -> None:
  1136. city_data_prettytable.set_style(DOUBLE_BORDER)
  1137. city_data_prettytable.title = "Title"
  1138. city_data_prettytable.hrules = ALL
  1139. assert (
  1140. city_data_prettytable.get_string().strip()
  1141. == """
  1142. ╔═════════════════════════════════════════════════╗
  1143. ║ Title ║
  1144. ╠═══════════╦══════╦════════════╦═════════════════╣
  1145. ║ City name ║ Area ║ Population ║ Annual Rainfall ║
  1146. ╠═══════════╬══════╬════════════╬═════════════════╣
  1147. ║ Adelaide ║ 1295 ║ 1158259 ║ 600.5 ║
  1148. ╠═══════════╬══════╬════════════╬═════════════════╣
  1149. ║ Brisbane ║ 5905 ║ 1857594 ║ 1146.4 ║
  1150. ╠═══════════╬══════╬════════════╬═════════════════╣
  1151. ║ Darwin ║ 112 ║ 120900 ║ 1714.7 ║
  1152. ╠═══════════╬══════╬════════════╬═════════════════╣
  1153. ║ Hobart ║ 1357 ║ 205556 ║ 619.5 ║
  1154. ╠═══════════╬══════╬════════════╬═════════════════╣
  1155. ║ Sydney ║ 2058 ║ 4336374 ║ 1214.8 ║
  1156. ╠═══════════╬══════╬════════════╬═════════════════╣
  1157. ║ Melbourne ║ 1566 ║ 3806092 ║ 646.9 ║
  1158. ╠═══════════╬══════╬════════════╬═════════════════╣
  1159. ║ Perth ║ 5386 ║ 1554769 ║ 869.4 ║
  1160. ╚═══════════╩══════╩════════════╩═════════════════╝""".strip()
  1161. )
  1162. def test_vrules_none(self, city_data_prettytable: PrettyTable) -> None:
  1163. city_data_prettytable.set_style(DOUBLE_BORDER)
  1164. city_data_prettytable.vrules = NONE
  1165. assert (
  1166. city_data_prettytable.get_string().strip()
  1167. == "═══════════════════════════════════════════════════\n"
  1168. " City name Area Population Annual Rainfall \n"
  1169. "═══════════════════════════════════════════════════\n"
  1170. " Adelaide 1295 1158259 600.5 \n"
  1171. " Brisbane 5905 1857594 1146.4 \n"
  1172. " Darwin 112 120900 1714.7 \n"
  1173. " Hobart 1357 205556 619.5 \n"
  1174. " Sydney 2058 4336374 1214.8 \n"
  1175. " Melbourne 1566 3806092 646.9 \n"
  1176. " Perth 5386 1554769 869.4 \n"
  1177. "═══════════════════════════════════════════════════".strip()
  1178. )
  1179. def test_vrules_frame_with_title(self, city_data_prettytable: PrettyTable) -> None:
  1180. city_data_prettytable.set_style(DOUBLE_BORDER)
  1181. city_data_prettytable.vrules = FRAME
  1182. city_data_prettytable.title = "Title"
  1183. assert (
  1184. city_data_prettytable.get_string().strip()
  1185. == """
  1186. ╔═════════════════════════════════════════════════╗
  1187. ║ Title ║
  1188. ╠═════════════════════════════════════════════════╣
  1189. ║ City name Area Population Annual Rainfall ║
  1190. ╠═════════════════════════════════════════════════╣
  1191. ║ Adelaide 1295 1158259 600.5 ║
  1192. ║ Brisbane 5905 1857594 1146.4 ║
  1193. ║ Darwin 112 120900 1714.7 ║
  1194. ║ Hobart 1357 205556 619.5 ║
  1195. ║ Sydney 2058 4336374 1214.8 ║
  1196. ║ Melbourne 1566 3806092 646.9 ║
  1197. ║ Perth 5386 1554769 869.4 ║
  1198. ╚═════════════════════════════════════════════════╝""".strip()
  1199. )
  1200. class TestStyle:
  1201. @pytest.mark.parametrize(
  1202. "style, expected",
  1203. [
  1204. pytest.param(
  1205. DEFAULT,
  1206. """
  1207. +---+---------+---------+---------+
  1208. | | Field 1 | Field 2 | Field 3 |
  1209. +---+---------+---------+---------+
  1210. | 1 | value 1 | value2 | value3 |
  1211. | 4 | value 4 | value5 | value6 |
  1212. | 7 | value 7 | value8 | value9 |
  1213. +---+---------+---------+---------+
  1214. """,
  1215. id="DEFAULT",
  1216. ),
  1217. pytest.param(
  1218. MARKDOWN, # TODO fix
  1219. """
  1220. | | Field 1 | Field 2 | Field 3 |
  1221. | :-: | :-----: | :-----: | :-----: |
  1222. | 1 | value 1 | value2 | value3 |
  1223. | 4 | value 4 | value5 | value6 |
  1224. | 7 | value 7 | value8 | value9 |
  1225. """,
  1226. id="MARKDOWN",
  1227. ),
  1228. pytest.param(
  1229. MSWORD_FRIENDLY,
  1230. """
  1231. | | Field 1 | Field 2 | Field 3 |
  1232. | 1 | value 1 | value2 | value3 |
  1233. | 4 | value 4 | value5 | value6 |
  1234. | 7 | value 7 | value8 | value9 |
  1235. """,
  1236. id="MSWORD_FRIENDLY",
  1237. ),
  1238. pytest.param(
  1239. ORGMODE,
  1240. """
  1241. |---+---------+---------+---------|
  1242. | | Field 1 | Field 2 | Field 3 |
  1243. |---+---------+---------+---------|
  1244. | 1 | value 1 | value2 | value3 |
  1245. | 4 | value 4 | value5 | value6 |
  1246. | 7 | value 7 | value8 | value9 |
  1247. |---+---------+---------+---------|
  1248. """,
  1249. id="ORGMODE",
  1250. ),
  1251. pytest.param(
  1252. PLAIN_COLUMNS,
  1253. """
  1254. Field 1 Field 2 Field 3
  1255. 1 value 1 value2 value3
  1256. 4 value 4 value5 value6
  1257. 7 value 7 value8 value9
  1258. """, # noqa: W291
  1259. id="PLAIN_COLUMNS",
  1260. ),
  1261. pytest.param(
  1262. RANDOM,
  1263. """
  1264. '^^^^^'^^^^^^^^^^^'^^^^^^^^^^'^^^^^^^^^^'
  1265. % 1% value 1% value2% value3%
  1266. '^^^^^'^^^^^^^^^^^'^^^^^^^^^^'^^^^^^^^^^'
  1267. % 4% value 4% value5% value6%
  1268. '^^^^^'^^^^^^^^^^^'^^^^^^^^^^'^^^^^^^^^^'
  1269. % 7% value 7% value8% value9%
  1270. '^^^^^'^^^^^^^^^^^'^^^^^^^^^^'^^^^^^^^^^'
  1271. """,
  1272. id="RANDOM",
  1273. ),
  1274. pytest.param(
  1275. DOUBLE_BORDER,
  1276. """
  1277. ╔═══╦═════════╦═════════╦═════════╗
  1278. ║ ║ Field 1 ║ Field 2 ║ Field 3 ║
  1279. ╠═══╬═════════╬═════════╬═════════╣
  1280. ║ 1 ║ value 1 ║ value2 ║ value3 ║
  1281. ║ 4 ║ value 4 ║ value5 ║ value6 ║
  1282. ║ 7 ║ value 7 ║ value8 ║ value9 ║
  1283. ╚═══╩═════════╩═════════╩═════════╝
  1284. """,
  1285. ),
  1286. pytest.param(
  1287. SINGLE_BORDER,
  1288. """
  1289. ┌───┬─────────┬─────────┬─────────┐
  1290. │ │ Field 1 │ Field 2 │ Field 3 │
  1291. ├───┼─────────┼─────────┼─────────┤
  1292. │ 1 │ value 1 │ value2 │ value3 │
  1293. │ 4 │ value 4 │ value5 │ value6 │
  1294. │ 7 │ value 7 │ value8 │ value9 │
  1295. └───┴─────────┴─────────┴─────────┘
  1296. """,
  1297. ),
  1298. ],
  1299. )
  1300. def test_style(self, style, expected) -> None:
  1301. # Arrange
  1302. t = helper_table()
  1303. random.seed(1234)
  1304. # Act
  1305. t.set_style(style)
  1306. # Assert
  1307. result = t.get_string()
  1308. assert result.strip() == expected.strip()
  1309. def test_style_invalid(self) -> None:
  1310. # Arrange
  1311. t = helper_table()
  1312. # Act / Assert
  1313. # This is an hrule style, not a table style
  1314. with pytest.raises(ValueError):
  1315. t.set_style(ALL)
  1316. @pytest.mark.parametrize(
  1317. "style, expected",
  1318. [
  1319. pytest.param(
  1320. MARKDOWN,
  1321. """
  1322. | l | c | r | Align left | Align centre | Align right |
  1323. | :-| :-: |-: | :----------| :----------: |-----------: |
  1324. | 1 | 2 | 3 | value 1 | value2 | value3 |
  1325. | 4 | 5 | 6 | value 4 | value5 | value6 |
  1326. | 7 | 8 | 9 | value 7 | value8 | value9 |
  1327. """,
  1328. id="MARKDOWN",
  1329. ),
  1330. ],
  1331. )
  1332. def test_style_align(self, style, expected) -> None:
  1333. # Arrange
  1334. t = PrettyTable(["l", "c", "r", "Align left", "Align centre", "Align right"])
  1335. v = 1
  1336. for row in range(3):
  1337. # Some have spaces, some not, to help test padding columns of
  1338. # different widths
  1339. t.add_row([v, v + 1, v + 2, f"value {v}", f"value{v + 1}", f"value{v + 2}"])
  1340. v += 3
  1341. # Act
  1342. t.set_style(style)
  1343. t.align["l"] = t.align["Align left"] = "l"
  1344. t.align["c"] = t.align["Align centre"] = "c"
  1345. t.align["r"] = t.align["Align right"] = "r"
  1346. # Assert
  1347. result = t.get_string()
  1348. assert result.strip() == expected.strip()
  1349. class TestCsvOutput:
  1350. def test_csv_output(self) -> None:
  1351. t = helper_table()
  1352. assert t.get_csv_string(delimiter="\t", header=False) == (
  1353. "1\tvalue 1\tvalue2\tvalue3\r\n"
  1354. "4\tvalue 4\tvalue5\tvalue6\r\n"
  1355. "7\tvalue 7\tvalue8\tvalue9\r\n"
  1356. )
  1357. assert t.get_csv_string() == (
  1358. ",Field 1,Field 2,Field 3\r\n"
  1359. "1,value 1,value2,value3\r\n"
  1360. "4,value 4,value5,value6\r\n"
  1361. "7,value 7,value8,value9\r\n"
  1362. )
  1363. class TestLatexOutput:
  1364. def test_latex_output(self) -> None:
  1365. t = helper_table()
  1366. assert t.get_latex_string() == (
  1367. "\\begin{tabular}{cccc}\r\n"
  1368. " & Field 1 & Field 2 & Field 3 \\\\\r\n"
  1369. "1 & value 1 & value2 & value3 \\\\\r\n"
  1370. "4 & value 4 & value5 & value6 \\\\\r\n"
  1371. "7 & value 7 & value8 & value9 \\\\\r\n"
  1372. "\\end{tabular}"
  1373. )
  1374. options = {"fields": ["Field 1", "Field 3"]}
  1375. assert t.get_latex_string(**options) == (
  1376. "\\begin{tabular}{cc}\r\n"
  1377. "Field 1 & Field 3 \\\\\r\n"
  1378. "value 1 & value3 \\\\\r\n"
  1379. "value 4 & value6 \\\\\r\n"
  1380. "value 7 & value9 \\\\\r\n"
  1381. "\\end{tabular}"
  1382. )
  1383. def test_latex_output_formatted(self) -> None:
  1384. t = helper_table()
  1385. assert t.get_latex_string(format=True) == (
  1386. "\\begin{tabular}{|c|c|c|c|}\r\n"
  1387. "\\hline\r\n"
  1388. " & Field 1 & Field 2 & Field 3 \\\\\r\n"
  1389. "1 & value 1 & value2 & value3 \\\\\r\n"
  1390. "4 & value 4 & value5 & value6 \\\\\r\n"
  1391. "7 & value 7 & value8 & value9 \\\\\r\n"
  1392. "\\hline\r\n"
  1393. "\\end{tabular}"
  1394. )
  1395. options = {"fields": ["Field 1", "Field 3"]}
  1396. assert t.get_latex_string(format=True, **options) == (
  1397. "\\begin{tabular}{|c|c|}\r\n"
  1398. "\\hline\r\n"
  1399. "Field 1 & Field 3 \\\\\r\n"
  1400. "value 1 & value3 \\\\\r\n"
  1401. "value 4 & value6 \\\\\r\n"
  1402. "value 7 & value9 \\\\\r\n"
  1403. "\\hline\r\n"
  1404. "\\end{tabular}"
  1405. )
  1406. options = {"vrules": FRAME}
  1407. assert t.get_latex_string(format=True, **options) == (
  1408. "\\begin{tabular}{|cccc|}\r\n"
  1409. "\\hline\r\n"
  1410. " & Field 1 & Field 2 & Field 3 \\\\\r\n"
  1411. "1 & value 1 & value2 & value3 \\\\\r\n"
  1412. "4 & value 4 & value5 & value6 \\\\\r\n"
  1413. "7 & value 7 & value8 & value9 \\\\\r\n"
  1414. "\\hline\r\n"
  1415. "\\end{tabular}"
  1416. )
  1417. options = {"hrules": ALL}
  1418. assert t.get_latex_string(format=True, **options) == (
  1419. "\\begin{tabular}{|c|c|c|c|}\r\n"
  1420. "\\hline\r\n"
  1421. " & Field 1 & Field 2 & Field 3 \\\\\r\n"
  1422. "\\hline\r\n"
  1423. "1 & value 1 & value2 & value3 \\\\\r\n"
  1424. "\\hline\r\n"
  1425. "4 & value 4 & value5 & value6 \\\\\r\n"
  1426. "\\hline\r\n"
  1427. "7 & value 7 & value8 & value9 \\\\\r\n"
  1428. "\\hline\r\n"
  1429. "\\end{tabular}"
  1430. )
  1431. def test_latex_output_header(self) -> None:
  1432. t = helper_table()
  1433. assert t.get_latex_string(format=True, hrules=HEADER) == (
  1434. "\\begin{tabular}{|c|c|c|c|}\r\n"
  1435. " & Field 1 & Field 2 & Field 3 \\\\\r\n"
  1436. "\\hline\r\n"
  1437. "1 & value 1 & value2 & value3 \\\\\r\n"
  1438. "4 & value 4 & value5 & value6 \\\\\r\n"
  1439. "7 & value 7 & value8 & value9 \\\\\r\n"
  1440. "\\end{tabular}"
  1441. )
  1442. class TestJSONConstructor:
  1443. def test_json_and_back(self, city_data_prettytable: PrettyTable) -> None:
  1444. json_string = city_data_prettytable.get_json_string()
  1445. new_table = from_json(json_string)
  1446. assert new_table.get_string() == city_data_prettytable.get_string()
  1447. class TestHtmlConstructor:
  1448. def test_html_and_back(self, city_data_prettytable: PrettyTable) -> None:
  1449. html_string = city_data_prettytable.get_html_string()
  1450. new_table = from_html(html_string)[0]
  1451. assert new_table.get_string() == city_data_prettytable.get_string()
  1452. def test_html_one_and_back(self, city_data_prettytable: PrettyTable) -> None:
  1453. html_string = city_data_prettytable.get_html_string()
  1454. new_table = from_html_one(html_string)
  1455. assert new_table.get_string() == city_data_prettytable.get_string()
  1456. def test_html_one_fail_on_many(self, city_data_prettytable: PrettyTable) -> None:
  1457. html_string = city_data_prettytable.get_html_string()
  1458. html_string += city_data_prettytable.get_html_string()
  1459. with pytest.raises(ValueError):
  1460. from_html_one(html_string)
  1461. @pytest.fixture
  1462. def japanese_pretty_table() -> PrettyTable:
  1463. table = PrettyTable(["Kanji", "Hiragana", "English"])
  1464. table.add_row(["神戸", "こうべ", "Kobe"])
  1465. table.add_row(["京都", "きょうと", "Kyoto"])
  1466. table.add_row(["長崎", "ながさき", "Nagasaki"])
  1467. table.add_row(["名古屋", "なごや", "Nagoya"])
  1468. table.add_row(["大阪", "おおさか", "Osaka"])
  1469. table.add_row(["札幌", "さっぽろ", "Sapporo"])
  1470. table.add_row(["東京", "とうきょう", "Tokyo"])
  1471. table.add_row(["横浜", "よこはま", "Yokohama"])
  1472. return table
  1473. @pytest.fixture
  1474. def emoji_pretty_table() -> PrettyTable:
  1475. thunder1 = [
  1476. '\033[38;5;226m _`/""\033[38;5;250m.-. \033[0m',
  1477. "\033[38;5;226m ,\\_\033[38;5;250m( ). \033[0m",
  1478. "\033[38;5;226m /\033[38;5;250m(___(__) \033[0m",
  1479. "\033[38;5;228;5m ⚡\033[38;5;111;25mʻ ʻ\033[38;5;228;5m"
  1480. "⚡\033[38;5;111;25mʻ ʻ \033[0m",
  1481. "\033[38;5;111m ʻ ʻ ʻ ʻ \033[0m",
  1482. ]
  1483. thunder2 = [
  1484. "\033[38;5;240;1m .-. \033[0m",
  1485. "\033[38;5;240;1m ( ). \033[0m",
  1486. "\033[38;5;240;1m (___(__) \033[0m",
  1487. "\033[38;5;21;1m ‚ʻ\033[38;5;228;5m⚡\033[38;5;21;25mʻ‚\033[38;5;228;5m"
  1488. "⚡\033[38;5;21;25m‚ʻ \033[0m",
  1489. "\033[38;5;21;1m ‚ʻ‚ʻ\033[38;5;228;5m⚡\033[38;5;21;25mʻ‚ʻ \033[0m",
  1490. ]
  1491. table = PrettyTable(["Thunderbolt", "Lightning"])
  1492. for i in range(len(thunder1)):
  1493. table.add_row([thunder1[i], thunder2[i]])
  1494. return table
  1495. class TestMultiPattern:
  1496. @pytest.mark.parametrize(
  1497. ["pt", "expected_output", "test_type"],
  1498. [
  1499. (
  1500. lf("city_data_prettytable"),
  1501. """
  1502. +-----------+------+------------+-----------------+
  1503. | City name | Area | Population | Annual Rainfall |
  1504. +-----------+------+------------+-----------------+
  1505. | Adelaide | 1295 | 1158259 | 600.5 |
  1506. | Brisbane | 5905 | 1857594 | 1146.4 |
  1507. | Darwin | 112 | 120900 | 1714.7 |
  1508. | Hobart | 1357 | 205556 | 619.5 |
  1509. | Sydney | 2058 | 4336374 | 1214.8 |
  1510. | Melbourne | 1566 | 3806092 | 646.9 |
  1511. | Perth | 5386 | 1554769 | 869.4 |
  1512. +-----------+------+------------+-----------------+
  1513. """,
  1514. "English Table",
  1515. ),
  1516. (
  1517. lf("japanese_pretty_table"),
  1518. """
  1519. +--------+------------+----------+
  1520. | Kanji | Hiragana | English |
  1521. +--------+------------+----------+
  1522. | 神戸 | こうべ | Kobe |
  1523. | 京都 | きょうと | Kyoto |
  1524. | 長崎 | ながさき | Nagasaki |
  1525. | 名古屋 | なごや | Nagoya |
  1526. | 大阪 | おおさか | Osaka |
  1527. | 札幌 | さっぽろ | Sapporo |
  1528. | 東京 | とうきょう | Tokyo |
  1529. | 横浜 | よこはま | Yokohama |
  1530. +--------+------------+----------+
  1531. """,
  1532. "Japanese table",
  1533. ),
  1534. (
  1535. lf("emoji_pretty_table"),
  1536. """
  1537. +-----------------+-----------------+
  1538. | Thunderbolt | Lightning |
  1539. +-----------------+-----------------+
  1540. | \x1b[38;5;226m _`/""\x1b[38;5;250m.-. \x1b[0m | \x1b[38;5;240;1m .-. \x1b[0m |
  1541. | \x1b[38;5;226m ,\\_\x1b[38;5;250m( ). \x1b[0m | \x1b[38;5;240;1m ( ). \x1b[0m |
  1542. | \x1b[38;5;226m /\x1b[38;5;250m(___(__) \x1b[0m | \x1b[38;5;240;1m (___(__) \x1b[0m |
  1543. | \x1b[38;5;228;5m ⚡\x1b[38;5;111;25mʻ ʻ\x1b[38;5;228;5m⚡\x1b[38;5;111;25mʻ ʻ \x1b[0m | \x1b[38;5;21;1m ‚ʻ\x1b[38;5;228;5m⚡\x1b[38;5;21;25mʻ‚\x1b[38;5;228;5m⚡\x1b[38;5;21;25m‚ʻ \x1b[0m |
  1544. | \x1b[38;5;111m ʻ ʻ ʻ ʻ \x1b[0m | \x1b[38;5;21;1m ‚ʻ‚ʻ\x1b[38;5;228;5m⚡\x1b[38;5;21;25mʻ‚ʻ \x1b[0m |
  1545. +-----------------+-----------------+
  1546. """, # noqa: E501
  1547. "Emoji table",
  1548. ),
  1549. ],
  1550. )
  1551. def test_multi_pattern_outputs(
  1552. self, pt: PrettyTable, expected_output: str, test_type: str
  1553. ) -> None:
  1554. printed_table = pt.get_string()
  1555. assert (
  1556. printed_table.strip() == expected_output.strip()
  1557. ), f"Error output for test output of type {test_type}"
  1558. def test_paginate() -> None:
  1559. # Arrange
  1560. t = helper_table(rows=7)
  1561. expected_page_1 = """
  1562. +----+----------+---------+---------+
  1563. | | Field 1 | Field 2 | Field 3 |
  1564. +----+----------+---------+---------+
  1565. | 1 | value 1 | value2 | value3 |
  1566. | 4 | value 4 | value5 | value6 |
  1567. | 7 | value 7 | value8 | value9 |
  1568. | 10 | value 10 | value11 | value12 |
  1569. +----+----------+---------+---------+
  1570. """.strip()
  1571. expected_page_2 = """
  1572. +----+----------+---------+---------+
  1573. | | Field 1 | Field 2 | Field 3 |
  1574. +----+----------+---------+---------+
  1575. | 13 | value 13 | value14 | value15 |
  1576. | 16 | value 16 | value17 | value18 |
  1577. | 19 | value 19 | value20 | value21 |
  1578. +----+----------+---------+---------+
  1579. """.strip()
  1580. # Act
  1581. paginated = t.paginate(page_length=4)
  1582. # Assert
  1583. paginated = paginated.strip()
  1584. assert paginated.startswith(expected_page_1)
  1585. assert "\f" in paginated
  1586. assert paginated.endswith(expected_page_2)
  1587. # Act
  1588. paginated = t.paginate(page_length=4, line_break="\n")
  1589. # Assert
  1590. assert "\f" not in paginated
  1591. assert "\n" in paginated
  1592. def test_add_rows() -> None:
  1593. """A table created with multiple add_row calls
  1594. is the same as one created with a single add_rows
  1595. """
  1596. # Arrange
  1597. table1 = PrettyTable(["A", "B", "C"])
  1598. table2 = PrettyTable(["A", "B", "C"])
  1599. table1.add_row([1, 2, 3])
  1600. table1.add_row([4, 5, 6])
  1601. rows = [
  1602. [1, 2, 3],
  1603. [4, 5, 6],
  1604. ]
  1605. # Act
  1606. table2.add_rows(rows)
  1607. # Assert
  1608. assert str(table1) == str(table2)
  1609. def test_autoindex() -> None:
  1610. """Testing that a table with a custom index row is
  1611. equal to the one produced by the function
  1612. .add_autoindex()
  1613. """
  1614. table1 = PrettyTable()
  1615. table1.field_names = ["City name", "Area", "Population", "Annual Rainfall"]
  1616. table1.add_row(["Adelaide", 1295, 1158259, 600.5])
  1617. table1.add_row(["Brisbane", 5905, 1857594, 1146.4])
  1618. table1.add_row(["Darwin", 112, 120900, 1714.7])
  1619. table1.add_row(["Hobart", 1357, 205556, 619.5])
  1620. table1.add_row(["Sydney", 2058, 4336374, 1214.8])
  1621. table1.add_row(["Melbourne", 1566, 3806092, 646.9])
  1622. table1.add_row(["Perth", 5386, 1554769, 869.4])
  1623. table1.add_autoindex(fieldname="Test")
  1624. table2 = PrettyTable()
  1625. table2.field_names = ["Test", "City name", "Area", "Population", "Annual Rainfall"]
  1626. table2.add_row([1, "Adelaide", 1295, 1158259, 600.5])
  1627. table2.add_row([2, "Brisbane", 5905, 1857594, 1146.4])
  1628. table2.add_row([3, "Darwin", 112, 120900, 1714.7])
  1629. table2.add_row([4, "Hobart", 1357, 205556, 619.5])
  1630. table2.add_row([5, "Sydney", 2058, 4336374, 1214.8])
  1631. table2.add_row([6, "Melbourne", 1566, 3806092, 646.9])
  1632. table2.add_row([7, "Perth", 5386, 1554769, 869.4])
  1633. assert str(table1) == str(table2)
  1634. @pytest.fixture(scope="function")
  1635. def unpadded_pt() -> PrettyTable:
  1636. table = PrettyTable(header=False, padding_width=0)
  1637. table.add_row("abc")
  1638. table.add_row("def")
  1639. table.add_row("g..")
  1640. return table
  1641. class TestUnpaddedTable:
  1642. def test_unbordered(self, unpadded_pt: PrettyTable) -> None:
  1643. unpadded_pt.border = False
  1644. result = unpadded_pt.get_string()
  1645. expected = """
  1646. abc
  1647. def
  1648. g..
  1649. """
  1650. assert result.strip() == expected.strip()
  1651. def test_bordered(self, unpadded_pt: PrettyTable) -> None:
  1652. unpadded_pt.border = True
  1653. result = unpadded_pt.get_string()
  1654. expected = """
  1655. +-+-+-+
  1656. |a|b|c|
  1657. |d|e|f|
  1658. |g|.|.|
  1659. +-+-+-+
  1660. """
  1661. assert result.strip() == expected.strip()
  1662. class TestCustomFormatter:
  1663. def test_init_custom_format_is_empty(self) -> None:
  1664. table = PrettyTable()
  1665. assert table.custom_format == {}
  1666. def test_init_custom_format_set_value(self) -> None:
  1667. table = PrettyTable(
  1668. custom_format={"col1": (lambda col_name, value: f"{value:.2}")}
  1669. )
  1670. assert len(table.custom_format) == 1
  1671. def test_init_custom_format_throw_error_is_not_callable(self) -> None:
  1672. with pytest.raises(ValueError) as e:
  1673. PrettyTable(custom_format={"col1": "{:.2}"})
  1674. assert "Invalid value for custom_format.col1. Must be a function." in str(
  1675. e.value
  1676. )
  1677. def test_can_set_custom_format_from_property_setter(self) -> None:
  1678. table = PrettyTable()
  1679. table.custom_format = {"col1": (lambda col_name, value: f"{value:.2}")}
  1680. assert len(table.custom_format) == 1
  1681. def test_set_custom_format_to_none_set_empty_dict(self) -> None:
  1682. table = PrettyTable()
  1683. table.custom_format = None
  1684. assert len(table.custom_format) == 0
  1685. assert isinstance(table.custom_format, dict)
  1686. def test_set_custom_format_invalid_type_throw_error(self) -> None:
  1687. table = PrettyTable()
  1688. with pytest.raises(TypeError) as e:
  1689. table.custom_format = "Some String"
  1690. assert "The custom_format property need to be a dictionary or callable" in str(
  1691. e.value
  1692. )
  1693. def test_use_custom_formatter_for_int(
  1694. self, city_data_prettytable: PrettyTable
  1695. ) -> None:
  1696. city_data_prettytable.custom_format["Annual Rainfall"] = lambda n, v: f"{v:.2f}"
  1697. assert (
  1698. city_data_prettytable.get_string().strip()
  1699. == """
  1700. +-----------+------+------------+-----------------+
  1701. | City name | Area | Population | Annual Rainfall |
  1702. +-----------+------+------------+-----------------+
  1703. | Adelaide | 1295 | 1158259 | 600.50 |
  1704. | Brisbane | 5905 | 1857594 | 1146.40 |
  1705. | Darwin | 112 | 120900 | 1714.70 |
  1706. | Hobart | 1357 | 205556 | 619.50 |
  1707. | Sydney | 2058 | 4336374 | 1214.80 |
  1708. | Melbourne | 1566 | 3806092 | 646.90 |
  1709. | Perth | 5386 | 1554769 | 869.40 |
  1710. +-----------+------+------------+-----------------+
  1711. """.strip()
  1712. )
  1713. def test_custom_format_multi_type(self) -> None:
  1714. table = PrettyTable(["col_date", "col_str", "col_float", "col_int"])
  1715. table.add_row([dt.date(2021, 1, 1), "January", 12345.12345, 12345678])
  1716. table.add_row([dt.date(2021, 2, 1), "February", 54321.12345, 87654321])
  1717. table.custom_format["col_date"] = lambda f, v: v.strftime("%d %b %Y")
  1718. table.custom_format["col_float"] = lambda f, v: f"{v:.3f}"
  1719. table.custom_format["col_int"] = lambda f, v: f"{v:,}"
  1720. assert (
  1721. table.get_string().strip()
  1722. == """
  1723. +-------------+----------+-----------+------------+
  1724. | col_date | col_str | col_float | col_int |
  1725. +-------------+----------+-----------+------------+
  1726. | 01 Jan 2021 | January | 12345.123 | 12,345,678 |
  1727. | 01 Feb 2021 | February | 54321.123 | 87,654,321 |
  1728. +-------------+----------+-----------+------------+
  1729. """.strip()
  1730. )
  1731. def test_custom_format_multi_type_using_on_function(self) -> None:
  1732. table = PrettyTable(["col_date", "col_str", "col_float", "col_int"])
  1733. table.add_row([dt.date(2021, 1, 1), "January", 12345.12345, 12345678])
  1734. table.add_row([dt.date(2021, 2, 1), "February", 54321.12345, 87654321])
  1735. def my_format(col: str, value: Any) -> str:
  1736. if col == "col_date":
  1737. return value.strftime("%d %b %Y")
  1738. if col == "col_float":
  1739. return f"{value:.3f}"
  1740. if col == "col_int":
  1741. return f"{value:,}"
  1742. return str(value)
  1743. table.custom_format = my_format
  1744. assert (
  1745. table.get_string().strip()
  1746. == """
  1747. +-------------+----------+-----------+------------+
  1748. | col_date | col_str | col_float | col_int |
  1749. +-------------+----------+-----------+------------+
  1750. | 01 Jan 2021 | January | 12345.123 | 12,345,678 |
  1751. | 01 Feb 2021 | February | 54321.123 | 87,654,321 |
  1752. +-------------+----------+-----------+------------+
  1753. """.strip()
  1754. )
  1755. class TestRepr:
  1756. def test_default_repr(self, row_prettytable: PrettyTable) -> None:
  1757. assert row_prettytable.__str__() == row_prettytable.__repr__()
  1758. def test_jupyter_repr(self, row_prettytable: PrettyTable) -> None:
  1759. assert row_prettytable._repr_html_() == row_prettytable.get_html_string()
  1760. class TestMinTableWidth:
  1761. @pytest.mark.parametrize(
  1762. "loops, fields, desired_width, border, internal_border",
  1763. [
  1764. (15, ["Test table"], 20, True, False),
  1765. (16, ["Test table"], 21, True, False),
  1766. (18, ["Test table", "Test table 2"], 40, True, False),
  1767. (19, ["Test table", "Test table 2"], 41, True, False),
  1768. (21, ["Test table", "Test col 2", "Test col 3"], 50, True, False),
  1769. (22, ["Test table", "Test col 2", "Test col 3"], 51, True, False),
  1770. (19, ["Test table"], 20, False, False),
  1771. (20, ["Test table"], 21, False, False),
  1772. (25, ["Test table", "Test table 2"], 40, False, False),
  1773. (26, ["Test table", "Test table 2"], 41, False, False),
  1774. (25, ["Test table", "Test col 2", "Test col 3"], 50, False, False),
  1775. (26, ["Test table", "Test col 2", "Test col 3"], 51, False, False),
  1776. (18, ["Test table"], 20, False, True),
  1777. (19, ["Test table"], 21, False, True),
  1778. (23, ["Test table", "Test table 2"], 40, False, True),
  1779. (24, ["Test table", "Test table 2"], 41, False, True),
  1780. (22, ["Test table", "Test col 2", "Test col 3"], 50, False, True),
  1781. (23, ["Test table", "Test col 2", "Test col 3"], 51, False, True),
  1782. ],
  1783. )
  1784. def test_min_table_width(
  1785. self, loops, fields, desired_width, border, internal_border
  1786. ) -> None:
  1787. for col_width in range(loops):
  1788. x = prettytable.PrettyTable()
  1789. x.border = border
  1790. x.preserve_internal_border = internal_border
  1791. x.field_names = fields
  1792. x.add_row(["X" * col_width] + ["" for _ in range(len(fields) - 1)])
  1793. x.min_table_width = desired_width
  1794. t = x.get_string()
  1795. if border is False and internal_border is False:
  1796. assert [len(x) for x in t.split("\n")] == [desired_width, desired_width]
  1797. elif border is False and internal_border is True:
  1798. assert [len(x) for x in t.split("\n")] == [
  1799. desired_width,
  1800. desired_width - 1,
  1801. desired_width,
  1802. ]
  1803. else:
  1804. assert [len(x) for x in t.split("\n")] == [
  1805. desired_width,
  1806. desired_width,
  1807. desired_width,
  1808. desired_width,
  1809. desired_width,
  1810. ]
  1811. class TestMaxTableWidth:
  1812. def test_max_table_width(self) -> None:
  1813. table = PrettyTable()
  1814. table.max_table_width = 5
  1815. table.add_row([0])
  1816. # FIXME: Table is wider than table.max_table_width
  1817. assert (
  1818. table.get_string().strip()
  1819. == """
  1820. +----+
  1821. | Fi |
  1822. +----+
  1823. | 0 |
  1824. +----+
  1825. """.strip()
  1826. )
  1827. def test_max_table_width_wide(self) -> None:
  1828. table = PrettyTable()
  1829. table.max_table_width = 52
  1830. table.add_row(
  1831. [
  1832. 0,
  1833. 0,
  1834. 0,
  1835. 0,
  1836. 0,
  1837. "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam "
  1838. "nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam "
  1839. "erat, sed diam voluptua",
  1840. ]
  1841. )
  1842. assert (
  1843. table.get_string().strip()
  1844. == """
  1845. +---+---+---+---+---+------------------------------+
  1846. | F | F | F | F | F | Field 6 |
  1847. +---+---+---+---+---+------------------------------+
  1848. | 0 | 0 | 0 | 0 | 0 | Lorem ipsum dolor sit amet, |
  1849. | | | | | | consetetur sadipscing elitr, |
  1850. | | | | | | sed diam nonumy eirmod |
  1851. | | | | | | tempor invidunt ut labore et |
  1852. | | | | | | dolore magna aliquyam erat, |
  1853. | | | | | | sed diam voluptua |
  1854. +---+---+---+---+---+------------------------------+""".strip()
  1855. )
  1856. def test_max_table_width_wide2(self) -> None:
  1857. table = PrettyTable()
  1858. table.max_table_width = 70
  1859. table.add_row(
  1860. [
  1861. "Lorem",
  1862. "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam ",
  1863. "ipsum",
  1864. "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam ",
  1865. "dolor",
  1866. "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam ",
  1867. ]
  1868. )
  1869. assert (
  1870. table.get_string().strip()
  1871. == """
  1872. +---+-----------------+---+-----------------+---+-----------------+
  1873. | F | Field 2 | F | Field 4 | F | Field 6 |
  1874. +---+-----------------+---+-----------------+---+-----------------+
  1875. | L | Lorem ipsum | i | Lorem ipsum | d | Lorem ipsum |
  1876. | o | dolor sit amet, | p | dolor sit amet, | o | dolor sit amet, |
  1877. | r | consetetur | s | consetetur | l | consetetur |
  1878. | e | sadipscing | u | sadipscing | o | sadipscing |
  1879. | m | elitr, sed diam | m | elitr, sed diam | r | elitr, sed diam |
  1880. +---+-----------------+---+-----------------+---+-----------------+""".strip()
  1881. )
  1882. def test_max_table_width_wide_vrules_frame(self) -> None:
  1883. table = PrettyTable()
  1884. table.max_table_width = 52
  1885. table.vrules = FRAME
  1886. table.add_row(
  1887. [
  1888. 0,
  1889. 0,
  1890. 0,
  1891. 0,
  1892. 0,
  1893. "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam "
  1894. "nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam "
  1895. "erat, sed diam voluptua",
  1896. ]
  1897. )
  1898. assert (
  1899. table.get_string().strip()
  1900. == """
  1901. +--------------------------------------------------+
  1902. | F F F F F Field 6 |
  1903. +--------------------------------------------------+
  1904. | 0 0 0 0 0 Lorem ipsum dolor sit amet, |
  1905. | consetetur sadipscing elitr, |
  1906. | sed diam nonumy eirmod |
  1907. | tempor invidunt ut labore et |
  1908. | dolore magna aliquyam erat, |
  1909. | sed diam voluptua |
  1910. +--------------------------------------------------+""".strip()
  1911. )
  1912. def test_max_table_width_wide_vrules_none(self) -> None:
  1913. table = PrettyTable()
  1914. table.max_table_width = 52
  1915. table.vrules = NONE
  1916. table.add_row(
  1917. [
  1918. 0,
  1919. 0,
  1920. 0,
  1921. 0,
  1922. 0,
  1923. "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam "
  1924. "nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam "
  1925. "erat, sed diam voluptua",
  1926. ]
  1927. )
  1928. assert (
  1929. table.get_string().strip()
  1930. == """
  1931. ----------------------------------------------------
  1932. F F F F F Field 6
  1933. ----------------------------------------------------
  1934. 0 0 0 0 0 Lorem ipsum dolor sit amet,
  1935. consetetur sadipscing elitr,
  1936. sed diam nonumy eirmod
  1937. tempor invidunt ut labore et
  1938. dolore magna aliquyam erat,
  1939. sed diam voluptua
  1940. ----------------------------------------------------""".strip() # noqa: W291
  1941. )
  1942. class TestRowEndSection:
  1943. def test_row_end_section(self) -> None:
  1944. table = PrettyTable()
  1945. v = 1
  1946. for row in range(4):
  1947. if row % 2 == 0:
  1948. table.add_row(
  1949. [f"value {v}", f"value{v+1}", f"value{v+2}"], divider=True
  1950. )
  1951. else:
  1952. table.add_row(
  1953. [f"value {v}", f"value{v+1}", f"value{v+2}"], divider=False
  1954. )
  1955. v += 3
  1956. table.del_row(0)
  1957. assert (
  1958. table.get_string().strip()
  1959. == """
  1960. +----------+---------+---------+
  1961. | Field 1 | Field 2 | Field 3 |
  1962. +----------+---------+---------+
  1963. | value 4 | value5 | value6 |
  1964. | value 7 | value8 | value9 |
  1965. +----------+---------+---------+
  1966. | value 10 | value11 | value12 |
  1967. +----------+---------+---------+
  1968. """.strip()
  1969. )
  1970. class TestClearing:
  1971. def test_clear_rows(self, row_prettytable: PrettyTable) -> None:
  1972. t = helper_table()
  1973. t.add_row([0, "a", "b", "c"], divider=True)
  1974. t.clear_rows()
  1975. assert t.rows == []
  1976. assert t.dividers == []
  1977. assert t.field_names == ["", "Field 1", "Field 2", "Field 3"]
  1978. def test_clear(self, row_prettytable: PrettyTable) -> None:
  1979. t = helper_table()
  1980. t.add_row([0, "a", "b", "c"], divider=True)
  1981. t.clear()
  1982. assert t.rows == []
  1983. assert t.dividers == []
  1984. assert t.field_names == []
  1985. class TestPreservingInternalBorders:
  1986. def test_internal_border_preserved(self) -> None:
  1987. pt = helper_table(3)
  1988. pt.border = False
  1989. pt.preserve_internal_border = True
  1990. assert (
  1991. pt.get_string().strip()
  1992. == """
  1993. | Field 1 | Field 2 | Field 3
  1994. ---+---------+---------+---------
  1995. 1 | value 1 | value2 | value3
  1996. 4 | value 4 | value5 | value6
  1997. 7 | value 7 | value8 | value9
  1998. """.strip() # noqa: W291
  1999. )
  2000. def test_internal_border_preserved_latex(self) -> None:
  2001. pt = helper_table(3)
  2002. pt.border = False
  2003. pt.format = True
  2004. pt.preserve_internal_border = True
  2005. assert pt.get_latex_string().strip() == (
  2006. "\\begin{tabular}{c|c|c|c}\r\n"
  2007. " & Field 1 & Field 2 & Field 3 \\\\\r\n"
  2008. "1 & value 1 & value2 & value3 \\\\\r\n"
  2009. "4 & value 4 & value5 & value6 \\\\\r\n"
  2010. "7 & value 7 & value8 & value9 \\\\\r\n"
  2011. "\\end{tabular}"
  2012. )
  2013. def test_internal_border_preserved_html(self) -> None:
  2014. pt = helper_table(3)
  2015. pt.format = True
  2016. pt.border = False
  2017. pt.preserve_internal_border = True
  2018. assert (
  2019. pt.get_html_string().strip()
  2020. == """
  2021. <table rules="cols">
  2022. <thead>
  2023. <tr>
  2024. <th style="padding-left: 1em; padding-right: 1em; text-align: center"></th>
  2025. <th style="padding-left: 1em; padding-right: 1em; text-align: center">Field 1</th>
  2026. <th style="padding-left: 1em; padding-right: 1em; text-align: center">Field 2</th>
  2027. <th style="padding-left: 1em; padding-right: 1em; text-align: center">Field 3</th>
  2028. </tr>
  2029. </thead>
  2030. <tbody>
  2031. <tr>
  2032. <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">1</td>
  2033. <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value 1</td>
  2034. <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value2</td>
  2035. <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value3</td>
  2036. </tr>
  2037. <tr>
  2038. <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">4</td>
  2039. <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value 4</td>
  2040. <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value5</td>
  2041. <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value6</td>
  2042. </tr>
  2043. <tr>
  2044. <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">7</td>
  2045. <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value 7</td>
  2046. <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value8</td>
  2047. <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value9</td>
  2048. </tr>
  2049. </tbody>
  2050. </table>
  2051. """.strip() # noqa: E501
  2052. )
  2053. class TestGeneralOutput:
  2054. def test_copy(self) -> None:
  2055. # Arrange
  2056. t = helper_table()
  2057. # Act
  2058. t_copy = t.copy()
  2059. # Assert
  2060. assert t.get_string() == t_copy.get_string()
  2061. def test_text(self) -> None:
  2062. t = helper_table()
  2063. assert t.get_formatted_string("text") == t.get_string()
  2064. # test with default arg, too
  2065. assert t.get_formatted_string() == t.get_string()
  2066. # args passed through
  2067. assert t.get_formatted_string(border=False) == t.get_string(border=False)
  2068. def test_csv(self) -> None:
  2069. t = helper_table()
  2070. assert t.get_formatted_string("csv") == t.get_csv_string()
  2071. # args passed through
  2072. assert t.get_formatted_string("csv", border=False) == t.get_csv_string(
  2073. border=False
  2074. )
  2075. def test_json(self) -> None:
  2076. t = helper_table()
  2077. assert t.get_formatted_string("json") == t.get_json_string()
  2078. # args passed through
  2079. assert t.get_formatted_string("json", border=False) == t.get_json_string(
  2080. border=False
  2081. )
  2082. def test_html(self) -> None:
  2083. t = helper_table()
  2084. assert t.get_formatted_string("html") == t.get_html_string()
  2085. # args passed through
  2086. assert t.get_formatted_string("html", border=False) == t.get_html_string(
  2087. border=False
  2088. )
  2089. def test_latex(self) -> None:
  2090. t = helper_table()
  2091. assert t.get_formatted_string("latex") == t.get_latex_string()
  2092. # args passed through
  2093. assert t.get_formatted_string("latex", border=False) == t.get_latex_string(
  2094. border=False
  2095. )
  2096. def test_invalid(self) -> None:
  2097. t = helper_table()
  2098. with pytest.raises(ValueError):
  2099. t.get_formatted_string("pdf")