feat: add mock content initialization command

This commit is contained in:
2026-09-27 12:26:33 +03:30
parent 149497d276
commit 94b32310af
4 changed files with 249 additions and 0 deletions

View File

@@ -0,0 +1,65 @@
from io import StringIO
import pytest
from django.contrib.admin.models import CHANGE, LogEntry
from django.contrib.contenttypes.models import ContentType
from django.core.management import call_command
from apps.content.current import AboutContent, HomeContent, ListingContent
from apps.users.models import User
pytestmark = [pytest.mark.django_db, pytest.mark.integration]
def test_command_replaces_unedited_migration_copy_with_mock_content_idempotently():
call_command("seed_mock_content", stdout=StringIO())
home = HomeContent.objects.get(pk=1)
about = AboutContent.objects.get(pk=1)
events = ListingContent.objects.get(pk="events")
blog = ListingContent.objects.get(pk="blog")
assert home.hero_heading == "عنوان نمونه صفحه اصلی"
assert about.title == "عنوان نمونه صفحه درباره ما"
assert about.items.filter(title="کمیته نمونه").exists()
assert not about.items.filter(title="انجمن علمی مهندسی کامپیوتر دانشکده‌ی فنی").exists()
assert events.title == "عنوان نمونه رویدادها"
assert blog.title == "عنوان نمونه بلاگ"
call_command("seed_mock_content", stdout=StringIO())
assert HomeContent.objects.get(pk=1).hero_heading == "عنوان نمونه صفحه اصلی"
def test_command_preserves_content_with_an_admin_change_log():
editor = User.objects.create_user(
username="mock-editor",
mobile="09120000001",
password="TestPass123!",
is_staff=True,
)
home = HomeContent.objects.get(pk=1)
home.hero_heading = "Admin's real heading"
home.save(update_fields=["hero_heading"])
LogEntry.objects.create(
user=editor,
content_type=ContentType.objects.get_for_model(HomeContent),
object_id="1",
object_repr=str(home),
action_flag=CHANGE,
change_message="Changed through admin",
)
call_command("seed_mock_content", stdout=StringIO())
assert HomeContent.objects.get(pk=1).hero_heading == "Admin's real heading"
assert ListingContent.objects.get(slug="events").title == "عنوان نمونه رویدادها"
def test_dry_run_does_not_change_seeded_content():
output = StringIO()
call_command("seed_mock_content", dry_run=True, stdout=output)
assert HomeContent.objects.get(pk=1).hero_heading == "انجمن علمی کامپیوتر دانشگاه گیلان"
assert "dry run" in output.getvalue().lower()