Skip to content

Commit fd8d86d

Browse files
authored
test: automatically count queries in webtest (#19544)
* test: automatically count queries in webtest Signed-off-by: Mike Fiedler <miketheman@gmail.com> * test: add functional test for JSON API Signed-off-by: Mike Fiedler <miketheman@gmail.com> --------- Signed-off-by: Mike Fiedler <miketheman@gmail.com>
1 parent 84e0d31 commit fd8d86d

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

tests/conftest.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,23 @@ def _make_email_renderers(
697697

698698

699699
class _TestApp(_webtest.TestApp):
700+
def __init__(self, app, engine, **kwargs):
701+
super().__init__(app, **kwargs)
702+
self._engine = engine
703+
self.query_recorder = QueryRecorder()
704+
event.listen(self._engine, "before_cursor_execute", self.query_recorder.record)
705+
706+
def do_request(self, *args, **kwargs):
707+
self.query_recorder.clear()
708+
self.query_recorder.start()
709+
try:
710+
return super().do_request(*args, **kwargs)
711+
finally:
712+
self.query_recorder.stop()
713+
714+
def close(self):
715+
event.remove(self._engine, "before_cursor_execute", self.query_recorder.record)
716+
700717
def xmlrpc(self, path, method, *args):
701718
body = xmlrpc.client.dumps(args, methodname=method)
702719
resp = self.post(path, body, headers={"Content-Type": "text/xml"})
@@ -731,12 +748,14 @@ def webtest(app_config_dbsession_from_env, tm):
731748
app_config_dbsession_from_env.add_settings(enforce_https=False)
732749

733750
app = app_config_dbsession_from_env.make_wsgi_app()
751+
engine = app_config_dbsession_from_env.registry["sqlalchemy.engine"]
734752

735753
with get_db_session_for_app_config(app_config_dbsession_from_env) as _db_session:
736754
# Register the app with the external test environment, telling
737755
# request.db to use this db_session and use the Transaction manager.
738756
testapp = _TestApp(
739757
app,
758+
engine,
740759
extra_environ={
741760
"warehouse.db_session": _db_session,
742761
"tm.active": True, # disable pyramid_tm
@@ -745,6 +764,7 @@ def webtest(app_config_dbsession_from_env, tm):
745764
},
746765
)
747766
yield testapp
767+
testapp.close()
748768

749769

750770
class _MockRedis:
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
from tests.common.db.packaging import ReleaseFactory
4+
5+
6+
def test_json_project(webtest):
7+
"""
8+
Testing JSON API for basic structure
9+
10+
Exercises `warehouse.legacy.api.json.json_project` with full web stack
11+
"""
12+
release = ReleaseFactory.create()
13+
14+
resp = webtest.get(f"/pypi/{release.project.normalized_name}/json")
15+
16+
assert resp.status_code == 200
17+
assert resp.headers["Cache-Control"] == "max-age=900, public"
18+
assert resp.headers["Content-Type"] == "application/json"
19+
assert resp.headers["X-PyPI-Last-Serial"] == str(release.project.last_serial)
20+
# How many database calls are needed to satisfy the data
21+
assert len(webtest.query_recorder.queries) == 7

0 commit comments

Comments
 (0)