Skip to content

Commit c2528c0

Browse files
trivial: Increase code coverage (#451)
1 parent 760f880 commit c2528c0

3 files changed

Lines changed: 308 additions & 0 deletions

File tree

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
"""Unit tests for Wikipedia page section functionality."""
2+
3+
import unittest
4+
from unittest.mock import MagicMock
5+
6+
from tests.mock_data import create_mock_wikipedia
7+
import wikipediaapi
8+
9+
10+
class TestWikipediaPageSection(unittest.TestCase):
11+
"""Test cases for WikipediaPageSection functionality."""
12+
13+
def setUp(self):
14+
"""Set up test fixtures."""
15+
self.wiki = create_mock_wikipedia()
16+
# Set the extract format to WIKI for consistent testing
17+
self.wiki.extract_format = wikipediaapi.ExtractFormat.WIKI
18+
19+
def test_subsection_by_title_found(self):
20+
"""Test getting a subsection when it exists."""
21+
# Create mock subsections
22+
subsection1 = MagicMock()
23+
subsection1.title = "Subsection 1"
24+
subsection1.level = 2
25+
subsection1.full_text.return_value = "Subsection 1 content"
26+
27+
subsection2 = MagicMock()
28+
subsection2.title = "Subsection 2"
29+
subsection2.level = 2
30+
subsection2.full_text.return_value = "Subsection 2 content"
31+
32+
# Create main section with subsections
33+
section = wikipediaapi.WikipediaPageSection(wiki=self.wiki, title="Main Section", level=1)
34+
section._section = [subsection1, subsection2]
35+
36+
# Test getting existing subsection
37+
result = section.section_by_title("Subsection 2")
38+
self.assertEqual(result, subsection2)
39+
40+
# Test getting first subsection when multiple exist
41+
result = section.section_by_title("Subsection 1")
42+
self.assertEqual(result, subsection1)
43+
44+
def test_subsection_by_title_not_found(self):
45+
"""Test getting a subsection when it doesn't exist."""
46+
section = wikipediaapi.WikipediaPageSection(wiki=self.wiki, title="Main Section", level=1)
47+
section._section = []
48+
49+
result = section.section_by_title("Nonexistent")
50+
self.assertIsNone(result)
51+
52+
def test_subsection_by_title_multiple_returns_last(self):
53+
"""Test that section_by_title returns the last matching subsection."""
54+
subsection1 = MagicMock()
55+
subsection1.title = "Same Name"
56+
subsection1.level = 2
57+
subsection1.full_text.return_value = "First content"
58+
59+
subsection2 = MagicMock()
60+
subsection2.title = "Same Name"
61+
subsection2.level = 3
62+
subsection2.full_text.return_value = "Second content"
63+
64+
section = wikipediaapi.WikipediaPageSection(wiki=self.wiki, title="Main Section", level=1)
65+
section._section = [subsection1, subsection2]
66+
67+
result = section.section_by_title("Same Name")
68+
self.assertEqual(result, subsection2) # Should return the last one
69+
70+
def test_full_text_wiki_format(self):
71+
"""Test full_text method with WIKI format."""
72+
section = wikipediaapi.WikipediaPageSection(
73+
wiki=self.wiki, title="Test Section", level=2, text="Section content"
74+
)
75+
76+
result = section.full_text()
77+
expected = "Test Section\nSection content\n\n"
78+
self.assertEqual(result, expected)
79+
80+
def test_full_text_html_format(self):
81+
"""Test full_text method with HTML format."""
82+
# Create a wiki with HTML format
83+
wiki_html = create_mock_wikipedia()
84+
wiki_html.extract_format = wikipediaapi.ExtractFormat.HTML
85+
86+
section = wikipediaapi.WikipediaPageSection(
87+
wiki=wiki_html,
88+
title="Test Section",
89+
level=1, # Use level 1 since full_text starts from level 1
90+
text="Section content",
91+
)
92+
93+
result = section.full_text()
94+
expected = "<h1>Test Section</h1>\nSection content\n\n"
95+
self.assertEqual(result, expected)
96+
97+
def test_full_text_unknown_format_raises_error(self):
98+
"""Test that full_text raises NotImplementedError for unknown format."""
99+
# Create a wiki with unknown format
100+
wiki_unknown = create_mock_wikipedia()
101+
wiki_unknown.extract_format = "unknown"
102+
103+
section = wikipediaapi.WikipediaPageSection(
104+
wiki=wiki_unknown, title="Test Section", level=1, text="Section content"
105+
)
106+
107+
with self.assertRaises(NotImplementedError) as cm:
108+
section.full_text()
109+
110+
self.assertIn("Unknown ExtractFormat type", str(cm.exception))
111+
112+
def test_full_text_no_text(self):
113+
"""Test full_text method when section has no text."""
114+
section = wikipediaapi.WikipediaPageSection(
115+
wiki=self.wiki, title="Empty Section", level=1, text=""
116+
)
117+
118+
result = section.full_text()
119+
expected = "Empty Section\n"
120+
self.assertEqual(result, expected)
121+
122+
def test_full_text_level_1(self):
123+
"""Test full_text method with level 1 section."""
124+
section = wikipediaapi.WikipediaPageSection(
125+
wiki=self.wiki, title="Top Level", level=1, text="Top level content"
126+
)
127+
128+
result = section.full_text()
129+
expected = "Top Level\nTop level content\n\n"
130+
self.assertEqual(result, expected)
131+
132+
def test_full_text_level_4(self):
133+
"""Test full_text method with level 4 section."""
134+
section = wikipediaapi.WikipediaPageSection(
135+
wiki=self.wiki, title="Deep Section", level=4, text="Deep content"
136+
)
137+
138+
result = section.full_text()
139+
expected = "Deep Section\nDeep content\n\n"
140+
self.assertEqual(result, expected)
141+
142+
def test_full_text_with_subsections(self):
143+
"""Test full_text method includes subsections."""
144+
# Create mock subsection
145+
subsection = MagicMock()
146+
subsection.title = "Subsection"
147+
subsection.level = 2
148+
subsection.full_text.return_value = "Subsection\nSub content\n\n"
149+
150+
section = wikipediaapi.WikipediaPageSection(
151+
wiki=self.wiki, title="Main Section", level=1, text="Main content"
152+
)
153+
section._section = [subsection]
154+
155+
result = section.full_text()
156+
expected = "Main Section\nMain content\n\nSubsection\nSub content\n\n"
157+
self.assertEqual(result, expected)
158+
159+
160+
if __name__ == "__main__":
161+
unittest.main()

tests/wikipedia_page_test.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,20 @@ def test_page_with_extra_parameters(self):
100100
wiki._query = wikipedia_api_request(wiki)
101101
page = wiki.page("Extra_API_Params")
102102
self.assertTrue(page.exists())
103+
104+
def test_sections_by_title_not_found(self):
105+
"""Test sections_by_title method when section doesn't exist."""
106+
wiki = wikipediaapi.Wikipedia(user_agent, "en")
107+
wiki._query = wikipedia_api_request(wiki)
108+
page = wiki.page("Test_1")
109+
110+
# Access sections to build the mapping
111+
_ = page.sections
112+
113+
# Test with a section that doesn't exist
114+
result = page.sections_by_title("Nonexistent Section")
115+
self.assertEqual(result, [])
116+
117+
# Also test with a section that definitely doesn't exist
118+
result2 = page.sections_by_title("Section That Does Not Exist At All")
119+
self.assertEqual(result2, [])

tests/wikipedia_test.py

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import unittest
2+
from unittest.mock import MagicMock
23

4+
from tests.mock_data import user_agent
35
import wikipediaapi
46

57

@@ -103,3 +105,131 @@ def test_user_agent_in_headers_win(self):
103105
user_agent,
104106
"header-user-agent (" + wikipediaapi.USER_AGENT + ")",
105107
)
108+
109+
def test_extracts_nonexistent_page(self):
110+
"""Test extracts method when page doesn't exist (pageid = -1)."""
111+
wiki = wikipediaapi.Wikipedia(user_agent, "en")
112+
113+
page = MagicMock()
114+
page.language = "en"
115+
page._attributes = {}
116+
117+
# Mock the API response to return pageid = -1 (nonexistent page)
118+
def mock_query(page_obj, params):
119+
return {"query": {"pages": {"-1": {}}}}
120+
121+
wiki._query = mock_query
122+
123+
result = wiki.extracts(page)
124+
self.assertEqual(result, "")
125+
# The pageid should be set to -1 in the attributes
126+
self.assertIn("pageid", page._attributes)
127+
self.assertEqual(page._attributes["pageid"], -1)
128+
129+
def test_info_nonexistent_page(self):
130+
"""Test info method when page doesn't exist (pageid = -1)."""
131+
wiki = wikipediaapi.Wikipedia(user_agent, "en")
132+
133+
page = MagicMock()
134+
page.language = "en"
135+
page._attributes = {}
136+
137+
# Mock the API response to return pageid = -1 (nonexistent page)
138+
def mock_query(page_obj, params):
139+
return {"query": {"pages": {"-1": {}}}}
140+
141+
wiki._query = mock_query
142+
143+
result = wiki.info(page)
144+
self.assertEqual(result, page)
145+
146+
def test_langlinks_nonexistent_page(self):
147+
"""Test langlinks method when page doesn't exist (pageid = -1)."""
148+
wiki = wikipediaapi.Wikipedia(user_agent, "en")
149+
150+
page = MagicMock()
151+
page.language = "en"
152+
page._attributes = {}
153+
154+
# Mock the API response to return pageid = -1 (nonexistent page)
155+
def mock_query(page_obj, params):
156+
return {"query": {"pages": {"-1": {}}}}
157+
158+
wiki._query = mock_query
159+
160+
result = wiki.langlinks(page)
161+
self.assertEqual(result, {})
162+
# The pageid should be set to -1 in the attributes
163+
self.assertIn("pageid", page._attributes)
164+
self.assertEqual(page._attributes["pageid"], -1)
165+
166+
def test_links_nonexistent_page(self):
167+
"""Test links method when page doesn't exist (pageid = -1)."""
168+
wiki = wikipediaapi.Wikipedia(user_agent, "en")
169+
170+
page = MagicMock()
171+
page.language = "en"
172+
page._attributes = {}
173+
174+
# Mock the API response to return pageid = -1 (nonexistent page)
175+
def mock_query(page_obj, params):
176+
return {"query": {"pages": {"-1": {}}}}
177+
178+
wiki._query = mock_query
179+
180+
result = wiki.links(page)
181+
self.assertEqual(result, {})
182+
# The pageid should be set to -1 in the attributes
183+
self.assertIn("pageid", page._attributes)
184+
self.assertEqual(page._attributes["pageid"], -1)
185+
186+
def test_backlinks_nonexistent_page(self):
187+
"""Test backlinks method when page doesn't exist (pageid = -1)."""
188+
wiki = wikipediaapi.Wikipedia(user_agent, "en")
189+
190+
page = MagicMock()
191+
page.language = "en"
192+
page._attributes = {}
193+
194+
# Mock the API response to return pageid = -1 (nonexistent page)
195+
def mock_query(page_obj, params):
196+
return {"query": {"pages": {"-1": {}}}}
197+
198+
wiki._query = mock_query
199+
200+
result = wiki.backlinks(page)
201+
self.assertEqual(result, {})
202+
203+
def test_categories_nonexistent_page(self):
204+
"""Test categories method when page doesn't exist (pageid = -1)."""
205+
wiki = wikipediaapi.Wikipedia(user_agent, "en")
206+
207+
page = MagicMock()
208+
page.language = "en"
209+
page._attributes = {}
210+
211+
# Mock the API response to return pageid = -1 (nonexistent page)
212+
def mock_query(page_obj, params):
213+
return {"query": {"pages": {"-1": {}}}}
214+
215+
wiki._query = mock_query
216+
217+
result = wiki.categories(page)
218+
self.assertEqual(result, {})
219+
220+
def test_categorymembers_nonexistent_page(self):
221+
"""Test categorymembers method when page doesn't exist (pageid = -1)."""
222+
wiki = wikipediaapi.Wikipedia(user_agent, "en")
223+
224+
page = MagicMock()
225+
page.language = "en"
226+
page._attributes = {}
227+
228+
# Mock the API response to return pageid = -1 (nonexistent page)
229+
def mock_query(page_obj, params):
230+
return {"query": {"pages": {"-1": {}}}}
231+
232+
wiki._query = mock_query
233+
234+
result = wiki.categorymembers(page)
235+
self.assertEqual(result, {})

0 commit comments

Comments
 (0)