Skip to content

Commit

Permalink
fix failing test due to changed used of request.json to request.text …
Browse files Browse the repository at this point in the history
…for process deploy
  • Loading branch information
fmigneault committed Jun 10, 2022
1 parent 21cffef commit be35e1a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
5 changes: 2 additions & 3 deletions tests/test_opensearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
import mock
import pytest
from pyramid import testing
from pyramid.testing import DummyRequest
from pywps.inout.inputs import LiteralInput

from tests.utils import setup_mongodb_processstore
from tests.utils import MockedRequest, setup_mongodb_processstore
from weaver.processes import opensearch
from weaver.processes.constants import OpenSearchField
from weaver.processes.opensearch import make_param_id
Expand Down Expand Up @@ -61,7 +60,7 @@ def load_json_test_file(filename):


def make_request(**kw):
request = DummyRequest(**kw)
request = MockedRequest(**kw)
if request.registry.settings is None:
request.registry.settings = {}
request.registry.settings["weaver.url"] = "localhost"
Expand Down
26 changes: 23 additions & 3 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from pyramid.config import Configurator
from pyramid.httpexceptions import HTTPException, HTTPNotFound, HTTPUnprocessableEntity
from pyramid.registry import Registry
from pyramid.testing import DummyRequest
from requests import Response
from webtest import TestApp, TestResponse

Expand All @@ -45,6 +46,7 @@
from weaver.formats import ContentType
from weaver.store.mongodb import MongodbJobStore, MongodbProcessStore, MongodbServiceStore
from weaver.utils import (
bytes2str,
fetch_file,
get_header,
get_path_kvp,
Expand All @@ -64,7 +66,14 @@
from owslib.wps import Process as ProcessOWSWPS
from pywps.app import Process as ProcessPyWPS

from weaver.typedefs import AnyHeadersContainer, AnyRequestMethod, AnyRequestType, AnyResponseType, SettingsType
from weaver.typedefs import (
JSON,
AnyHeadersContainer,
AnyRequestMethod,
AnyRequestType,
AnyResponseType,
SettingsType
)

# pylint: disable=C0103,invalid-name,E1101,no-member
MockPatch = mock._patch # noqa
Expand Down Expand Up @@ -385,12 +394,23 @@ def run_command(command, trim=True, expect_error=False, entrypoint=None):
return out_lines


class MockedRequest(DummyRequest):
"""
Patch missing properties that are expected from :mod:`pyramid` requests.
"""
json = {} # type: JSON

@property
def text(self):
return bytes2str(self.body) if self.body else json.dumps(self.json, ensure_ascii=False)


class MockedResponse(TestResponse):
"""
Replaces the ``json`` property by the expected callable from all real response implementations.
Replaces the ``json`` property by the expected callable from responses using :mod:`requests` implementation.
"""
def json(self): # pylint: disable=W0236,invalid-overridden-method
return self.json_body or json.loads(self.body.decode("UTF-8"))
return self.json_body or json.loads(bytes2str(self.body))


def mocked_file_response(path, url):
Expand Down
4 changes: 2 additions & 2 deletions weaver/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -883,7 +883,7 @@ def str2bytes(string):
raise TypeError(f"Cannot convert item to bytes: {type(string)!r}")
if isinstance(string, bytes):
return string
return string.encode()
return string.encode("UTF-8")


def bytes2str(string):
Expand All @@ -895,7 +895,7 @@ def bytes2str(string):
raise TypeError(f"Cannot convert item to unicode: {type(string)!r}")
if not isinstance(string, bytes):
return string
return string.decode()
return string.decode("UTF-8")


def islambda(func):
Expand Down

0 comments on commit be35e1a

Please sign in to comment.