|
1 | 1 | from __future__ import with_statement
|
2 | 2 |
|
3 | 3 | import atexit
|
| 4 | +import tempfile |
| 5 | +import os |
4 | 6 | import unittest
|
5 | 7 | from datetime import datetime
|
6 | 8 | import flask
|
@@ -384,12 +386,10 @@ def index():
|
384 | 386 | class BindsTestCase(unittest.TestCase):
|
385 | 387 |
|
386 | 388 | def test_basic_binds(self):
|
387 |
| - import tempfile |
388 | 389 | _, db1 = tempfile.mkstemp()
|
389 | 390 | _, db2 = tempfile.mkstemp()
|
390 | 391 |
|
391 | 392 | def _remove_files():
|
392 |
| - import os |
393 | 393 | try:
|
394 | 394 | os.remove(db1)
|
395 | 395 | os.remove(db2)
|
@@ -456,6 +456,44 @@ class Baz(db.Model):
|
456 | 456 | Baz.__table__: db.get_engine(app, None)
|
457 | 457 | })
|
458 | 458 |
|
| 459 | + def test_abstract_binds(self): |
| 460 | + _, db1 = tempfile.mkstemp() |
| 461 | + _, db2 = tempfile.mkstemp() |
| 462 | + |
| 463 | + def _remove_files(): |
| 464 | + try: |
| 465 | + os.remove(db1) |
| 466 | + os.remove(db2) |
| 467 | + except IOError: |
| 468 | + pass |
| 469 | + atexit.register(_remove_files) |
| 470 | + |
| 471 | + app = flask.Flask(__name__) |
| 472 | + app.config['SQLALCHEMY_ENGINE'] = 'sqlite://' |
| 473 | + app.config['SQLALCHEMY_BINDS'] = { |
| 474 | + 'foo': 'sqlite:///' + db1, |
| 475 | + 'bar': 'sqlite:///' + db2 |
| 476 | + } |
| 477 | + db = sqlalchemy.SQLAlchemy(app) |
| 478 | + |
| 479 | + class AbstractFooBoundModel(db.Model): |
| 480 | + __abstract__ = True |
| 481 | + __bind_key__ = 'foo' |
| 482 | + |
| 483 | + class FooBoundModel(AbstractFooBoundModel): |
| 484 | + id = db.Column(db.Integer, primary_key=True) |
| 485 | + |
| 486 | + db.create_all() |
| 487 | + |
| 488 | + # does the model have the correct engines? |
| 489 | + self.assertEqual(db.metadata.tables['foo_bound_model'].info['bind_key'], 'foo') |
| 490 | + |
| 491 | + # see the tables created in an engine |
| 492 | + metadata = db.MetaData() |
| 493 | + metadata.reflect(bind=db.get_engine(app, 'foo')) |
| 494 | + self.assertEqual(len(metadata.tables), 1) |
| 495 | + self.assertTrue('foo_bound_model' in metadata.tables) |
| 496 | + |
459 | 497 |
|
460 | 498 | class DefaultQueryClassTestCase(unittest.TestCase):
|
461 | 499 |
|
|
0 commit comments