Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions linode_api4/linode_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
15 changes: 10 additions & 5 deletions linode_api4/objects/filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -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<Filter>` with an "and" operation, matching
any results that match both of the given filters.
Combines two or more :any:`Filters<Filter>` 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):
Expand Down
90 changes: 90 additions & 0 deletions test/unit/objects/filtering_test.py
Original file line number Diff line number Diff line change
@@ -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
)