-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_from_dict_validation.py
More file actions
65 lines (54 loc) · 2.46 KB
/
Copy pathtest_from_dict_validation.py
File metadata and controls
65 lines (54 loc) · 2.46 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
from __future__ import annotations
import os
import sys
import unittest
REPO_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
if REPO_ROOT not in sys.path:
sys.path.insert(0, REPO_ROOT)
from models.errors import SchemaError
from models.from_dict_validation import (
require_non_empty_str_field,
require_non_empty_str_fields,
)
class RequireNonEmptyStrFieldMessages(unittest.TestCase):
def test_absent_key_raises_missing_required_field(self) -> None:
with self.assertRaises(SchemaError) as cm:
require_non_empty_str_field({}, "composerId", model="TestModel")
self.assertEqual(cm.exception.field, "composerId")
self.assertIn("missing required field", str(cm.exception))
self.assertNotIn("invalid field", str(cm.exception))
def test_wrong_type_raises_invalid_field(self) -> None:
with self.assertRaises(SchemaError) as cm:
require_non_empty_str_field(
{"composerId": 123},
"composerId",
model="TestModel",
)
self.assertEqual(cm.exception.field, "composerId")
self.assertIn("invalid field", str(cm.exception))
self.assertIn("expected non-empty str, got int", str(cm.exception))
self.assertNotIn("missing required field", str(cm.exception))
class RequireNonEmptyStrFieldsMessages(unittest.TestCase):
def test_absent_key_raises_missing_required_field(self) -> None:
with self.assertRaises(SchemaError) as cm:
require_non_empty_str_fields(
{"title": "x", "workspace": "w"},
("log_id", "title", "workspace"),
model="ExportEntry",
)
self.assertEqual(cm.exception.field, "log_id")
self.assertIn("missing required field", str(cm.exception))
self.assertNotIn("invalid field", str(cm.exception))
def test_wrong_type_raises_invalid_field(self) -> None:
with self.assertRaises(SchemaError) as cm:
require_non_empty_str_fields(
{"log_id": 1, "title": "x", "workspace": "w"},
("log_id", "title", "workspace"),
model="ExportEntry",
)
self.assertEqual(cm.exception.field, "log_id")
self.assertIn("invalid field", str(cm.exception))
self.assertIn("expected non-empty str, got int", str(cm.exception))
self.assertNotIn("missing required field", str(cm.exception))
if __name__ == "__main__":
unittest.main()