Skip to content

Commit 7baf167

Browse files
committed
Fixed issue #63.
1 parent bbc4b5a commit 7baf167

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* Incorporated pull request to remove dependency on deprecated pkg_resources package (thanks @reubenmiller).
99
* Incorporated pull request to support context handlers.
1010
* Many additional unit tests and integration tests.
11+
* Fixed issue [#63](https://github.com/Cumulocity-IoT/cumulocity-python-api/issues/63) (tenant option select function did not filter categories correctly).
1112

1213

1314
## Version 2.1

c8y_api/model/tenant_options.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,17 @@ def select(self, category: str = None, limit: int = None,
156156
Returns:
157157
Generator for TenantObject instances
158158
"""
159-
base_query = self._prepare_query(category=category, page_size=page_size)
160-
return super()._iterate(base_query, page_number, limit, TenantOption.from_json)
159+
if not category:
160+
# select all
161+
base_query = self._prepare_query(page_size=page_size)
162+
yield from super()._iterate(base_query, page_number, limit, TenantOption.from_json)
163+
else:
164+
# select by category, this is just a single request
165+
options_json = self.c8y.get(f'{self.resource}/{category}')
166+
for key, value in options_json.items():
167+
result = TenantOption(category=category, key=key, value=value)
168+
result.c8y = self.c8y # inject c8y connection into instance
169+
yield result
161170

162171
def get_all(self, category: str = None, limit: int = None,
163172
page_size: int = 1000, page_number: int = None) -> List[TenantOption]:
@@ -260,7 +269,7 @@ def update_by(self, category: str, options: dict[str, str]) -> None:
260269
category (str): Option category
261270
options (dict): A dictionary of option keys and values
262271
"""
263-
self.c8y.put(resource=self.resource + '/' + category, json=options, accept=None)
272+
self.c8y.put(resource=f'{self.resource}/{category}', json=options, accept=None)
264273

265274
def delete(self, *options: TenantOption) -> None:
266275
""" Delete options within the database.

integration_tests/test_tenant_options.py

+15
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,21 @@ def test_crud_2(live_c8y: CumulocityApi):
8888
live_c8y.tenant_options.delete(option)
8989

9090

91+
def test_select_by(live_c8y: CumulocityApi):
92+
"""Verify that selecting tenant options work as expected."""
93+
all_options = live_c8y.tenant_options.get_all()
94+
95+
categories = {x.category for x in all_options}
96+
by_category = {c: [x for x in all_options if x.category == c] for c in categories}
97+
for category, xs in by_category.items():
98+
options = live_c8y.tenant_options.get_all(category=category)
99+
options_mapped = live_c8y.tenant_options.get_all_mapped(category=category)
100+
assert len(options) == len(by_category[category])
101+
assert len(options_mapped) == len(by_category[category])
102+
assert {x.key for x in options} == {x.key for x in xs}
103+
assert {x for x, _ in options_mapped.items()} == {x.key for x in xs}
104+
105+
91106
def test_set_value_and_update_and_delete_by(live_c8y: CumulocityApi):
92107
"""Verify that functions set_value, update_by and delete_by work
93108
as expected."""

0 commit comments

Comments
 (0)