Skip to content

Commit

Permalink
add DictStr
Browse files Browse the repository at this point in the history
  • Loading branch information
Seth Wang committed Feb 3, 2021
1 parent 86dc6b6 commit 1985172
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions believe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 9 additions & 0 deletions believe/dict_matcher.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
from .internal import BelieveBase
from .internal import validate

Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions believe/str_matcher.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import uuid
import json

from .internal import BelieveBase

Expand Down
36 changes: 36 additions & 0 deletions tests/test_believe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down

0 comments on commit 1985172

Please sign in to comment.