Files
markitect-main/tests/test_issue_38_command_restructuring.py
tegwick 1358ca17ec
Some checks failed
Test Suite / unit-tests (3.11) (push) Has been cancelled
Test Suite / unit-tests (3.12) (push) Has been cancelled
Test Suite / integration-tests (push) Has been cancelled
Test Suite / e2e-tests (push) Has been cancelled
Test Suite / performance-tests (push) Has been cancelled
Test Suite / code-quality (push) Has been cancelled
Test Suite / security-scan (push) Has been cancelled
Test Suite / test-summary (push) Has been cancelled
refactor: Remove circular test dependencies and meta-testing anti-patterns
Clean up test infrastructure by removing problematic tests that create
circular dependencies and execute the test suite from within tests.

Key removals:
- Delete test_issue_57_test_efficiency_improvements.py entirely (12 tests)
  - Contained tests that ran `make test-tdd`, `make test-status` etc.
  - Created circular dependencies where tests execute the entire test suite
  - Violated separation of concerns between testing and test infrastructure

- Remove self-execution blocks from 11 test files
  - Eliminated `if __name__ == '__main__': pytest.main([__file__, '-v'])` patterns
  - Prevents confusion and potential circular execution paths
  - Test files should be run via pytest, not as standalone scripts

Test Infrastructure Improvements:
- Reduced test count from 701 to 689 tests (removed 12 problematic tests)
- Eliminated subprocess calls to `make test-*` commands from within tests
- Removed `pytest.main()` calls that could cause circular execution
- Maintained clean separation between test infrastructure and actual tests

Impact:
- No more tests testing tests (circular dependency elimination)
- Cleaner test execution without subprocess complexity
- Proper test isolation and independence
- Faster and more reliable test runs

The proper way to test infrastructure is to test the underlying functions
directly, not to execute the entire test suite from within a test.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-01 21:05:36 +02:00

215 lines
7.8 KiB
Python

