From dc2160a0c90e69573c13f270deff12e6a1d48e94 Mon Sep 17 00:00:00 2001 From: han0 Date: Thu, 19 Nov 2020 09:29:28 +0800 Subject: [PATCH] test: validate --- nc_http/tests/validate.py | 45 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 nc_http/tests/validate.py diff --git a/nc_http/tests/validate.py b/nc_http/tests/validate.py new file mode 100644 index 0000000..75a25b5 --- /dev/null +++ b/nc_http/tests/validate.py @@ -0,0 +1,45 @@ +import unittest + +from nc_http.tools.validate import DataFormatValidator + + +class ValidateTestCase(unittest.TestCase): + test_rules = { + "id": "int|choices:2,3,4,5", + "name": "required", + "create_time": "int|range:,1000000", + "remark": "required|str|max:5|min:1", + "tags": "list:url", + "avatar": "url", + "email": "email", + "mobile": "mobile", + "birthday": "datetime:%Y-%m-%d %H:%M:%S", + "ip": "ip", + "department": "dict", + "code": "number" + } + + test_data = { + "id": 4, + "name": "aaa", + "create_time": 111111, + "remark": "中国123", + "tags": (u'http://baidu.com', 'http://baidu.com'), + "avatar": "https://aaa111.com/a.php?a=b", + "email": "ruyi111@sina.com", + "mobile": "15880445400", + "birthday": "2018-10-12 12:00:00", + "ip": "111.1.1.1", + "department": {"a": "b"}, + "code": '11.111' + } + + def test_create(self): + validator = DataFormatValidator(self.test_rules) + result = validator.check(self.test_data) + validator.get_error() + self.assertEqual(result, True) + + +if __name__ == '__main__': + unittest.main()