Skip to content

Commit

Permalink
Tell warehouse.db to reuse db_session test fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaoming committed May 31, 2024
1 parent 0097571 commit 9957047
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 24 deletions.
46 changes: 25 additions & 21 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import stripe
import transaction
import webtest as _webtest
import zope.sqlalchemy

from jinja2 import Environment, FileSystemLoader
from psycopg.errors import InvalidCatalogName
Expand Down Expand Up @@ -623,33 +624,36 @@ def xmlrpc(self, path, method, *args):


@pytest.fixture
def webtest(app_config, db_session):
def tm():
tm = transaction.TransactionManager(explicit=True)
tm.begin()
tm.doom()

yield tm

tm.abort()


@pytest.fixture
def webtest(app_config, tm, db_session):
# TODO: Ensure that we have per test isolation of the database level
# changes. This probably involves flushing the database or something
# between test cases to wipe any committed changes.

# We want to disable anything that relies on TLS here.
app_config.add_settings(enforce_https=False)

try:
tm = transaction.TransactionManager(explicit=True)
tm.begin()
# tm.doom() # ensure no one can call tm.commit() manually

# I tried setting the tm.manager_hook to return the given
# transaction, but it doesn't appear to be called. e.g.,
# if I change it to return "42", the test still runs.
app_config.add_settings({ "tm.manager_hook": lambda request: tm })

app = app_config.make_wsgi_app()
testapp = _TestApp(app, extra_environ={
'tm.active': True, # disable pyramid_tm
'tm.manager': tm, # pass in our own tm for the app to use
})
yield testapp
finally:
tm.abort()
app_config.registry["sqlalchemy.engine"].dispose()
zope.sqlalchemy.register(db_session, transaction_manager=tm)

app = app_config.make_wsgi_app()
testapp = _TestApp(
app,
extra_environ={
"warehouse.db_session": db_session, # Tell warehouse.db to use this dbsession
"tm.active": True, # disable pyramid_tm
"tm.manager": tm, # pass in our own tm for the app to use
},
)
yield testapp


class _MockRedis:
Expand Down
20 changes: 17 additions & 3 deletions tests/functional/test_user_profile.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
from tests.common.db.accounts import UserFactory
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from sqlalchemy import text

def test_user_profile(db_session, webtest):
from tests.common.db.accounts import UserFactory


def test_user_profile(webtest, db_session):
user = UserFactory.create()
assert user.username
print(f"got user {user.username}", flush=True)
result = db_session.execute(text("select * from users"))
actual = ["; ".join([f"{s}" for s in row]) for row in result]
print(actual, flush=True)
# vist user's page
# visit user's page
resp = webtest.get(f"/user/{user.username}/")
assert resp.status_code == 200
6 changes: 6 additions & 0 deletions warehouse/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ def _configure_alembic(config):


def _create_session(request):

# Use pre-configured db session (usually for integration test transactions)
db_session = request.environ.get("warehouse.db_session")
if db_session:
return db_session

metrics = request.find_service(IMetricsService, context=None)
metrics.increment("warehouse.db.session.start")

Expand Down

0 comments on commit 9957047

Please sign in to comment.