Skip to content

Commit acbccb9

Browse files
committed
code review feedback
1 parent 87c2f92 commit acbccb9

File tree

3 files changed

+62
-53
lines changed

3 files changed

+62
-53
lines changed

src/lando/api/tests/conftest.py

+60-49
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ def proxy_client(monkeypatch):
515515
reimplemented to not need a response or response-like object.
516516
"""
517517

518-
class Response:
518+
class MockResponse:
519519
"""Mock response class to satisfy some requirements of tests."""
520520

521521
# NOTE: The methods tested that rely on this class should be reimplemented
@@ -524,39 +524,68 @@ def __init__(self, status_code=200, json=None):
524524
self.json = json or {}
525525
self.status_code = status_code
526526
self.content_type = (
527-
"application/json"
528-
if str(status_code)[0] not in ("4", "5")
529-
else "application/problem+json"
527+
"application/json" if status_code < 400 else "application/problem+json"
530528
)
531529

532-
class _proxy:
530+
class ProxyClient:
531+
def _handle__get__stacks__id(self, path):
532+
revision_id = path.lstrip("/stacks/")
533+
json_response = legacy_api_stacks.get(revision_id)
534+
if isinstance(json_response, HttpResponse):
535+
# In some cases, an actual response object is returned.
536+
return json_response
537+
# In other cases, just the data is returned, and it should be
538+
# mapped to a response.
539+
return MockResponse(json=json.loads(json.dumps(json_response)))
540+
541+
def _handle__get__transplants__id(self, path):
542+
stack_revision_id = path.lstrip("/transplants?stack_revision_id=")
543+
result = legacy_api_transplants.get_list(
544+
stack_revision_id=stack_revision_id
545+
)
546+
if isinstance(result, tuple):
547+
# For these endpoints, some responses contain different status codes
548+
# which are represented as the second item in a tuple.
549+
json_response, status_code = result
550+
return MockResponse(
551+
json=json.loads(json.dumps(json_response)),
552+
status_code=status_code,
553+
)
554+
# In the rest of the cases, the returned result is a response object.
555+
return result
556+
557+
def _handle__post__transplants__dryrun(self, **kwargs):
558+
json_response = legacy_api_transplants.dryrun(kwargs["json"])
559+
return MockResponse(json=json.loads(json.dumps(json_response)))
560+
561+
def _handle__post__transplants(self, path, **kwargs):
562+
try:
563+
json_response, status_code = legacy_api_transplants.post(kwargs["json"])
564+
except ProblemException as e:
565+
# Handle exceptions and pass along the status code to the response object.
566+
if e.json_detail:
567+
return MockResponse(json=e.json_detail, status_code=e.status_code)
568+
return MockResponse(json=e.args, status_code=e.status_code)
569+
except Exception:
570+
# TODO: double check that this is a thing in legacy?
571+
# Added this due to a validation error (test_transplant_wrong_landing_path_format)
572+
return MockResponse(json=["error"], status_code=400)
573+
return MockResponse(
574+
json=json.loads(json.dumps(json_response)), status_code=status_code
575+
)
576+
577+
def _handle__put__landing_jobs__id(self, path, **kwargs):
578+
job_id = int(path.lstrip("/landing_jobs/"))
579+
json_response = legacy_api_landing_jobs.put(job_id, kwargs["json"])
580+
return MockResponse(json=json.loads(json.dumps(json_response)))
581+
533582
def get(self, path, *args, **kwargs):
534583
"""Handle various get endpoints."""
535584
if path.startswith("/stacks/D"):
536-
revision_id = path.lstrip("/stacks/")
537-
json_response = legacy_api_stacks.get(revision_id)
538-
if isinstance(json_response, HttpResponse):
539-
# In some cases, an actual response object is returned.
540-
return json_response
541-
# In other cases, just the data is returned, and it should be
542-
# mapped to a response.
543-
return Response(json=json.loads(json.dumps(json_response)))
585+
return self._handle__get__stacks__id(path)
544586

545587
if path.startswith("/transplants?"):
546-
stack_revision_id = path.lstrip("/transplants?stack_revision_id=")
547-
result = legacy_api_transplants.get_list(
548-
stack_revision_id=stack_revision_id
549-
)
550-
if isinstance(result, tuple):
551-
# For these endpoints, some responses contain different status codes
552-
# which are represented as the second item in a tuple.
553-
json_response, status_code = result
554-
return Response(
555-
json=json.loads(json.dumps(json_response)),
556-
status_code=status_code,
557-
)
558-
# In the rest of the cases, the returned result is a response object.
559-
return result
588+
return self._handle__get__transplants__id(path)
560589

561590
def post(self, path, **kwargs):
562591
"""Handle various post endpoints."""
@@ -565,26 +594,10 @@ def post(self, path, **kwargs):
565594
monkeypatch.setattr("lando.api.legacy.auth.request", mock_request)
566595

567596
if path.startswith("/transplants/dryrun"):
568-
json_response = legacy_api_transplants.dryrun(kwargs["json"])
569-
return Response(json=json.loads(json.dumps(json_response)))
597+
return self._handle__post__transplants__dryrun(**kwargs)
570598

571599
if path == "/transplants":
572-
try:
573-
json_response, status_code = legacy_api_transplants.post(
574-
kwargs["json"]
575-
)
576-
except ProblemException as e:
577-
# Handle exceptions and pass along the status code to the response object.
578-
if e.json_detail:
579-
return Response(json=e.json_detail, status_code=e.status_code)
580-
return Response(json=e.args, status_code=e.status_code)
581-
except Exception:
582-
# TODO: double check that this is a thing in legacy?
583-
# Added this due to a validation error (test_transplant_wrong_landing_path_format)
584-
return Response(json=["error"], status_code=400)
585-
return Response(
586-
json=json.loads(json.dumps(json_response)), status_code=status_code
587-
)
600+
return self._handle__post__transplants(path, **kwargs)
588601

589602
def put(self, path, **kwargs):
590603
"""Handle put endpoints."""
@@ -593,8 +606,6 @@ def put(self, path, **kwargs):
593606
monkeypatch.setattr("lando.api.legacy.auth.request", mock_request)
594607

595608
if path.startswith("/landing_jobs/"):
596-
job_id = int(path.lstrip("/landing_jobs/"))
597-
json_response = legacy_api_landing_jobs.put(job_id, kwargs["json"])
598-
return Response(json=json.loads(json.dumps(json_response)))
609+
return self._handle__put__landing_jobs__id(path, **kwargs)
599610

600-
return _proxy()
611+
return ProxyClient()

src/lando/main/models/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class Meta:
4949

5050
@classmethod
5151
@property
52-
def lock_table(cls, *args, **kwargs):
52+
def lock_table(cls):
5353
return LockTableContextManager(cls)
5454

5555
@classmethod

src/lando/main/models/landing_job.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
)
1313

1414
from django.db import models
15-
from django.db.models import QuerySet, Case, When, IntegerField
15+
from django.db.models import Case, IntegerField, Q, QuerySet, When
1616
from django.utils.translation import gettext_lazy
1717
from mots.config import FileConfig
1818
from mots.directory import Directory
@@ -182,8 +182,6 @@ def revisions_query(cls, revisions: Iterable[str]) -> QuerySet:
182182
that stores revisions and diff IDs. Those records are now deprecated and will
183183
not be included in this query.
184184
"""
185-
from django.db.models import Q
186-
187185
revisions = [str(int(r)) for r in revisions]
188186
return cls.objects.filter(
189187
Q(unsorted_revisions__revision_id__in=revisions)

0 commit comments

Comments
 (0)