|
| 1 | +"""Unit tests for package validation with schema version 1.2.2. |
| 2 | +
|
| 3 | +This module tests the validation functionality for packages using schema |
| 4 | +version 1.2.2, which introduces conda package manager support for Python |
| 5 | +dependencies. |
| 6 | +""" |
| 7 | + |
| 8 | +import unittest |
| 9 | +import json |
| 10 | +from pathlib import Path |
| 11 | +from typing import Dict |
| 12 | + |
| 13 | +# Add parent directory to path for imports |
| 14 | +import sys |
| 15 | +sys.path.insert(0, str(Path(__file__).parent.parent)) |
| 16 | + |
| 17 | +from hatch_validator.core.validation_context import ValidationContext |
| 18 | +from hatch_validator.core.validator_factory import ValidatorFactory |
| 19 | +from hatch_validator.core.pkg_accessor_factory import HatchPkgAccessorFactory |
| 20 | + |
| 21 | + |
| 22 | +class TestV122PackageValidation(unittest.TestCase): |
| 23 | + """Test cases for v1.2.2 package validation.""" |
| 24 | + |
| 25 | + @classmethod |
| 26 | + def setUpClass(cls): |
| 27 | + """Set up test fixtures.""" |
| 28 | + # Create minimal test registry data |
| 29 | + cls.registry_data = { |
| 30 | + "registry_schema_version": "1.0.0", |
| 31 | + "repositories": [] |
| 32 | + } |
| 33 | + |
| 34 | + def setUp(self): |
| 35 | + """Set up each test.""" |
| 36 | + self.context = ValidationContext( |
| 37 | + registry_data=self.registry_data, |
| 38 | + allow_local_dependencies=False, |
| 39 | + force_schema_update=False |
| 40 | + ) |
| 41 | + |
| 42 | + def test_valid_v122_package_with_conda_dependencies(self): |
| 43 | + """Test validation of valid v1.2.2 package with conda dependencies.""" |
| 44 | + metadata = { |
| 45 | + "package_schema_version": "1.2.2", |
| 46 | + "name": "test_conda_package", |
| 47 | + "version": "1.0.0", |
| 48 | + "description": "Test package with conda dependencies", |
| 49 | + "tags": ["test", "conda"], |
| 50 | + "author": {"name": "Test Author", "email": "test@example.com"}, |
| 51 | + "license": {"name": "MIT"}, |
| 52 | + "entry_point": { |
| 53 | + "mcp_server": "server.py", |
| 54 | + "hatch_mcp_server": "hatch_server.py" |
| 55 | + }, |
| 56 | + "dependencies": { |
| 57 | + "python": [ |
| 58 | + { |
| 59 | + "name": "numpy", |
| 60 | + "version_constraint": ">=1.20.0", |
| 61 | + "package_manager": "conda", |
| 62 | + "channel": "conda-forge" |
| 63 | + }, |
| 64 | + { |
| 65 | + "name": "scipy", |
| 66 | + "version_constraint": ">=1.7.0", |
| 67 | + "package_manager": "conda", |
| 68 | + "channel": "bioconda" |
| 69 | + } |
| 70 | + ] |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + validator = ValidatorFactory.create_validator_chain("1.2.2") |
| 75 | + is_valid, errors = validator.validate(metadata, self.context) |
| 76 | + |
| 77 | + # Note: This will fail schema validation until we have the actual files |
| 78 | + # but it tests the validator chain construction |
| 79 | + self.assertIsNotNone(validator) |
| 80 | + |
| 81 | + def test_valid_v122_package_with_pip_dependencies(self): |
| 82 | + """Test validation of valid v1.2.2 package with pip dependencies (backward compatibility).""" |
| 83 | + metadata = { |
| 84 | + "package_schema_version": "1.2.2", |
| 85 | + "name": "test_pip_package", |
| 86 | + "version": "1.0.0", |
| 87 | + "description": "Test package with pip dependencies", |
| 88 | + "tags": ["test", "pip"], |
| 89 | + "author": {"name": "Test Author"}, |
| 90 | + "license": {"name": "MIT"}, |
| 91 | + "entry_point": { |
| 92 | + "mcp_server": "server.py", |
| 93 | + "hatch_mcp_server": "hatch_server.py" |
| 94 | + }, |
| 95 | + "dependencies": { |
| 96 | + "python": [ |
| 97 | + { |
| 98 | + "name": "requests", |
| 99 | + "version_constraint": ">=2.28.0", |
| 100 | + "package_manager": "pip" |
| 101 | + } |
| 102 | + ] |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + validator = ValidatorFactory.create_validator_chain("1.2.2") |
| 107 | + is_valid, errors = validator.validate(metadata, self.context) |
| 108 | + |
| 109 | + self.assertIsNotNone(validator) |
| 110 | + |
| 111 | + def test_valid_v122_package_with_mixed_dependencies(self): |
| 112 | + """Test validation of valid v1.2.2 package with mixed pip and conda dependencies.""" |
| 113 | + metadata = { |
| 114 | + "package_schema_version": "1.2.2", |
| 115 | + "name": "test_mixed_package", |
| 116 | + "version": "1.0.0", |
| 117 | + "description": "Test package with mixed dependencies", |
| 118 | + "tags": ["test", "mixed"], |
| 119 | + "author": {"name": "Test Author"}, |
| 120 | + "license": {"name": "MIT"}, |
| 121 | + "entry_point": { |
| 122 | + "mcp_server": "server.py", |
| 123 | + "hatch_mcp_server": "hatch_server.py" |
| 124 | + }, |
| 125 | + "dependencies": { |
| 126 | + "python": [ |
| 127 | + { |
| 128 | + "name": "requests", |
| 129 | + "version_constraint": ">=2.28.0", |
| 130 | + "package_manager": "pip" |
| 131 | + }, |
| 132 | + { |
| 133 | + "name": "numpy", |
| 134 | + "version_constraint": ">=1.20.0", |
| 135 | + "package_manager": "conda", |
| 136 | + "channel": "conda-forge" |
| 137 | + } |
| 138 | + ] |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + validator = ValidatorFactory.create_validator_chain("1.2.2") |
| 143 | + is_valid, errors = validator.validate(metadata, self.context) |
| 144 | + |
| 145 | + self.assertIsNotNone(validator) |
| 146 | + |
| 147 | + def test_invalid_channel_for_pip_package(self): |
| 148 | + """Test that channel specification for pip package is invalid.""" |
| 149 | + from hatch_validator.package.v1_2_2.dependency_validation import DependencyValidation |
| 150 | + |
| 151 | + dep_validation = DependencyValidation() |
| 152 | + |
| 153 | + # Pip package with channel should fail |
| 154 | + dep = { |
| 155 | + "name": "requests", |
| 156 | + "version_constraint": ">=2.28.0", |
| 157 | + "package_manager": "pip", |
| 158 | + "channel": "conda-forge" # Invalid for pip |
| 159 | + } |
| 160 | + |
| 161 | + is_valid, errors = dep_validation._validate_single_python_dependency(dep, self.context) |
| 162 | + |
| 163 | + self.assertFalse(is_valid) |
| 164 | + self.assertTrue(any("Channel" in error and "pip" in error for error in errors)) |
| 165 | + |
| 166 | + def test_invalid_channel_format(self): |
| 167 | + """Test that invalid channel format is rejected.""" |
| 168 | + from hatch_validator.package.v1_2_2.dependency_validation import DependencyValidation |
| 169 | + |
| 170 | + dep_validation = DependencyValidation() |
| 171 | + |
| 172 | + # Conda package with invalid channel format |
| 173 | + dep = { |
| 174 | + "name": "numpy", |
| 175 | + "version_constraint": ">=1.20.0", |
| 176 | + "package_manager": "conda", |
| 177 | + "channel": "invalid channel!" # Invalid format (contains space and !) |
| 178 | + } |
| 179 | + |
| 180 | + is_valid, errors = dep_validation._validate_single_python_dependency(dep, self.context) |
| 181 | + |
| 182 | + self.assertFalse(is_valid) |
| 183 | + self.assertTrue(any("channel format" in error.lower() for error in errors)) |
| 184 | + |
| 185 | + def test_valid_channel_formats(self): |
| 186 | + """Test that valid channel formats are accepted.""" |
| 187 | + from hatch_validator.package.v1_2_2.dependency_validation import DependencyValidation |
| 188 | + |
| 189 | + dep_validation = DependencyValidation() |
| 190 | + |
| 191 | + valid_channels = ["conda-forge", "bioconda", "colomoto", "my_channel", "channel123"] |
| 192 | + |
| 193 | + for channel in valid_channels: |
| 194 | + dep = { |
| 195 | + "name": "numpy", |
| 196 | + "version_constraint": ">=1.20.0", |
| 197 | + "package_manager": "conda", |
| 198 | + "channel": channel |
| 199 | + } |
| 200 | + |
| 201 | + is_valid, errors = dep_validation._validate_single_python_dependency(dep, self.context) |
| 202 | + |
| 203 | + # Should be valid (no channel format errors) |
| 204 | + channel_format_errors = [e for e in errors if "channel format" in e.lower()] |
| 205 | + self.assertEqual(len(channel_format_errors), 0, |
| 206 | + f"Channel '{channel}' should be valid but got errors: {channel_format_errors}") |
| 207 | + |
| 208 | + def test_invalid_package_manager(self): |
| 209 | + """Test that invalid package_manager value is rejected.""" |
| 210 | + from hatch_validator.package.v1_2_2.dependency_validation import DependencyValidation |
| 211 | + |
| 212 | + dep_validation = DependencyValidation() |
| 213 | + |
| 214 | + dep = { |
| 215 | + "name": "numpy", |
| 216 | + "version_constraint": ">=1.20.0", |
| 217 | + "package_manager": "apt" # Invalid - only pip or conda allowed |
| 218 | + } |
| 219 | + |
| 220 | + is_valid, errors = dep_validation._validate_single_python_dependency(dep, self.context) |
| 221 | + |
| 222 | + self.assertFalse(is_valid) |
| 223 | + self.assertTrue(any("package_manager" in error and "apt" in error for error in errors)) |
| 224 | + |
| 225 | + def test_conda_package_without_channel(self): |
| 226 | + """Test that conda package without channel is valid (channel is optional).""" |
| 227 | + from hatch_validator.package.v1_2_2.dependency_validation import DependencyValidation |
| 228 | + |
| 229 | + dep_validation = DependencyValidation() |
| 230 | + |
| 231 | + dep = { |
| 232 | + "name": "numpy", |
| 233 | + "version_constraint": ">=1.20.0", |
| 234 | + "package_manager": "conda" |
| 235 | + # No channel specified - should be valid |
| 236 | + } |
| 237 | + |
| 238 | + is_valid, errors = dep_validation._validate_single_python_dependency(dep, self.context) |
| 239 | + |
| 240 | + # Should be valid (channel is optional) |
| 241 | + self.assertTrue(is_valid, f"Conda package without channel should be valid, but got errors: {errors}") |
| 242 | + |
| 243 | + def test_default_package_manager_is_pip(self): |
| 244 | + """Test that package_manager defaults to pip when not specified.""" |
| 245 | + from hatch_validator.package.v1_2_2.dependency_validation import DependencyValidation |
| 246 | + |
| 247 | + dep_validation = DependencyValidation() |
| 248 | + |
| 249 | + dep = { |
| 250 | + "name": "requests", |
| 251 | + "version_constraint": ">=2.28.0" |
| 252 | + # No package_manager specified - should default to pip |
| 253 | + } |
| 254 | + |
| 255 | + is_valid, errors = dep_validation._validate_single_python_dependency(dep, self.context) |
| 256 | + |
| 257 | + # Should be valid (defaults to pip) |
| 258 | + self.assertTrue(is_valid, f"Package without package_manager should default to pip, but got errors: {errors}") |
| 259 | + |
| 260 | + |
| 261 | +class TestV122AccessorChain(unittest.TestCase): |
| 262 | + """Test cases for v1.2.2 accessor chain.""" |
| 263 | + |
| 264 | + def test_accessor_chain_construction(self): |
| 265 | + """Test that v1.2.2 accessor chain is constructed correctly.""" |
| 266 | + accessor = HatchPkgAccessorFactory.create_accessor_chain("1.2.2") |
| 267 | + |
| 268 | + self.assertIsNotNone(accessor) |
| 269 | + self.assertTrue(accessor.can_handle("1.2.2")) |
| 270 | + |
| 271 | + def test_accessor_delegates_to_v121(self): |
| 272 | + """Test that v1.2.2 accessor delegates to v1.2.1 for unchanged operations.""" |
| 273 | + accessor = HatchPkgAccessorFactory.create_accessor_chain("1.2.2") |
| 274 | + |
| 275 | + metadata = { |
| 276 | + "package_schema_version": "1.2.2", |
| 277 | + "name": "test_package", |
| 278 | + "version": "1.0.0", |
| 279 | + "entry_point": { |
| 280 | + "mcp_server": "server.py", |
| 281 | + "hatch_mcp_server": "hatch_server.py" |
| 282 | + } |
| 283 | + } |
| 284 | + |
| 285 | + # Test that accessor can access entry points (delegated to v1.2.1) |
| 286 | + mcp_entry = accessor.get_mcp_entry_point(metadata) |
| 287 | + self.assertEqual(mcp_entry, "server.py") |
| 288 | + |
| 289 | + hatch_mcp_entry = accessor.get_hatch_mcp_entry_point(metadata) |
| 290 | + self.assertEqual(hatch_mcp_entry, "hatch_server.py") |
| 291 | + |
| 292 | + |
| 293 | +class TestV122ValidatorChain(unittest.TestCase): |
| 294 | + """Test cases for v1.2.2 validator chain.""" |
| 295 | + |
| 296 | + def test_validator_chain_construction(self): |
| 297 | + """Test that v1.2.2 validator chain is constructed correctly.""" |
| 298 | + validator = ValidatorFactory.create_validator_chain("1.2.2") |
| 299 | + |
| 300 | + self.assertIsNotNone(validator) |
| 301 | + self.assertTrue(validator.can_handle("1.2.2")) |
| 302 | + |
| 303 | + def test_validator_chain_includes_all_versions(self): |
| 304 | + """Test that v1.2.2 validator chain includes all previous versions.""" |
| 305 | + validator = ValidatorFactory.create_validator_chain("1.2.2") |
| 306 | + |
| 307 | + # Check chain includes v1.2.2, v1.2.1, v1.2.0, v1.1.0 |
| 308 | + current = validator |
| 309 | + versions_in_chain = [] |
| 310 | + |
| 311 | + while current: |
| 312 | + if hasattr(current, 'can_handle'): |
| 313 | + # Find which version this validator handles |
| 314 | + for version in ["1.2.2", "1.2.1", "1.2.0", "1.1.0"]: |
| 315 | + if current.can_handle(version): |
| 316 | + versions_in_chain.append(version) |
| 317 | + break |
| 318 | + current = getattr(current, 'next_validator', None) |
| 319 | + |
| 320 | + self.assertIn("1.2.2", versions_in_chain) |
| 321 | + self.assertIn("1.2.1", versions_in_chain) |
| 322 | + self.assertIn("1.2.0", versions_in_chain) |
| 323 | + self.assertIn("1.1.0", versions_in_chain) |
| 324 | + |
| 325 | + |
| 326 | +if __name__ == '__main__': |
| 327 | + unittest.main() |
| 328 | + |
| 329 | + |
0 commit comments