"""
Tests for Issue #38 Phase 1: Command Restructuring
This module tests the restructuring of metadata command to db-data
and ensures backward compatibility with proper deprecation warnings.
Issue #38: Access metadata, frontmatter, content separately in CLI
Phase 1: Rename metadata command to db-data for consistency
"""
import pytest
from click.testing import CliRunner
from unittest.mock import patch, MagicMock
from markitect.cli import cli
class TestIssue38CommandRestructuring:
"""Test suite for Issue #38 Phase 1: Command restructuring."""
def setup_method(self):
"""Set up test fixtures."""
self.runner = CliRunner()
self.sample_metadata = {
'id': 1,
'filename': 'test.md',
'created_at': '2025-09-30 12:00:00',
'front_matter': '{"title": "Test Document", "author": "Test Author"}',
'content': '# Test Document\n\nThis is test content.'
}
def test_db_data_command_exists(self):
"""
Test that new db-data command exists and is accessible.
Issue #38 Phase 1: Command restructuring
"""
result = self.runner.invoke(cli, ['db-data', '--help'])
assert result.exit_code == 0
assert 'db-data' in result.output.lower()
assert 'display file metadata' in result.output.lower() or 'metadata' in result.output.lower()
def test_db_data_command_functionality(self):
"""
Test that db-data command works with same functionality as old metadata command.
Issue #38 Phase 1: Command restructuring
"""
with patch('markitect.cli.DatabaseManager') as mock_db_mgr:
mock_db_instance = MagicMock()
mock_db_mgr.return_value = mock_db_instance
mock_db_instance.get_markdown_file.return_value = self.sample_metadata
result = self.runner.invoke(cli, ['db-data', 'test.md'])
assert result.exit_code == 0
assert 'test.md' in result.output
assert 'Test Document' in result.output
assert 'Test Author' in result.output
def test_db_data_supports_all_output_formats(self):
"""
Test that db-data command supports all output formats (table, json, yaml).
Issue #38 Phase 1: Command restructuring
"""
with patch('markitect.cli.DatabaseManager') as mock_db_mgr:
mock_db_instance = MagicMock()
mock_db_mgr.return_value = mock_db_instance
mock_db_instance.get_markdown_file.return_value = self.sample_metadata
# Test JSON format
result = self.runner.invoke(cli, ['db-data', 'test.md', '--format', 'json'])
assert result.exit_code == 0
# Test YAML format
result = self.runner.invoke(cli, ['db-data', 'test.md', '--format', 'yaml'])
assert result.exit_code == 0
# Test table format
result = self.runner.invoke(cli, ['db-data', 'test.md', '--format', 'table'])
assert result.exit_code == 0
def test_metadata_command_still_exists_with_deprecation_warning(self):
"""
Test that old metadata command still works but shows deprecation warning.
Issue #38 Phase 1: Backward compatibility with deprecation
"""
with patch('markitect.cli.DatabaseManager') as mock_db_mgr:
mock_db_instance = MagicMock()
mock_db_mgr.return_value = mock_db_instance
mock_db_instance.get_markdown_file.return_value = self.sample_metadata
result = self.runner.invoke(cli, ['metadata', 'test.md'])
# Should still work (backward compatibility)
assert result.exit_code == 0
assert 'test.md' in result.output
# Should show deprecation warning
assert ('deprecated' in result.output.lower() or
'warning' in result.output.lower() or
'db-data' in result.output.lower())
def test_metadata_command_redirect_suggestion(self):
"""
Test that metadata command suggests using db-data instead.
Issue #38 Phase 1: User guidance for migration
"""
result = self.runner.invoke(cli, ['metadata', '--help'])
assert result.exit_code == 0
# Should suggest the new command
assert 'db-data' in result.output.lower()
def test_db_data_command_error_handling(self):
"""
Test that db-data command handles errors gracefully.
Issue #38 Phase 1: Error handling consistency
"""
with patch('markitect.cli.DatabaseManager') as mock_db_mgr:
mock_db_instance = MagicMock()
mock_db_mgr.return_value = mock_db_instance
mock_db_instance.get_markdown_file.side_effect = FileNotFoundError("File not found")
result = self.runner.invoke(cli, ['db-data', 'nonexistent.md'])
# Should handle error gracefully
assert result.exit_code != 0 or 'not found' in result.output.lower()
def test_help_consistency_between_commands(self):
"""
Test that db-data and metadata commands have consistent help information.
Issue #38 Phase 1: Documentation consistency
"""
# Get help for both commands
db_data_help = self.runner.invoke(cli, ['db-data', '--help'])
metadata_help = self.runner.invoke(cli, ['metadata', '--help'])
assert db_data_help.exit_code == 0
assert metadata_help.exit_code == 0
# Both should mention similar functionality
assert '--format' in db_data_help.output
assert '--format' in metadata_help.output
def test_db_data_command_follows_db_prefix_pattern(self):
"""
Test that db-data command follows the established db- prefix pattern.
Issue #38 Phase 1: Consistency with Issue #39 db- reorganization
"""
# Check that db-data command appears in help alongside other db- commands
result = self.runner.invoke(cli, ['--help'])
assert result.exit_code == 0
help_output = result.output.lower()
# Should see db-data alongside other db- commands
assert 'db-data' in help_output
# Should also see other established db- commands
assert 'db-query' in help_output or 'db-schema' in help_output
class TestIssue38BackwardCompatibility:
"""Test suite for backward compatibility during command restructuring."""
def setup_method(self):
"""Set up test fixtures."""
self.runner = CliRunner()
def test_existing_scripts_continue_working(self):
"""
Test that existing scripts using metadata command continue to work.
Issue #38 Phase 1: Backward compatibility guarantee
"""
with patch('markitect.cli.DatabaseManager') as mock_db_mgr:
mock_db_instance = MagicMock()
mock_db_mgr.return_value = mock_db_instance
mock_db_instance.get_markdown_file.return_value = {
'filename': 'test.md',
'front_matter': '{"title": "Test"}'
}
# Simulate existing script usage
result = self.runner.invoke(cli, ['metadata', 'test.md', '--format', 'json'])
# Must continue working for backward compatibility
assert result.exit_code == 0
assert 'test.md' in result.output
def test_migration_path_documentation(self):
"""
Test that clear migration path is provided from metadata to db-data.
Issue #38 Phase 1: User migration guidance
"""
result = self.runner.invoke(cli, ['metadata', '--help'])
assert result.exit_code == 0
help_text = result.output.lower()
# Should provide clear migration guidance
assert ('db-data' in help_text or
'use db-data instead' in help_text or
'deprecated' in help_text)