Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix `setdefault` on `None` object and add session scope tests.

  • Loading branch information...
commit f65ef9107dc60d53953e44a8a787873e71c515fc 1 parent 6ec9d82
@s0undt3ch s0undt3ch authored
Showing with 43 additions and 1 deletion.
  1. +4 −1 flaskext/sqlalchemy.py
  2. +39 −0 test_sqlalchemy.py
View
5 flaskext/sqlalchemy.py
@@ -598,7 +598,10 @@ def __init__(self, app=None, use_native_unicode=True,
self.session_extensions = to_list(session_extensions, []) + \
[_SignallingSessionExtension()]
- session_options.set_default(
+ if session_options is None:
+ session_options = {}
+
+ session_options.setdefault(
'scopefunc', _request_ctx_stack.__ident_func__
)
View
39 test_sqlalchemy.py
@@ -374,6 +374,44 @@ def test_connection_binds(self):
db = sqlalchemy.SQLAlchemy(app)
assert db.session.connection()
+class SessionScopingTestCase(unittest.TestCase):
+
+ def test_default_session_scoping(self):
+ app = flask.Flask(__name__)
+ app.config['SQLALCHEMY_ENGINE'] = 'sqlite://'
+ app.config['TESTING'] = True
+ db = sqlalchemy.SQLAlchemy(app)
+
+ class FOOBar(db.Model):
+ id = db.Column(db.Integer, primary_key=True)
+
+ db.create_all()
+
+ with app.test_request_context():
+ fb = FOOBar()
+ db.session.add(fb)
+ assert fb in db.session
+
+ def test_session_scoping_changing(self):
+ app = flask.Flask(__name__)
+ app.config['SQLALCHEMY_ENGINE'] = 'sqlite://'
+ app.config['TESTING'] = True
+
+ def scopefunc():
+ return id(dict())
+
+ db = sqlalchemy.SQLAlchemy(app, session_options=dict(scopefunc=scopefunc))
+
+ class FOOBar(db.Model):
+ id = db.Column(db.Integer, primary_key=True)
+
+ db.create_all()
+
+ with app.test_request_context():
+ fb = FOOBar()
+ db.session.add(fb)
+ assert fb not in db.session # because a new scope is generated on each call
+
def suite():
suite = unittest.TestSuite()
@@ -385,6 +423,7 @@ def suite():
suite.addTest(unittest.makeSuite(DefaultQueryClassTestCase))
suite.addTest(unittest.makeSuite(SQLAlchemyIncludesTestCase))
suite.addTest(unittest.makeSuite(RegressionTestCase))
+ suite.addTest(unittest.makeSuite(SessionScopingTestCase))
if flask.signals_available:
suite.addTest(unittest.makeSuite(SignallingTestCase))
return suite
Please sign in to comment.
Something went wrong with that request. Please try again.