Skip to content
Merged
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
17 changes: 11 additions & 6 deletions caldav/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ def _flag_component_type(searcher: "Searcher", flag: str) -> None:
## Whether servers honour negate-condition, or a bare prop-filter, has not
## been probed yet (caldav-server-tester), so search() decides these
## client-side. https://github.com/python-caldav/caldav/issues/568
_NEGATED_OR_PRESENCE_OPERATORS = ("!=", "!contains", "def")
## "in" (equal to one of several values) has no CalDAV form at all.
_NEGATED_OR_PRESENCE_OPERATORS = ("!=", "!contains", "def", "in")


def _collation_str(searcher: "Searcher", property: str) -> str:
Expand Down Expand Up @@ -497,8 +498,8 @@ def add_property_filter(
Special virtual property "category" (singular) is also supported
for substring matching within category names
:param value: Filter value, should adhere to the type defined in the RFC
:param operator: Comparison operator ("contains", "==", "!contains",
"!=", "def", "undef"; see below). If not
:param operator: Comparison operator ("contains", "==", "in",
"!contains", "!=", "def", "undef"; see below). If not
specified, the server decides the matching behavior (usually
substring search per RFC). If explicitly set to "contains"
and the server doesn't support substring search, client-side
Expand All @@ -517,17 +518,21 @@ def add_property_filter(
* **contains** - substring match (e.g., "rain" matches "Training session"
and "Singing in the rain")
* **==** - exact match required, enforced client-side
* **in** - equal to one of the values in a collection (a list, a
range ...), e.g. ``PRIORITY`` in ``range(1, 4)``; the property must
be defined. Not for categories.
* **!contains** - no substring match; the property must be defined
* **!=** (or **<>**) - not an exact match; the property must be defined
* **def** - matches if property is defined (value parameter ignored)
* **undef** - matches if property is not defined (value parameter ignored)

The negations and ``def`` need icalendar-searcher 1.1.0 or later.
The negations and ``def`` need icalendar-searcher 1.1.0 or later,
``in`` an icalendar-searcher release with it.
search() decides them client-side and leaves them out of the
server query, unless ``post_filter=False``; then, as with
build_search_xml_query() on its own, they are sent as the
closest CalDAV filter that never excludes a match, and != and
def match any object where the property is defined.
closest CalDAV filter that never excludes a match, and !=, def
and in match any object where the property is defined.

**Special handling for categories:**

Expand Down
1 change: 1 addition & 0 deletions changes/+in-operator.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Searches with the new `in` operator of icalendar-searcher (the property equals one of several values) are decided client-side, as CalDAV has no such filter. It needs an icalendar-searcher release with the operator, https://github.com/pycalendar/icalendar-searcher/pull/27
75 changes: 75 additions & 0 deletions tests/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -1846,6 +1846,20 @@ def test_search_results_populated_after_batch_load(self) -> None:
assert len(results) == 1


def _searcher_has_in_operator() -> bool:
try:
CalDAVSearcher().add_property_filter("SUMMARY", ["x"], operator="in")
except NotImplementedError:
return False
return True


## The "in" operator is newer than the oldest icalendar-searcher supported
needs_in_operator = pytest.mark.skipif(
not _searcher_has_in_operator(), reason="icalendar-searcher has no in operator"
)


class TestNegatedOperators:
"""The negated and presence operators: ``!=``, ``!contains`` and ``def``.

Expand Down Expand Up @@ -1986,6 +2000,67 @@ def test_xml_not_equal_is_a_bare_prop_filter(self) -> None:
assert b'prop-filter name="SUMMARY"' in xml
assert b"text-match" not in xml

@needs_in_operator
@pytest.mark.parametrize(
("values", "expected"),
[
(["rain", "Training session"], {"training", "rain"}),
(["rain", "Training"], {"rain"}),
(["Rain"], set()),
],
)
@pytest.mark.parametrize(
"supported", [True, False, "search.text"], ids=["all", "none", "no-text-search"]
)
def test_search_in_filters_client_side(
self, values: list[str], expected: set[str], supported: bool | str
) -> None:
"""``in`` (equal to one of several values) has no CalDAV form either."""
cal = self._calendar(supported)
searcher = CalDAVSearcher(event=True)
searcher.add_property_filter("SUMMARY", values, operator="in")
results = searcher.search(cal)
uids = {str(r.icalendar_component["UID"]).split("@")[0] for r in results}
assert uids == expected

@needs_in_operator
@pytest.mark.parametrize(
"supported", [True, False, "search.text"], ids=["all", "none", "no-text-search"]
)
def test_search_priority_in_range(self, supported: bool | str) -> None:
"""The motivating case: PRIORITY in range(1, 4), an integer property."""
self._events = {"one": "1", "three": "3", "five": "5", "none": None}
cal = self._calendar(supported, prop="PRIORITY")
searcher = CalDAVSearcher(event=True)
searcher.add_property_filter("PRIORITY", range(1, 4), operator="in")
results = searcher.search(cal)
uids = {str(r.icalendar_component["UID"]).split("@")[0] for r in results}
assert uids == {"one", "three"}

@needs_in_operator
def test_search_in_without_post_filter_sends_bare_prop_filter(self) -> None:
"""With post_filter=False the server's answer is trusted, and the
query asks only for the property to be defined: a superset."""
from lxml import etree

cal = self._calendar(True)
searcher = CalDAVSearcher(event=True)
searcher.add_property_filter("SUMMARY", ["rain"], operator="in")
results = searcher.search(cal, post_filter=False)
assert len(results) == 3
for call in cal._request_report_build_resultlist.call_args_list:
body = etree.tostring(call.args[0].xmlelement())
assert b'prop-filter name="SUMMARY"' in body
assert b"text-match" not in body

@needs_in_operator
def test_xml_in_is_a_bare_prop_filter(self) -> None:
searcher = CalDAVSearcher(event=True)
searcher.add_property_filter("SUMMARY", ["rain", "Training"], operator="in")
xml = self._xml(searcher)
assert b'prop-filter name="SUMMARY"' in xml
assert b"text-match" not in xml

def test_xml_def_is_a_bare_prop_filter(self) -> None:
searcher = CalDAVSearcher(event=True)
searcher.add_property_filter("SUMMARY", None, operator="def")
Expand Down
Loading