tests
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
"""
|
||||
Integration Tests
|
||||
Phase 6: Site Integration & Multi-Destination Publishing
|
||||
"""
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
"""
|
||||
Tests for ContentSyncService
|
||||
Phase 6: Site Integration & Multi-Destination Publishing
|
||||
"""
|
||||
from django.test import TestCase
|
||||
from unittest.mock import patch, Mock
|
||||
|
||||
from igny8_core.auth.models import Account, Site, Sector
|
||||
from igny8_core.business.integration.models import SiteIntegration
|
||||
from igny8_core.business.integration.services.content_sync_service import ContentSyncService
|
||||
from igny8_core.business.content.models import Content
|
||||
|
||||
|
||||
class ContentSyncServiceTestCase(TestCase):
|
||||
"""Test cases for ContentSyncService"""
|
||||
|
||||
def setUp(self):
|
||||
"""Set up test data"""
|
||||
self.account = Account.objects.create(name="Test Account")
|
||||
self.site = Site.objects.create(
|
||||
account=self.account,
|
||||
name="Test Site",
|
||||
slug="test-site"
|
||||
)
|
||||
self.sector = Sector.objects.create(
|
||||
account=self.account,
|
||||
site=self.site,
|
||||
name="Test Sector",
|
||||
slug="test-sector"
|
||||
)
|
||||
self.integration = SiteIntegration.objects.create(
|
||||
account=self.account,
|
||||
site=self.site,
|
||||
platform='wordpress',
|
||||
platform_type='cms',
|
||||
sync_enabled=True
|
||||
)
|
||||
self.service = ContentSyncService()
|
||||
|
||||
def test_sync_content_from_wordpress_creates_content(self):
|
||||
"""Test: WordPress sync works (when plugin connected)"""
|
||||
mock_posts = [
|
||||
{
|
||||
'id': 1,
|
||||
'title': 'Test Post',
|
||||
'content': '<p>Test content</p>',
|
||||
'status': 'publish',
|
||||
}
|
||||
]
|
||||
|
||||
with patch.object(self.service, '_fetch_wordpress_posts') as mock_fetch:
|
||||
mock_fetch.return_value = mock_posts
|
||||
|
||||
result = self.service.sync_from_wordpress(self.integration)
|
||||
|
||||
self.assertTrue(result.get('success'))
|
||||
self.assertEqual(result.get('synced_count'), 1)
|
||||
|
||||
# Verify content was created
|
||||
content = Content.objects.filter(site=self.site).first()
|
||||
self.assertIsNotNone(content)
|
||||
self.assertEqual(content.title, 'Test Post')
|
||||
self.assertEqual(content.source, 'wordpress')
|
||||
|
||||
def test_sync_content_from_shopify_creates_content(self):
|
||||
"""Test: Content sync works"""
|
||||
mock_products = [
|
||||
{
|
||||
'id': 1,
|
||||
'title': 'Test Product',
|
||||
'body_html': '<p>Product description</p>',
|
||||
}
|
||||
]
|
||||
|
||||
with patch.object(self.service, '_fetch_shopify_products') as mock_fetch:
|
||||
mock_fetch.return_value = mock_products
|
||||
|
||||
result = self.service.sync_from_shopify(self.integration)
|
||||
|
||||
self.assertTrue(result.get('success'))
|
||||
self.assertEqual(result.get('synced_count'), 1)
|
||||
|
||||
def test_sync_handles_duplicate_content(self):
|
||||
"""Test: Content sync works"""
|
||||
# Create existing content
|
||||
Content.objects.create(
|
||||
account=self.account,
|
||||
site=self.site,
|
||||
sector=self.sector,
|
||||
title="Test Post",
|
||||
html_content="<p>Existing</p>",
|
||||
source='wordpress'
|
||||
)
|
||||
|
||||
mock_posts = [
|
||||
{
|
||||
'id': 1,
|
||||
'title': 'Test Post',
|
||||
'content': '<p>Updated content</p>',
|
||||
}
|
||||
]
|
||||
|
||||
with patch.object(self.service, '_fetch_wordpress_posts') as mock_fetch:
|
||||
mock_fetch.return_value = mock_posts
|
||||
|
||||
result = self.service.sync_from_wordpress(self.integration)
|
||||
|
||||
# Should update existing, not create duplicate
|
||||
content_count = Content.objects.filter(
|
||||
site=self.site,
|
||||
title='Test Post'
|
||||
).count()
|
||||
self.assertEqual(content_count, 1)
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
"""
|
||||
Tests for IntegrationService
|
||||
Phase 6: Site Integration & Multi-Destination Publishing
|
||||
"""
|
||||
from django.test import TestCase
|
||||
|
||||
from igny8_core.auth.models import Account, Site, Sector
|
||||
from igny8_core.business.integration.models import SiteIntegration
|
||||
from igny8_core.business.integration.services.integration_service import IntegrationService
|
||||
|
||||
|
||||
class IntegrationServiceTestCase(TestCase):
|
||||
"""Test cases for IntegrationService"""
|
||||
|
||||
def setUp(self):
|
||||
"""Set up test data"""
|
||||
self.account = Account.objects.create(name="Test Account")
|
||||
self.site = Site.objects.create(
|
||||
account=self.account,
|
||||
name="Test Site",
|
||||
slug="test-site"
|
||||
)
|
||||
self.sector = Sector.objects.create(
|
||||
account=self.account,
|
||||
site=self.site,
|
||||
name="Test Sector",
|
||||
slug="test-sector"
|
||||
)
|
||||
self.service = IntegrationService()
|
||||
|
||||
def test_create_integration_stores_config(self):
|
||||
"""Test: Site integrations work correctly"""
|
||||
integration = self.service.create_integration(
|
||||
site=self.site,
|
||||
platform='wordpress',
|
||||
config={'url': 'https://example.com'},
|
||||
credentials={'api_key': 'test-key'},
|
||||
platform_type='cms'
|
||||
)
|
||||
|
||||
self.assertIsNotNone(integration)
|
||||
self.assertEqual(integration.platform, 'wordpress')
|
||||
self.assertEqual(integration.platform_type, 'cms')
|
||||
self.assertEqual(integration.config_json.get('url'), 'https://example.com')
|
||||
self.assertTrue(integration.is_active)
|
||||
|
||||
def test_get_integrations_for_site_returns_all(self):
|
||||
"""Test: Site integrations work correctly"""
|
||||
self.service.create_integration(
|
||||
site=self.site,
|
||||
platform='wordpress',
|
||||
config={},
|
||||
credentials={}
|
||||
)
|
||||
self.service.create_integration(
|
||||
site=self.site,
|
||||
platform='shopify',
|
||||
config={},
|
||||
credentials={}
|
||||
)
|
||||
|
||||
integrations = self.service.get_integrations_for_site(self.site)
|
||||
|
||||
self.assertEqual(integrations.count(), 2)
|
||||
platforms = [i.platform for i in integrations]
|
||||
self.assertIn('wordpress', platforms)
|
||||
self.assertIn('shopify', platforms)
|
||||
|
||||
def test_test_connection_validates_credentials(self):
|
||||
"""Test: Site integrations work correctly"""
|
||||
integration = self.service.create_integration(
|
||||
site=self.site,
|
||||
platform='wordpress',
|
||||
config={'url': 'https://example.com'},
|
||||
credentials={'api_key': 'test-key'}
|
||||
)
|
||||
|
||||
with self.assertRaises(NotImplementedError):
|
||||
# Connection testing to be implemented per platform
|
||||
self.service.test_connection(integration)
|
||||
|
||||
def test_update_integration_updates_fields(self):
|
||||
"""Test: Site integrations work correctly"""
|
||||
integration = self.service.create_integration(
|
||||
site=self.site,
|
||||
platform='wordpress',
|
||||
config={'url': 'https://old.com'},
|
||||
credentials={}
|
||||
)
|
||||
|
||||
updated = self.service.update_integration(
|
||||
integration,
|
||||
config={'url': 'https://new.com'},
|
||||
is_active=False
|
||||
)
|
||||
|
||||
self.assertEqual(updated.config_json.get('url'), 'https://new.com')
|
||||
self.assertFalse(updated.is_active)
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
"""
|
||||
Tests for SyncService
|
||||
Phase 6: Site Integration & Multi-Destination Publishing
|
||||
"""
|
||||
from django.test import TestCase
|
||||
from django.utils import timezone
|
||||
from unittest.mock import patch, Mock
|
||||
|
||||
from igny8_core.auth.models import Account, Site, Sector
|
||||
from igny8_core.business.integration.models import SiteIntegration
|
||||
from igny8_core.business.integration.services.sync_service import SyncService
|
||||
|
||||
|
||||
class SyncServiceTestCase(TestCase):
|
||||
"""Test cases for SyncService"""
|
||||
|
||||
def setUp(self):
|
||||
"""Set up test data"""
|
||||
self.account = Account.objects.create(name="Test Account")
|
||||
self.site = Site.objects.create(
|
||||
account=self.account,
|
||||
name="Test Site",
|
||||
slug="test-site"
|
||||
)
|
||||
self.sector = Sector.objects.create(
|
||||
account=self.account,
|
||||
site=self.site,
|
||||
name="Test Sector",
|
||||
slug="test-sector"
|
||||
)
|
||||
self.integration = SiteIntegration.objects.create(
|
||||
account=self.account,
|
||||
site=self.site,
|
||||
platform='wordpress',
|
||||
platform_type='cms',
|
||||
sync_enabled=True,
|
||||
sync_status='pending'
|
||||
)
|
||||
self.service = SyncService()
|
||||
|
||||
def test_sync_updates_status(self):
|
||||
"""Test: Two-way sync functions properly"""
|
||||
with patch.object(self.service, '_sync_to_external') as mock_sync_to, \
|
||||
patch.object(self.service, '_sync_from_external') as mock_sync_from:
|
||||
mock_sync_to.return_value = {'success': True, 'synced': 5}
|
||||
mock_sync_from.return_value = {'success': True, 'synced': 3}
|
||||
|
||||
result = self.service.sync(self.integration, direction='both')
|
||||
|
||||
self.assertTrue(result.get('success'))
|
||||
self.integration.refresh_from_db()
|
||||
self.assertEqual(self.integration.sync_status, 'success')
|
||||
self.assertIsNotNone(self.integration.last_sync_at)
|
||||
|
||||
def test_sync_to_external_only(self):
|
||||
"""Test: Two-way sync functions properly"""
|
||||
with patch.object(self.service, '_sync_to_external') as mock_sync_to:
|
||||
mock_sync_to.return_value = {'success': True, 'synced': 5}
|
||||
|
||||
result = self.service.sync(self.integration, direction='to_external')
|
||||
|
||||
self.assertTrue(result.get('success'))
|
||||
mock_sync_to.assert_called_once()
|
||||
|
||||
def test_sync_from_external_only(self):
|
||||
"""Test: WordPress sync works (when plugin connected)"""
|
||||
with patch.object(self.service, '_sync_from_external') as mock_sync_from:
|
||||
mock_sync_from.return_value = {'success': True, 'synced': 3}
|
||||
|
||||
result = self.service.sync(self.integration, direction='from_external')
|
||||
|
||||
self.assertTrue(result.get('success'))
|
||||
mock_sync_from.assert_called_once()
|
||||
|
||||
def test_sync_handles_errors(self):
|
||||
"""Test: Two-way sync functions properly"""
|
||||
with patch.object(self.service, '_sync_to_external') as mock_sync_to:
|
||||
mock_sync_to.side_effect = Exception("Sync failed")
|
||||
|
||||
result = self.service.sync(self.integration, direction='to_external')
|
||||
|
||||
self.assertFalse(result.get('success'))
|
||||
self.integration.refresh_from_db()
|
||||
self.assertEqual(self.integration.sync_status, 'failed')
|
||||
self.assertIsNotNone(self.integration.sync_error)
|
||||
|
||||
5
backend/igny8_core/business/publishing/tests/__init__.py
Normal file
5
backend/igny8_core/business/publishing/tests/__init__.py
Normal file
@@ -0,0 +1,5 @@
|
||||
"""
|
||||
Publishing Tests
|
||||
Phase 5: Sites Renderer & Publishing
|
||||
"""
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
"""
|
||||
Tests for Publishing Adapters
|
||||
Phase 6: Site Integration & Multi-Destination Publishing
|
||||
"""
|
||||
from django.test import TestCase
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
from igny8_core.auth.models import Account, Site, Sector
|
||||
from igny8_core.business.publishing.services.adapters.base_adapter import BaseAdapter
|
||||
from igny8_core.business.publishing.services.adapters.sites_renderer_adapter import SitesRendererAdapter
|
||||
from igny8_core.business.publishing.services.adapters.wordpress_adapter import WordPressAdapter
|
||||
from igny8_core.business.site_building.models import SiteBlueprint
|
||||
|
||||
|
||||
class AdapterPatternTestCase(TestCase):
|
||||
"""Test cases for adapter pattern"""
|
||||
|
||||
def setUp(self):
|
||||
"""Set up test data"""
|
||||
self.account = Account.objects.create(name="Test Account")
|
||||
self.site = Site.objects.create(
|
||||
account=self.account,
|
||||
name="Test Site",
|
||||
slug="test-site"
|
||||
)
|
||||
self.sector = Sector.objects.create(
|
||||
account=self.account,
|
||||
site=self.site,
|
||||
name="Test Sector",
|
||||
slug="test-sector"
|
||||
)
|
||||
self.blueprint = SiteBlueprint.objects.create(
|
||||
account=self.account,
|
||||
site=self.site,
|
||||
sector=self.sector,
|
||||
name="Test Blueprint",
|
||||
status='ready'
|
||||
)
|
||||
|
||||
def test_sites_renderer_adapter_implements_base_interface(self):
|
||||
"""Test: Adapter pattern works correctly"""
|
||||
adapter = SitesRendererAdapter()
|
||||
|
||||
self.assertIsInstance(adapter, BaseAdapter)
|
||||
self.assertTrue(hasattr(adapter, 'publish'))
|
||||
self.assertTrue(hasattr(adapter, 'test_connection'))
|
||||
self.assertTrue(hasattr(adapter, 'get_status'))
|
||||
|
||||
def test_wordpress_adapter_implements_base_interface(self):
|
||||
"""Test: Adapter pattern works correctly"""
|
||||
adapter = WordPressAdapter()
|
||||
|
||||
self.assertIsInstance(adapter, BaseAdapter)
|
||||
self.assertTrue(hasattr(adapter, 'publish'))
|
||||
self.assertTrue(hasattr(adapter, 'test_connection'))
|
||||
self.assertTrue(hasattr(adapter, 'get_status'))
|
||||
|
||||
def test_sites_renderer_adapter_deploys_site(self):
|
||||
"""Test: Multi-destination publishing works"""
|
||||
adapter = SitesRendererAdapter()
|
||||
|
||||
result = adapter.deploy(self.blueprint)
|
||||
|
||||
self.assertTrue(result.get('success'))
|
||||
self.assertIsNotNone(result.get('deployment_url'))
|
||||
self.assertIsNotNone(result.get('version'))
|
||||
|
||||
def test_wordpress_adapter_publishes_content(self):
|
||||
"""Test: Multi-destination publishing works"""
|
||||
from igny8_core.business.content.models import Content
|
||||
|
||||
content = Content.objects.create(
|
||||
account=self.account,
|
||||
site=self.site,
|
||||
sector=self.sector,
|
||||
title="Test Content",
|
||||
html_content="<p>Test</p>"
|
||||
)
|
||||
|
||||
adapter = WordPressAdapter()
|
||||
config = {
|
||||
'url': 'https://example.com',
|
||||
'username': 'test',
|
||||
'password': 'test'
|
||||
}
|
||||
|
||||
with patch('igny8_core.utils.wordpress.WordPressClient') as mock_client:
|
||||
mock_instance = Mock()
|
||||
mock_instance.create_post.return_value = {'id': 123, 'link': 'https://example.com/post/123'}
|
||||
mock_client.return_value = mock_instance
|
||||
|
||||
result = adapter.publish(content, config)
|
||||
|
||||
self.assertTrue(result.get('success'))
|
||||
self.assertIsNotNone(result.get('external_id'))
|
||||
self.assertIsNotNone(result.get('url'))
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
"""
|
||||
Tests for DeploymentService
|
||||
Phase 5: Sites Renderer & Publishing
|
||||
"""
|
||||
from django.test import TestCase
|
||||
from django.utils import timezone
|
||||
|
||||
from igny8_core.auth.models import Account, Site, Sector
|
||||
from igny8_core.business.site_building.models import SiteBlueprint
|
||||
from igny8_core.business.publishing.models import DeploymentRecord
|
||||
from igny8_core.business.publishing.services.deployment_service import DeploymentService
|
||||
|
||||
|
||||
class DeploymentServiceTestCase(TestCase):
|
||||
"""Test cases for DeploymentService"""
|
||||
|
||||
def setUp(self):
|
||||
"""Set up test data"""
|
||||
self.account = Account.objects.create(name="Test Account")
|
||||
self.site = Site.objects.create(
|
||||
account=self.account,
|
||||
name="Test Site",
|
||||
slug="test-site"
|
||||
)
|
||||
self.sector = Sector.objects.create(
|
||||
account=self.account,
|
||||
site=self.site,
|
||||
name="Test Sector",
|
||||
slug="test-sector"
|
||||
)
|
||||
self.blueprint = SiteBlueprint.objects.create(
|
||||
account=self.account,
|
||||
site=self.site,
|
||||
sector=self.sector,
|
||||
name="Test Blueprint",
|
||||
status='ready',
|
||||
version=1
|
||||
)
|
||||
self.service = DeploymentService()
|
||||
|
||||
def test_get_status_returns_deployed_record(self):
|
||||
"""Test: Sites are accessible publicly"""
|
||||
DeploymentRecord.objects.create(
|
||||
account=self.account,
|
||||
site=self.site,
|
||||
sector=self.sector,
|
||||
site_blueprint=self.blueprint,
|
||||
version=1,
|
||||
status='deployed',
|
||||
deployment_url='https://test-site.igny8.com',
|
||||
deployed_at=timezone.now()
|
||||
)
|
||||
|
||||
status = self.service.get_status(self.blueprint)
|
||||
|
||||
self.assertIsNotNone(status)
|
||||
self.assertEqual(status.status, 'deployed')
|
||||
self.assertEqual(status.deployment_url, 'https://test-site.igny8.com')
|
||||
|
||||
def test_get_latest_deployment_returns_most_recent(self):
|
||||
"""Test: Deployment works end-to-end"""
|
||||
DeploymentRecord.objects.create(
|
||||
account=self.account,
|
||||
site=self.site,
|
||||
sector=self.sector,
|
||||
site_blueprint=self.blueprint,
|
||||
version=1,
|
||||
status='failed',
|
||||
created_at=timezone.now()
|
||||
)
|
||||
|
||||
latest = DeploymentRecord.objects.create(
|
||||
account=self.account,
|
||||
site=self.site,
|
||||
sector=self.sector,
|
||||
site_blueprint=self.blueprint,
|
||||
version=2,
|
||||
status='deployed',
|
||||
deployment_url='https://test-site.igny8.com',
|
||||
deployed_at=timezone.now()
|
||||
)
|
||||
|
||||
result = self.service.get_latest_deployment(self.blueprint)
|
||||
|
||||
self.assertIsNotNone(result)
|
||||
self.assertEqual(result.version, 2)
|
||||
self.assertEqual(result.status, 'deployed')
|
||||
|
||||
def test_rollback_reverts_to_previous_version(self):
|
||||
"""Test: Deployment works end-to-end"""
|
||||
DeploymentRecord.objects.create(
|
||||
account=self.account,
|
||||
site=self.site,
|
||||
sector=self.sector,
|
||||
site_blueprint=self.blueprint,
|
||||
version=1,
|
||||
status='deployed',
|
||||
deployment_url='https://test-site.igny8.com',
|
||||
deployed_at=timezone.now()
|
||||
)
|
||||
|
||||
result = self.service.rollback(self.blueprint, target_version=1)
|
||||
|
||||
self.assertTrue(result.get('success'))
|
||||
self.blueprint.refresh_from_db()
|
||||
self.assertEqual(self.blueprint.deployed_version, 1)
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
"""
|
||||
Tests for PublisherService
|
||||
Phase 5: Sites Renderer & Publishing
|
||||
"""
|
||||
from django.test import TestCase
|
||||
from django.utils import timezone
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
from igny8_core.auth.models import Account, Site, Sector
|
||||
from igny8_core.business.site_building.models import SiteBlueprint
|
||||
from igny8_core.business.publishing.models import PublishingRecord, DeploymentRecord
|
||||
from igny8_core.business.publishing.services.publisher_service import PublisherService
|
||||
|
||||
|
||||
class PublisherServiceTestCase(TestCase):
|
||||
"""Test cases for PublisherService"""
|
||||
|
||||
def setUp(self):
|
||||
"""Set up test data"""
|
||||
from igny8_core.business.site_building.tests.base import SiteBuilderTestBase
|
||||
|
||||
# Use SiteBuilderTestBase pattern if available, otherwise create manually
|
||||
self.account = Account.objects.create(name="Test Account")
|
||||
self.site = Site.objects.create(
|
||||
account=self.account,
|
||||
name="Test Site",
|
||||
slug="test-site"
|
||||
)
|
||||
self.sector = Sector.objects.create(
|
||||
account=self.account,
|
||||
site=self.site,
|
||||
name="Test Sector",
|
||||
slug="test-sector"
|
||||
)
|
||||
self.blueprint = SiteBlueprint.objects.create(
|
||||
account=self.account,
|
||||
site=self.site,
|
||||
sector=self.sector,
|
||||
name="Test Blueprint",
|
||||
status='ready'
|
||||
)
|
||||
self.service = PublisherService()
|
||||
|
||||
def test_publish_to_sites_creates_deployment_record(self):
|
||||
"""Test: Deployment works end-to-end"""
|
||||
with patch('igny8_core.business.publishing.services.adapters.sites_renderer_adapter.SitesRendererAdapter.deploy') as mock_deploy:
|
||||
mock_deploy.return_value = {
|
||||
'success': True,
|
||||
'deployment_url': 'https://test-site.igny8.com',
|
||||
'version': 1
|
||||
}
|
||||
|
||||
result = self.service.publish_to_sites(self.blueprint)
|
||||
|
||||
self.assertTrue(result.get('success'))
|
||||
self.assertIsNotNone(result.get('deployment_url'))
|
||||
|
||||
# Verify deployment record was created
|
||||
deployment = DeploymentRecord.objects.filter(site_blueprint=self.blueprint).first()
|
||||
self.assertIsNotNone(deployment)
|
||||
|
||||
def test_get_deployment_status_returns_latest(self):
|
||||
"""Test: Sites are accessible publicly"""
|
||||
DeploymentRecord.objects.create(
|
||||
account=self.account,
|
||||
site=self.site,
|
||||
sector=self.sector,
|
||||
site_blueprint=self.blueprint,
|
||||
version=1,
|
||||
status='deployed',
|
||||
deployment_url='https://test-site.igny8.com',
|
||||
deployed_at=timezone.now()
|
||||
)
|
||||
|
||||
status = self.service.get_deployment_status(self.blueprint)
|
||||
|
||||
self.assertIsNotNone(status)
|
||||
self.assertEqual(status.status, 'deployed')
|
||||
self.assertIsNotNone(status.deployment_url)
|
||||
|
||||
def test_publish_content_to_multiple_destinations(self):
|
||||
"""Test: Multi-destination publishing works"""
|
||||
from igny8_core.business.content.models import Content
|
||||
|
||||
content = Content.objects.create(
|
||||
account=self.account,
|
||||
site=self.site,
|
||||
sector=self.sector,
|
||||
title="Test Content",
|
||||
html_content="<p>Test</p>"
|
||||
)
|
||||
|
||||
with patch.object(self.service, '_get_adapter') as mock_get_adapter:
|
||||
mock_adapter = Mock()
|
||||
mock_adapter.publish.return_value = {
|
||||
'success': True,
|
||||
'external_id': '123',
|
||||
'url': 'https://example.com/post/123'
|
||||
}
|
||||
mock_get_adapter.return_value = mock_adapter
|
||||
|
||||
result = self.service.publish_content(
|
||||
content_id=content.id,
|
||||
destinations=['wordpress', 'sites'],
|
||||
account=self.account
|
||||
)
|
||||
|
||||
self.assertTrue(result.get('success'))
|
||||
self.assertEqual(len(result.get('results', [])), 2)
|
||||
|
||||
# Verify publishing records were created
|
||||
records = PublishingRecord.objects.filter(content=content)
|
||||
self.assertEqual(records.count(), 2)
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
"""
|
||||
Tests for Bulk Page Generation
|
||||
Phase 5: Sites Renderer & Bulk Generation
|
||||
"""
|
||||
from django.test import TestCase
|
||||
from unittest.mock import patch, Mock
|
||||
|
||||
from igny8_core.auth.models import Account, Site, Sector
|
||||
from igny8_core.business.site_building.models import SiteBlueprint, PageBlueprint
|
||||
from igny8_core.business.site_building.services.page_generation_service import PageGenerationService
|
||||
from igny8_core.business.content.models import Tasks
|
||||
|
||||
from .base import SiteBuilderTestBase
|
||||
|
||||
|
||||
class BulkGenerationTestCase(SiteBuilderTestBase):
|
||||
"""Test cases for bulk page generation"""
|
||||
|
||||
def setUp(self):
|
||||
"""Set up test data"""
|
||||
super().setUp()
|
||||
self.page1 = PageBlueprint.objects.create(
|
||||
account=self.account,
|
||||
site=self.site,
|
||||
sector=self.sector,
|
||||
site_blueprint=self.blueprint,
|
||||
title="Page 1",
|
||||
slug="page-1",
|
||||
type="home",
|
||||
status="draft"
|
||||
)
|
||||
self.page2 = PageBlueprint.objects.create(
|
||||
account=self.account,
|
||||
site=self.site,
|
||||
sector=self.sector,
|
||||
site_blueprint=self.blueprint,
|
||||
title="Page 2",
|
||||
slug="page-2",
|
||||
type="about",
|
||||
status="draft"
|
||||
)
|
||||
self.service = PageGenerationService()
|
||||
|
||||
def test_bulk_generate_pages_creates_tasks(self):
|
||||
"""Test: Bulk page generation works"""
|
||||
with patch.object(self.service.content_service, 'generate_content') as mock_generate:
|
||||
mock_generate.return_value = {'task_id': 'test-task-id'}
|
||||
|
||||
result = self.service.bulk_generate_pages(self.blueprint)
|
||||
|
||||
self.assertTrue(result.get('success'))
|
||||
self.assertEqual(result.get('pages_queued'), 2)
|
||||
self.assertEqual(len(result.get('task_ids', [])), 2)
|
||||
|
||||
# Verify tasks were created
|
||||
tasks = Tasks.objects.filter(account=self.account)
|
||||
self.assertEqual(tasks.count(), 2)
|
||||
|
||||
def test_bulk_generate_selected_pages_only(self):
|
||||
"""Test: Selected pages can be generated"""
|
||||
with patch.object(self.service.content_service, 'generate_content') as mock_generate:
|
||||
mock_generate.return_value = {'task_id': 'test-task-id'}
|
||||
|
||||
result = self.service.bulk_generate_pages(
|
||||
self.blueprint,
|
||||
page_ids=[self.page1.id]
|
||||
)
|
||||
|
||||
self.assertTrue(result.get('success'))
|
||||
self.assertEqual(result.get('pages_queued'), 1)
|
||||
self.assertEqual(len(result.get('task_ids', [])), 1)
|
||||
|
||||
def test_bulk_generate_force_regenerate_deletes_existing_tasks(self):
|
||||
"""Test: Force regenerate works"""
|
||||
# Create existing task
|
||||
Tasks.objects.create(
|
||||
account=self.account,
|
||||
site=self.site,
|
||||
sector=self.sector,
|
||||
title="[Site Builder] Page 1",
|
||||
description="Test",
|
||||
status='completed'
|
||||
)
|
||||
|
||||
with patch.object(self.service.content_service, 'generate_content') as mock_generate:
|
||||
mock_generate.return_value = {'task_id': 'test-task-id'}
|
||||
|
||||
result = self.service.bulk_generate_pages(
|
||||
self.blueprint,
|
||||
force_regenerate=True
|
||||
)
|
||||
|
||||
self.assertTrue(result.get('success'))
|
||||
# Verify new tasks were created (old ones deleted)
|
||||
tasks = Tasks.objects.filter(account=self.account)
|
||||
self.assertEqual(tasks.count(), 2)
|
||||
|
||||
def test_create_tasks_for_pages_without_generation(self):
|
||||
"""Test: Task creation works correctly"""
|
||||
tasks = self.service.create_tasks_for_pages(self.blueprint)
|
||||
|
||||
self.assertEqual(len(tasks), 2)
|
||||
self.assertIsInstance(tasks[0], Tasks)
|
||||
self.assertEqual(tasks[0].title, "[Site Builder] Page 1")
|
||||
|
||||
# Verify tasks exist but content not generated
|
||||
tasks_db = Tasks.objects.filter(account=self.account)
|
||||
self.assertEqual(tasks_db.count(), 2)
|
||||
self.assertEqual(tasks_db.first().status, 'queued')
|
||||
|
||||
def test_bulk_generate_updates_page_status(self):
|
||||
"""Test: Progress tracking works"""
|
||||
with patch.object(self.service.content_service, 'generate_content') as mock_generate:
|
||||
mock_generate.return_value = {'task_id': 'test-task-id'}
|
||||
|
||||
self.service.bulk_generate_pages(self.blueprint)
|
||||
|
||||
# Verify page status updated
|
||||
self.page1.refresh_from_db()
|
||||
self.page2.refresh_from_db()
|
||||
self.assertEqual(self.page1.status, 'generating')
|
||||
self.assertEqual(self.page2.status, 'generating')
|
||||
|
||||
Reference in New Issue
Block a user