Skip to content

Commit

Permalink
Merge pull request #8099 from ckan/create_with_upload-with-better-sto…
Browse files Browse the repository at this point in the history
…rage

create_with_upload fixture with more "native" FileStorage
  • Loading branch information
wardi committed Mar 5, 2024
2 parents 6905235 + 28a61f3 commit 0b3935d
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions ckan/tests/pytest_ckan/fixtures.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""This is a collection of pytest fixtures for use in tests.
All fixtures below available anywhere under the root of CKAN
Expand Down Expand Up @@ -27,10 +26,12 @@
<https://docs.pytest.org/en/latest/fixture.html>`_
"""
from __future__ import annotations

import smtplib

from io import BytesIO
from typing import Any, IO
import copy

import pytest
Expand All @@ -45,6 +46,7 @@
import ckan.plugins
import ckan.cli
import ckan.model as model
from ckan import types
from ckan.common import config
from ckan.lib import redis, search

Expand Down Expand Up @@ -471,12 +473,8 @@ def test_example():


class FakeFileStorage(FlaskFileStorage):
content_type = None

def __init__(self, stream, filename):
self.stream = stream
self.filename = filename
self.name = u"upload"
def __init__(self, stream: IO[bytes], filename: str):
super(FakeFileStorage, self).__init__(stream, filename, "uplod")


@pytest.fixture
Expand Down Expand Up @@ -509,11 +507,18 @@ def test_uploaded_resource(create_with_upload):
"""
monkeypatch.setitem(ckan_config, u'ckan.storage_path', str(tmpdir))

def factory(data, filename, context={}, **kwargs):
def factory(
data: str | bytes,
filename: str,
context: types.Context | None = None,
**kwargs: Any
):
if context is None:
context = {}
action = kwargs.pop("action", "resource_create")
field = kwargs.pop("upload_field_name", "upload")
test_file = BytesIO()
if not isinstance(data, bytes):
if isinstance(data, str):
data = bytes(data, encoding="utf-8")
test_file.write(data)
test_file.seek(0)
Expand Down

0 comments on commit 0b3935d

Please sign in to comment.