Skip to content

Commit

Permalink
Fixed issue #63.
Browse files Browse the repository at this point in the history
  • Loading branch information
chsou committed Nov 21, 2024
1 parent bbc4b5a commit 7baf167
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Incorporated pull request to remove dependency on deprecated pkg_resources package (thanks @reubenmiller).
* Incorporated pull request to support context handlers.
* Many additional unit tests and integration tests.
* Fixed issue [#63](https://github.com/Cumulocity-IoT/cumulocity-python-api/issues/63) (tenant option select function did not filter categories correctly).


## Version 2.1
Expand Down
15 changes: 12 additions & 3 deletions c8y_api/model/tenant_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,17 @@ def select(self, category: str = None, limit: int = None,
Returns:
Generator for TenantObject instances
"""
base_query = self._prepare_query(category=category, page_size=page_size)
return super()._iterate(base_query, page_number, limit, TenantOption.from_json)
if not category:
# select all
base_query = self._prepare_query(page_size=page_size)
yield from super()._iterate(base_query, page_number, limit, TenantOption.from_json)
else:
# select by category, this is just a single request
options_json = self.c8y.get(f'{self.resource}/{category}')
for key, value in options_json.items():
result = TenantOption(category=category, key=key, value=value)
result.c8y = self.c8y # inject c8y connection into instance
yield result

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

def delete(self, *options: TenantOption) -> None:
""" Delete options within the database.
Expand Down
15 changes: 15 additions & 0 deletions integration_tests/test_tenant_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,21 @@ def test_crud_2(live_c8y: CumulocityApi):
live_c8y.tenant_options.delete(option)


def test_select_by(live_c8y: CumulocityApi):
"""Verify that selecting tenant options work as expected."""
all_options = live_c8y.tenant_options.get_all()

categories = {x.category for x in all_options}
by_category = {c: [x for x in all_options if x.category == c] for c in categories}
for category, xs in by_category.items():
options = live_c8y.tenant_options.get_all(category=category)
options_mapped = live_c8y.tenant_options.get_all_mapped(category=category)
assert len(options) == len(by_category[category])
assert len(options_mapped) == len(by_category[category])
assert {x.key for x in options} == {x.key for x in xs}
assert {x for x, _ in options_mapped.items()} == {x.key for x in xs}


def test_set_value_and_update_and_delete_by(live_c8y: CumulocityApi):
"""Verify that functions set_value, update_by and delete_by work
as expected."""
Expand Down

0 comments on commit 7baf167

Please sign in to comment.