Skip to content

Commit 38c2d3c

Browse files
committed
testing: fix up various aspects that are breaking tests (bug 1887042)
1 parent fa23f51 commit 38c2d3c

15 files changed

+35
-80
lines changed

pyproject.toml

+3
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,6 @@ build-backend = "setuptools.build_meta"
3232

3333
[tool.setuptools.packages.find]
3434
where = ["src"]
35+
36+
[tool.pytest.ini_options]
37+
DJANGO_SETTINGS_MODULE = "lando.settings"

requirements.txt

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
black
22
jinja2
3+
kombu
34
pytest
45
pytest-django
6+
python-hglib
7+
python-jose
8+
redis
9+
requests-mock
510
ruff
611
uwsgi

src/lando/api/legacy/projects.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
import logging
55
from typing import Optional
66

7-
from lando.api.legacy.cache import DEFAULT_CACHE_KEY_TIMEOUT_SECONDS, cache
7+
from django.core.cache import cache
8+
89
from lando.api.legacy.phabricator import PhabricatorClient, result_list_to_phid_dict
910

1011
logger = logging.getLogger(__name__)
@@ -57,7 +58,7 @@ def project_search(
5758
"project.search", constraints={"phids": project_phids}
5859
)
5960
result = result_list_to_phid_dict(phabricator.expect(projects, "data"))
60-
cache.set(cache_key, result, timeout=DEFAULT_CACHE_KEY_TIMEOUT_SECONDS)
61+
cache.set(cache_key, result)
6162
return result
6263

6364

@@ -92,7 +93,7 @@ def get_project_phid(
9293
)
9394

9495
value = phabricator.expect(project, "phid") if project else None
95-
cache.set(key, value, timeout=DEFAULT_CACHE_KEY_TIMEOUT_SECONDS)
96+
cache.set(key, value)
9697
return value
9798

9899

src/lando/api/legacy/uplift.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
Version,
1919
)
2020

