Skip to content

Commit

Permalink
test: add regression tests for [VIO-1278]
Browse files Browse the repository at this point in the history
  • Loading branch information
edaniszewski committed Aug 9, 2021
1 parent e4f6d3d commit 0118a6c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
45 changes: 45 additions & 0 deletions tests/unit/api/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,51 @@ async def mock_read_cache(*args, **kwargs):
mock_cmd.assert_called_once()
mock_cmd.assert_called_with('', '')

def test_ok_with_bytes(self, synse_app):
"""Ensure that streaming responses works when values are provided as bytes instead
of as strings.
Regression test for: https://vaporio.atlassian.net/browse/VIO-1278
"""

# Need to define a side-effect function for the test rather than utilizing
# asynctest's implicit behavior for iterable side_effects because the function
# we are mocking (cmd.read_cache) is an async generator, and the implicit
# handling via asynctest does not appear to to handle that case well.
async def mock_read_cache(*args, **kwargs):
values = [
{
'value': 1,
'type': b'temperature',
},
{
b'value': 2,
'type': 'temperature',
},
{
b'value': 3,
b'type': b'temperature',
},
]

for v in values:
yield v

with asynctest.patch('synse_server.api.http.cmd.read_cache') as mock_cmd:
mock_cmd.side_effect = mock_read_cache

_, resp = synse_app.test_client.get('/v3/readcache', gather_request=False)
assert resp.status == 200
assert resp.headers['Transfer-Encoding'] == 'chunked'
assert resp.headers['Content-Type'] == 'application/json; charset=utf-8'

# The response is streamed, so we cannot simply load it (it will not be
# a valid single JSON document), so we compare just the body.
assert resp.body == b'{"value":1,"type":"temperature"}\n{"value":2,"type":"temperature"}\n{"value":3,"type":"temperature"}\n' # noqa: E501

mock_cmd.assert_called_once()
mock_cmd.assert_called_with('', '')

def test_error(self, synse_app):
# Need to define a side-effect function for the test rather than utilizing
# asynctest's implicit behavior for iterable side_effects because the function
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ def test_rfc3339now():
([1, 2, 3], '[1,2,3]\n'),
([1, 2, [2, 3, 4]], '[1,2,[2,3,4]]\n'),
([{'one': 1}, {'two': 2}], '[{"one":1},{"two":2}]\n'),
# -- Regression tests for https://vaporio.atlassian.net/browse/VIO-1278
({b'foo': 'bar'}, '{"foo":"bar"}\n'),
({'foo': b'bar'}, '{"foo":"bar"}\n'),
({b'foo': b'bar'}, '{"foo":"bar"}\n'),
],
)
def test_dumps(data, expected):
Expand Down

0 comments on commit 0118a6c

Please sign in to comment.