diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d527e2..fe0dd61 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/c8y_api/model/tenant_options.py b/c8y_api/model/tenant_options.py index 0d30d32..3832cea 100644 --- a/c8y_api/model/tenant_options.py +++ b/c8y_api/model/tenant_options.py @@ -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]: @@ -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. diff --git a/integration_tests/test_tenant_options.py b/integration_tests/test_tenant_options.py index 78eb388..3da545c 100644 --- a/integration_tests/test_tenant_options.py +++ b/integration_tests/test_tenant_options.py @@ -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."""