From 19851722a80dfeb77fce6a607e28af56ff095907 Mon Sep 17 00:00:00 2001 From: Seth Wang Date: Wed, 3 Feb 2021 11:38:14 +0800 Subject: [PATCH] add DictStr --- believe/__init__.py | 1 + believe/dict_matcher.py | 9 +++++++++ believe/str_matcher.py | 1 + tests/test_believe.py | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 47 insertions(+) diff --git a/believe/__init__.py b/believe/__init__.py index 930c116..6db04fc 100644 --- a/believe/__init__.py +++ b/believe/__init__.py @@ -5,6 +5,7 @@ from .internal import validate from .dict_matcher import Dict +from .dict_matcher import DictStr from .dict_matcher import DictOf from .dict_matcher import Optional diff --git a/believe/dict_matcher.py b/believe/dict_matcher.py index 7fbefe1..07c93b2 100644 --- a/believe/dict_matcher.py +++ b/believe/dict_matcher.py @@ -1,3 +1,4 @@ +import json from .internal import BelieveBase from .internal import validate @@ -24,6 +25,14 @@ def validate(self, rhs, e_path=""): else: self.raise_validate_error(rhs, e_path=e_path, e_msg="unknown_field", e_unsafe_msg=f'unknown_field: {k}') +class DictStr(Dict): + def validate(self, rhs, e_path=""): + try: + rhs_dict = json.loads(rhs) + except: + self.raise_validate_error(rhs, e_path=e_path, e_msg="not_dict_string") + super().validate(rhs_dict, e_path) + class DictOf(BelieveBase): def initialize(self, key, value, n_item=None, min_item=None, max_item=None): self.key = key diff --git a/believe/str_matcher.py b/believe/str_matcher.py index 3346b68..e6721f6 100644 --- a/believe/str_matcher.py +++ b/believe/str_matcher.py @@ -1,4 +1,5 @@ import uuid +import json from .internal import BelieveBase diff --git a/tests/test_believe.py b/tests/test_believe.py index d8aff74..1819a52 100644 --- a/tests/test_believe.py +++ b/tests/test_believe.py @@ -235,6 +235,42 @@ def test_dict_matcher__Dict(self): "[e_path=$.req1] {} != {'req11': 1}", "[e_path=$.req1]") + def test_dict_matcher__DictStr(self): + exp_dict = self.DictStr({"req1": 1, + "req2": 2, + "opt1": self.Optional(1), + "opt2": self.Optional(2)}) + + self.assertEqual("DictStr({'req1': 1, 'req2': 2, 'opt1': Optional(1), 'opt2': Optional(2)})", str(exp_dict)) + + self.assertEqual(exp_dict, '{"req1": 1, "req2": 2, "opt1": 1, "opt2": 2}') + self.assertEqual(exp_dict, '{"req1": 1, "req2": 2, "opt1": 1}') + self.assertEqual(exp_dict, '{"req1": 1, "req2": 2, "opt2": 2}') + self.assertEqual(exp_dict, '{"req1": 1, "req2": 2}') + self.assertEqual(exp_dict, b'{"req1": 1, "req2": 2}') + + self.assertNotEqual(exp_dict, '{"req1": 1}') + self.assertNotEqual(exp_dict, '{"req1": 1, "opt1": 1}') + self.assertNotEqual(exp_dict, '{"req1": 1, "req2": 2, "opt3": 3}') + self.assertNotEqual(exp_dict, '{"req1": 1, "req2": 2, "req3": 3}') + + self.fail_validate_with({}, self.DictStr({"req1": 1}), + "[e_msg=not_dict_string] {} != DictStr({'req1': 1})", + "[e_msg=not_dict_string]") + + self.fail_validate_with('{}', self.DictStr({"req1": 1}), + "[e_msg=missing_required_field: req1] {} != DictStr({'req1': 1})", + "[e_msg=missing_required_field: req1]") + + self.fail_validate_with('{"req1": 1}', self.DictStr({}), + "[e_msg=unknown_field] [e_unsafe_msg=unknown_field: req1] {'req1': 1} != DictStr({})", + "[e_msg=unknown_field]", + ) + + self.fail_validate_with('{"req1": {}}', self.DictStr({"req1": {"req11": 1}}), + "[e_path=$.req1] {} != {'req11': 1}", + "[e_path=$.req1]") + def test_dict_matcher__DictOf(self): exp_dict = self.DictOf(self.AnyStr(), self.AnyStr())