From 10cd39259faf495cf93930f90e6de37f4590c65c Mon Sep 17 00:00:00 2001 From: Mathieu Leplatre Date: Thu, 9 Apr 2026 12:33:54 +0200 Subject: [PATCH] Remove useless code --- kinto/core/cornice/service.py | 4 --- kinto/core/storage/utils.py | 37 --------------------- kinto/core/utils.py | 10 ------ tests/core/support.py | 19 ++++++++++- tests/core/test_storage.py | 61 ----------------------------------- tests/core/test_utils.py | 5 +-- 6 files changed, 21 insertions(+), 115 deletions(-) delete mode 100644 kinto/core/storage/utils.py diff --git a/kinto/core/cornice/service.py b/kinto/core/cornice/service.py index 982dbf729..dd3b3d846 100644 --- a/kinto/core/cornice/service.py +++ b/kinto/core/cornice/service.py @@ -18,10 +18,6 @@ SERVICES = [] -def clear_services(): - SERVICES[:] = [] - - def get_services(names=None, exclude=None): def _keep(service): if exclude is not None and service.name in exclude: diff --git a/kinto/core/storage/utils.py b/kinto/core/storage/utils.py deleted file mode 100644 index 432f84941..000000000 --- a/kinto/core/storage/utils.py +++ /dev/null @@ -1,37 +0,0 @@ -""" -kinto.core.storage.utils: methods for making it easier to work with kinto storage objects -""" - -from kinto.core.storage import Filter -from kinto.core.utils import COMPARISON - - -BATCH_SIZE = 25 - - -def paginated(storage, *args, sorting, batch_size=BATCH_SIZE, **kwargs): - """A generator used to access paginated results from storage.list_all. - - :param kwargs: Passed through unchanged to list_all. - """ - - if len(sorting) > 1: - raise NotImplementedError("FIXME: only supports one-length sorting") # pragma: nocover - pagination_direction = COMPARISON.GT if sorting[0].direction > 0 else COMPARISON.LT - - object_pagination = None - while True: - objects = storage.list_all( - sorting=sorting, limit=batch_size, pagination_rules=object_pagination, **kwargs - ) - - if not objects: - break - - for obj in objects: - yield obj - - object_pagination = [ - # FIXME: support more than one-length sorting - [Filter(sorting[0].field, obj[sorting[0].field], pagination_direction)] - ] diff --git a/kinto/core/utils.py b/kinto/core/utils.py index 2c3a2d014..e37b646ac 100644 --- a/kinto/core/utils.py +++ b/kinto/core/utils.py @@ -510,16 +510,6 @@ def instance_uri(request, resource_name, **params): return strip_uri_prefix(request.route_path(f"{resource_name}-object", **params)) -def instance_uri_registry(registry, resource_name, **params): - """Return the URI for the given resource, even if you don't have a request. - - This gins up a request using Request.blank and so does not support - any routes with pregenerators. - """ - request = Request.blank(path="") - request.registry = registry - return instance_uri(request, resource_name, **params) - def apply_json_patch(obj, ops): """ diff --git a/tests/core/support.py b/tests/core/support.py index fc22fc08c..ab5aed846 100644 --- a/tests/core/support.py +++ b/tests/core/support.py @@ -3,15 +3,32 @@ import pytest from pyramid.authorization import Authenticated from pyramid.interfaces import IAuthorizationPolicy +from pyramid.request import Request from zope.interface import implementer from kinto.core import testing +from kinto.core.cornice.service import SERVICES from kinto.core.storage.exceptions import BackendError -from kinto.core.utils import sqlalchemy +from kinto.core.utils import instance_uri, sqlalchemy from .testapp import main as testapp +def clear_services(): + SERVICES[:] = [] + + +def instance_uri_registry(registry, resource_name, **params): + """Return the URI for the given resource, even if you don't have a request. + + This gins up a request using Request.blank and so does not support + any routes with pregenerators. + """ + request = Request.blank(path="") + request.registry = registry + return instance_uri(request, resource_name, **params) + + # This is the principal a connected user should have (in the tests). USER_PRINCIPAL = "basicauth:8a931a10fc88ab2f6d1cc02a07d3a81b5d4768f6f13e85c5d8d4180419acb1b4" diff --git a/tests/core/test_storage.py b/tests/core/test_storage.py index d03925dba..b4f64147d 100644 --- a/tests/core/test_storage.py +++ b/tests/core/test_storage.py @@ -14,7 +14,6 @@ postgresql, ) from kinto.core.storage.testing import StorageTest -from kinto.core.storage.utils import paginated from kinto.core.testing import skip_if_no_postgresql, unittest from kinto.core.utils import COMPARISON, json from kinto.core.utils import sqlalchemy as sa @@ -314,66 +313,6 @@ def test_pagination_with_modified_field_filter(self): self.assertLess(obj["last_modified"], before) -class PaginatedTest(unittest.TestCase): - def setUp(self): - self.storage = mock.Mock() - self.sample_objects = [ - {"id": "object-01", "flavor": "strawberry"}, - {"id": "object-02", "flavor": "banana"}, - {"id": "object-03", "flavor": "mint"}, - {"id": "object-04", "flavor": "plain"}, - {"id": "object-05", "flavor": "peanut"}, - ] - - def sample_objects_side_effect(*args, **kwargs): - return self.sample_objects - - self.storage.list_all.side_effect = sample_objects_side_effect - - def test_paginated_passes_sort(self): - i = paginated(self.storage, sorting=[Sort("id", -1)]) - next(i) # make the generator do anything - self.storage.list_all.assert_called_with( - sorting=[Sort("id", -1)], limit=25, pagination_rules=None - ) - - def test_paginated_passes_batch_size(self): - i = paginated(self.storage, sorting=[Sort("id", -1)], batch_size=17) - next(i) # make the generator do anything - self.storage.list_all.assert_called_with( - sorting=[Sort("id", -1)], limit=17, pagination_rules=None - ) - - def test_paginated_yields_objects(self): - iter = paginated(self.storage, sorting=[Sort("id", -1)]) - assert next(iter) == {"id": "object-01", "flavor": "strawberry"} - - def test_paginated_fetches_next_page(self): - objects = self.sample_objects - objects.reverse() - - def list_all_mock(*args, **kwargs): - this_objects = objects[:3] - del objects[:3] - return this_objects - - self.storage.list_all.side_effect = list_all_mock - - list(paginated(self.storage, sorting=[Sort("id", -1)])) - assert self.storage.list_all.call_args_list == [ - mock.call(sorting=[Sort("id", -1)], limit=25, pagination_rules=None), - mock.call( - sorting=[Sort("id", -1)], - limit=25, - pagination_rules=[[Filter("id", "object-03", COMPARISON.LT)]], - ), - mock.call( - sorting=[Sort("id", -1)], - limit=25, - pagination_rules=[[Filter("id", "object-01", COMPARISON.LT)]], - ), - ] - class FormatConditionsEQContainmentTest(unittest.TestCase): """Test that _format_conditions uses JSONB containment (@>) for EQ filters diff --git a/tests/core/test_utils.py b/tests/core/test_utils.py index 56e90023a..d6c077fef 100644 --- a/tests/core/test_utils.py +++ b/tests/core/test_utils.py @@ -16,7 +16,6 @@ find_nested_value, follow_subrequest, hmac_digest, - instance_uri_registry, native_value, prefixed_principals, random_bytes_hex, @@ -25,6 +24,8 @@ strip_whitespace, ) +from .support import instance_uri_registry + def build_real_request(wsgi_environ): """Build a Pyramid request, as if it was instantiated by Pyramid.""" @@ -317,7 +318,7 @@ def test_merge_non_dict(self): class InstanceURIRegistryTest(unittest.TestCase): - @mock.patch("kinto.core.utils.instance_uri") + @mock.patch("tests.core.support.instance_uri") def test_instance_uri_registry_calls_instance_uri(self, instance_uri): registry = mock.Mock() instance_uri_registry(registry, "object", a=1)