Skip to content
This repository has been archived by the owner on May 26, 2021. It is now read-only.

Commit

Permalink
issue-76: fixes partial coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
lowitea committed Mar 17, 2021
1 parent 09ec6df commit be14833
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
6 changes: 3 additions & 3 deletions http_stubs/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@

@celery_app.task()
def run_request_script(
log_id: Optional[int],
script: str,
request_body: str,
log_id: Optional[int] = None,
) -> None:
"""Task for run custom scripts from http stubs.
Expand All @@ -38,7 +38,7 @@ def run_request_script(
:param request_body: text body from a request
"""
log: Optional[LogEntry] = None
if log_id:
if log_id is not None:
log = LogEntry.objects.get(pk=log_id)

loc = {'request_body': request_body, **restricted_builtins}
Expand All @@ -52,6 +52,6 @@ def run_request_script(
else:
log_msg = 'Done'

if log:
if log is not None:
log.result_script = log_msg
log.save()
11 changes: 10 additions & 1 deletion http_stubs/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ def test_run_request_script(script, expect, log_entity_factory):
:param log_entity_factory: a factory for make LogEntity
"""
log = log_entity_factory()
run_request_script.delay(log_id=log.id, script=script, request_body='')
run_request_script.delay(script=script, request_body='', log_id=log.id)
log.refresh_from_db()
assert log.result_script == expect


@pytest.mark.parametrize('script', ('a = 1', '1 / 0'))
def test_run_without_log(script):
"""Tests run task without log_id.
:param script: a request script
"""
run_request_script.run(script=script, request_body='')
2 changes: 1 addition & 1 deletion http_stubs/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ def dispatch(self, request: HttpRequest, *args, **kwargs) -> HttpResponse:

if stub.request_script:
run_request_script.delay(
log_id=log.pk if log else None,
script=stub.request_script,
request_body=request.body.decode('utf-8'),
log_id=log.pk if log else None,
)

sleep(stub.resp_delay / 1000)
Expand Down

0 comments on commit be14833

Please sign in to comment.