- Fixed undefined 'tool' variable in display_history function - Changed '[tool]' markup tag usage to proper Rich syntax - All tests now pass (38/38 unit tests) - Type checking passes with mypy --strict
183 lines
6.2 KiB
Python
183 lines
6.2 KiB
Python
"""Tests for validation module."""
|
|
|
|
import pytest
|
|
from pathlib import Path
|
|
|
|
|
|
class TestSchemaValidation:
|
|
"""Test cases for schema validation."""
|
|
|
|
def test_load_empty_schema(self, temp_dir):
|
|
"""Test loading schema when no file exists."""
|
|
from env_pro.core.validator import load_schema
|
|
|
|
schema = load_schema(temp_dir)
|
|
assert schema is None
|
|
|
|
def test_load_schema(self, temp_dir):
|
|
"""Test loading a valid schema."""
|
|
from env_pro.core.validator import load_schema, EnvSchema
|
|
|
|
schema_file = temp_dir / ".env.schema.yaml"
|
|
schema_file.write_text("""
|
|
variables:
|
|
DATABASE_URL:
|
|
type: url
|
|
required: true
|
|
description: Database connection URL
|
|
DEBUG:
|
|
type: bool
|
|
default: false
|
|
""")
|
|
|
|
schema = load_schema(temp_dir)
|
|
assert schema is not None
|
|
assert "DATABASE_URL" in schema.variables
|
|
assert schema.variables["DATABASE_URL"].type == "url"
|
|
assert schema.variables["DATABASE_URL"].required
|
|
|
|
def test_validate_valid_string(self):
|
|
"""Test validation of valid string value."""
|
|
from env_pro.core.validator import validate_value, VariableSchema
|
|
|
|
schema = VariableSchema(type="string")
|
|
errors = validate_value("TEST_VAR", "some-value", schema)
|
|
assert errors == []
|
|
|
|
def test_validate_required_missing(self):
|
|
"""Test validation when required variable is missing."""
|
|
from env_pro.core.validator import validate_value, VariableSchema
|
|
|
|
schema = VariableSchema(type="string", required=True)
|
|
errors = validate_value("TEST_VAR", "", schema)
|
|
assert len(errors) > 0
|
|
assert "required" in errors[0].lower()
|
|
|
|
def test_validate_int_type(self):
|
|
"""Test validation of integer type."""
|
|
from env_pro.core.validator import validate_value, VariableSchema
|
|
|
|
schema = VariableSchema(type="int")
|
|
errors = validate_value("PORT", "8080", schema)
|
|
assert errors == []
|
|
|
|
errors = validate_value("PORT", "not-a-number", schema)
|
|
assert len(errors) > 0
|
|
|
|
def test_validate_bool_type(self):
|
|
"""Test validation of boolean type."""
|
|
from env_pro.core.validator import validate_value, VariableSchema
|
|
|
|
schema = VariableSchema(type="bool")
|
|
errors = validate_value("DEBUG", "true", schema)
|
|
assert errors == []
|
|
|
|
errors = validate_value("DEBUG", "yes", schema)
|
|
assert errors == []
|
|
|
|
def test_validate_email_type(self):
|
|
"""Test validation of email type."""
|
|
from env_pro.core.validator import validate_value, VariableSchema
|
|
|
|
schema = VariableSchema(type="email")
|
|
errors = validate_value("EMAIL", "user@example.com", schema)
|
|
assert errors == []
|
|
|
|
errors = validate_value("EMAIL", "invalid-email", schema)
|
|
assert len(errors) > 0
|
|
|
|
def test_validate_url_type(self):
|
|
"""Test validation of URL type."""
|
|
from env_pro.core.validator import validate_value, VariableSchema
|
|
|
|
schema = VariableSchema(type="url")
|
|
errors = validate_value("API_URL", "https://api.example.com", schema)
|
|
assert errors == []
|
|
|
|
errors = validate_value("API_URL", "not-a-url", schema)
|
|
assert len(errors) > 0
|
|
|
|
def test_validate_pattern(self):
|
|
"""Test validation with regex pattern."""
|
|
from env_pro.core.validator import validate_value, VariableSchema
|
|
|
|
schema = VariableSchema(type="string", pattern=r"^[A-Z]+$")
|
|
errors = validate_value("PREFIX", "ABC123", schema)
|
|
assert len(errors) > 0
|
|
|
|
errors = validate_value("PREFIX", "ABC", schema)
|
|
assert errors == []
|
|
|
|
def test_validate_enum(self):
|
|
"""Test validation with enum values."""
|
|
from env_pro.core.validator import validate_value, VariableSchema
|
|
|
|
schema = VariableSchema(type="string", enum=["dev", "staging", "prod"])
|
|
errors = validate_value("ENV", "dev", schema)
|
|
assert errors == []
|
|
|
|
errors = validate_value("ENV", "invalid", schema)
|
|
assert len(errors) > 0
|
|
|
|
def test_validate_min_max_length(self):
|
|
"""Test validation with min/max length constraints."""
|
|
from env_pro.core.validator import validate_value, VariableSchema
|
|
|
|
schema = VariableSchema(type="string", min_length=3, max_length=10)
|
|
errors = validate_value("NAME", "ab", schema)
|
|
assert len(errors) > 0
|
|
|
|
errors = validate_value("NAME", "abcdefghijkl", schema)
|
|
assert len(errors) > 0
|
|
|
|
errors = validate_value("NAME", "valid", schema)
|
|
assert errors == []
|
|
|
|
def test_validate_min_max_number(self):
|
|
"""Test validation with min/max number constraints."""
|
|
from env_pro.core.validator import validate_value, VariableSchema
|
|
|
|
schema = VariableSchema(type="int", minimum=1, maximum=100)
|
|
errors = validate_value("PORT", "0", schema)
|
|
assert len(errors) > 0
|
|
|
|
errors = validate_value("PORT", "101", schema)
|
|
assert len(errors) > 0
|
|
|
|
errors = validate_value("PORT", "50", schema)
|
|
assert errors == []
|
|
|
|
def test_validate_env_vars_full(self):
|
|
"""Test full environment validation."""
|
|
from env_pro.core.validator import validate_env_vars, EnvSchema, VariableSchema
|
|
|
|
schema = EnvSchema(variables={
|
|
"DATABASE_URL": VariableSchema(type="string", required=True),
|
|
"DEBUG": VariableSchema(type="bool", default="false")
|
|
})
|
|
|
|
vars_dict = {
|
|
"DATABASE_URL": "postgresql://localhost:5432/db",
|
|
"DEBUG": "true"
|
|
}
|
|
|
|
errors = validate_env_vars(vars_dict, schema)
|
|
assert errors == []
|
|
|
|
def test_check_required_vars(self):
|
|
"""Test checking for missing required variables."""
|
|
from env_pro.core.validator import check_required_vars, EnvSchema, VariableSchema
|
|
|
|
schema = EnvSchema(variables={
|
|
"REQUIRED_VAR": VariableSchema(type="string", required=True),
|
|
"OPTIONAL_VAR": VariableSchema(type="string", required=False)
|
|
})
|
|
|
|
vars_dict = {"OPTIONAL_VAR": "value"}
|
|
missing = check_required_vars(vars_dict, schema)
|
|
assert "REQUIRED_VAR" in missing
|
|
|
|
vars_dict = {"REQUIRED_VAR": "value"}
|
|
missing = check_required_vars(vars_dict, schema)
|
|
assert len(missing) == 0
|