Skip to content

Commit 36fe09c

Browse files
authored
Merge pull request #5 from cloudblue/feature/assert-improvement-and-readme-update
Feature/Asserts improvements and README updates.
2 parents 23d022d + f5ba87a commit 36fe09c

4 files changed

Lines changed: 63 additions & 42 deletions

File tree

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
# Connect DevOps Testing Library
22

3+
![pyversions](https://img.shields.io/pypi/pyversions/connect-devops-testing-library.svg) [![PyPi Status](https://img.shields.io/pypi/v/connect-devops-testing-library.svg)](https://pypi.org/project/connect-devops-testing-library/) [![Build Status](https://github.com/cloudblue/connect-devops-testing-library/actions/workflows/test.yml/badge.svg)](https://github.com/cloudblue/connect-devops-testing-library/actions/workflows/test.yml) [![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=connect-devops-testing-library&metric=alert_status)](https://sonarcloud.io/dashboard?id=connect-devops-testing-library)
4+
35
Testing library to ease Connect EaaS Processors development.
46

7+
## Install
8+
9+
`Connect DevOps Testing Library` can be installed from [pypi.org](https://pypi.org/project/connect-devops-testing-library/) using pip:
10+
11+
```bash
12+
$ pip install connect-devops-testing-library
13+
```
14+
15+
## Usage
16+
517
DevOps Testing Library has a small request builder to ease the manipulation of the connect requests during testing:
618

719
````python
@@ -90,7 +102,7 @@ def test_should_approve_purchase_request_successfully():
90102

91103
Once the request is dispatched the Dispatcher will reload the request again every `10` seconds a maximum of `20`
92104
attempts. If the request has not been processed the asserts may fail. The wait time between request reload can be
93-
configured directly in the `.provision_request(api_key=, api_url=)` method call.
105+
configured directly in the `.provision_request(timeout=10, max_attempt=20)` method call.
94106

95107
Obviously, some Connect processors may take a lot of time to process a request, for those type of processors this kind
96108
of end-to-end test is not suitable.
@@ -145,3 +157,7 @@ def step_impl(context):
145157
```
146158

147159
The `@when("subscription request is processed")` is provided by the DevOps Testing Library.
160+
161+
## License
162+
163+
`Connect DevOps Testing Library` is released under the [Apache License Version 2.0](https://www.apache.org/licenses/LICENSE-2.0).

connect/devops_testing/asserts.py

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,29 @@
1111

1212

1313
def request_status(request: dict, expected: str):
14-
assert request['status'] == expected
14+
assert request.get('status') == expected
15+
16+
17+
def request_reason(request: dict, expected: str):
18+
assert request.get('reason') == expected
19+
20+
21+
def request_note(request: dict, expected: str):
22+
assert request.get('note') == expected
1523

1624

1725
def task_response_status(task_response, expected: str):
1826
assert task_response.status == expected
1927

2028

2129
def asset_status(request: dict, expected: str):
22-
assert request['asset']['status'] == expected
30+
assert request.get('asset', {}).get('status') == expected
2331

2432

2533
def asset_params_value(request: dict, param_id: str, operator: str, expected: Any):
2634
fn = __operators.get(operator)
27-
param = find_by_id(request['asset']['params'], param_id)
28-
assert fn(param['value'], expected)
35+
param = find_by_id(request.get('asset', {}).get('params', []), param_id, {})
36+
assert fn(param.get('value'), expected)
2937

3038

3139
def asset_params_value_equal(request: dict, param_id: str, expected: Any):
@@ -36,32 +44,16 @@ def asset_params_value_not_equal(request: dict, param_id: str, expected: Any):
3644
asset_params_value(request, param_id, '!=', expected)
3745

3846

39-
def asset_params_value_greater(request: dict, param_id: str, expected: Any):
40-
asset_params_value(request, param_id, '<', expected)
41-
42-
43-
def asset_params_value_greater_or_equal(request: dict, param_id: str, expected: Any):
44-
asset_params_value(request, param_id, '<=', expected)
45-
46-
47-
def asset_params_value_lesser(request: dict, param_id: str, expected: Any):
48-
asset_params_value(request, param_id, '>', expected)
49-
50-
51-
def asset_params_value_lesser_or_equal(request: dict, param_id: str, expected: Any):
52-
asset_params_value(request, param_id, '>=', expected)
53-
54-
5547
def asset_params_value_contains(request: dict, param_id: str, expected: Any):
5648
fn = __operators.get('in')
57-
param = find_by_id(request['asset']['params'], param_id)
49+
param = find_by_id(request.get('asset', {}).get('params', []), param_id, {})
5850
assert fn(expected, param['value'])
5951

6052

6153
def asset_params_value_error(request: dict, param_id: str, operator: str, expected: Any):
6254
fn = __operators.get(operator)
63-
param = find_by_id(request['asset']['params'], param_id)
64-
assert fn(param['value_error'], expected)
55+
param = find_by_id(request.get('asset', {}).get('params', []), param_id, {})
56+
assert fn(param.get('value_error'), expected)
6557

6658

6759
def asset_params_value_equal_error(request: dict, param_id: str, expected: Any):
@@ -74,18 +66,18 @@ def asset_params_value_not_equal_error(request: dict, param_id: str, expected: A
7466

7567
def asset_params_value_contains_error(request: dict, param_id: str, expected: Any):
7668
fn = __operators.get('in')
77-
param = find_by_id(request['asset']['params'], param_id)
78-
assert fn(expected, param['value_error'])
69+
param = find_by_id(request.get('asset', {}).get('params', []), param_id, {})
70+
assert fn(param.get('value_error'), expected)
7971

8072

8173
def tcr_status(request: dict, expected: str):
82-
assert request['configuration']['status'] == expected
74+
assert request.get('configuration', {}).get('status') == expected
8375

8476

8577
def tcr_params_value(request: dict, param_id: str, operator: str, expected: Any):
8678
fn = __operators.get(operator)
87-
param = find_by_id(request['configuration']['params'], param_id)
88-
assert fn(param['value'], expected)
79+
param = find_by_id(request.get('configuration', {}).get('params', []), param_id, {})
80+
assert fn(param.get('value'), expected)
8981

9082

9183
def tcr_params_value_equal(request: dict, param_id: str, expected: Any):
@@ -98,14 +90,14 @@ def tcr_params_value_not_equal(request: dict, param_id: str, expected: Any):
9890

9991
def tcr_params_value_contains(request: dict, param_id: str, expected: Any):
10092
fn = __operators.get('in')
101-
param = find_by_id(request['configuration']['params'], param_id)
102-
assert fn(expected, param['value'])
93+
param = find_by_id(request.get('configuration', {}).get('params', []), param_id, {})
94+
assert fn(param.get('value'), expected)
10395

10496

10597
def tcr_params_value_error(request: dict, param_id: str, operator: str, expected: Any):
10698
fn = __operators.get(operator)
107-
param = find_by_id(request['configuration']['params'], param_id)
108-
assert fn(param['value_error'], expected)
99+
param = find_by_id(request.get('configuration', {}).get('params', []), param_id, {})
100+
assert fn(param.get('value_error'), expected)
109101

110102

111103
def tcr_params_value_equal_error(request: dict, param_id: str, expected: Any):
@@ -118,5 +110,5 @@ def tcr_params_value_not_equal_error(request: dict, param_id: str, expected: Any
118110

119111
def tcr_params_value_contains_error(request: dict, param_id: str, expected: Any):
120112
fn = __operators.get('in')
121-
param = find_by_id(request['configuration']['params'], param_id)
122-
assert fn(expected, param['value_error'])
113+
param = find_by_id(request.get('configuration', {}).get('params', []), param_id, {})
114+
assert fn(param.get('value_error'), expected)

connect/devops_testing/utils.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
from typing import List, Optional
22

33

4-
def find_by_id(collection: List, element_id: str) -> Optional[dict]:
4+
def find_by_id(collection: List, element_id: str, default: Optional[dict] = None) -> Optional[dict]:
55
"""
66
Searches for a parameter/item with the given ``id`` within the ``list``.
77
88
:param collection: The list of parameters/items to search.
99
:param element_id: The id of the parameter/item to find.
10-
:return: The parameter/list, or ``None`` if it was not found.
10+
:param default: Default value to return if item is not found.
11+
:return: The parameter/list, or ``default`` if it was not found.
1112
"""
1213
filtered = list(filter(lambda element: element['id'] == element_id, collection))
13-
return filtered[0] if filtered else None
14+
return filtered[0] if filtered else default

tests/test_asserts.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
asset_request = {
44
'status': 'approved',
55
'type': 'purchase',
6+
'reason': 'some reason',
7+
'note': 'some note',
68
'asset': {
79
'status': 'active',
810
'params': [
@@ -36,6 +38,14 @@ def __init__(self, status: str):
3638
asserts.task_response_status(TaskResponse('success'), 'success')
3739

3840

41+
def test_should_assert_request_note():
42+
asserts.request_note(asset_request, 'some note')
43+
44+
45+
def test_should_assert_request_reason():
46+
asserts.request_reason(asset_request, 'some reason')
47+
48+
3949
def test_should_assert_asset_status():
4050
asserts.asset_status(asset_request, 'active')
4151

@@ -49,7 +59,7 @@ def test_should_assert_asset_params_value_not_equal():
4959

5060

5161
def test_should_assert_asset_params_value_contains():
52-
asserts.asset_params_value_contains(asset_request, 'ID', ['value'])
62+
asserts.asset_params_value_contains(asset_request, 'ID', 'value')
5363

5464

5565
def test_should_assert_asset_params_value_equal_error():
@@ -61,7 +71,8 @@ def test_should_assert_asset_params_value_not_equal_error():
6171

6272

6373
def test_should_assert_asset_params_value_contains_error():
64-
asserts.asset_params_value_contains_error(asset_request, 'ID', ['some error'])
74+
asserts.asset_params_value_contains_error(asset_request, 'ID', 'some error')
75+
asserts.asset_params_value_contains_error(asset_request, 'ID', 'some')
6576

6677

6778
def test_should_assert_tcr_status():
@@ -77,7 +88,7 @@ def test_should_assert_tcr_params_value_not_equal():
7788

7889

7990
def test_should_assert_tcr_params_value_contains():
80-
asserts.tcr_params_value_contains(config_request, 'ID', ['value'])
91+
asserts.tcr_params_value_contains(config_request, 'ID', 'value')
8192

8293

8394
def test_should_assert_tcr_params_value_equal_error():
@@ -89,4 +100,5 @@ def test_should_assert_tcr_params_value_not_equal_error():
89100

90101

91102
def test_should_assert_tcr_params_value_contains_error():
92-
asserts.tcr_params_value_contains_error(config_request, 'ID', ['some error'])
103+
asserts.tcr_params_value_contains_error(config_request, 'ID', 'some error')
104+
asserts.tcr_params_value_contains_error(config_request, 'ID', 'some')

0 commit comments

Comments
 (0)