Skip to content

Commit f514af9

Browse files
authored
Merge pull request #9 from Attumm/add_readme_extended_types
Added readme for extended tests plus tests
2 parents c6d76f5 + 9e1714e commit f514af9

File tree

4 files changed

+150
-2
lines changed

4 files changed

+150
-2
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,38 @@ Nesting is done without any performance hit.
125125
}
126126
```
127127

128+
129+
## Extending Maat with custom validation
130+
```python
131+
>>> from maat import types
132+
133+
134+
>>> def datetime_parse(val, key, formats="%Y-%m-%dT%H:%M:%S.%f", *args, **kwargs):
135+
""" uses to parse iso format 'formats': '%Y-%m-%dT%H:%M:%S.%f'"""
136+
try:
137+
return datetime.strptime(val, formats)
138+
except Exception as e:
139+
raise Invalid(f'key: "{key}" contains invalid item')
140+
141+
>>> types['custom_datetime'] = datetime_parse
142+
143+
>>> input = {
144+
"created": "2022-01-28T15:01:46.0000",
145+
}
146+
147+
148+
>>> validation = {
149+
"created": {
150+
"type": "custom_datetime",
151+
}
152+
}
153+
154+
>>> validate(input, validation)
155+
{'created': datetime.datetime(2022, 1, 28, 15, 1, 46)}
156+
157+
```
158+
159+
128160
## Installation
129161
```sh
130162
pip install maat

maat/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
VERSION = "3.0.4"
1+
VERSION = "3.0.7"

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
name='Maat',
1010
author='Melvin Bijman',
1111
author_email='[email protected]',
12-
version='3.0.6',
12+
version='3.0.7',
1313
license='MIT',
1414

1515
py_modules=['maat'],

tests/test_extend_types.py

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import os
2+
import sys
3+
import uuid
4+
from datetime import datetime
5+
import unittest
6+
7+
from hypothesis import given
8+
import hypothesis.strategies as st
9+
10+
from deepdiff import DeepDiff as ddiff
11+
12+
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../")))
13+
14+
from maat import validate
15+
16+
from maat import types
17+
18+
from maat import protected
19+
from maat.exceptions import Invalid
20+
21+
22+
def datetime_parse(val, key, formats="%Y-%m-%dT%H:%M:%S.%f", *args, **kwargs):
23+
""" uses to parse iso format 'formats': '%Y-%m-%dT%H:%M:%S.%f'"""
24+
try:
25+
return datetime.strptime(val, formats)
26+
except Exception as e:
27+
raise Invalid(f'key: "{key}" contains invalid item "{type(val).__name__}": unable to convert from type')
28+
29+
30+
class TestExtendMaat(unittest.TestCase):
31+
32+
def test_extend_maat_datetime_happy_path(self):
33+
"""Happy path test, Maat c"""
34+
35+
types['custom_datetime'] = datetime_parse
36+
37+
test_input = {
38+
"created": "2022-01-28T15:01:46.0000",
39+
}
40+
test_validation = {
41+
"created": {
42+
"type": "custom_datetime",
43+
}
44+
}
45+
expected = {"created": datetime(2022, 1, 28, 15, 1, 46)}
46+
47+
validated_items = validate(test_input, test_validation)
48+
difference = ddiff(validated_items, expected)
49+
# if the differ finds no difference a empty dictionary is returned
50+
self.assertEqual(difference, {})
51+
52+
def test_extend_maat_datetime_invalid_input(self):
53+
"""Test with invalid time string, this string doens't adhere to full iso format.
54+
It only contains a date.
55+
"""
56+
57+
types['custom_datetime'] = datetime_parse
58+
59+
test_input = {
60+
"created": "2022-01-28",
61+
}
62+
test_validation = {
63+
"created": {
64+
"type": "custom_datetime",
65+
}
66+
}
67+
68+
with self.assertRaisesRegex(Invalid, 'key: "created" contains invalid item "str": unable to convert from type'):
69+
_ = validate(test_input, test_validation)
70+
71+
def test_extend_maat_datetime_with_argument(self):
72+
"""Test with invalid time string, this string doens't adhere to full iso format.
73+
It only contains a date.
74+
"""
75+
76+
types['custom_datetime'] = datetime_parse
77+
78+
test_input = {
79+
"created": "2022-01-28",
80+
}
81+
test_validation = {
82+
"created": {
83+
"type": "custom_datetime",
84+
"formats": "%Y-%m-%d",
85+
}
86+
}
87+
88+
expected = {"created": datetime(2022, 1, 28)}
89+
90+
validated_items = validate(test_input, test_validation)
91+
difference = ddiff(validated_items, expected)
92+
# if the differ finds no difference a empty dictionary is returned
93+
self.assertEqual(difference, {})
94+
95+
def test_extend_maat_datetime_invalid_input_int(self):
96+
"""Test with invalid time string, this string doens't adhere to full iso format.
97+
It only contains a date.
98+
"""
99+
100+
types['custom_datetime'] = datetime_parse
101+
102+
test_input = {
103+
"created": 161234344,
104+
}
105+
test_validation = {
106+
"created": {
107+
"type": "custom_datetime",
108+
"formats": "%Y-%m-%d",
109+
}
110+
}
111+
112+
with self.assertRaisesRegex(Invalid, 'key: "created" contains invalid item "int": unable to convert from type'):
113+
_ = validate(test_input, test_validation)
114+
115+
if __name__ == "__main__":
116+
unittest.main()

0 commit comments

Comments
 (0)