Skip to content

Commit

Permalink
chore: py2 compatible tests
Browse files Browse the repository at this point in the history
  • Loading branch information
smotornyuk committed Mar 11, 2024
1 parent c77c7aa commit c3f5fec
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 15 deletions.
10 changes: 6 additions & 4 deletions ckanext/files/logic/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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")

Expand All @@ -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")

Expand Down
4 changes: 2 additions & 2 deletions ckanext/files/logic/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
27 changes: 20 additions & 7 deletions ckanext/files/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion ckanext/files/tests/logic/test_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
)


Expand Down
4 changes: 4 additions & 0 deletions ckanext/files/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
3 changes: 2 additions & 1 deletion ckanext/files/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ test =
pytest-ckan
pytest-cov
pytest-faker
pytest-freezegun
responses

dev =
Expand Down

0 comments on commit c3f5fec

Please sign in to comment.