Skip to content

Commit

Permalink
Merge branch 'pr/135'
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko committed Jul 20, 2013
2 parents 2edb3f1 + b698ff0 commit c4c00fa
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
7 changes: 5 additions & 2 deletions flask_sqlalchemy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,11 @@ def mapper_signal_after_update(self, mapper, connection, target):

@staticmethod
def _record(mapper, target, operation):
pk = tuple(mapper.primary_key_from_instance(target))
orm.object_session(target)._model_changes[pk] = (target, operation)
s = orm.object_session(target)
if isinstance(s, _SignallingSession):
pk = tuple(mapper.primary_key_from_instance(target))
s._model_changes[pk] = (target, operation)



class _EngineDebuggingSignalEvents(object):
Expand Down
35 changes: 35 additions & 0 deletions test_sqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from datetime import datetime
import flask
from flask.ext import sqlalchemy
from sqlalchemy.orm import sessionmaker


def make_todo_model(db):
Expand Down Expand Up @@ -454,6 +455,39 @@ def test_roll_back_on_failure(self):
self.assertEqual(self.client.get('/').data, b'')


class StandardSessionTestCase(unittest.TestCase):
def test_insert_update_delete(self):
# Ensure _SignalTrackingMapperExtension doesn't croak when
# faced with a vanilla SQLAlchemy session.
#
# Verifies that "AttributeError: 'SessionMaker' object has no attribute '_model_changes'"
# is not thrown.
app = flask.Flask(__name__)
app.config['SQLALCHEMY_ENGINE'] = 'sqlite://'
app.config['TESTING'] = True
db = sqlalchemy.SQLAlchemy(app)
Session = sessionmaker(bind=db.engine)

class QazWsx(db.Model):
id = db.Column(db.Integer, primary_key=True)
x = db.Column(db.String, default='')

db.create_all()
session = Session()
session.add(QazWsx())
session.flush() # issues an INSERT.
session.expunge_all()
qaz_wsx = session.query(QazWsx).first()
assert qaz_wsx.x == ''
qaz_wsx.x = 'test'
session.flush() # issues an UPDATE.
session.expunge_all()
qaz_wsx = session.query(QazWsx).first()
assert qaz_wsx.x == 'test'
session.delete(qaz_wsx) # issues a DELETE.
assert session.query(QazWsx).first() is None


def suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(BasicAppTestCase))
Expand All @@ -468,6 +502,7 @@ def suite():
suite.addTest(unittest.makeSuite(CommitOnTeardownTestCase))
if flask.signals_available:
suite.addTest(unittest.makeSuite(SignallingTestCase))
suite.addTest(unittest.makeSuite(StandardSessionTestCase))
return suite


Expand Down

0 comments on commit c4c00fa

Please sign in to comment.