diff --git a/README.rst b/README.rst index 3f7f00a30..8dcc5d114 100644 --- a/README.rst +++ b/README.rst @@ -5,8 +5,8 @@ The official python library for the `Linode API v4`_ in python. .. _Linode API v4: https://techdocs.akamai.com/linode-api/reference/api -.. image:: https://img.shields.io/github/actions/workflow/status/linode/linode_api4-python/main.yml?label=tests - :target: https://img.shields.io/github/actions/workflow/status/linode/linode_api4-python/main.yml?label=tests +.. image:: https://img.shields.io/github/actions/workflow/status/linode/linode_api4-python/ci.yml?label=tests + :target: https://github.com/linode/linode_api4-python/actions/workflows/ci.yml .. image:: https://badge.fury.io/py/linode-api4.svg :target: https://badge.fury.io/py/linode-api4 diff --git a/linode_api4/linode_client.py b/linode_api4/linode_client.py index 0e89142b3..52d882e73 100644 --- a/linode_api4/linode_client.py +++ b/linode_api4/linode_client.py @@ -317,9 +317,7 @@ def _get_and_filter( parsed_filters = None if filters: if len(filters) > 1: - parsed_filters = and_( - *filters - ).dct # pylint: disable=no-value-for-parameter + parsed_filters = and_(*filters).dct else: parsed_filters = filters[0].dct diff --git a/linode_api4/objects/filtering.py b/linode_api4/objects/filtering.py index 3616eb505..602af3fc2 100644 --- a/linode_api4/objects/filtering.py +++ b/linode_api4/objects/filtering.py @@ -66,20 +66,25 @@ def or_(a, b): return a.__or__(b) -def and_(a, b): +def and_(a, b, *filters): """ - Combines two :any:`Filters` with an "and" operation, matching - any results that match both of the given filters. + Combines two or more :any:`Filters` with an "and" operation, + matching any results that match all of the given filters. :param a: The first filter to consider. :type a: Filter :param b: The second filter to consider. :type b: Filter + :param filters: Any additional filters to consider. + :type filters: Filter - :returns: A filter that matches both a and b + :returns: A filter that matches all of the given filters :rtype: Filter """ - return a.__and__(b) + result = a.__and__(b) + for f in filters: + result = result.__and__(f) + return result def order_by(field, desc=False): diff --git a/test/unit/objects/filtering_test.py b/test/unit/objects/filtering_test.py new file mode 100644 index 000000000..92512bafd --- /dev/null +++ b/test/unit/objects/filtering_test.py @@ -0,0 +1,90 @@ +import json +from test.unit.base import ClientBaseCase + +from linode_api4 import Instance, and_, or_ + + +class FilteringTest(ClientBaseCase): + """ + Tests combining filters and passing them to collection methods + """ + + def test_and_two_filters(self): + """ + Tests that and_ combines two filters + """ + f = and_(Instance.label == "web", Instance.region == "us-east") + + self.assertEqual( + f.dct, {"+and": [{"label": "web"}, {"region": "us-east"}]} + ) + + def test_and_many_filters(self): + """ + Tests that and_ accepts more than two filters + """ + f = and_( + Instance.label == "web", + Instance.region == "us-east", + Instance.tags.contains("prod"), + or_(Instance.group == "a", Instance.group == "b"), + ) + + self.assertEqual( + f.dct, + { + "+and": [ + {"label": "web"}, + {"region": "us-east"}, + {"tags": {"+contains": "prod"}}, + {"+or": [{"group": "a"}, {"group": "b"}]}, + ] + }, + ) + + def test_and_requires_two_filters(self): + """ + Tests that and_ still requires at least two filters + """ + with self.assertRaises(TypeError): + and_( + Instance.label == "web" + ) # pylint: disable=no-value-for-parameter + + def test_collection_with_many_filters(self): + """ + Tests that a collection method accepts more than two filters and + sends all of them in the X-Filter header + """ + cases = ( + ( + (Instance.label == "web",), + {"label": "web"}, + ), + ( + (Instance.label == "web", Instance.region == "us-east"), + {"+and": [{"label": "web"}, {"region": "us-east"}]}, + ), + ( + ( + Instance.label == "web", + Instance.region == "us-east", + Instance.tags.contains("prod"), + ), + { + "+and": [ + {"label": "web"}, + {"region": "us-east"}, + {"tags": {"+contains": "prod"}}, + ] + }, + ), + ) + + for filters, expected in cases: + with self.mock_get("linode/instances") as m: + self.client.linode.instances(*filters) + + self.assertEqual( + json.loads(m.call_headers["X-Filter"]), expected + )