Skip to content

Commit

Permalink
Allow init_model to return the initialized session
Browse files Browse the repository at this point in the history
  • Loading branch information
amol- committed Feb 22, 2016
1 parent 272902b commit a2bbc43
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
18 changes: 15 additions & 3 deletions tests/test_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from datetime import datetime
from sqlalchemy.orm import scoped_session
from sqlalchemy.orm import sessionmaker
from sqlalchemy.engine import Engine
from ming import Session
from ming.orm import ThreadLocalORMSession
from tg.configuration.hooks import _TGGlobalHooksNamespace
Expand Down Expand Up @@ -40,6 +41,7 @@ def teardown():
milestones._reset_all()
teardown_session_dir()


class PackageWithModel:
__name__ = 'tests'
__file__ = __file__
Expand All @@ -53,16 +55,21 @@ class FakeDBSession:
def remove(self):
self.DBSESSION_REMOVED=True

@classmethod
def init_model(package, engine):
pass
def init_model(self, engine):
if isinstance(engine, Engine):
# SQLA
return self.DBSession
else:
# Ming
return dict(ming=True)

class lib:
class app_globals:
class Globals:
pass
PackageWithModel.__name__ = 'tests'


class UncopiableList(list):
"""
This is to test configuration methods that make a copy
Expand Down Expand Up @@ -814,9 +821,14 @@ def test_setup_sqla_and_ming_both(self):
app = base_config.make_wsgi_app()
assert app is not None

assert config['MingSession'], config
assert config['tg.app_globals'].ming_datastore, config['tg.app_globals']

assert config['SQLASession'], config
assert config['tg.app_globals'].sa_engine, config['tg.app_globals']

assert config['DBSession'] is config['SQLASession'], config

def test_setup_ming_persistance_with_url_and_db(self):
package = PackageWithModel()
conf = AppConfig(minimal=True, root_controller=None)
Expand Down
12 changes: 10 additions & 2 deletions tg/configuration/app_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,11 @@ def mongo_read_pref(value):
if model is None:
raise TGConfigError('Ming enabled, but no models provided')

model.init_model(datastore)
ming_session = model.init_model(datastore)
if ming_session is not None:
# If init_model returns a specific session, keep it around
# as the MongoDB Session.
conf['MingSession'] = ming_session

if 'DBSession' not in conf:
# If the user hasn't specified a default session, assume
Expand Down Expand Up @@ -826,7 +830,11 @@ def setup_sqlalchemy(self):
if model is None:
raise TGConfigError('SQLAlchemy enabled, but no models provided')

model.init_model(engine)
sqla_session = model.init_model(engine)
if sqla_session is not None:
# If init_model returns a specific session, keep it around
# as the SQLAlchemy Session.
conf['SQLASession'] = sqla_session

if 'DBSession' not in conf:
# If the user hasn't specified a default session, assume
Expand Down

0 comments on commit a2bbc43

Please sign in to comment.