Skip to content

Commit d952b97

Browse files
authored
Merge pull request #5 from amahlaka/Coverage_patches
Coverage patches
2 parents cd1e440 + 8dd456b commit d952b97

File tree

3 files changed

+187
-7
lines changed

3 files changed

+187
-7
lines changed

.github/workflows/pylint.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ jobs:
1818
run: |
1919
python -m pip install --upgrade pip
2020
pip install pylint
21+
pip install pytest
2122
- name: Analysing the code with pylint
2223
run: |
2324
pylint $(git ls-files '*.py')

src/markdown_helper/__init__.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ def sort_table(self, disable_convert: bool = False):
109109
# If multiple sort keys are provided, prioritize the first one, then the second, etc.
110110
sort_keys = self.sort_key.split(",")
111111
for sort_key in sort_keys:
112+
if sort_key not in self.headers:
113+
raise ValueError(f"sort_key {sort_key} not in headers")
112114
if disable_convert:
113115
self.rows = sorted(
114116
self.rows,
@@ -243,8 +245,10 @@ def __str__(self):
243245
return self.html()
244246
return self.markdown()
245247
def __repr__(self):
246-
return f"""Image(url={self.url}, title={self.title}, alt={self.alt},
247-
width={self.width}, height={self.height}, align={self.align}, caption={self.caption})"""
248+
return_string = f"Image(url={self.url}, title={self.title}, alt={self.alt}"
249+
return_string += f", width={self.width}, height={self.height}, align={self.align},"
250+
return_string +=f"caption={self.caption})"
251+
return return_string
248252

249253

250254
class Link:

tests/test_markdown.py

Lines changed: 180 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
"""
22
This file contains the pytest tests for the markdown_helper.py file.
33
"""
4+
import pytest
45
try:
56
from src import markdown_helper as markdown
67
except ModuleNotFoundError:
78
try:
89
import markdown_helper as markdown
910
except ModuleNotFoundError:
10-
from ..src import markdown_helper as markdown # pylint: disable=import-error, relative-beyond-top-level
11+
from ..src import ( # pylint: disable=relative-beyond-top-level
12+
markdown_helper as markdown, # pylint: disable=import-error
13+
) # pylint: disable=relative-beyond-top-level
14+
15+
1116
def test_ordered_list():
1217
"""
1318
This function tests the markdown.List class.
@@ -18,6 +23,16 @@ def test_ordered_list():
1823
), "String representation of ordered list is incorrect."
1924
assert list_1.items == ["item 1", "item 2", "item 3"], "List items are incorrect."
2025

26+
def test_empty_list():
27+
"""
28+
This function tests the markdown.List class.
29+
"""
30+
list_1: markdown.List = markdown.List([], ordered=True)
31+
assert (
32+
str(list_1) == ""
33+
), "String representation of ordered list is incorrect."
34+
assert list_1.items == [], "List items are incorrect."
35+
assert repr(list_1) == "List(title=False, items=[], ordered=True)", "List items are incorrect."
2136

2237
def test_modify_list():
2338
"""
@@ -69,6 +84,9 @@ def test_link():
6984
), "String representation of link is incorrect."
7085
assert link_1.url == "http://www.google.com", "Link URL is incorrect."
7186
assert link_1.text == "Google", "Link text is incorrect."
87+
assert (
88+
repr(link_1) == "Link(url=http://www.google.com, text=Google, title=False, new_tab=False)"
89+
)
7290

7391

7492
def test_link_no_trailing():
@@ -119,6 +137,22 @@ def test_image():
119137
assert image_1.alt == "Google", "Image text is incorrect."
120138

121139

140+
def test_image_texts():
141+
"""
142+
This function tests the markdown.Image class.
143+
"""
144+
image_1 = markdown.Image(
145+
"http://www.google.com", alt="Google", title="Title", caption="Caption"
146+
)
147+
assert (
148+
image_1.markdown() == "### Title\n![Google](http://www.google.com)\n_Caption_\n"
149+
), "Markdown string representation of image is incorrect."
150+
assert (
151+
repr(image_1)
152+
== "Image(url=http://www.google.com, title=Title, alt=Google, width=False, height=False, align=False,caption=Caption)" # pylint: disable=line-too-long
153+
), "String representation of image is incorrect."
154+
155+
122156
def test_image_size():
123157
"""
124158
This function tests the markdown.Image class.
@@ -182,6 +216,7 @@ def test_table_add_row():
182216
{"col 1": "item 1", "col 2": "item 2", "col 3": "item 3"}
183217
], "Table rows are incorrect."
184218

219+
185220
def test_table_add_row_no_headers():
186221
"""
187222
This function tests the table.add_row function with a list input.
@@ -202,6 +237,23 @@ def test_table_add_row_no_headers():
202237
], "Table rows are incorrect."
203238

204239

