Skip to content

Commit

Permalink
Merge pull request #11 from cloudblue/LITE-23653-generalize-response-…
Browse files Browse the repository at this point in the history
…classes

LITE-23653 generalize responses x task category
  • Loading branch information
Francesco Faraone authored May 16, 2022
2 parents e0e9b84 + c3bc525 commit 862450c
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 32 deletions.
36 changes: 20 additions & 16 deletions connect/eaas/core/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def fail(cls, output=None):
return cls(ResultType.FAIL, output=output)


class ProcessingResponse(_Response):
class BackgroundResponse(_Response):

def __init__(self, status, countdown=30, output=None):
super().__init__(status, output)
Expand All @@ -37,21 +37,11 @@ def slow_process_reschedule(cls, countdown=300):
)


class ValidationResponse(_Response):
def __init__(self, status, data, output=None):
super().__init__(status, output)
self.data = data

@classmethod
def done(cls, data):
return cls(ResultType.SUCCESS, data)

@classmethod
def fail(cls, data=None, output=None):
return cls(ResultType.FAIL, data=data, output=output)
class ProcessingResponse(BackgroundResponse):
pass


class _InteractiveTaskResponse(_Response):
class InteractiveResponse(_Response):
def __init__(self, status, http_status, headers, body, output):
super().__init__(status, output)
self.http_status = http_status
Expand All @@ -75,11 +65,25 @@ def fail(cls, http_status=400, headers=None, body=None, output=None):
return cls(ResultType.FAIL, http_status, headers, body, output)


class CustomEventResponse(_InteractiveTaskResponse):
class ValidationResponse(InteractiveResponse):
def __init__(self, status, data, output=None):
http_status = 200 if status == ResultType.SUCCESS else 400
super().__init__(status, http_status, None, data, output)

@classmethod
def done(cls, data):
return cls(ResultType.SUCCESS, data)

@classmethod
def fail(cls, data=None, output=None):
return cls(ResultType.FAIL, data=data, output=output)


class CustomEventResponse(InteractiveResponse):
pass


class ProductActionResponse(_InteractiveTaskResponse):
class ProductActionResponse(InteractiveResponse):
pass


Expand Down
56 changes: 40 additions & 16 deletions tests/connect/eaas/core/test_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

from connect.eaas.core.enums import ResultType
from connect.eaas.core.responses import (
BackgroundResponse,
CustomEventResponse,
InteractiveResponse,
ProcessingResponse,
ProductActionResponse,
ScheduledExecutionResponse,
Expand All @@ -11,20 +13,16 @@


def test_result_ok():
assert ProcessingResponse.done().status == ResultType.SUCCESS
assert BackgroundResponse.done().status == ResultType.SUCCESS
assert ScheduledExecutionResponse.done().status == ResultType.SUCCESS
data = {'test': 'data'}
ok = ValidationResponse.done(data)
assert ok.status == ResultType.SUCCESS
assert ok.data == data


def test_result_skip():
assert ProcessingResponse.skip().status == ResultType.SKIP
assert BackgroundResponse.skip().status == ResultType.SKIP


def test_result_skip_with_output():
skip = ProcessingResponse.skip('output')
skip = BackgroundResponse.skip('output')
assert skip.status == ResultType.SKIP
assert skip.output == 'output'

Expand All @@ -41,7 +39,7 @@ def test_result_skip_with_output():
),
)
def test_result_reschedule(countdown, expected):
r = ProcessingResponse.reschedule(countdown)
r = BackgroundResponse.reschedule(countdown)

assert r.status == ResultType.RESCHEDULE
assert r.countdown == expected
Expand All @@ -59,7 +57,7 @@ def test_result_reschedule(countdown, expected):
),
)
def test_result_slow_reschedule(countdown, expected):
r = ProcessingResponse.slow_process_reschedule(countdown)
r = BackgroundResponse.slow_process_reschedule(countdown)

assert r.status == ResultType.RESCHEDULE
assert r.countdown == expected
Expand All @@ -68,8 +66,7 @@ def test_result_slow_reschedule(countdown, expected):
@pytest.mark.parametrize(
'response_cls',
(
ProcessingResponse, ValidationResponse,
CustomEventResponse, ProductActionResponse,
BackgroundResponse, InteractiveResponse,
ScheduledExecutionResponse,
),
)
Expand All @@ -80,8 +77,8 @@ def test_result_fail(response_cls):
assert r.output == 'reason of failure'


def test_custom_event():
r = CustomEventResponse.done(headers={'X-Custom-Header': 'value'}, body='text')
def test_interactive_response():
r = InteractiveResponse.done(headers={'X-Custom-Header': 'value'}, body='text')

assert r.status == ResultType.SUCCESS
assert r.data == {
Expand All @@ -91,10 +88,37 @@ def test_custom_event():
}


def test_product_action():
r = ProductActionResponse.done(headers={'X-Custom-Header': 'value'}, body='text')
def test_v1_responses():
r = ProcessingResponse.done()
assert isinstance(r, BackgroundResponse)

assert r.status == ResultType.SUCCESS
r = ValidationResponse.done({'key': 'value'})
assert isinstance(r, InteractiveResponse)
assert r.data == {
'http_status': 200,
'headers': None,
'body': {'key': 'value'},
}

r = ValidationResponse.fail(data={'key': 'value'}, output='invalid data')
assert isinstance(r, InteractiveResponse)
assert r.data == {
'http_status': 400,
'headers': None,
'body': {'key': 'value'},
}
assert r.output == 'invalid data'

r = CustomEventResponse.done(headers={'X-Custom-Header': 'value'}, body='text')
assert isinstance(r, InteractiveResponse)
assert r.data == {
'http_status': 200,
'headers': {'X-Custom-Header': 'value'},
'body': 'text',
}

r = ProductActionResponse.done(headers={'X-Custom-Header': 'value'}, body='text')
assert isinstance(r, InteractiveResponse)
assert r.data == {
'http_status': 200,
'headers': {'X-Custom-Header': 'value'},
Expand Down

0 comments on commit 862450c

Please sign in to comment.