@@ -108,6 +108,29 @@ class Meta:
108
108
database = database
109
109
110
110
111
+ class TestModelAlpha (peewee .Model ):
112
+ text = peewee .CharField ()
113
+
114
+ class Meta :
115
+ database = database
116
+
117
+
118
+ class TestModelBeta (peewee .Model ):
119
+ alpha = peewee .ForeignKeyField (TestModelAlpha , related_name = 'betas' )
120
+ text = peewee .CharField ()
121
+
122
+ class Meta :
123
+ database = database
124
+
125
+
126
+ class TestModelGamma (peewee .Model ):
127
+ text = peewee .CharField ()
128
+ beta = peewee .ForeignKeyField (TestModelBeta , related_name = 'gammas' )
129
+
130
+ class Meta :
131
+ database = database
132
+
133
+
111
134
class UUIDTestModel (peewee .Model ):
112
135
id = peewee .UUIDField (primary_key = True , default = uuid .uuid4 )
113
136
text = peewee .CharField ()
@@ -117,6 +140,8 @@ class Meta:
117
140
118
141
119
142
class BaseAsyncPostgresTestCase (unittest .TestCase ):
143
+ db_tables = [TestModel , UUIDTestModel , TestModelAlpha , TestModelBeta , TestModelGamma ]
144
+
120
145
@classmethod
121
146
def setUpClass (cls , * args , ** kwargs ):
122
147
# Sync connect
@@ -129,22 +154,38 @@ def test():
129
154
yield from database .connect_async (loop = cls .loop )
130
155
cls .loop .run_until_complete (test ())
131
156
132
- # Clean up after possible errors
133
- TestModel . drop_table ( True )
134
- UUIDTestModel .drop_table (True )
157
+ for table in reversed ( cls . db_tables ):
158
+ # Clean up after possible errors
159
+ table .drop_table (True , cascade = True )
135
160
136
- # Create table with sync connection
137
- TestModel . create_table ()
138
- UUIDTestModel .create_table ()
161
+ for table in cls . db_tables :
162
+ # Create table with sync connection
163
+ table .create_table ()
139
164
140
165
# Create at least one object per model
141
166
cls .obj = TestModel .create (text = '[sync] Hello!' )
142
167
cls .uuid_obj = UUIDTestModel .create (text = '[sync] Hello!' )
143
168
169
+ cls .alpha_1 = TestModelAlpha .create (text = 'Alpha 1' )
170
+ cls .alpha_2 = TestModelAlpha .create (text = 'Alpha 2' )
171
+
172
+ cls .beta_11 = TestModelBeta .create (text = 'Beta 1' , alpha = cls .alpha_1 )
173
+ cls .beta_12 = TestModelBeta .create (text = 'Beta 2' , alpha = cls .alpha_1 )
174
+
175
+ cls .beta_21 = TestModelBeta .create (text = 'Beta 1' , alpha = cls .alpha_2 )
176
+ cls .beta_22 = TestModelBeta .create (text = 'Beta 2' , alpha = cls .alpha_2 )
177
+
178
+ cls .gamma_111 = TestModelGamma .create (text = 'Gamma 1' , beta = cls .beta_11 )
179
+ cls .gamma_112 = TestModelGamma .create (text = 'Gamma 2' , beta = cls .beta_11 )
180
+
181
+ cls .gamma_121 = TestModelGamma .create (text = 'Gamma 1' , beta = cls .beta_12 )
182
+
183
+
144
184
@classmethod
145
185
def tearDownClass (cls , * args , ** kwargs ):
186
+ for table in reversed (cls .db_tables ):
146
187
# Finally, clean up
147
- TestModel .drop_table ()
188
+ table .drop_table ()
148
189
149
190
# Close database
150
191
database .close ()
@@ -351,6 +392,29 @@ def test():
351
392
352
393
self .run_until_complete (test ())
353
394
395
+ def test_prefetch (self ):
396
+ # Async prefetch
397
+ @asyncio .coroutine
398
+ def test ():
399
+ with sync_unwanted (database ):
400
+ result = yield from peewee_async .prefetch (TestModelAlpha .select (), TestModelBeta .select (),
401
+ TestModelGamma .select ())
402
+
403
+ result = list (result ) # this should NOT fire any call (will read it from query cache)
404
+
405
+ # Check if we have here both alpha items in specific order
406
+ self .assertEqual (result , [self .alpha_1 , self .alpha_2 ])
407
+
408
+ alpha_1 = result [0 ]
409
+ self .assertEqual (alpha_1 .betas_prefetch , [self .beta_11 , self .beta_12 ])
410
+
411
+ beta_11 = alpha_1 .betas_prefetch [0 ]
412
+ self .assertEqual (beta_11 , self .beta_11 )
413
+
414
+ self .assertEqual (beta_11 .gammas_prefetch , [self .gamma_111 , self .gamma_112 ])
415
+
416
+ self .run_until_complete (test ())
417
+
354
418
355
419
if sys .version_info >= (3 , 5 ):
356
420
from .tests_py35 import *
0 commit comments