240+
def test_table_title():
241+
"""
242+
This function tests the markdown.Table class.
243+
"""
244+
table_1 = markdown.Table(["col 1", "col 2", "col 3"], title="My Table")
245+
table_1.add_row({"col 1": "value 1", "col 2": "value 2", "col 3": "value 3"})
246+
assert (
247+
str(table_1)
248+
== "### My Table\n| col 1 | col 2 | col 3 |\n| --- | --- | --- |\n| value 1 | value 2 | value 3 |\n" # pylint: disable=line-too-long
249+
), "String representation of table is incorrect."
250+
assert table_1.headers == [
251+
"col 1",
252+
"col 2",
253+
"col 3",
254+
], "Table columns are incorrect."
255+
assert table_1.title == "My Table", "Table title is incorrect."
256+
205257

206258
def test_table_sort():
207259
"""
@@ -214,21 +266,63 @@ def test_table_sort():
214266
table_1.add_row({"Name": "Third", "Value": 3})
215267
assert (
216268
str(table_1)
217-
== "| Name | Value |\n| --- | --- |\n| First | 1 |\n| Second | 2 |\n| Third | 3 |\n| Fourth | 4 |\n" # pylint: disable=line-too-long
269+
== "| Name | Value |\n| --- | --- |\n| First | 1 |\n| Second | 2 |\n| Third | 3 |\n| Fourth | 4 |\n" # pylint: disable=line-too-long
218270
), "Sorted Table is incorrect."
219271
assert table_1.headers == ["Name", "Value"], "Table columns are incorrect."
220272
table_1.sort_reverse = True
221273
assert (
222274
str(table_1)
223-
== "| Name | Value |\n| --- | --- |\n| Fourth | 4 |\n| Third | 3 |\n| Second | 2 |\n| First | 1 |\n" # pylint: disable=line-too-long
275+
== "| Name | Value |\n| --- | --- |\n| Fourth | 4 |\n| Third | 3 |\n| Second | 2 |\n| First | 1 |\n" # pylint: disable=line-too-long
224276
), "Reverse sorted Table is incorrect."
225277
table_1.sort_reverse = False
226278
table_1.sort_key = "Name"
227279
assert (
228280
str(table_1)
229-
== "| Name | Value |\n| --- | --- |\n| First | 1 |\n| Fourth | 4 |\n| Second | 2 |\n| Third | 3 |\n" # pylint: disable=line-too-long
281+
== "| Name | Value |\n| --- | --- |\n| First | 1 |\n| Fourth | 4 |\n| Second | 2 |\n| Third | 3 |\n" # pylint: disable=line-too-long
230282
), "Second sorted Table is incorrect."
231283

284+
def test_table_sort_error():
285+
"""
286+
This function tests the markdown.Table class.
287+
"""
288+
table_1 = markdown.Table(["Name", "Value"], sort_key="Value")
289+
table_1.add_row({"Name": "First", "Value": 1})
290+
table_1.add_row({"Name": "Second", "Value": False})
291+
table_1.add_row({"Name": "Fourth", "Value": 4})
292+
table_1.add_row({"Name": "Third", "Value": "String"})
293+
with pytest.raises(ValueError):
294+
table_1.sort_key = "invalid"
295+
table_1.sort_table()
296+
297+
def test_table_disable_convert():
298+
"""
299+
This function tests the markdown.Table class.
300+
"""
301+
table_1 = markdown.Table(["Name", "Value"], sort_key="Value")
302+
table_1.add_row({"Name": "First", "Value": 1})
303+
table_1.add_row({"Name": "Second", "Value": 0})
304+
table_1.add_row({"Name": "Fourth", "Value": 1})
305+
table_1.add_row({"Name": "Third", "Value": 0})
306+
table_1.sort_table(True)
307+
assert (
308+
table_1.rows == [{'Name': 'Second', 'Value': 0}, {'Name': 'Third', 'Value': 0},
309+
{'Name': 'First', 'Value': 1}, {'Name': 'Fourth', 'Value': 1}]
310+
), "Sorted Table is incorrect."
311+
312+
def test_table_sort_convert():
313+
"""
314+
This function tests the markdown.Table class.
315+
"""
316+
table_1 = markdown.Table(["Name", "Value"], sort_key="Value")
317+
table_1.add_row({"Name": "First", "Value": 1})
318+
table_1.add_row({"Name": "Second", "Value": 0})
319+
table_1.add_row({"Name": "Fourth", "Value": 1})
320+
table_1.add_row({"Name": "Third", "Value": 0})
321+
table_1.sort_table()
322+
assert (
323+
table_1.rows == [{'Name': 'Second', 'Value': False}, {'Name': 'Third', 'Value': False},
324+
{'Name': 'First', 'Value': True}, {'Name': 'Fourth', 'Value': True}]
325+
), "Sorted Table is incorrect."
232326

233327
def test_table_flexible():
234328
"""
@@ -241,11 +335,55 @@ def test_table_flexible():
241335
table_1.add_row({"Name": "Fourth", "Value": 4})
242336
assert (
243337
str(table_1)
244-
== "| Name | Value | Extra |\n| --- | --- | --- |\n| First | 1 | |\n| Second | 2 | |\n| Third | 3 | Extra Value |\n| Fourth | 4 | |\n" # pylint: disable=line-too-long
338+
== "| Name | Value | Extra |\n| --- | --- | --- |\n| First | 1 | |\n| Second | 2 | |\n| Third | 3 | Extra Value |\n| Fourth | 4 | |\n" # pylint: disable=line-too-long
245339
), "Flexible Table is incorrect."
246340
assert table_1.headers == ["Name", "Value", "Extra"], "Table columns are incorrect."
247341

