Skip to content

Commit

Permalink
Merge pull request #33980 from dimagi/nh/test
Browse files Browse the repository at this point in the history
Just some tests
  • Loading branch information
kaapstorm committed Jan 16, 2024
2 parents ad5b749 + 148b60e commit a97e198
Showing 1 changed file with 50 additions and 2 deletions.
52 changes: 50 additions & 2 deletions corehq/ex-submodules/dimagi/utils/tests/test_web.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from testil import eq
from testil import eq, assert_raises

from dimagi.utils.web import get_ip
from dimagi.utils.web import get_ip, json_request


def test_get_ip():
Expand All @@ -26,3 +26,51 @@ def __init__(self):
self.META = request_meta

eq(get_ip(FakeRequest()), expected_value)


def test_json_request():
# empty
yield _test_json_request, {}, {}, {}
# quoted string
yield _test_json_request, {'hello': '"world"'}, {}, {'hello': 'world'}
# string of an integer
yield _test_json_request, {'hello': '123'}, {}, {'hello': 123}
# string of an object
yield (
_test_json_request,
{'hello': '{"foo": "bar"}'},
{},
{'hello': {'foo': 'bar'}},
)
# boolean
yield (
_test_json_request,
{'hello': 'true'},
{},
{'hello': True},
)
# booleans as strings
yield (
_test_json_request,
{'hello': 'true'},
{'booleans_as_strings': True},
{'hello': 'true'},
)
# key is not a string
yield _test_json_request, {123: '"world"'}, {}, {'123': 'world'}
# lenient
yield (
_test_json_request,
{'hello': 'not JSON'},
{}, # {'lenient': True}
{'hello': 'not JSON'},
)


def _test_json_request(params, kwargs, expected_result):
eq(json_request(params, **kwargs), expected_result)


def test_not_lenient_json_request():
with assert_raises(ValueError):
json_request({'hello': 'not JSON'}, lenient=False)

0 comments on commit a97e198

Please sign in to comment.