From c3f5fecf3de96b0cf2b857227c40228e61579dd4 Mon Sep 17 00:00:00 2001 From: Sergey Motornyuk Date: Tue, 12 Mar 2024 01:03:43 +0200 Subject: [PATCH] chore: py2 compatible tests --- ckanext/files/logic/action.py | 10 +++++---- ckanext/files/logic/validators.py | 4 ++-- ckanext/files/tests/conftest.py | 27 ++++++++++++++++++------ ckanext/files/tests/logic/test_action.py | 2 +- ckanext/files/tests/test_config.py | 4 ++++ ckanext/files/utils.py | 3 ++- setup.cfg | 1 + 7 files changed, 36 insertions(+), 15 deletions(-) diff --git a/ckanext/files/logic/action.py b/ckanext/files/logic/action.py index 073a1f7..b63f0d6 100644 --- a/ckanext/files/logic/action.py +++ b/ckanext/files/logic/action.py @@ -142,8 +142,10 @@ def _add_owner(context, item_type, item_id): def _delete_owners(context, item_type, item_id): # type: (types.Any, str, str) -> None stmt = sa.delete(Owner).where( - Owner.item_type == item_type, - Owner.item_id == item_id, + sa.and_( + Owner.item_type == item_type, + Owner.item_id == item_id, + ) ) context["session"].execute(stmt) @@ -155,7 +157,7 @@ def files_file_delete(context, data_dict): tk.check_access("files_file_delete", context, data_dict) data_dict["id"] - fileobj = context["session"].get(File, data_dict["id"]) + fileobj = context["session"].query(File).filter_by(id=data_dict["id"]).one_or_none() if not fileobj: raise tk.ObjectNotFound("file") @@ -179,7 +181,7 @@ def files_file_show(context, data_dict): tk.check_access("files_file_show", context, data_dict) data_dict["id"] - fileobj = context["session"].get(File, data_dict["id"]) + fileobj = context["session"].query(File).filter_by(id=data_dict["id"]).one_or_none() if not fileobj: raise tk.ObjectNotFound("file") diff --git a/ckanext/files/logic/validators.py b/ckanext/files/logic/validators.py index e72341a..e276803 100644 --- a/ckanext/files/logic/validators.py +++ b/ckanext/files/logic/validators.py @@ -50,10 +50,10 @@ def files_into_upload(value): content_length=size, ) - if isinstance(value, str): + if isinstance(value, six.text_type): value = value.encode() - if isinstance(value, bytes): + if isinstance(value, (bytes, bytearray)): stream = BytesIO(value) mime = magic.from_buffer(stream.read(1024), True) size = stream.seek(0, 2) diff --git a/ckanext/files/tests/conftest.py b/ckanext/files/tests/conftest.py index 1e576dd..5d78a00 100644 --- a/ckanext/files/tests/conftest.py +++ b/ckanext/files/tests/conftest.py @@ -6,16 +6,29 @@ from ckan.lib.redis import connect_to_redis from ckan.tests.helpers import call_action +import ckan.plugins.toolkit as tk if six.PY3: from typing import Any # isort: skip # noqa: F401 -@pytest.fixture -def clean_db(reset_db, migrate_db_for): - # type: (Any, Any) -> None - reset_db() - migrate_db_for("files") +if tk.check_ckan_version("2.9"): + @pytest.fixture + def clean_db(reset_db, migrate_db_for): + # type: (Any, Any) -> None + reset_db() + migrate_db_for("files") + +else: + @pytest.fixture + def clean_db(reset_db): + # type: (Any) -> None + from ckanext.files.command import create_tables, drop_tables + reset_db() + drop_tables() + create_tables() + + class FakeFileStorage(FileStorage): @@ -43,8 +56,8 @@ def factory(data, filename, context=None, **kwargs): action = kwargs.pop("action", "resource_create") field = kwargs.pop("upload_field_name", "upload") test_file = BytesIO() - if not isinstance(data, bytes): - data = bytes(data, encoding="utf-8") + if isinstance(data, six.text_type): + data = data.encode() test_file.write(data) test_file.seek(0) test_resource = FakeFileStorage(test_file, filename) diff --git a/ckanext/files/tests/logic/test_action.py b/ckanext/files/tests/logic/test_action.py index f1ee6aa..3fadf6c 100644 --- a/ckanext/files/tests/logic/test_action.py +++ b/ckanext/files/tests/logic/test_action.py @@ -14,7 +14,7 @@ def random_file(create_with_upload, faker): faker.binary(10), faker.file_name(), action="files_file_create", - name=faker.unique.name(), + name=faker.name(), ) diff --git a/ckanext/files/tests/test_config.py b/ckanext/files/tests/test_config.py index 24b9aca..836a6b1 100644 --- a/ckanext/files/tests/test_config.py +++ b/ckanext/files/tests/test_config.py @@ -33,6 +33,10 @@ def adapt_to_ckan_version(self, settings): if tk.check_ckan_version("2.10"): settings.setdefault("max_size", 0) + else: + settings.pop("prefix") + settings.pop("name") + return settings def test_empty(self): diff --git a/ckanext/files/utils.py b/ckanext/files/utils.py index 16e625e..eef8ad5 100644 --- a/ckanext/files/utils.py +++ b/ckanext/files/utils.py @@ -116,7 +116,8 @@ def ensure_size(upload, max_size): filesize = upload.content_length if not filesize: - filesize = upload.stream.seek(0, 2) + # in py2 .seek returns None for empty stream. + filesize = upload.stream.seek(0, 2) or 0 upload.stream.seek(0) if filesize > max_size: diff --git a/setup.cfg b/setup.cfg index d4f92de..47a7ace 100644 --- a/setup.cfg +++ b/setup.cfg @@ -35,6 +35,7 @@ test = pytest-ckan pytest-cov pytest-faker + pytest-freezegun responses dev =