|
| 1 | +# SPDX-FileCopyrightText: 2025 Sequent Tech Inc <legal@sequentech.io> |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +"""Shared pytest fixtures for end-to-end tests.""" |
| 6 | + |
| 7 | +import pytest |
| 8 | +import tempfile |
| 9 | +import shutil |
| 10 | +from pathlib import Path |
| 11 | +from unittest.mock import Mock, MagicMock, patch |
| 12 | +from git import Repo |
| 13 | + |
| 14 | +from release_tool.db import Database |
| 15 | +from release_tool.config import Config |
| 16 | +from release_tool.models import PullRequest, Issue, Label, Repository |
| 17 | +from helpers.git_helpers import init_git_repo, GitScenario |
| 18 | +from helpers.config_helpers import create_test_config, write_config_file |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture |
| 22 | +def tmp_git_repo(tmp_path): |
| 23 | + """ |
| 24 | + Create a temporary git repository. |
| 25 | +
|
| 26 | + Returns: |
| 27 | + Tuple of (repo_path, Repo object) |
| 28 | + """ |
| 29 | + repo_path = tmp_path / "test_repo" |
| 30 | + repo_path.mkdir() |
| 31 | + |
| 32 | + repo = init_git_repo(repo_path) |
| 33 | + |
| 34 | + yield repo_path, repo |
| 35 | + |
| 36 | + # Cleanup handled by tmp_path |
| 37 | + |
| 38 | + |
| 39 | +@pytest.fixture |
| 40 | +def git_scenario(tmp_git_repo, test_db): |
| 41 | + """ |
| 42 | + Create a GitScenario helper for building complex git histories. |
| 43 | +
|
| 44 | + Returns: |
| 45 | + GitScenario instance with database sync |
| 46 | + """ |
| 47 | + repo_path, repo = tmp_git_repo |
| 48 | + db, repo_id = test_db |
| 49 | + scenario = GitScenario(repo, db=db, repo_id=repo_id) |
| 50 | + |
| 51 | + return scenario |
| 52 | + |
| 53 | + |
| 54 | +@pytest.fixture |
| 55 | +def test_db(tmp_path): |
| 56 | + """ |
| 57 | + Create an in-memory test database with test data. |
| 58 | +
|
| 59 | + Returns: |
| 60 | + Database instance |
| 61 | + """ |
| 62 | + db_path = tmp_path / "test.db" |
| 63 | + db = Database(str(db_path)) |
| 64 | + db.connect() # Initialize database connection and schema |
| 65 | + |
| 66 | + # Create test repository |
| 67 | + test_repo = Repository( |
| 68 | + owner="test", |
| 69 | + name="repo", |
| 70 | + full_name="test/repo", |
| 71 | + url="https://github.com/test/repo" |
| 72 | + ) |
| 73 | + repo_id = db.upsert_repository(test_repo) |
| 74 | + |
| 75 | + yield db, repo_id |
| 76 | + |
| 77 | + # Cleanup |
| 78 | + db.close() |
| 79 | + |
| 80 | + |
| 81 | +@pytest.fixture |
| 82 | +def mock_github_client(): |
| 83 | + """ |
| 84 | + Create a mock GitHub client that doesn't make external calls. |
| 85 | +
|
| 86 | + Returns: |
| 87 | + Mock GitHub client |
| 88 | + """ |
| 89 | + client = Mock() |
| 90 | + |
| 91 | + # Mock common methods |
| 92 | + client.get_pull_request = Mock(return_value=None) |
| 93 | + client.get_issue = Mock(return_value=None) |
| 94 | + client.create_release = Mock(return_value={'html_url': 'https://github.com/test/repo/releases/tag/v1.0.0'}) |
| 95 | + client.create_pull_request = Mock(return_value={'html_url': 'https://github.com/test/repo/pull/1'}) |
| 96 | + |
| 97 | + return client |
| 98 | + |
| 99 | + |
| 100 | +@pytest.fixture |
| 101 | +def test_config_dict(): |
| 102 | + """ |
| 103 | + Create a basic test configuration dictionary. |
| 104 | +
|
| 105 | + Returns: |
| 106 | + Configuration dictionary |
| 107 | + """ |
| 108 | + return create_test_config(code_repo="test/repo") |
| 109 | + |
| 110 | + |
| 111 | +@pytest.fixture |
| 112 | +def test_config(test_config_dict): |
| 113 | + """ |
| 114 | + Create a Config object from test configuration. |
| 115 | +
|
| 116 | + Returns: |
| 117 | + Config instance |
| 118 | + """ |
| 119 | + return Config.from_dict(test_config_dict) |
| 120 | + |
| 121 | + |
| 122 | +@pytest.fixture |
| 123 | +def test_config_file(tmp_path, test_config_dict): |
| 124 | + """ |
| 125 | + Create a temporary config file. |
| 126 | +
|
| 127 | + Returns: |
| 128 | + Path to config file |
| 129 | + """ |
| 130 | + config_path = tmp_path / "config.yaml" |
| 131 | + write_config_file(config_path, test_config_dict) |
| 132 | + |
| 133 | + return config_path |
| 134 | + |
| 135 | + |
| 136 | +@pytest.fixture |
| 137 | +def populated_db(test_db): |
| 138 | + """ |
| 139 | + Create a database populated with test PRs and issues. |
| 140 | +
|
| 141 | + Returns: |
| 142 | + Tuple of (Database, repo_id, test_data) |
| 143 | + """ |
| 144 | + db, repo_id = test_db |
| 145 | + |
| 146 | + test_data = { |
| 147 | + 'prs': {}, |
| 148 | + 'issues': {} |
| 149 | + } |
| 150 | + |
| 151 | + # Create test PRs (101-110) |
| 152 | + for pr_num in range(101, 111): |
| 153 | + pr = PullRequest( |
| 154 | + repo_id=repo_id, |
| 155 | + number=pr_num, |
| 156 | + title=f"Test PR #{pr_num}", |
| 157 | + body=f"Test PR body for #{pr_num}", |
| 158 | + state="closed", |
| 159 | + merged=True, |
| 160 | + labels=[Label(name="enhancement")], |
| 161 | + created_at="2024-01-01T00:00:00Z", |
| 162 | + updated_at="2024-01-01T00:00:00Z", |
| 163 | + closed_at="2024-01-01T00:00:00Z", |
| 164 | + merged_at="2024-01-01T00:00:00Z", |
| 165 | + merge_commit_sha=f"abc{pr_num}def", |
| 166 | + head_ref=f"feature/test-{pr_num}", |
| 167 | + base_ref="main" |
| 168 | + ) |
| 169 | + db.upsert_pull_request(pr) |
| 170 | + test_data['prs'][pr_num] = pr |
| 171 | + |
| 172 | + # Create corresponding issues |
| 173 | + for issue_num in range(101, 111): |
| 174 | + issue = Issue( |
| 175 | + repo_id=repo_id, |
| 176 | + number=issue_num, |
| 177 | + key=f"#{issue_num}", # Issue key (e.g., "#101") |
| 178 | + title=f"Test Issue #{issue_num}", |
| 179 | + body=f"Test issue body for #{issue_num}", |
| 180 | + state="closed", |
| 181 | + labels=[Label(name="enhancement")], |
| 182 | + created_at="2024-01-01T00:00:00Z", |
| 183 | + closed_at="2024-01-01T00:00:00Z" |
| 184 | + ) |
| 185 | + db.upsert_issue(issue) |
| 186 | + test_data['issues'][issue_num] = issue |
| 187 | + |
| 188 | + yield db, repo_id, test_data |
| 189 | + |
| 190 | + |
| 191 | +@pytest.fixture |
| 192 | +def mock_github_api(): |
| 193 | + """ |
| 194 | + Patch GitHub API calls to avoid external dependencies. |
| 195 | +
|
| 196 | + Use as a context manager or decorator. |
| 197 | + """ |
| 198 | + with patch('release_tool.github_utils.GitHubClient') as mock_client_class: |
| 199 | + # Create a mock instance |
| 200 | + mock_instance = Mock() |
| 201 | + |
| 202 | + # Mock the class to return our instance |
| 203 | + mock_client_class.return_value = mock_instance |
| 204 | + |
| 205 | + # Setup default return values |
| 206 | + mock_instance.get_pull_request.return_value = None |
| 207 | + mock_instance.get_issue.return_value = None |
| 208 | + |
| 209 | + yield mock_instance |
0 commit comments