@@ -59,14 +59,6 @@ def test_basic_insert(self):
59
59
rv = c .get ('/' )
60
60
assert rv .data == 'First Item\n 2nd Item'
61
61
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
-
70
62
def test_query_recording (self ):
71
63
with self .app .test_request_context ():
72
64
todo = self .Todo ('Test 1' , 'test' )
@@ -86,6 +78,41 @@ def test_helper_api(self):
86
78
self .assertEqual (self .db .metadata , self .db .Model .metadata )
87
79
88
80
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
+
89
116
class SignallingTestCase (unittest .TestCase ):
90
117
91
118
def setUp (self ):
0 commit comments