aider-gitea/test/test_validation.py
Jon Michael Aanes 42e32faba7
All checks were successful
Run Python tests (through Pytest) / Test (push) Successful in 25s
Verify Python project can be installed, loaded and have version checked / Test (push) Successful in 23s
Improve codebase with meaningful changes beyond ruff formatting
- Convert magic numbers and hardcoded values to module-level constants
- Add comprehensive input validation with detailed error messages
- Improve error handling for file operations and edge cases
- Add proper type checking and validation for model detection
- Create comprehensive test suite for new validation features
- Fix existing tests to match actual implementation

This addresses issue #112 by ensuring substantial code improvements
accompany formatting changes, making the codebase more maintainable
and robust.

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-06-09 18:12:23 +02:00

140 lines
5.2 KiB
Python

import pytest
from aider_gitea import RepositoryConfig, generate_branch_name
class TestRepositoryConfigValidation:
"""Test validation in RepositoryConfig dataclass."""
def test_valid_repository_config(self):
"""Test that valid configurations work correctly."""
config = RepositoryConfig(
gitea_url='https://gitea.example.com',
owner='testowner',
repo='testrepo',
base_branch='main',
)
assert config.gitea_url == 'https://gitea.example.com'
assert config.owner == 'testowner'
assert config.repo == 'testrepo'
assert config.base_branch == 'main'
def test_empty_gitea_url_validation(self):
"""Test that empty gitea_url raises ValueError."""
with pytest.raises(ValueError, match='gitea_url cannot be empty'):
RepositoryConfig(
gitea_url='',
owner='testowner',
repo='testrepo',
base_branch='main',
)
def test_empty_owner_validation(self):
"""Test that empty owner raises ValueError."""
with pytest.raises(ValueError, match='owner cannot be empty'):
RepositoryConfig(
gitea_url='https://gitea.example.com',
owner='',
repo='testrepo',
base_branch='main',
)
def test_empty_repo_validation(self):
"""Test that empty repo raises ValueError."""
with pytest.raises(ValueError, match='repo cannot be empty'):
RepositoryConfig(
gitea_url='https://gitea.example.com',
owner='testowner',
repo='',
base_branch='main',
)
def test_empty_base_branch_validation(self):
"""Test that empty base_branch raises ValueError."""
with pytest.raises(ValueError, match='base_branch cannot be empty'):
RepositoryConfig(
gitea_url='https://gitea.example.com',
owner='testowner',
repo='testrepo',
base_branch='',
)
def test_gitea_url_api_v1_suffix_validation(self):
"""Test that gitea_url with /api/v1 suffix raises ValueError."""
with pytest.raises(
ValueError,
match="gitea_url should not include '/api/v1' suffix",
):
RepositoryConfig(
gitea_url='https://gitea.example.com/api/v1',
owner='testowner',
repo='testrepo',
base_branch='main',
)
def test_whitespace_only_fields_validation(self):
"""Test that whitespace-only fields raise ValueError."""
with pytest.raises(ValueError, match='owner cannot be empty'):
RepositoryConfig(
gitea_url='https://gitea.example.com',
owner=' ',
repo='testrepo',
base_branch='main',
)
class TestBranchNameGeneration:
"""Test validation in generate_branch_name function."""
def test_valid_branch_name_generation(self):
"""Test that valid inputs generate correct branch names."""
result = generate_branch_name('123', 'Fix authentication bug')
expected = 'issue-123-fix-authentication-bug'
assert result == expected
def test_empty_issue_number_validation(self):
"""Test that empty issue_number raises ValueError."""
with pytest.raises(ValueError, match='Issue number cannot be empty'):
generate_branch_name('', 'Fix bug')
def test_whitespace_issue_number_validation(self):
"""Test that whitespace-only issue_number raises ValueError."""
with pytest.raises(ValueError, match='Issue number cannot be empty'):
generate_branch_name(' ', 'Fix bug')
def test_non_string_issue_number_validation(self):
"""Test that non-string issue_number raises TypeError."""
with pytest.raises(
TypeError,
match='Both issue_number and issue_title must be strings',
):
generate_branch_name(123, 'Fix bug')
def test_non_string_issue_title_validation(self):
"""Test that non-string issue_title raises TypeError."""
with pytest.raises(
TypeError,
match='Both issue_number and issue_title must be strings',
):
generate_branch_name('123', 456)
def test_empty_issue_title_handling(self):
"""Test that empty issue_title is handled gracefully."""
result = generate_branch_name('123', '')
expected = 'issue-123-untitled'
assert result == expected
def test_none_issue_title_handling(self):
"""Test that None issue_title is handled gracefully."""
with pytest.raises(
TypeError,
match='Both issue_number and issue_title must be strings',
):
generate_branch_name('123', None)
def test_special_characters_sanitization(self):
"""Test that special characters are properly sanitized."""
result = generate_branch_name('456', 'Fix #bug [with] @special! chars?')
expected = 'issue-456-fix-bug-with-special-chars'
assert result == expected