initial
This commit is contained in:
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,100 @@
|
||||
"""
|
||||
Unit tests for the context resolution system.
|
||||
"""
|
||||
import pytest
|
||||
import tempfile
|
||||
import os
|
||||
from src.pr_reviewer.context import resolve_context
|
||||
from src.pr_reviewer.state import PRReviewState, ContextOverrides
|
||||
|
||||
|
||||
def test_resolve_context_with_defaults():
|
||||
"""Test resolving context when default files exist."""
|
||||
# Create temporary directory structure
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
# Create contexts/defaults directory
|
||||
defaults_dir = os.path.join(temp_dir, "contexts", "defaults")
|
||||
os.makedirs(defaults_dir)
|
||||
|
||||
# Create default context files
|
||||
with open(os.path.join(defaults_dir, "code_review.md"), "w") as f:
|
||||
f.write("# Code Review\n\nFollow PEP8.")
|
||||
|
||||
with open(os.path.join(defaults_dir, "security_review.md"), "w") as f:
|
||||
f.write("# Security Review\n\nCheck for SQL injection.")
|
||||
|
||||
with open(os.path.join(defaults_dir, "infra_review.md"), "w") as f:
|
||||
f.write("# Infra Review\n\nValidate Dockerfile.")
|
||||
|
||||
# Change to temp directory
|
||||
original_cwd = os.getcwd()
|
||||
os.chdir(temp_dir)
|
||||
|
||||
try:
|
||||
# Create state without overrides
|
||||
state = PRReviewState(
|
||||
pr_id="123",
|
||||
pr_title="Test PR",
|
||||
repo_name="test-repo",
|
||||
repo_url="https://github.com/user/test-repo",
|
||||
branch="main",
|
||||
base_branch="main"
|
||||
)
|
||||
|
||||
# Resolve context
|
||||
context = resolve_context(state)
|
||||
|
||||
# Assertions
|
||||
assert context["code_review"] == "# Code Review\n\nFollow PEP8."
|
||||
assert context["security_review"] == "# Security Review\n\nCheck for SQL injection."
|
||||
assert context["infra_review"] == "# Infra Review\n\nValidate Dockerfile."
|
||||
finally:
|
||||
os.chdir(original_cwd)
|
||||
|
||||
|
||||
def test_resolve_context_with_overrides():
|
||||
"""Test resolving context when overrides are provided."""
|
||||
# Create state with context overrides
|
||||
state = PRReviewState(
|
||||
pr_id="123",
|
||||
pr_title="Test PR",
|
||||
repo_name="test-repo",
|
||||
repo_url="https://github.com/user/test-repo",
|
||||
branch="main",
|
||||
base_branch="main",
|
||||
context_overrides=ContextOverrides(
|
||||
code_review="Custom code review guidelines",
|
||||
security_review="Custom security guidelines"
|
||||
# infra_review is not provided, should use default if file exists
|
||||
)
|
||||
)
|
||||
|
||||
# Since we don't have default files in this test, infra_review should be empty
|
||||
context = resolve_context(state)
|
||||
|
||||
# Assertions
|
||||
assert context["code_review"] == "Custom code review guidelines"
|
||||
assert context["security_review"] == "Custom security guidelines"
|
||||
assert context["infra_review"] == "" # Empty because no default file and no override
|
||||
|
||||
|
||||
def test_resolve_context_empty_state():
|
||||
"""Test resolving context with minimal state."""
|
||||
state = PRReviewState(
|
||||
pr_id="123",
|
||||
pr_title="Test PR",
|
||||
repo_name="test-repo",
|
||||
repo_url="https://github.com/user/test-repo",
|
||||
branch="main",
|
||||
base_branch="main"
|
||||
)
|
||||
|
||||
# Without default files in the current directory, all contexts should be empty
|
||||
# But since we have default files in the project, we need to check what they contain
|
||||
context = resolve_context(state)
|
||||
|
||||
# The function should return the content of the default files
|
||||
# We're just checking that it returns strings (could be empty or contain default content)
|
||||
assert isinstance(context["code_review"], str)
|
||||
assert isinstance(context["security_review"], str)
|
||||
assert isinstance(context["infra_review"], str)
|
||||
@@ -0,0 +1,69 @@
|
||||
"""
|
||||
Unit tests for the state management module.
|
||||
"""
|
||||
import pytest
|
||||
from src.pr_reviewer.state import FileInfo, ContextOverrides, PRReviewState
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
def test_file_info_creation():
|
||||
"""Test creating a FileInfo instance."""
|
||||
file_info = FileInfo(
|
||||
path="src/main.py",
|
||||
content="print('hello')",
|
||||
status="modified",
|
||||
additions=5,
|
||||
deletions=2
|
||||
)
|
||||
|
||||
assert file_info.path == "src/main.py"
|
||||
assert file_info.content == "print('hello')"
|
||||
assert file_info.status == "modified"
|
||||
assert file_info.additions == 5
|
||||
assert file_info.deletions == 2
|
||||
|
||||
|
||||
def test_context_overrides_creation():
|
||||
"""Test creating a ContextOverrides instance."""
|
||||
context_overrides = ContextOverrides(
|
||||
code_review="Follow PEP8",
|
||||
security_review="Check for SQL injection",
|
||||
infra_review="Validate Dockerfile"
|
||||
)
|
||||
|
||||
assert context_overrides.code_review == "Follow PEP8"
|
||||
assert context_overrides.security_review == "Check for SQL injection"
|
||||
assert context_overrides.infra_review == "Validate Dockerfile"
|
||||
|
||||
|
||||
def test_context_overrides_partial():
|
||||
"""Test creating a ContextOverrides instance with partial fields."""
|
||||
context_overrides = ContextOverrides(
|
||||
code_review="Follow PEP8"
|
||||
)
|
||||
|
||||
assert context_overrides.code_review == "Follow PEP8"
|
||||
assert context_overrides.security_review is None
|
||||
assert context_overrides.infra_review is None
|
||||
|
||||
|
||||
def test_pr_review_state_creation():
|
||||
"""Test creating a PRReviewState instance."""
|
||||
state = PRReviewState(
|
||||
pr_id="123",
|
||||
pr_title="Add feature",
|
||||
repo_name="test-repo",
|
||||
repo_url="https://github.com/user/test-repo",
|
||||
branch="feature",
|
||||
base_branch="main"
|
||||
)
|
||||
|
||||
assert state.pr_id == "123"
|
||||
assert state.pr_title == "Add feature"
|
||||
assert state.repo_name == "test-repo"
|
||||
assert state.repo_url == "https://github.com/user/test-repo"
|
||||
assert state.branch == "feature"
|
||||
assert state.base_branch == "main"
|
||||
assert state.files == []
|
||||
assert state.started_at is None
|
||||
assert state.completed_at is None
|
||||
Reference in New Issue
Block a user