21-
from lando.api import bmo
22-
from lando.api.legacy.cache import DEFAULT_CACHE_KEY_TIMEOUT_SECONDS, cache
21+
from lando.api.legacy import bmo
2322
from lando.api.legacy.phabricator import PhabricatorClient
2423
from lando.api.legacy.phabricator_patch import patch_to_changes
2524
from lando.api.legacy.repos import (
@@ -68,9 +67,9 @@ def get_uplift_request_form(revision: dict) -> Optional[str]:
6867
return bug
6968

7069

71-
@cache.cached(
72-
key_prefix="uplift-repositories", timeout=DEFAULT_CACHE_KEY_TIMEOUT_SECONDS
73-
)
70+
#@cache.cached(
71+
# key_prefix="uplift-repositories"
72+
#)
7473
def get_uplift_repositories(phab: PhabricatorClient) -> list:
7574
repos = phab.call_conduit(
7675
"diffusion.repository.search",

src/lando/api/legacy/workers/landing_worker.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
TreeApprovalRequired,
2727
TreeClosed,
2828
)
29-
from lando.api.legacy.models.configuration import ConfigurationKey
30-
from lando.api.legacy.models.landing_job import LandingJob, LandingJobAction, LandingJobStatus
29+
from lando.main.models.configuration import ConfigurationKey
30+
from lando.main.models.landing_job import LandingJob, LandingJobAction, LandingJobStatus
3131
from lando.api.legacy.notifications import (
3232
notify_user_of_bug_update_failure,
3333
notify_user_of_landing_failure,

src/lando/api/tests/conftest.py

+8-60
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,15 @@
99
from pathlib import Path
1010
from types import SimpleNamespace
1111

12-
import flask.testing
12+
from django.test import Client
1313
import pytest
1414
import redis
1515
import requests
1616
import requests_mock
17-
import sqlalchemy
18-
from flask import current_app
19-
from pytest_flask.plugin import JSONResponse
17+
from django.http import JsonResponse as JSONResponse
18+
from django.core.cache import cache
2019

21-
from lando.api.legacy.app import SUBSYSTEMS, construct_app, load_config
22-
from lando.api.legacy.cache import cache
20+
from lando import settings
2321
from lando.api.legacy.mocks.auth import TEST_JWKS, MockAuth0
2422
from lando.api.legacy.phabricator import PhabricatorClient
2523
from lando.api.legacy.projects import (
@@ -29,10 +27,8 @@
2927
SEC_PROJ_SLUG,
3028
)
3129
from lando.api.legacy.repos import SCM_LEVEL_1, SCM_LEVEL_3, Repo
32-
from lando.api.legacy.storage import db as _db
33-
from lando.api.legacy.tasks import celery
3430
from lando.api.legacy.transplants import CODE_FREEZE_OFFSET, tokens_are_equal
35-
from tests.mocks import PhabricatorDouble, TreeStatusDouble
31+
from lando.api.tests.mocks import PhabricatorDouble, TreeStatusDouble
3632

3733
PATCH_NORMAL_1 = r"""
3834
# HG changeset patch
@@ -102,7 +98,7 @@ def _patch(number=0):
10298
return _patch
10399

104100

105-
class JSONClient(flask.testing.FlaskClient):
101+
class JSONClient(Client):
106102
"""Custom Flask test client that sends JSON by default.
107103
108104
HTTP methods have a 'json=...' keyword that will JSON-encode the
@@ -242,39 +238,6 @@ def init_app(self, app, db):
242238
monkeypatch.setattr("landoapi.storage.migrate", StubAlembic())
243239

244240

245-
@pytest.fixture
246-
def app(versionfile, docker_env_vars, disable_migrations, mocked_repo_config):
247-
"""Needed for pytest-flask."""
248-
config = load_config()
249-
# We need the TESTING setting turned on to get tracebacks when testing API
250-
# endpoints with the TestClient.
251-
config["TESTING"] = True
252-
config["CACHE_DISABLED"] = True
253-
app = construct_app(config)
254-
flask_app = app.app
255-
flask_app.test_client_class = JSONClient
256-
for system in SUBSYSTEMS:
257-
system.init_app(flask_app)
258-
259-
return flask_app
260-
261-
262-
@pytest.fixture
263-
def db(app):
264-
"""Reset database for each test."""
265-
try:
266-
_db.engine.connect()
267-
except sqlalchemy.exc.OperationalError:
268-
if EXTERNAL_SERVICES_SHOULD_BE_PRESENT:
269-
raise
270-
else:
271-
pytest.skip("Could not connect to PostgreSQL")
272-
_db.create_all()
273-
yield _db
274-
_db.session.remove()
275-
_db.drop_all()
276-
277-
278241
@pytest.fixture
279242
def jwks(monkeypatch):
280243
monkeypatch.setattr("landoapi.auth.get_jwks", lambda *args, **kwargs: TEST_JWKS)
@@ -356,8 +319,8 @@ def set_value(val):
356319
@pytest.fixture
357320
def get_phab_client(app):
358321
def get_client(api_key=None):
359-
api_key = api_key or current_app.config["PHABRICATOR_UNPRIVILEGED_API_KEY"]
360-
return PhabricatorClient(current_app.config["PHABRICATOR_URL"], api_key)
322+
api_key = api_key or settings.PHABRICATOR_UNPRIVILEGED_API_KEY
323+
return PhabricatorClient(settings.PHABRICATOR_URL, api_key)
361324

362325
return get_client
363326

@@ -379,21 +342,6 @@ def redis_cache(app):
379342
cache.init_app(app, config={"CACHE_TYPE": "null", "CACHE_NO_NULL_WARNING": True})
380343

381344

382-
@pytest.fixture
383-
def celery_app(app):
384-
"""Configure our app's Celery instance for use with the celery_worker fixture."""
385-
# The test suite will fail if we don't override the default worker and
386-
# default task set.
387-
# Note: the test worker will fail if we don't specify a result_backend. The test
388-
# harness uses the backend for a custom ping() task that it uses as a health check.
389-
celery.conf.update(broker_url="memory://", result_backend="rpc")
390-
# Workaround for https://github.com/celery/celery/issues/4032. If 'tasks.ping' is
391-
# missing from the loaded task list then the test worker will fail with an
392-
# AssertionError.
393-
celery.loader.import_module("celery.contrib.testing.tasks")
394-
return celery
395-
396-
397345
@pytest.fixture
398346
def treestatus_url():
399347
"""A string holding the Tree Status base URL."""

src/lando/api/tests/mocks.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
ReviewerStatus,
1414
)
1515
from lando.api.legacy.treestatus import TreeStatus, TreeStatusError
16-
from tests.canned_responses.phabricator.diffs import (
16+
from lando.api.tests.canned_responses.phabricator.diffs import (
1717
CANNED_DEFAULT_DIFF_CHANGES,
1818
CANNED_RAW_DEFAULT_DIFF,
1919
)

src/lando/api/tests/test_try.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
get_timestamp_from_git_date_header,
1212
parse_git_author_information,
1313
)
14-
from lando.api.legacy.models.landing_job import LandingJob, LandingJobStatus
14+
from lando.main.models.landing_job import LandingJob, LandingJobStatus
1515
from lando.api.legacy.repos import SCM_LEVEL_1, Repo
1616
from lando.api.legacy.workers.landing_worker import LandingWorker
1717

File renamed without changes.

src/lando/main/admin.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from django.contrib import admin
22

3-
from lando.main.models import LandingJob, Repo, Revision, Worker
3+
from lando.main.models.landing_job import LandingJob
4+
from lando.main.models.revision import Revision
5+
from lando.main.models.base import Repo, Worker
46

57
admin.site.register(LandingJob, admin.ModelAdmin)
68
admin.site.register(Revision, admin.ModelAdmin)

src/lando/main/models/__init__.py

-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +0,0 @@
1-
from lando.main.models.base import *
2-
from lando.main.models.landing_job import *
3-
from lando.main.models.revision import *

src/lando/main/models/configuration.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from django.db import models
99
from django.utils.translation import gettext_lazy
1010

11-
from lando.main.models import BaseModel
11+
from lando.main.models.base import BaseModel
1212

1313
logger = logging.getLogger(__name__)
1414

src/lando/main/models/landing_job.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from mots.config import FileConfig
1818
from mots.directory import Directory
1919

20-
from lando.main.models import BaseModel
20+
from lando.main.models.base import BaseModel
2121
from lando.main.models.revision import Revision, RevisionLandingJob
2222

2323
logger = logging.getLogger(__name__)

src/lando/main/models/revision.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from django.db import models
1414
from django.utils.translation import gettext_lazy
1515

16-
from lando.main.models import BaseModel
16+
from lando.main.models.base import BaseModel
1717
from lando.utils import build_patch_for_revision
1818

1919
logger = logging.getLogger(__name__)

src/lando/settings.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
"DIRS": [],
6060
"APP_DIRS": True,
6161
"OPTIONS": {
62-
"environment": "lando.jinja2.environment"
62+
"environment": "lando.jinja.environment"
6363
},
6464
},
6565
{

0 commit comments

Comments
 (0)