Skip to content

Commit

Permalink
tests(agent): backport TestResponse werkzeug<0.15
Browse files Browse the repository at this point in the history
  • Loading branch information
rezib committed Dec 4, 2024
1 parent 36c36cc commit 28f0900
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
7 changes: 4 additions & 3 deletions slurmweb/tests/lib/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,12 @@ def setup_client(self, additional_conf=None):
),
duration=3600,
)
# werkzeug.test.TestResponse class does not have text property in
# werkzeug <= 2.1. When such version is installed, use custom test
# response class to backport this text property.
# werkzeug.test.TestResponse class does not have text and json
# properties in werkzeug <= 0.15. When such version is installed, use
# custom test response class to backport these text and json properties.
try:
getattr(werkzeug.test.TestResponse, "text")
getattr(werkzeug.test.TestResponse, "json")
except AttributeError:
self.app.response_class = SlurmwebCustomTestResponse
self.client = self.app.test_client()
Expand Down
8 changes: 7 additions & 1 deletion slurmweb/tests/lib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,18 @@ def mock_slurmrestd_responses(slurmrestd, slurm_version, assets):

class SlurmwebCustomTestResponse(flask.Response):
"""Custom flask Response class to backport text property of
werkzeug.test.TestResponse class on werkzeug < 2.1 on Python 3.6."""
werkzeug.test.TestResponse class on werkzeug < 0.15."""

@property
def text(self):
return self.get_data(as_text=True)

@property
def json(self):
if not self.mimetype != "application/json":
return None
return json.loads(self.text)


def fake_text_response():
"""Return a real requests.Response() initialized with fake text content, designed
Expand Down
7 changes: 4 additions & 3 deletions slurmweb/tests/views/test_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@
class TestGatewayViews(TestGatewayBase):
def setUp(self):
self.setup_app()
# werkzeug.test.TestResponse class does not have text property in
# werkzeug <= 2.1. When such version is installed, use custom test
# response class to backport this text property.
# werkzeug.test.TestResponse class does not have text and json
# properties in werkzeug <= 0.15. When such version is installed, use
# custom test response class to backport these text and json properties.
try:
getattr(werkzeug.test.TestResponse, "text")
getattr(werkzeug.test.TestResponse, "json")
except AttributeError:
self.app.response_class = SlurmwebCustomTestResponse
self.client = self.app.test_client()
Expand Down

0 comments on commit 28f0900

Please sign in to comment.