342+
def test_table_add_rows():
343+
"""
344+
This function tests the markdown.Table class.
345+
"""
346+
table_1 = markdown.Table(["Name", "Value"])
347+
table_1.add_rows([{"Name": "First", "Value": 1}, {"Name": "Second", "Value": 2}])
348+
assert (
349+
str(table_1)
350+
== "| Name | Value |\n| --- | --- |\n| First | 1 |\n| Second | 2 |\n"
351+
), "Table is incorrect."
352+
assert table_1.headers == ["Name", "Value"], "Table columns are incorrect."
248353

354+
def test_table_remap():
355+
"""
356+
This function tests the markdown.Table class.
357+
"""
358+
table_1 = markdown.Table(["Name", "Value"], custom_map={"Name":{"First":"1st","Second":"2nd"}})
359+
table_1.add_row({"Name": "First", "Value": 1})
360+
table_1.add_row({"Name": "Second", "Value": 2})
361+
assert (
362+
str(table_1.get_table())
363+
== "| Name | Value |\n| --- | --- |\n| 1st | 1 |\n| 2nd | 2 |\n" # pylint: disable=line-too-long
364+
), "Remapped Table is incorrect."
365+
assert table_1.headers == ["Name", "Value"], "Table columns are incorrect."
366+
table_2 = markdown.Table(["Name", "Value"], custom_map={"Name":{"First":"1st","Second":"2nd"}})
367+
table_2.add_row({"Name": "First", "Value": 1})
368+
table_2.remap()
369+
assert (
370+
str(table_2)
371+
== "| Name | Value |\n| --- | --- |\n| 1st | 1 |\n" # pylint: disable=line-too-long
372+
), "Remapped Table is incorrect."
373+
374+
375+
def test_header():
376+
"""
377+
This function tests the markdown.Header class.
378+
"""
379+
header_1 = markdown.Header("Header 1", 2)
380+
assert (
381+
str(header_1) == "## Header 1"
382+
), "String representation of header is incorrect."
383+
assert header_1.text == "Header 1", "Header text is incorrect."
384+
assert (
385+
repr(header_1) == "## Header 1"
386+
)
249387
def test_section():
250388
"""
251389
This function tests the markdown.Section class.
@@ -266,6 +404,23 @@ def test_section_add():
266404
assert (
267405
str(section_1) == "# Section 1\nThis is a paragraph. \n"
268406
), "String representation of section is incorrect."
407+
assert section_1.title.text == "Section 1", "Section title is incorrect."
408+
assert (
409+
repr(section_1) == "Section(title=# Section 1, content=This is a paragraph. )"
410+
), "Section representation is incorrect."
411+
412+
413+
def test_section_add_nonempty():
414+
"""
415+
This function tests the markdown.Section class.
416+
"""
417+
section_1 = markdown.Section("Section 1")
418+
section_1.add("This is a paragraph.")
419+
section_1.add("This is another paragraph.")
420+
assert (
421+
str(section_1) == "# Section 1\nThis is a paragraph. \nThis is another paragraph. \n"
422+
), "String representation of section is incorrect."
423+
assert section_1.title.text == "Section 1", "Section title is incorrect."
269424

270425

271426
def test_document():
@@ -283,6 +438,26 @@ def test_document():
283438
str(document_1) == "# Document 1\n## Section 1\nThis is a paragraph. \n"
284439
), "String representation of document is incorrect."
285440

441+
def test_add_section():
442+
"""
443+
This function tests the markdown.Document class.
444+
"""
445+
document_1 = markdown.Document("Document 1", filename="document_1.md")
446+
document_1.add_section("RAW SECTION")
447+
assert (
448+
str(document_1) == "# Document 1\n# RAW SECTION\n\n"
449+
), "String representation of document is incorrect."
450+
451+
def test_document_toc():
452+
"""
453+
This function tests the markdown.Document class.
454+
"""
455+
document_1 = markdown.Document("Document 1", filename="document_1.md", table_of_contents=True) # pylint: disable=line-too-long
456+
document_1.add_section(markdown.Section(markdown.Header("Section 1", 2)))
457+
assert (
458+
document_1.get_document() == "# Document 1\n## Table of Contents\n* [Section 1](#section-1)\n## Section 1\n\n" # pylint: disable=line-too-long
459+
), "String representation of document is incorrect."
460+
286461

287462
def test_document_save(tmp_path):
288463
"""

0 commit comments

Comments
 (0)