Skip to content

Commit 73a0198

Browse files
miracle2kmitsuhiko
authored andcommitted
Added a test case for the Model.query behavior.
Signed-off-by: Armin Ronacher <armin.ronacher@active-4.com>
1 parent 2e80abc commit 73a0198

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

test_sqlalchemy.py

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,6 @@ def test_basic_insert(self):
5959
rv = c.get('/')
6060
assert rv.data == 'First Item\n2nd Item'
6161

62-
def test_request_context(self):
63-
self.assertEqual(self.Todo.query, None)
64-
with self.app.test_request_context():
65-
todo = self.Todo('Test', 'test')
66-
self.db.session.add(todo)
67-
self.db.session.commit()
68-
self.assertEqual(len(self.Todo.query.all()), 1)
69-
7062
def test_query_recording(self):
7163
with self.app.test_request_context():
7264
todo = self.Todo('Test 1', 'test')
@@ -86,6 +78,41 @@ def test_helper_api(self):
8678
self.assertEqual(self.db.metadata, self.db.Model.metadata)
8779

8880

81+
class TestQueryProperty(unittest.TestCase):
82+
83+
def setUp(self):
84+
self.app = flask.Flask(__name__)
85+
self.app.config['SQLALCHEMY_ENGINE'] = 'sqlite://'
86+
self.app.config['TESTING'] = True
87+
88+
def test_no_app_bound(self):
89+
db = sqlalchemy.SQLAlchemy()
90+
db.init_app(self.app)
91+
Todo = make_todo_model(db)
92+
93+
# If no app is bound to the SQLAlchemy instance, a
94+
# request context is required to access Model.query.
95+
self.assertRaises(RuntimeError, getattr, Todo, 'query')
96+
with self.app.test_request_context():
97+
db.create_all()
98+
todo = Todo('Test', 'test')
99+
db.session.add(todo)
100+
db.session.commit()
101+
self.assertEqual(len(Todo.query.all()), 1)
102+
103+
def test_app_bound(self):
104+
db = sqlalchemy.SQLAlchemy(self.app)
105+
Todo = make_todo_model(db)
106+
db.create_all()
107+
108+
# If an app was passed to the SQLAlchemy constructor,
109+
# the query property is always available.
110+
todo = Todo('Test', 'test')
111+
db.session.add(todo)
112+
db.session.commit()
113+
self.assertEqual(len(Todo.query.all()), 1)
114+
115+
89116
class SignallingTestCase(unittest.TestCase):
90117

91118
def setUp(self):

0 commit comments

Comments
 (0)