From 0b8b715c93ce492df4c313640bbd7d18d1cc50a2 Mon Sep 17 00:00:00 2001 From: Tobias Brox Date: Mon, 16 Nov 2026 19:23:18 +0100 Subject: [PATCH] feat: conformance suite can reuse its collections ecloud (Nextcloud) refuses MKCALENDAR with 429 after about 10 calendars an hour, and the suite makes one per test, so most tests failed there. A new hook, reuses_collections(), makes the suite keep each collection it makes and use it again by name, as it already does where collections cannot be deleted; only what the suite wrote is removed after a test. The test of making and deleting a collection still makes one per run, and the first run on an account a few more. The CalDAV tests turn it on where caldav's rate-limit hint gives create_calendar_per_hour, which the caldav PR https://github.com/python-caldav/caldav/pull/871 adds for ecloud. With a caldav without it, nothing changes. Prompts: > [pick from the task list: "calendaring: conformance follow-up - > decisions and push after 2026-11-11 session"] > (on ecloud's calendar-creation rate limit making 174 tests fail) I > thought we already had a fix here, reusing calendars? > (on the options: a caldav hint, keying on the server name, or skipping > ecloud) We go for [a caldav calendar-creation rate-limit hint], > probably. I don't think we should have a probe for this. This means > it's not a "server-feature", but either a "client-feature" or > "server-observation" or something like that. > please fix it now Assisted-By: Claude Opus 5.5 Reviewed-by: Tobias Brox --- CONTRIBUTING.md | 3 +++ news/+reuse-collections.feature.rst | 1 + src/calendaring/_async/conformance.py | 29 +++++++++++++++----- src/calendaring/_sync/conformance.py | 29 +++++++++++++++----- tests/_async/test_caldav_conformance.py | 18 +++++++++++++ tests/_async/test_memory_conformance.py | 35 +++++++++++++++++++++++++ tests/_sync/test_caldav_conformance.py | 18 +++++++++++++ tests/_sync/test_memory_conformance.py | 35 +++++++++++++++++++++++++ 8 files changed, 154 insertions(+), 14 deletions(-) create mode 100644 news/+reuse-collections.feature.rst diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index aa2e01e..e7e2661 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -150,6 +150,9 @@ annotated, and do not silence an `unused-coroutine` error. 5. Add `tests/_async/test__conformance.py`, with a class that subclasses `calendaring.testing.AsyncConformance` and returns the backend from `open_backend()`. The generator writes the sync tests. + If the backend limits how many collections may be made in a while, + have `reuses_collections()` return `True`: the suite then keeps the + collections it makes rather than make one per test. ## Commit messages diff --git a/news/+reuse-collections.feature.rst b/news/+reuse-collections.feature.rst new file mode 100644 index 0000000..28c6f1c --- /dev/null +++ b/news/+reuse-collections.feature.rst @@ -0,0 +1 @@ +New conformance-suite hook ``reuses_collections()``: a backend that limits how many collections may be made in a while overrides it to return ``True``, and the suite then keeps each collection it makes and uses it again, as it already did where collections cannot be deleted, rather than make one per test. The test of making and deleting a collection still makes one per run, and the first run on an account makes a few more. The CalDAV tests turn it on where caldav's ``rate-limit`` hint gives ``create_calendar_per_hour`` (ecloud, which refuses calendar creation after about 10 an hour). diff --git a/src/calendaring/_async/conformance.py b/src/calendaring/_async/conformance.py index 2ab0689..cfa1266 100644 --- a/src/calendaring/_async/conformance.py +++ b/src/calendaring/_async/conformance.py @@ -296,12 +296,13 @@ async def open_collection( ) -> AsyncCollection: """A collection to test against: made if the backend may be able to, else the first one listed. - Where collections can be made but not deleted, the one a previous run - left is used again, so a run leaves one collection per name behind. + Where collections can be made but not deleted, or ``reuses_collections()`` + says so, the one a previous test or run left is used again, so a run + leaves one collection per name behind. """ if backend.capabilities.level(Feature.CREATE_COLLECTION) is not Support.UNSUPPORTED: - deletable = backend.capabilities.level(Feature.DELETE_COLLECTION) is not Support.UNSUPPORTED - left = None if deletable else await self.left_behind(backend, name) + kept = self.keeps_collections(backend) + left = await self.left_behind(backend, name) if kept else None if left is not None: return await self.borrow(left) try: @@ -310,7 +311,7 @@ async def open_collection( if backend.capabilities.level(Feature.CREATE_COLLECTION) is not Support.UNKNOWN: raise else: - if not deletable: + if kept: return await self.borrow(made) self._made.append(made) return made @@ -319,6 +320,19 @@ async def open_collection( pytest.fail("the backend can neither create a collection nor list one") return await self.borrow(existing[0]) + def reuses_collections(self, backend: AsyncBackend) -> bool: + """Whether the suite keeps the collections it makes, to use them again, though it could delete them. + + For a backend that limits how many collections may be made in a while + (Nextcloud: about 10 an hour), where one per test would fail most of them. + """ + return False + + def keeps_collections(self, backend: AsyncBackend) -> bool: + """Whether a made collection is kept for later tests; not a hook, override ``reuses_collections()``.""" + deletable = backend.capabilities.level(Feature.DELETE_COLLECTION) is not Support.UNSUPPORTED + return not deletable or self.reuses_collections(backend) + async def left_behind(self, backend: AsyncBackend, name: str) -> AsyncCollection | None: named = [c for c in await backend.collections() if c.name == name] return named[0] if named else None @@ -1435,7 +1449,8 @@ async def test_create_collection_holds_one_of_what_was_asked(self, backend: Asyn if not backend.capabilities.supports(Feature.CREATE_COLLECTION, allow_lossy=True): pytest.skip("the backend makes no collections") deletable = backend.capabilities.level(Feature.DELETE_COLLECTION) is not Support.UNSUPPORTED - if not deletable and await self.left_behind(backend, "conformance-events") is not None: + kept = self.keeps_collections(backend) + if kept and await self.left_behind(backend, "conformance-events") is not None: return # the run that left it checked it before = [c.id for c in await backend.collections()] refused: UnsupportedError | None = None @@ -1448,7 +1463,7 @@ async def test_create_collection_holds_one_of_what_was_asked(self, backend: Asyn if deletable: assert [c.id for c in await backend.collections()] == before, "a refused collection was left behind" return - if deletable: + if not kept: self._made.append(made) assert Component.EVENT in made.components diff --git a/src/calendaring/_sync/conformance.py b/src/calendaring/_sync/conformance.py index f1377ad..b7a34a5 100644 --- a/src/calendaring/_sync/conformance.py +++ b/src/calendaring/_sync/conformance.py @@ -296,12 +296,13 @@ def open_collection( ) -> Collection: """A collection to test against: made if the backend may be able to, else the first one listed. - Where collections can be made but not deleted, the one a previous run - left is used again, so a run leaves one collection per name behind. + Where collections can be made but not deleted, or ``reuses_collections()`` + says so, the one a previous test or run left is used again, so a run + leaves one collection per name behind. """ if backend.capabilities.level(Feature.CREATE_COLLECTION) is not Support.UNSUPPORTED: - deletable = backend.capabilities.level(Feature.DELETE_COLLECTION) is not Support.UNSUPPORTED - left = None if deletable else self.left_behind(backend, name) + kept = self.keeps_collections(backend) + left = self.left_behind(backend, name) if kept else None if left is not None: return self.borrow(left) try: @@ -310,7 +311,7 @@ def open_collection( if backend.capabilities.level(Feature.CREATE_COLLECTION) is not Support.UNKNOWN: raise else: - if not deletable: + if kept: return self.borrow(made) self._made.append(made) return made @@ -319,6 +320,19 @@ def open_collection( pytest.fail("the backend can neither create a collection nor list one") return self.borrow(existing[0]) + def reuses_collections(self, backend: Backend) -> bool: + """Whether the suite keeps the collections it makes, to use them again, though it could delete them. + + For a backend that limits how many collections may be made in a while + (Nextcloud: about 10 an hour), where one per test would fail most of them. + """ + return False + + def keeps_collections(self, backend: Backend) -> bool: + """Whether a made collection is kept for later tests; not a hook, override ``reuses_collections()``.""" + deletable = backend.capabilities.level(Feature.DELETE_COLLECTION) is not Support.UNSUPPORTED + return not deletable or self.reuses_collections(backend) + def left_behind(self, backend: Backend, name: str) -> Collection | None: named = [c for c in backend.collections() if c.name == name] return named[0] if named else None @@ -1435,7 +1449,8 @@ def test_create_collection_holds_one_of_what_was_asked(self, backend: Backend) - if not backend.capabilities.supports(Feature.CREATE_COLLECTION, allow_lossy=True): pytest.skip("the backend makes no collections") deletable = backend.capabilities.level(Feature.DELETE_COLLECTION) is not Support.UNSUPPORTED - if not deletable and self.left_behind(backend, "conformance-events") is not None: + kept = self.keeps_collections(backend) + if kept and self.left_behind(backend, "conformance-events") is not None: return # the run that left it checked it before = [c.id for c in backend.collections()] refused: UnsupportedError | None = None @@ -1448,7 +1463,7 @@ def test_create_collection_holds_one_of_what_was_asked(self, backend: Backend) - if deletable: assert [c.id for c in backend.collections()] == before, "a refused collection was left behind" return - if deletable: + if not kept: self._made.append(made) assert Component.EVENT in made.components diff --git a/tests/_async/test_caldav_conformance.py b/tests/_async/test_caldav_conformance.py index 71dafca..ce52e0c 100644 --- a/tests/_async/test_caldav_conformance.py +++ b/tests/_async/test_caldav_conformance.py @@ -12,6 +12,11 @@ from calendaring.testing import AsyncConformance +def creation_limited(features: Any) -> bool: + """Whether the server lets an account make only so many calendars an hour (caldav's rate-limit hint: ecloud).""" + return bool(features.is_supported("rate-limit", dict).get("create_calendar_per_hour")) + + class CalDAVConformance(AsyncConformance): # A CalDAV server is probed, not known in advance (§5.1). allow_unknown = True @@ -23,9 +28,22 @@ async def open_backend(self) -> AsyncBackend: wait_for_server(backend, self.monkeypatch) return backend + def reuses_collections(self, backend: AsyncBackend) -> bool: + return creation_limited(backend._dav.client.features) # type: ignore[attr-defined] + class TestCalDAVServers(CalDAVConformance): @pytest.fixture(autouse=True) def _server(self, caldav_server: Any, monkeypatch: pytest.MonkeyPatch) -> None: self.server = caldav_server self.monkeypatch = monkeypatch + + +def test_a_server_limiting_calendar_creation_keeps_its_calendars() -> None: + from caldav.compatibility_hints import FeatureSet as Unannotated + + FeatureSet: Any = Unannotated # caldav leaves it unannotated + limited = FeatureSet({"rate-limit": {"enable": True, "create_calendar_per_hour": 10}}) + assert creation_limited(limited) + assert not creation_limited(FeatureSet({"rate-limit": {"enable": True, "interval": 1, "count": 1}})) + assert not creation_limited(FeatureSet({})) diff --git a/tests/_async/test_memory_conformance.py b/tests/_async/test_memory_conformance.py index e736da5..896316a 100644 --- a/tests/_async/test_memory_conformance.py +++ b/tests/_async/test_memory_conformance.py @@ -104,6 +104,41 @@ async def test_the_borrowed_collection_is_left_as_it_was(self) -> None: assert {i.native_id for i in await collection.search()} == before +class TestReusedCollections(AsyncConformance): + """Where making collections is rate-limited (Nextcloud), the suite keeps the ones it makes and uses them again. + + One backend serves every test of the class, so each test meets the + collections the tests before it kept, as on the server. + """ + + shared: ClassVar[AsyncMemoryBackend | None] = None + + def reuses_collections(self, backend: AsyncBackend) -> bool: + return True + + async def open_backend(self) -> AsyncBackend: + if TestReusedCollections.shared is None: + TestReusedCollections.shared = AsyncMemoryBackend(profile=FULL) + return TestReusedCollections.shared + + async def test_a_collection_is_made_once_and_kept(self) -> None: + backend = AsyncMemoryBackend(profile=FULL) + self._made, self._borrowed = [], [] + first = await self.open_collection(backend) + await first.add_task(summary="the suite's") + await self.tidy_up(backend) + assert first.id in [c.id for c in await backend.collections()] + assert await first.search() == [] + other = await first.add_task(summary="another client's") + self._made, self._borrowed = [], [] + again = await self.open_collection(backend) + assert again.id == first.id + await again.add_task(summary="the suite's") + await self.tidy_up(backend) + assert [i.native_id for i in await again.search()] == [other.native_id] + assert [c.name for c in await backend.collections()].count(first.name) == 1 + + class TestFeedProfile(AsyncConformance): async def open_backend(self) -> AsyncBackend: backend = AsyncMemoryBackend(profile=FEED) diff --git a/tests/_sync/test_caldav_conformance.py b/tests/_sync/test_caldav_conformance.py index 2a3ca0b..593aee2 100644 --- a/tests/_sync/test_caldav_conformance.py +++ b/tests/_sync/test_caldav_conformance.py @@ -12,6 +12,11 @@ from calendaring.testing import SyncConformance +def creation_limited(features: Any) -> bool: + """Whether the server lets an account make only so many calendars an hour (caldav's rate-limit hint: ecloud).""" + return bool(features.is_supported("rate-limit", dict).get("create_calendar_per_hour")) + + class CalDAVConformance(SyncConformance): # A CalDAV server is probed, not known in advance (§5.1). allow_unknown = True @@ -23,9 +28,22 @@ def open_backend(self) -> Backend: wait_for_server(backend, self.monkeypatch) return backend + def reuses_collections(self, backend: Backend) -> bool: + return creation_limited(backend._dav.client.features) # type: ignore[attr-defined] + class TestCalDAVServers(CalDAVConformance): @pytest.fixture(autouse=True) def _server(self, caldav_server: Any, monkeypatch: pytest.MonkeyPatch) -> None: self.server = caldav_server self.monkeypatch = monkeypatch + + +def test_a_server_limiting_calendar_creation_keeps_its_calendars() -> None: + from caldav.compatibility_hints import FeatureSet as Unannotated + + FeatureSet: Any = Unannotated # caldav leaves it unannotated + limited = FeatureSet({"rate-limit": {"enable": True, "create_calendar_per_hour": 10}}) + assert creation_limited(limited) + assert not creation_limited(FeatureSet({"rate-limit": {"enable": True, "interval": 1, "count": 1}})) + assert not creation_limited(FeatureSet({})) diff --git a/tests/_sync/test_memory_conformance.py b/tests/_sync/test_memory_conformance.py index e8a259d..2805b16 100644 --- a/tests/_sync/test_memory_conformance.py +++ b/tests/_sync/test_memory_conformance.py @@ -104,6 +104,41 @@ def test_the_borrowed_collection_is_left_as_it_was(self) -> None: assert {i.native_id for i in collection.search()} == before +class TestReusedCollections(SyncConformance): + """Where making collections is rate-limited (Nextcloud), the suite keeps the ones it makes and uses them again. + + One backend serves every test of the class, so each test meets the + collections the tests before it kept, as on the server. + """ + + shared: ClassVar[MemoryBackend | None] = None + + def reuses_collections(self, backend: Backend) -> bool: + return True + + def open_backend(self) -> Backend: + if TestReusedCollections.shared is None: + TestReusedCollections.shared = MemoryBackend(profile=FULL) + return TestReusedCollections.shared + + def test_a_collection_is_made_once_and_kept(self) -> None: + backend = MemoryBackend(profile=FULL) + self._made, self._borrowed = [], [] + first = self.open_collection(backend) + first.add_task(summary="the suite's") + self.tidy_up(backend) + assert first.id in [c.id for c in backend.collections()] + assert first.search() == [] + other = first.add_task(summary="another client's") + self._made, self._borrowed = [], [] + again = self.open_collection(backend) + assert again.id == first.id + again.add_task(summary="the suite's") + self.tidy_up(backend) + assert [i.native_id for i in again.search()] == [other.native_id] + assert [c.name for c in backend.collections()].count(first.name) == 1 + + class TestFeedProfile(SyncConformance): def open_backend(self) -> Backend: backend = MemoryBackend(profile=FEED)