-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_validators.py
More file actions
83 lines (71 loc) · 3.15 KB
/
Copy pathtest_validators.py
File metadata and controls
83 lines (71 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""
Basic sanity unit tests for validators using unittest framework.
"""
import unittest
from datetime import datetime
from app.utils.logger import setup_logger
from app.utils.validators import SensorRecord, CSVRowValidator
from pydantic import ValidationError
logger = setup_logger(__name__)
class TestSensorRecord(unittest.TestCase):
"""Test SensorRecord validation."""
def test_valid_sensor_record(self):
"""Test creating a valid sensor record."""
logger.info(f"[{self._testMethodName}] Testing valid sensor record creation")
record = SensorRecord(
sensor_id="sensor_001",
timestamp="2024-01-01T12:00:00",
value=25.5
)
self.assertEqual(record.sensor_id, "sensor_001")
self.assertIsInstance(record.timestamp, datetime)
self.assertEqual(record.value, 25.5)
logger.info(f"[{self._testMethodName}] Valid sensor record test passed")
def test_invalid_empty_sensor_id(self):
"""Test that empty sensor_id raises validation error."""
logger.info(f"[{self._testMethodName}] Testing empty sensor_id validation")
with self.assertRaises(ValidationError):
SensorRecord(
sensor_id="",
timestamp="2024-01-01T12:00:00",
value=25.5
)
logger.info(f"[{self._testMethodName}] Empty sensor_id validation test passed")
def test_invalid_timestamp(self):
"""Test that invalid timestamp raises validation error."""
logger.info(f"[{self._testMethodName}] Testing invalid timestamp validation")
with self.assertRaises(ValidationError):
SensorRecord(
sensor_id="sensor_001",
timestamp="invalid-date",
value=25.5
)
logger.info(f"[{self._testMethodName}] Invalid timestamp validation test passed")
class TestCSVRowValidator(unittest.TestCase):
"""Test CSVRowValidator functionality."""
def test_validate_valid_row(self):
"""Test validating a valid CSV row."""
logger.info(f"[{self._testMethodName}] Testing valid CSV row validation")
row = {
'sensor_id': 'sensor_001',
'timestamp': '2024-01-01T12:00:00',
'value': '25.5'
}
is_valid, error_msg = CSVRowValidator.validate_csv_row(row, 1)
self.assertTrue(is_valid)
self.assertIsNone(error_msg)
logger.info(f"[{self._testMethodName}] Valid CSV row validation test passed")
def test_validate_invalid_row(self):
"""Test validating an invalid CSV row."""
logger.info(f"[{self._testMethodName}] Testing invalid CSV row validation")
row = {
'sensor_id': '',
'timestamp': '2024-01-01T12:00:00',
'value': '25.5'
}
is_valid, error_msg = CSVRowValidator.validate_csv_row(row, 1)
self.assertFalse(is_valid)
self.assertIsNotNone(error_msg)
logger.info(f"[{self._testMethodName}] Invalid CSV row validation test passed")
if __name__ == '__main__':
unittest.main()