import os import tempfile from io import StringIO from unittest.mock import patch, Mock from django.test import TestCase, override_settings from django.core.management import call_command from django.core.files.storage import default_storage from django.core.files.base import ContentFile class ClearDiagramCacheCommandTestCase(TestCase): """Test cases for clear_diagram_cache management command.""" @override_settings(MEDIA_ROOT=tempfile.mkdtemp()) def test_clear_all_cache(self): """Test clearing all cached diagrams.""" # Create test cache files test_files = [ 'diagram_cache/plantuml/hash1.svg', 'diagram_cache/plantuml/hash2.svg', 'diagram_cache/mermaid/hash3.svg', ] for path in test_files: full_path = default_storage.path(path) os.makedirs(os.path.dirname(full_path), exist_ok=True) default_storage.save(path, ContentFile(b'test diagram')) # Verify files exist for path in test_files: self.assertTrue(default_storage.exists(path)) # Run command out = StringIO() call_command('clear_diagram_cache', stdout=out) # Verify files are deleted for path in test_files: self.assertFalse(default_storage.exists(path)) # Verify success message output = out.getvalue() self.assertIn('Clearing all diagram caches', output) self.assertIn('Cache cleared successfully', output) @override_settings(MEDIA_ROOT=tempfile.mkdtemp()) def test_clear_cache_by_type(self): """Test clearing cache for specific diagram type.""" # Create test cache files plantuml_files = [ 'diagram_cache/plantuml/hash1.svg', 'diagram_cache/plantuml/hash2.svg', ] mermaid_files = [ 'diagram_cache/mermaid/hash3.svg', ] for path in plantuml_files + mermaid_files: full_path = default_storage.path(path) os.makedirs(os.path.dirname(full_path), exist_ok=True) default_storage.save(path, ContentFile(b'test diagram')) # Run command for plantuml only out = StringIO() call_command('clear_diagram_cache', type='plantuml', stdout=out) # Verify only plantuml files are deleted for path in plantuml_files: self.assertFalse(default_storage.exists(path)) for path in mermaid_files: self.assertTrue(default_storage.exists(path)) # Verify output message output = out.getvalue() self.assertIn('Clearing cache for plantuml', output) self.assertIn('Cache cleared successfully', output) @override_settings(MEDIA_ROOT=tempfile.mkdtemp()) def test_clear_empty_cache(self): """Test clearing cache when no files exist.""" # Don't create any files # Should not raise error out = StringIO() call_command('clear_diagram_cache', stdout=out) # Should still show success output = out.getvalue() self.assertIn('Cache cleared successfully', output) @override_settings(MEDIA_ROOT=tempfile.mkdtemp()) @patch('diagramm_proxy.diagram_cache.clear_cache') def test_command_calls_clear_cache_function(self, mock_clear): """Test that command properly calls the clear_cache function.""" # Call without type call_command('clear_diagram_cache') mock_clear.assert_called_once_with(None) # Call with type mock_clear.reset_mock() call_command('clear_diagram_cache', type='plantuml') mock_clear.assert_called_once_with('plantuml') def test_command_help_text(self): """Test that command has proper help text.""" out = StringIO() call_command('clear_diagram_cache', '--help', stdout=out) output = out.getvalue() self.assertIn('Clear cached diagrams', output) self.assertIn('--type', output) class ManagementCommandIntegrationTestCase(TestCase): """Integration tests for management command with real filesystem.""" @override_settings(MEDIA_ROOT=tempfile.mkdtemp()) def test_full_command_workflow(self): """Test complete workflow: create cache, clear specific type, clear all.""" # Create mixed cache files files = { 'plantuml': [ 'diagram_cache/plantuml/abc123.svg', 'diagram_cache/plantuml/def456.svg', ], 'mermaid': [ 'diagram_cache/mermaid/ghi789.svg', ], 'graphviz': [ 'diagram_cache/graphviz/jkl012.svg', ] } # Create all files for file_list in files.values(): for path in file_list: full_path = default_storage.path(path) os.makedirs(os.path.dirname(full_path), exist_ok=True) default_storage.save(path, ContentFile(b'diagram content')) # Verify all exist for file_list in files.values(): for path in file_list: self.assertTrue(default_storage.exists(path)) # Clear plantuml call_command('clear_diagram_cache', type='plantuml') # Verify plantuml deleted, others remain for path in files['plantuml']: self.assertFalse(default_storage.exists(path)) for path in files['mermaid']: self.assertTrue(default_storage.exists(path)) for path in files['graphviz']: self.assertTrue(default_storage.exists(path)) # Clear all remaining call_command('clear_diagram_cache') # Verify all deleted for file_list in files.values(): for path in file_list: self.assertFalse(default_storage.exists(path))