askrobots / dbbasic-content
Public
WordPress-style content management with block-based pages, stored as JSON files on disk instead of MySQL.
test_content.py · 16.3 KB · 547 lines
| 1 | """ |
| 2 | Tests for ContentDB class. |
| 3 | """ |
| 4 | |
| 5 | import pytest |
| 6 | import json |
| 7 | import tempfile |
| 8 | from pathlib import Path |
| 9 | from dbbasic_content import ContentDB |
| 10 | |
| 11 | |
| 12 | @pytest.fixture |
| 13 | def content_dir(): |
| 14 | """Create temporary content directory.""" |
| 15 | with tempfile.TemporaryDirectory() as tmpdir: |
| 16 | yield tmpdir |
| 17 | |
| 18 | |
| 19 | @pytest.fixture |
| 20 | def content_db(content_dir): |
| 21 | """Create ContentDB instance.""" |
| 22 | return ContentDB(content_dir) |
| 23 | |
| 24 | |
| 25 | @pytest.fixture |
| 26 | def sample_blocks(): |
| 27 | """Sample block content.""" |
| 28 | return [ |
| 29 | { |
| 30 | 'type': 'paragraph', |
| 31 | 'data': {'content': 'Hello world!'} |
| 32 | }, |
| 33 | { |
| 34 | 'type': 'heading', |
| 35 | 'data': {'level': 2, 'content': 'Subheading'} |
| 36 | } |
| 37 | ] |
| 38 | |
| 39 | |
| 40 | class TestContentDB: |
| 41 | """Test ContentDB functionality.""" |
| 42 | |
| 43 | def test_init_creates_directories(self, content_dir): |
| 44 | """Test that ContentDB creates necessary directories.""" |
| 45 | db = ContentDB(content_dir) |
| 46 | articles_dir = Path(content_dir) / "articles" |
| 47 | assert articles_dir.exists() |
| 48 | assert articles_dir.is_dir() |
| 49 | |
| 50 | def test_create_post(self, content_db, sample_blocks): |
| 51 | """Test creating a post.""" |
| 52 | post = content_db.create_post( |
| 53 | slug='test-post', |
| 54 | title='Test Post', |
| 55 | blocks=sample_blocks, |
| 56 | author='john', |
| 57 | categories=['Technology'], |
| 58 | tags=['python', 'testing'] |
| 59 | ) |
| 60 | |
| 61 | assert post['slug'] == 'test-post' |
| 62 | assert post['title'] == 'Test Post' |
| 63 | assert post['author'] == 'john' |
| 64 | assert post['blocks'] == sample_blocks |
| 65 | assert 'Technology' in post['categories'] |
| 66 | assert 'python' in post['tags'] |
| 67 | |
| 68 | def test_get_post(self, content_db, sample_blocks): |
| 69 | """Test getting a post by slug.""" |
| 70 | # Create post |
| 71 | content_db.create_post( |
| 72 | slug='hello-world', |
| 73 | title='Hello World', |
| 74 | blocks=sample_blocks |
| 75 | ) |
| 76 | |
| 77 | # Get post |
| 78 | post = content_db.get_post('hello-world') |
| 79 | assert post is not None |
| 80 | assert post['slug'] == 'hello-world' |
| 81 | assert post['title'] == 'Hello World' |
| 82 | |
| 83 | def test_get_post_not_found(self, content_db): |
| 84 | """Test getting non-existent post returns None.""" |
| 85 | post = content_db.get_post('does-not-exist') |
| 86 | assert post is None |
| 87 | |
| 88 | def test_post_exists(self, content_db, sample_blocks): |
| 89 | """Test checking if post exists.""" |
| 90 | assert not content_db.post_exists('test-post') |
| 91 | |
| 92 | content_db.create_post( |
| 93 | slug='test-post', |
| 94 | title='Test', |
| 95 | blocks=sample_blocks |
| 96 | ) |
| 97 | |
| 98 | assert content_db.post_exists('test-post') |
| 99 | |
| 100 | def test_get_posts(self, content_db, sample_blocks): |
| 101 | """Test getting multiple posts.""" |
| 102 | # Create multiple posts |
| 103 | for i in range(5): |
| 104 | content_db.create_post( |
| 105 | slug=f'post-{i}', |
| 106 | title=f'Post {i}', |
| 107 | blocks=sample_blocks, |
| 108 | status='published' |
| 109 | ) |
| 110 | |
| 111 | posts = content_db.get_posts() |
| 112 | assert len(posts) == 5 |
| 113 | |
| 114 | def test_get_posts_with_limit(self, content_db, sample_blocks): |
| 115 | """Test getting posts with limit.""" |
| 116 | # Create posts |
| 117 | for i in range(10): |
| 118 | content_db.create_post( |
| 119 | slug=f'post-{i}', |
| 120 | title=f'Post {i}', |
| 121 | blocks=sample_blocks |
| 122 | ) |
| 123 | |
| 124 | posts = content_db.get_posts(limit=5) |
| 125 | assert len(posts) == 5 |
| 126 | |
| 127 | def test_get_posts_with_offset(self, content_db, sample_blocks): |
| 128 | """Test getting posts with offset.""" |
| 129 | # Create posts |
| 130 | for i in range(10): |
| 131 | content_db.create_post( |
| 132 | slug=f'post-{i}', |
| 133 | title=f'Post {i}', |
| 134 | blocks=sample_blocks |
| 135 | ) |
| 136 | |
| 137 | posts = content_db.get_posts(offset=5) |
| 138 | assert len(posts) == 5 |
| 139 | |
| 140 | def test_get_posts_filter_by_status(self, content_db, sample_blocks): |
| 141 | """Test filtering posts by status.""" |
| 142 | content_db.create_post( |
| 143 | slug='published-post', |
| 144 | title='Published', |
| 145 | blocks=sample_blocks, |
| 146 | status='published' |
| 147 | ) |
| 148 | content_db.create_post( |
| 149 | slug='draft-post', |
| 150 | title='Draft', |
| 151 | blocks=sample_blocks, |
| 152 | status='draft' |
| 153 | ) |
| 154 | |
| 155 | published = content_db.get_posts(status='published') |
| 156 | assert len(published) == 1 |
| 157 | assert published[0]['slug'] == 'published-post' |
| 158 | |
| 159 | drafts = content_db.get_posts(status='draft') |
| 160 | assert len(drafts) == 1 |
| 161 | assert drafts[0]['slug'] == 'draft-post' |
| 162 | |
| 163 | def test_get_posts_filter_by_categories(self, content_db, sample_blocks): |
| 164 | """Test filtering posts by categories.""" |
| 165 | content_db.create_post( |
| 166 | slug='tech-post', |
| 167 | title='Tech Post', |
| 168 | blocks=sample_blocks, |
| 169 | categories=['Technology'] |
| 170 | ) |
| 171 | content_db.create_post( |
| 172 | slug='design-post', |
| 173 | title='Design Post', |
| 174 | blocks=sample_blocks, |
| 175 | categories=['Design'] |
| 176 | ) |
| 177 | |
| 178 | tech_posts = content_db.get_posts(categories=['Technology']) |
| 179 | assert len(tech_posts) == 1 |
| 180 | assert tech_posts[0]['slug'] == 'tech-post' |
| 181 | |
| 182 | def test_get_posts_filter_by_tags(self, content_db, sample_blocks): |
| 183 | """Test filtering posts by tags.""" |
| 184 | content_db.create_post( |
| 185 | slug='python-post', |
| 186 | title='Python Post', |
| 187 | blocks=sample_blocks, |
| 188 | tags=['python'] |
| 189 | ) |
| 190 | content_db.create_post( |
| 191 | slug='javascript-post', |
| 192 | title='JavaScript Post', |
| 193 | blocks=sample_blocks, |
| 194 | tags=['javascript'] |
| 195 | ) |
| 196 | |
| 197 | python_posts = content_db.get_posts(tags=['python']) |
| 198 | assert len(python_posts) == 1 |
| 199 | assert python_posts[0]['slug'] == 'python-post' |
| 200 | |
| 201 | def test_get_posts_filter_by_author(self, content_db, sample_blocks): |
| 202 | """Test filtering posts by author.""" |
| 203 | content_db.create_post( |
| 204 | slug='john-post', |
| 205 | title='John Post', |
| 206 | blocks=sample_blocks, |
| 207 | author='john' |
| 208 | ) |
| 209 | content_db.create_post( |
| 210 | slug='jane-post', |
| 211 | title='Jane Post', |
| 212 | blocks=sample_blocks, |
| 213 | author='jane' |
| 214 | ) |
| 215 | |
| 216 | john_posts = content_db.get_posts(author='john') |
| 217 | assert len(john_posts) == 1 |
| 218 | assert john_posts[0]['slug'] == 'john-post' |
| 219 | |
| 220 | def test_update_post(self, content_db, sample_blocks): |
| 221 | """Test updating a post.""" |
| 222 | # Create post |
| 223 | content_db.create_post( |
| 224 | slug='test-post', |
| 225 | title='Original Title', |
| 226 | blocks=sample_blocks |
| 227 | ) |
| 228 | |
| 229 | # Update post |
| 230 | updated = content_db.update_post( |
| 231 | 'test-post', |
| 232 | title='Updated Title', |
| 233 | status='draft' |
| 234 | ) |
| 235 | |
| 236 | assert updated['title'] == 'Updated Title' |
| 237 | assert updated['status'] == 'draft' |
| 238 | |
| 239 | # Verify persistence |
| 240 | post = content_db.get_post('test-post') |
| 241 | assert post['title'] == 'Updated Title' |
| 242 | |
| 243 | def test_update_post_not_found(self, content_db): |
| 244 | """Test updating non-existent post returns None.""" |
| 245 | result = content_db.update_post('does-not-exist', title='New Title') |
| 246 | assert result is None |
| 247 | |
| 248 | def test_delete_post(self, content_db, sample_blocks): |
| 249 | """Test deleting a post.""" |
| 250 | # Create post |
| 251 | content_db.create_post( |
| 252 | slug='test-post', |
| 253 | title='Test', |
| 254 | blocks=sample_blocks |
| 255 | ) |
| 256 | |
| 257 | assert content_db.post_exists('test-post') |
| 258 | |
| 259 | # Delete post |
| 260 | result = content_db.delete_post('test-post') |
| 261 | assert result is True |
| 262 | assert not content_db.post_exists('test-post') |
| 263 | |
| 264 | def test_delete_post_not_found(self, content_db): |
| 265 | """Test deleting non-existent post returns False.""" |
| 266 | result = content_db.delete_post('does-not-exist') |
| 267 | assert result is False |
| 268 | |
| 269 | def test_get_categories(self, content_db, sample_blocks): |
| 270 | """Test getting all categories.""" |
| 271 | content_db.create_post( |
| 272 | slug='post-1', |
| 273 | title='Post 1', |
| 274 | blocks=sample_blocks, |
| 275 | categories=['Technology', 'Python'] |
| 276 | ) |
| 277 | content_db.create_post( |
| 278 | slug='post-2', |
| 279 | title='Post 2', |
| 280 | blocks=sample_blocks, |
| 281 | categories=['Design', 'Python'] |
| 282 | ) |
| 283 | |
| 284 | categories = content_db.get_categories() |
| 285 | assert 'Technology' in categories |
| 286 | assert 'Design' in categories |
| 287 | assert 'Python' in categories |
| 288 | assert len(categories) == 3 |
| 289 | |
| 290 | def test_get_tags(self, content_db, sample_blocks): |
| 291 | """Test getting all tags.""" |
| 292 | content_db.create_post( |
| 293 | slug='post-1', |
| 294 | title='Post 1', |
| 295 | blocks=sample_blocks, |
| 296 | tags=['python', 'flask'] |
| 297 | ) |
| 298 | content_db.create_post( |
| 299 | slug='post-2', |
| 300 | title='Post 2', |
| 301 | blocks=sample_blocks, |
| 302 | tags=['javascript', 'python'] |
| 303 | ) |
| 304 | |
| 305 | tags = content_db.get_tags() |
| 306 | assert 'python' in tags |
| 307 | assert 'flask' in tags |
| 308 | assert 'javascript' in tags |
| 309 | assert len(tags) == 3 |
| 310 | |
| 311 | def test_get_authors(self, content_db, sample_blocks): |
| 312 | """Test getting all authors.""" |
| 313 | content_db.create_post( |
| 314 | slug='post-1', |
| 315 | title='Post 1', |
| 316 | blocks=sample_blocks, |
| 317 | author='john' |
| 318 | ) |
| 319 | content_db.create_post( |
| 320 | slug='post-2', |
| 321 | title='Post 2', |
| 322 | blocks=sample_blocks, |
| 323 | author='jane' |
| 324 | ) |
| 325 | |
| 326 | authors = content_db.get_authors() |
| 327 | assert 'john' in authors |
| 328 | assert 'jane' in authors |
| 329 | assert len(authors) == 2 |
| 330 | |
| 331 | def test_search_by_title(self, content_db, sample_blocks): |
| 332 | """Test searching posts by title.""" |
| 333 | content_db.create_post( |
| 334 | slug='python-tutorial', |
| 335 | title='Python Tutorial', |
| 336 | blocks=sample_blocks |
| 337 | ) |
| 338 | content_db.create_post( |
| 339 | slug='javascript-guide', |
| 340 | title='JavaScript Guide', |
| 341 | blocks=sample_blocks |
| 342 | ) |
| 343 | |
| 344 | results = content_db.search('Python') |
| 345 | assert len(results) == 1 |
| 346 | assert results[0]['slug'] == 'python-tutorial' |
| 347 | |
| 348 | def test_search_by_content(self, content_db): |
| 349 | """Test searching posts by content.""" |
| 350 | content_db.create_post( |
| 351 | slug='post-1', |
| 352 | title='Post 1', |
| 353 | blocks=[ |
| 354 | {'type': 'paragraph', 'data': {'content': 'This is about Python programming'}} |
| 355 | ] |
| 356 | ) |
| 357 | content_db.create_post( |
| 358 | slug='post-2', |
| 359 | title='Post 2', |
| 360 | blocks=[ |
| 361 | {'type': 'paragraph', 'data': {'content': 'This is about JavaScript'}} |
| 362 | ] |
| 363 | ) |
| 364 | |
| 365 | results = content_db.search('Python') |
| 366 | assert len(results) == 1 |
| 367 | assert results[0]['slug'] == 'post-1' |
| 368 | |
| 369 | def test_search_case_insensitive(self, content_db, sample_blocks): |
| 370 | """Test search is case insensitive.""" |
| 371 | content_db.create_post( |
| 372 | slug='test-post', |
| 373 | title='Test Post', |
| 374 | blocks=sample_blocks |
| 375 | ) |
| 376 | |
| 377 | results = content_db.search('test') |
| 378 | assert len(results) == 1 |
| 379 | |
| 380 | results = content_db.search('TEST') |
| 381 | assert len(results) == 1 |
| 382 | |
| 383 | results = content_db.search('TeSt') |
| 384 | assert len(results) == 1 |
| 385 | |
| 386 | def test_post_ordering(self, content_db, sample_blocks): |
| 387 | """Test posts are ordered by date descending by default.""" |
| 388 | content_db.create_post( |
| 389 | slug='post-1', |
| 390 | title='Post 1', |
| 391 | date='2025-01-01', |
| 392 | blocks=sample_blocks |
| 393 | ) |
| 394 | content_db.create_post( |
| 395 | slug='post-2', |
| 396 | title='Post 2', |
| 397 | date='2025-01-15', |
| 398 | blocks=sample_blocks |
| 399 | ) |
| 400 | content_db.create_post( |
| 401 | slug='post-3', |
| 402 | title='Post 3', |
| 403 | date='2025-01-10', |
| 404 | blocks=sample_blocks |
| 405 | ) |
| 406 | |
| 407 | posts = content_db.get_posts(order_by='date', order='desc') |
| 408 | assert posts[0]['slug'] == 'post-2' # Most recent |
| 409 | assert posts[1]['slug'] == 'post-3' |
| 410 | assert posts[2]['slug'] == 'post-1' # Oldest |
| 411 | |
| 412 | |
| 413 | class TestPages: |
| 414 | """Test page functionality (static content).""" |
| 415 | |
| 416 | def test_create_page(self, content_db, sample_blocks): |
| 417 | """Test creating a page.""" |
| 418 | page = content_db.create_page( |
| 419 | slug='about', |
| 420 | title='About Us', |
| 421 | blocks=sample_blocks, |
| 422 | author='admin' |
| 423 | ) |
| 424 | |
| 425 | assert page['slug'] == 'about' |
| 426 | assert page['title'] == 'About Us' |
| 427 | assert page['content_type'] == 'page' |
| 428 | assert page['blocks'] == sample_blocks |
| 429 | |
| 430 | def test_get_page(self, content_db, sample_blocks): |
| 431 | """Test getting a page by slug.""" |
| 432 | content_db.create_page( |
| 433 | slug='about', |
| 434 | title='About Us', |
| 435 | blocks=sample_blocks |
| 436 | ) |
| 437 | |
| 438 | page = content_db.get_page('about') |
| 439 | assert page is not None |
| 440 | assert page['slug'] == 'about' |
| 441 | assert page['title'] == 'About Us' |
| 442 | assert page['content_type'] == 'page' |
| 443 | |
| 444 | def test_get_page_not_found(self, content_db): |
| 445 | """Test getting non-existent page returns None.""" |
| 446 | page = content_db.get_page('does-not-exist') |
| 447 | assert page is None |
| 448 | |
| 449 | def test_page_exists(self, content_db, sample_blocks): |
| 450 | """Test checking if page exists.""" |
| 451 | assert not content_db.page_exists('about') |
| 452 | |
| 453 | content_db.create_page( |
| 454 | slug='about', |
| 455 | title='About', |
| 456 | blocks=sample_blocks |
| 457 | ) |
| 458 | |
| 459 | assert content_db.page_exists('about') |
| 460 | |
| 461 | def test_get_pages(self, content_db, sample_blocks): |
| 462 | """Test getting multiple pages.""" |
| 463 | for i in range(5): |
| 464 | content_db.create_page( |
| 465 | slug=f'page-{i}', |
| 466 | title=f'Page {i}', |
| 467 | blocks=sample_blocks |
| 468 | ) |
| 469 | |
| 470 | pages = content_db.get_pages() |
| 471 | assert len(pages) == 5 |
| 472 | # All should be pages |
| 473 | for page in pages: |
| 474 | assert page['content_type'] == 'page' |
| 475 | |
| 476 | def test_update_page(self, content_db, sample_blocks): |
| 477 | """Test updating a page.""" |
| 478 | content_db.create_page( |
| 479 | slug='about', |
| 480 | title='Original Title', |
| 481 | blocks=sample_blocks |
| 482 | ) |
| 483 | |
| 484 | updated = content_db.update_page( |
| 485 | 'about', |
| 486 | title='Updated Title' |
| 487 | ) |
| 488 | |
| 489 | assert updated['title'] == 'Updated Title' |
| 490 | |
| 491 | # Verify persistence |
| 492 | page = content_db.get_page('about') |
| 493 | assert page['title'] == 'Updated Title' |
| 494 | |
| 495 | def test_delete_page(self, content_db, sample_blocks): |
| 496 | """Test deleting a page.""" |
| 497 | content_db.create_page( |
| 498 | slug='about', |
| 499 | title='About', |
| 500 | blocks=sample_blocks |
| 501 | ) |
| 502 | |
| 503 | assert content_db.page_exists('about') |
| 504 | |
| 505 | result = content_db.delete_page('about') |
| 506 | assert result is True |
| 507 | assert not content_db.page_exists('about') |
| 508 | |
| 509 | def test_pages_and_posts_separate(self, content_db, sample_blocks): |
| 510 | """Test that pages and posts are stored separately.""" |
| 511 | # Create a post |
| 512 | content_db.create_post( |
| 513 | slug='hello-world', |
| 514 | title='Hello World', |
| 515 | blocks=sample_blocks, |
| 516 | content_type='post' |
| 517 | ) |
| 518 | |
| 519 | # Create a page with same slug |
| 520 | content_db.create_page( |
| 521 | slug='hello-world', |
| 522 | title='Hello World Page', |
| 523 | blocks=sample_blocks |
| 524 | ) |
| 525 | |
| 526 | # Both should exist in different directories |
| 527 | post = content_db.get_post('hello-world', content_type='post') |
| 528 | page = content_db.get_page('hello-world') |
| 529 | |
| 530 | assert post is not None |
| 531 | assert page is not None |
| 532 | assert post['title'] == 'Hello World' |
| 533 | assert page['title'] == 'Hello World Page' |
| 534 | assert post['content_type'] == 'post' |
| 535 | assert page['content_type'] == 'page' |
| 536 | |
| 537 | def test_pages_no_categories_or_tags(self, content_db, sample_blocks): |
| 538 | """Test that pages typically don't use categories/tags.""" |
| 539 | page = content_db.create_page( |
| 540 | slug='about', |
| 541 | title='About', |
| 542 | blocks=sample_blocks |
| 543 | ) |
| 544 | |
| 545 | # Pages can have categories/tags if needed, but typically don't |
| 546 | assert 'categories' not in page or page.get('categories') is None |
| 547 | assert 'tags' not in page or page.get('tags') is None |