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
3 changes: 3 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ annotated, and do not silence an `unused-coroutine` error.
5. Add `tests/_async/test_<name>_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

Expand Down
1 change: 1 addition & 0 deletions news/+reuse-collections.feature.rst
Original file line number Diff line number Diff line change
@@ -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).
29 changes: 22 additions & 7 deletions src/calendaring/_async/conformance.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down
29 changes: 22 additions & 7 deletions src/calendaring/_sync/conformance.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions tests/_async/test_caldav_conformance.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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({}))
35 changes: 35 additions & 0 deletions tests/_async/test_memory_conformance.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions tests/_sync/test_caldav_conformance.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions tests/_sync/test_memory_conformance.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading