Add test files
This commit is contained in:
79
app/tests/test_validator.py
Normal file
79
app/tests/test_validator.py
Normal file
@@ -0,0 +1,79 @@
|
||||
"""Tests for validation module."""
|
||||
|
||||
import pytest
|
||||
from pathlib import Path
|
||||
import tempfile
|
||||
|
||||
|
||||
class TestValidator:
|
||||
"""Test cases for schema validation."""
|
||||
|
||||
def test_validate_missing_required(self):
|
||||
"""Test validation catches missing required variables."""
|
||||
from env_pro.core.validator import Validator, Schema, VarSchema
|
||||
|
||||
schema = Schema(variables={
|
||||
"DATABASE_URL": VarSchema(required=True),
|
||||
"API_KEY": VarSchema(required=True),
|
||||
})
|
||||
|
||||
validator = Validator(schema)
|
||||
errors = validator.validate({"DATABASE_URL": "some-url"})
|
||||
|
||||
error_keys = [e.key for e in errors]
|
||||
assert "API_KEY" in error_keys
|
||||
|
||||
def test_validate_pattern_mismatch(self):
|
||||
"""Test validation catches pattern mismatches."""
|
||||
from env_pro.core.validator import Validator, Schema, VarSchema
|
||||
|
||||
schema = Schema(variables={
|
||||
"PORT": VarSchema(type="int", pattern=r"^\d+$"),
|
||||
})
|
||||
|
||||
validator = Validator(schema)
|
||||
errors = validator.validate({"PORT": "abc"})
|
||||
|
||||
assert len(errors) > 0
|
||||
assert errors[0].key == "PORT"
|
||||
|
||||
def test_validate_min_length(self):
|
||||
"""Test validation checks minimum length."""
|
||||
from env_pro.core.validator import Validator, Schema, VarSchema
|
||||
|
||||
schema = Schema(variables={
|
||||
"PASSWORD": VarSchema(min_length=8),
|
||||
})
|
||||
|
||||
validator = Validator(schema)
|
||||
errors = validator.validate({"PASSWORD": "short"})
|
||||
|
||||
assert len(errors) > 0
|
||||
assert "PASSWORD" in [e.key for e in errors]
|
||||
|
||||
def test_from_file_no_schema(self, temp_dir):
|
||||
"""Test loading validator when no schema file exists."""
|
||||
from env_pro.core.validator import Validator
|
||||
|
||||
import os
|
||||
os.chdir(temp_dir)
|
||||
|
||||
validator = Validator.from_file()
|
||||
assert validator.schema is None
|
||||
|
||||
def test_from_file_with_schema(self, temp_dir):
|
||||
"""Test loading validator with schema file."""
|
||||
from env_pro.core.validator import Validator
|
||||
|
||||
schema_content = """variables:
|
||||
DATABASE_URL:
|
||||
type: url
|
||||
required: true
|
||||
"""
|
||||
(temp_dir / ".env.schema.yaml").write_text(schema_content)
|
||||
|
||||
import os
|
||||
os.chdir(temp_dir)
|
||||
|
||||
validator = Validator.from_file()
|
||||
assert validator.schema is not None
|
||||
Reference in New Issue
Block a user