mirror of
https://pagure.io/fm-orchestrator.git
synced 2026-05-02 06:30:55 +08:00
Separate use of database sessions
This patch separates the use of database session in different MBS components and do not mix them together. In general, MBS components could be separated as the REST API (implemented based on Flask) and non-REST API including the backend build workflow (implemented as a fedmsg consumer on top of fedmsg-hub and running independently) and library shared by them. As a result, there are two kind of database session used in MBS, one is created and managed by Flask-SQLAlchemy, and another one is created from SQLAclhemy Session API directly. The goal of this patch is to make ensure session object is used properly in the right place. All the changes follow these rules: * REST API related code uses the session object db.session created and managed by Flask-SQLAlchemy. * Non-REST API related code uses the session object created with SQLAlchemy Session API. Function make_db_session does that. * Shared code does not created a new session object as much as possible. Instead, it accepts an argument db_session. The first two rules are applicable to tests as well. Major changes: * Switch tests back to run with a file-based SQLite database. * make_session is renamed to make_db_session and SQLAlchemy connection pool options are applied for PostgreSQL backend. * Frontend Flask related code uses db.session * Shared code by REST API and backend build workflow accepts SQLAlchemy session object as an argument. For example, resolver class is constructed with a database session, and some functions accepts an argument for database session. * Build workflow related code use session object returned from make_db_session and ensure db.session is not used. * Only tests for views use db.session, and other tests use db_session fixture to access database. * All argument name session, that is for database access, are renamed to db_session. * Functions model_tests_init_data, reuse_component_init_data and reuse_shared_userspace_init_data, which creates fixture data for tests, are converted into pytest fixtures from original function called inside setup_method or a test method. The reason of this conversion is to use fixture ``db_session`` rather than create a new one. That would also benefit the whole test suite to reduce the number of SQLAlchemy session objects. Signed-off-by: Chenxiong Qi <cqi@redhat.com>
This commit is contained in:
@@ -36,7 +36,7 @@ import pytest
|
||||
import re
|
||||
import sqlalchemy
|
||||
|
||||
from tests import app, init_data, clean_database, reuse_component_init_data, staged_data_filename
|
||||
from tests import app, init_data, clean_database, staged_data_filename
|
||||
from tests import read_staged_data
|
||||
from tests.test_scm import base_dir as scm_base_dir
|
||||
from module_build_service.errors import UnprocessableEntity
|
||||
@@ -65,6 +65,7 @@ class FakeSCM(object):
|
||||
commit=None,
|
||||
checkout_raise=False,
|
||||
get_latest_raise=False,
|
||||
get_latest_error=None,
|
||||
branch="master",
|
||||
):
|
||||
"""
|
||||
@@ -96,8 +97,8 @@ class FakeSCM(object):
|
||||
self.mocked_scm.return_value.name = self.name
|
||||
self.mocked_scm.return_value.commit = self.commit
|
||||
if get_latest_raise:
|
||||
self.mocked_scm.return_value.get_latest.side_effect = UnprocessableEntity(
|
||||
"Failed to get_latest commit")
|
||||
self.mocked_scm.return_value.get_latest.side_effect = \
|
||||
get_latest_error or UnprocessableEntity("Failed to get_latest commit")
|
||||
else:
|
||||
self.mocked_scm.return_value.get_latest = self.get_latest
|
||||
self.mocked_scm.return_value.repository_root = "https://src.stg.fedoraproject.org/modules/"
|
||||
@@ -245,8 +246,8 @@ class TestViews:
|
||||
assert data["version"] == "2"
|
||||
assert data["virtual_streams"] == []
|
||||
|
||||
@pytest.mark.usefixtures("reuse_component_init_data")
|
||||
def test_query_build_with_br_verbose_mode(self):
|
||||
reuse_component_init_data()
|
||||
rv = self.client.get("/module-build-service/1/module-builds/2?verbose=true")
|
||||
data = json.loads(rv.data)
|
||||
assert data["base_module_buildrequires"] == [{
|
||||
@@ -464,6 +465,7 @@ class TestViews:
|
||||
for key, part in zip(nsvc_keys, nsvc_parts):
|
||||
assert item[key] == part
|
||||
|
||||
@pytest.mark.usefixtures("reuse_component_init_data")
|
||||
@patch("module_build_service.builder.KojiModuleBuilder.KojiClientSession")
|
||||
def test_query_builds_with_binary_rpm(self, ClientSession):
|
||||
"""
|
||||
@@ -471,7 +473,6 @@ class TestViews:
|
||||
which contain the rpm.
|
||||
"""
|
||||
# update database with builds which contain koji tags.
|
||||
reuse_component_init_data()
|
||||
mock_rpm_md = {"build_id": 1065871}
|
||||
mock_tags = [
|
||||
{"name": "module-testmodule-master-20170219191323-c40c156c"},
|
||||
@@ -827,8 +828,8 @@ class TestViews:
|
||||
}
|
||||
assert error == expected
|
||||
|
||||
@pytest.mark.usefixtures("reuse_component_init_data")
|
||||
def test_query_base_module_br_filters(self):
|
||||
reuse_component_init_data()
|
||||
mmd = load_mmd(read_staged_data("platform"))
|
||||
mmd = mmd.copy(mmd.get_module_name(), "f30.1.3")
|
||||
import_mmd(db.session, mmd)
|
||||
|
||||
Reference in New Issue
Block a user