-
-
Notifications
You must be signed in to change notification settings - Fork 901
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support binds on abstract models #373
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
from __future__ import with_statement | ||
|
||
import atexit | ||
import tempfile | ||
import os | ||
import unittest | ||
from datetime import datetime | ||
import flask | ||
|
@@ -384,12 +386,10 @@ def index(): | |
class BindsTestCase(unittest.TestCase): | ||
|
||
def test_basic_binds(self): | ||
import tempfile | ||
_, db1 = tempfile.mkstemp() | ||
_, db2 = tempfile.mkstemp() | ||
|
||
def _remove_files(): | ||
import os | ||
try: | ||
os.remove(db1) | ||
os.remove(db2) | ||
|
@@ -456,6 +456,44 @@ class Baz(db.Model): | |
Baz.__table__: db.get_engine(app, None) | ||
}) | ||
|
||
def test_abstract_binds(self): | ||
_, db1 = tempfile.mkstemp() | ||
_, db2 = tempfile.mkstemp() | ||
|
||
def _remove_files(): | ||
try: | ||
os.remove(db1) | ||
os.remove(db2) | ||
except IOError: | ||
pass | ||
atexit.register(_remove_files) | ||
|
||
app = flask.Flask(__name__) | ||
app.config['SQLALCHEMY_ENGINE'] = 'sqlite://' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should be |
||
app.config['SQLALCHEMY_BINDS'] = { | ||
'foo': 'sqlite:///' + db1, | ||
'bar': 'sqlite:///' + db2 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This bind is never used. |
||
} | ||
db = sqlalchemy.SQLAlchemy(app) | ||
|
||
class AbstractFooBoundModel(db.Model): | ||
__abstract__ = True | ||
__bind_key__ = 'foo' | ||
|
||
class FooBoundModel(AbstractFooBoundModel): | ||
id = db.Column(db.Integer, primary_key=True) | ||
|
||
db.create_all() | ||
|
||
# does the model have the correct engines? | ||
self.assertEqual(db.metadata.tables['foo_bound_model'].info['bind_key'], 'foo') | ||
|
||
# see the tables created in an engine | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This section seems unnecessary. Could be replaced with |
||
metadata = db.MetaData() | ||
metadata.reflect(bind=db.get_engine(app, 'foo')) | ||
self.assertEqual(len(metadata.tables), 1) | ||
self.assertTrue('foo_bound_model' in metadata.tables) | ||
|
||
|
||
class DefaultQueryClassTestCase(unittest.TestCase): | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no need for creating and removing db files, just use
sqlite://
for all binds, each will connect to a different in-memory db.