|
| 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() |
0 commit comments