askrobots / dbbasic-content
Public
WordPress-style content management with block-based pages, stored as JSON files on disk instead of MySQL.
test_wordpress_importer.py · 7.4 KB · 226 lines
| 1 | """ |
| 2 | Tests for WordPress importer. |
| 3 | """ |
| 4 | |
| 5 | import pytest |
| 6 | from dbbasic_content.wordpress_importer import ( |
| 7 | HTMLToBlocksParser, |
| 8 | WordPressImporter |
| 9 | ) |
| 10 | |
| 11 | |
| 12 | class TestHTMLToBlocksParser: |
| 13 | """Test HTML to blocks conversion.""" |
| 14 | |
| 15 | def test_parse_paragraph(self): |
| 16 | """Test parsing paragraph tags.""" |
| 17 | parser = HTMLToBlocksParser() |
| 18 | parser.feed('<p>Hello world</p>') |
| 19 | blocks = parser.get_blocks() |
| 20 | |
| 21 | assert len(blocks) == 1 |
| 22 | assert blocks[0]['type'] == 'paragraph' |
| 23 | assert blocks[0]['data']['content'] == 'Hello world' |
| 24 | |
| 25 | def test_parse_multiple_paragraphs(self): |
| 26 | """Test parsing multiple paragraphs.""" |
| 27 | parser = HTMLToBlocksParser() |
| 28 | parser.feed('<p>First paragraph</p><p>Second paragraph</p>') |
| 29 | blocks = parser.get_blocks() |
| 30 | |
| 31 | assert len(blocks) == 2 |
| 32 | assert blocks[0]['data']['content'] == 'First paragraph' |
| 33 | assert blocks[1]['data']['content'] == 'Second paragraph' |
| 34 | |
| 35 | def test_parse_heading(self): |
| 36 | """Test parsing heading tags.""" |
| 37 | parser = HTMLToBlocksParser() |
| 38 | parser.feed('<h2>My Heading</h2>') |
| 39 | blocks = parser.get_blocks() |
| 40 | |
| 41 | assert len(blocks) == 1 |
| 42 | assert blocks[0]['type'] == 'heading' |
| 43 | assert blocks[0]['data']['level'] == 2 |
| 44 | assert blocks[0]['data']['content'] == 'My Heading' |
| 45 | |
| 46 | def test_parse_multiple_heading_levels(self): |
| 47 | """Test parsing different heading levels.""" |
| 48 | for level in range(1, 7): |
| 49 | parser = HTMLToBlocksParser() |
| 50 | parser.feed(f'<h{level}>Heading</h{level}>') |
| 51 | blocks = parser.get_blocks() |
| 52 | |
| 53 | assert blocks[0]['type'] == 'heading' |
| 54 | assert blocks[0]['data']['level'] == level |
| 55 | |
| 56 | def test_parse_unordered_list(self): |
| 57 | """Test parsing unordered list.""" |
| 58 | parser = HTMLToBlocksParser() |
| 59 | parser.feed('<ul><li>Item 1</li><li>Item 2</li></ul>') |
| 60 | blocks = parser.get_blocks() |
| 61 | |
| 62 | assert len(blocks) == 1 |
| 63 | assert blocks[0]['type'] == 'list' |
| 64 | assert blocks[0]['data']['listType'] == 'unordered' |
| 65 | assert blocks[0]['data']['items'] == ['Item 1', 'Item 2'] |
| 66 | |
| 67 | def test_parse_ordered_list(self): |
| 68 | """Test parsing ordered list.""" |
| 69 | parser = HTMLToBlocksParser() |
| 70 | parser.feed('<ol><li>First</li><li>Second</li></ol>') |
| 71 | blocks = parser.get_blocks() |
| 72 | |
| 73 | assert len(blocks) == 1 |
| 74 | assert blocks[0]['type'] == 'list' |
| 75 | assert blocks[0]['data']['listType'] == 'ordered' |
| 76 | assert blocks[0]['data']['items'] == ['First', 'Second'] |
| 77 | |
| 78 | def test_parse_blockquote(self): |
| 79 | """Test parsing blockquote.""" |
| 80 | parser = HTMLToBlocksParser() |
| 81 | parser.feed('<blockquote>A quote</blockquote>') |
| 82 | blocks = parser.get_blocks() |
| 83 | |
| 84 | assert len(blocks) == 1 |
| 85 | assert blocks[0]['type'] == 'quote' |
| 86 | assert blocks[0]['data']['content'] == 'A quote' |
| 87 | |
| 88 | def test_parse_code_block(self): |
| 89 | """Test parsing pre/code tags.""" |
| 90 | parser = HTMLToBlocksParser() |
| 91 | parser.feed('<pre>print("hello")</pre>') |
| 92 | blocks = parser.get_blocks() |
| 93 | |
| 94 | assert len(blocks) == 1 |
| 95 | assert blocks[0]['type'] == 'code' |
| 96 | assert blocks[0]['data']['code'] == 'print("hello")' |
| 97 | assert blocks[0]['data']['language'] == 'text' |
| 98 | |
| 99 | def test_parse_mixed_content(self): |
| 100 | """Test parsing mixed content types.""" |
| 101 | html = ''' |
| 102 | <h1>Title</h1> |
| 103 | <p>First paragraph</p> |
| 104 | <p>Second paragraph</p> |
| 105 | <ul> |
| 106 | <li>Item 1</li> |
| 107 | <li>Item 2</li> |
| 108 | </ul> |
| 109 | ''' |
| 110 | parser = HTMLToBlocksParser() |
| 111 | parser.feed(html) |
| 112 | blocks = parser.get_blocks() |
| 113 | |
| 114 | assert len(blocks) == 4 |
| 115 | assert blocks[0]['type'] == 'heading' |
| 116 | assert blocks[1]['type'] == 'paragraph' |
| 117 | assert blocks[2]['type'] == 'paragraph' |
| 118 | assert blocks[3]['type'] == 'list' |
| 119 | |
| 120 | def test_parse_empty_content(self): |
| 121 | """Test parsing empty content.""" |
| 122 | parser = HTMLToBlocksParser() |
| 123 | parser.feed('') |
| 124 | blocks = parser.get_blocks() |
| 125 | |
| 126 | assert len(blocks) == 0 |
| 127 | |
| 128 | def test_parse_whitespace_only(self): |
| 129 | """Test parsing whitespace-only content.""" |
| 130 | parser = HTMLToBlocksParser() |
| 131 | parser.feed(' \n\n ') |
| 132 | blocks = parser.get_blocks() |
| 133 | |
| 134 | assert len(blocks) == 0 |
| 135 | |
| 136 | def test_parse_plain_text(self): |
| 137 | """Test parsing plain text without tags.""" |
| 138 | parser = HTMLToBlocksParser() |
| 139 | parser.feed('Just plain text') |
| 140 | blocks = parser.get_blocks() |
| 141 | |
| 142 | assert len(blocks) == 1 |
| 143 | assert blocks[0]['type'] == 'paragraph' |
| 144 | assert blocks[0]['data']['content'] == 'Just plain text' |
| 145 | |
| 146 | def test_ignore_empty_paragraphs(self): |
| 147 | """Test that empty paragraphs are ignored.""" |
| 148 | parser = HTMLToBlocksParser() |
| 149 | parser.feed('<p></p><p>Content</p><p> </p>') |
| 150 | blocks = parser.get_blocks() |
| 151 | |
| 152 | assert len(blocks) == 1 |
| 153 | assert blocks[0]['data']['content'] == 'Content' |
| 154 | |
| 155 | def test_strip_whitespace(self): |
| 156 | """Test that whitespace is properly stripped.""" |
| 157 | parser = HTMLToBlocksParser() |
| 158 | parser.feed('<p> Content with spaces </p>') |
| 159 | blocks = parser.get_blocks() |
| 160 | |
| 161 | assert blocks[0]['data']['content'] == 'Content with spaces' |
| 162 | |
| 163 | |
| 164 | class TestWordPressImporterUnit: |
| 165 | """Unit tests for WordPressImporter (no database required).""" |
| 166 | |
| 167 | def test_convert_empty_content(self): |
| 168 | """Test converting empty content.""" |
| 169 | importer = WordPressImporter() |
| 170 | blocks = importer._convert_content_to_blocks('') |
| 171 | assert blocks == [] |
| 172 | |
| 173 | def test_convert_simple_content(self): |
| 174 | """Test converting simple HTML content.""" |
| 175 | importer = WordPressImporter() |
| 176 | html = '<p>Hello world</p>' |
| 177 | blocks = importer._convert_content_to_blocks(html) |
| 178 | |
| 179 | assert len(blocks) == 1 |
| 180 | assert blocks[0]['type'] == 'paragraph' |
| 181 | |
| 182 | def test_convert_complex_content(self): |
| 183 | """Test converting complex HTML content.""" |
| 184 | importer = WordPressImporter() |
| 185 | html = ''' |
| 186 | <h1>Title</h1> |
| 187 | <p>Introduction paragraph</p> |
| 188 | <ul> |
| 189 | <li>Point 1</li> |
| 190 | <li>Point 2</li> |
| 191 | </ul> |
| 192 | ''' |
| 193 | blocks = importer._convert_content_to_blocks(html) |
| 194 | |
| 195 | assert len(blocks) >= 3 |
| 196 | assert any(b['type'] == 'heading' for b in blocks) |
| 197 | assert any(b['type'] == 'paragraph' for b in blocks) |
| 198 | assert any(b['type'] == 'list' for b in blocks) |
| 199 | |
| 200 | def test_importer_context_manager(self): |
| 201 | """Test using importer as context manager.""" |
| 202 | with WordPressImporter() as importer: |
| 203 | assert importer is not None |
| 204 | assert importer._connection is None # Not connected yet |
| 205 | |
| 206 | def test_importer_initialization(self): |
| 207 | """Test importer initialization with custom params.""" |
| 208 | importer = WordPressImporter( |
| 209 | host='localhost', |
| 210 | database='wp_db', |
| 211 | user='wp_user', |
| 212 | password='wp_pass', |
| 213 | port=3307, |
| 214 | table_prefix='custom_' |
| 215 | ) |
| 216 | |
| 217 | assert importer.host == 'localhost' |
| 218 | assert importer.database == 'wp_db' |
| 219 | assert importer.user == 'wp_user' |
| 220 | assert importer.password == 'wp_pass' |
| 221 | assert importer.port == 3307 |
| 222 | assert importer.table_prefix == 'custom_' |
| 223 | |
| 224 | |
| 225 | # Integration tests would require actual WordPress database |
| 226 | # These would be in a separate test file: test_wordpress_importer_integration.py |