115 lines
3.5 KiB
Python
115 lines
3.5 KiB
Python
"""
|
|
Tests for data processors.
|
|
"""
|
|
import pytest
|
|
from data_processors.validator import DataValidator
|
|
from data_processors.storage import DataStorage
|
|
import tempfile
|
|
import json
|
|
from pathlib import Path
|
|
|
|
|
|
class TestDataValidator:
|
|
"""Test DataValidator class."""
|
|
|
|
def test_validate_email(self):
|
|
"""Test email validation."""
|
|
assert DataValidator.validate_email("test@example.com") is True
|
|
assert DataValidator.validate_email("invalid-email") is False
|
|
assert DataValidator.validate_email("test@.com") is False
|
|
|
|
def test_validate_url(self):
|
|
"""Test URL validation."""
|
|
assert DataValidator.validate_url("https://example.com") is True
|
|
assert DataValidator.validate_url("http://test.com/path") is True
|
|
assert DataValidator.validate_url("not-a-url") is False
|
|
|
|
def test_validate_required_fields(self):
|
|
"""Test required fields validation."""
|
|
data = {"name": "John", "email": "john@example.com", "age": ""}
|
|
required = ["name", "email", "age", "phone"]
|
|
|
|
result = DataValidator.validate_required_fields(data, required)
|
|
|
|
assert result["valid"] is False
|
|
assert "phone" in result["missing_fields"]
|
|
assert "age" in result["empty_fields"]
|
|
|
|
def test_clean_text(self):
|
|
"""Test text cleaning."""
|
|
text = " Multiple spaces and\n\nnewlines "
|
|
cleaned = DataValidator.clean_text(text)
|
|
|
|
assert cleaned == "Multiple spaces and newlines"
|
|
|
|
def test_sanitize_data(self):
|
|
"""Test data sanitization."""
|
|
data = {
|
|
"name": " John Doe ",
|
|
"email": "john@example.com",
|
|
"nested": {
|
|
"value": " test "
|
|
}
|
|
}
|
|
|
|
sanitized = DataValidator.sanitize_data(data)
|
|
|
|
assert sanitized["name"] == "John Doe"
|
|
assert sanitized["nested"]["value"] == "test"
|
|
|
|
|
|
class TestDataStorage:
|
|
"""Test DataStorage class."""
|
|
|
|
@pytest.fixture
|
|
def temp_storage(self):
|
|
"""Create temporary storage directory."""
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
yield DataStorage(output_dir=Path(tmpdir))
|
|
|
|
def test_save_json(self, temp_storage):
|
|
"""Test JSON saving."""
|
|
data = {"name": "Test", "value": 123}
|
|
filepath = temp_storage.save_json(data, "test.json")
|
|
|
|
assert filepath.exists()
|
|
|
|
with open(filepath, 'r') as f:
|
|
loaded = json.load(f)
|
|
|
|
assert loaded == data
|
|
|
|
def test_save_csv(self, temp_storage):
|
|
"""Test CSV saving."""
|
|
data = [
|
|
{"name": "John", "age": 30},
|
|
{"name": "Jane", "age": 25}
|
|
]
|
|
filepath = temp_storage.save_csv(data, "test.csv")
|
|
|
|
assert filepath.exists()
|
|
|
|
def test_save_text(self, temp_storage):
|
|
"""Test text saving."""
|
|
content = "This is a test"
|
|
filepath = temp_storage.save_text(content, "test.txt")
|
|
|
|
assert filepath.exists()
|
|
|
|
with open(filepath, 'r') as f:
|
|
loaded = f.read()
|
|
|
|
assert loaded == content
|
|
|
|
def test_timestamped_filename(self, temp_storage):
|
|
"""Test timestamped filename generation."""
|
|
filename = temp_storage.create_timestamped_filename("data", "json")
|
|
|
|
assert filename.startswith("data_")
|
|
assert filename.endswith(".json")
|
|
assert len(filename) > 15 # Has timestamp
|
|
|
|
|
|
if __name__ == "__main__":
|
|
pytest.main([__file__, "-v"])
|
|
|