|
| 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