@@ -41,14 +41,15 @@ def _select(reporter_id: UUID, article_id: UUID) -> None:
41
41
42
42
43
43
class BasicTestsFor3ChoicesOfCaptureQueries :
44
- """Обязательно должен быть передан аргумент -s при запуске pytest, для вывода output """
44
+ """"""
45
45
46
46
def setup_method (self , method : Callable [..., Any ]) -> None :
47
47
self .reporter , self .article = request_to_db ()
48
48
49
49
def call_capture_queries (self , ** kwargs : Any ) -> CaptureQueries :
50
50
raise NotImplementedError
51
51
52
+ # @pytest.mark.usefixtures('_debug_true')
52
53
def test_basic_logic (self ) -> None :
53
54
obj = self .call_capture_queries ()
54
55
@@ -74,14 +75,14 @@ def test_basic_logic(self) -> None:
74
75
data = obj .queries_log [0 ]['sql' ]
75
76
assert obj .queries_log [0 ]['sql' ] == (
76
77
'SELECT "tests_reporter"."id", "tests_reporter"."full_name" '
77
- 'FROM "tests_reporter" WHERE "tests_reporter"."id" = %s'
78
+ 'FROM "tests_reporter" WHERE "tests_reporter"."id" = %s' % self . reporter . pk
78
79
), data
79
80
80
81
data = obj .queries_log [1 ]['sql' ]
81
82
assert obj .queries_log [1 ]['sql' ] == (
82
83
'SELECT "tests_article"."id", "tests_article"."pub_date", "tests_article"."headline", '
83
84
'"tests_article"."content", "tests_article"."reporter_id" '
84
- 'FROM "tests_article" WHERE "tests_article"."id" = %s'
85
+ 'FROM "tests_article" WHERE "tests_article"."id" = %s' % self . article . pk
85
86
), data
86
87
87
88
with pytest .raises (KeyError , match = 'explain' ):
@@ -110,14 +111,14 @@ def test_param__explain(self) -> None:
110
111
data = obj .queries_log [0 ]['sql' ]
111
112
assert data == (
112
113
'SELECT "tests_reporter"."id", "tests_reporter"."full_name" '
113
- 'FROM "tests_reporter" WHERE "tests_reporter"."id" = %s'
114
+ 'FROM "tests_reporter" WHERE "tests_reporter"."id" = %s' % self . reporter . pk
114
115
), data
115
116
116
117
data = obj .queries_log [1 ]['sql' ]
117
118
assert data == (
118
119
'SELECT "tests_article"."id", "tests_article"."pub_date", "tests_article"."headline", '
119
120
'"tests_article"."content", "tests_article"."reporter_id" '
120
- 'FROM "tests_article" WHERE "tests_article"."id" = %s'
121
+ 'FROM "tests_article" WHERE "tests_article"."id" = %s' % self . article . pk
121
122
), data
122
123
123
124
# data = obj.queries_log[0]['explain']
@@ -134,6 +135,7 @@ def test_param__connection(self) -> None:
134
135
class FakeConnection :
135
136
vendor = 'fake_vendor'
136
137
queries_limit = 4
138
+ ops = connection .ops
137
139
138
140
@contextmanager
139
141
def execute_wrapper (
@@ -177,6 +179,31 @@ def test_param__number_runs(self) -> None:
177
179
gt , lt = 0.08 , 0.13
178
180
assert gt < data < lt , f'{ gt } < { data } < { lt } '
179
181
182
+ def test_execute_raw_sql (self ) -> None :
183
+ reporter_raw_sql = (
184
+ 'SELECT "tests_reporter"."id", "tests_reporter"."full_name" '
185
+ 'FROM "tests_reporter" WHERE "tests_reporter"."id" = %s' % self .reporter .pk
186
+ )
187
+ article_raw_sql = (
188
+ 'SELECT "tests_article"."id", "tests_article"."pub_date", "tests_article"."headline", '
189
+ '"tests_article"."content", "tests_article"."reporter_id" '
190
+ 'FROM "tests_article" WHERE "tests_article"."id" = %s' % self .article .pk
191
+ )
192
+ obj = CaptureQueries ()
193
+ for _ in obj :
194
+ list (Reporter .objects .raw (reporter_raw_sql ))
195
+ list (Article .objects .raw (article_raw_sql ))
196
+
197
+ data = obj .queries_log [0 ]['sql' ]
198
+ assert data == (reporter_raw_sql ), data
199
+
200
+ data = obj .queries_log [1 ]['sql' ]
201
+ assert data == (article_raw_sql ), data
202
+
203
+ def test_without_requests (self ) -> None :
204
+ for _ in CaptureQueries (advanced_verb = True ):
205
+ pass # no have requests
206
+
180
207
181
208
@pytest .mark .django_db (transaction = True )
182
209
class TestDecoratorCaptureQueries (BasicTestsFor3ChoicesOfCaptureQueries ):
@@ -230,6 +257,34 @@ def test_param__auto_call_func(self) -> None:
230
257
gt , lt = 0.08 , 0.13
231
258
assert gt < data < lt , f'{ gt } < { data } < { lt } '
232
259
260
+ def test_execute_raw_sql (self ) -> None :
261
+ reporter_raw_sql = (
262
+ 'SELECT "tests_reporter"."id", "tests_reporter"."full_name" '
263
+ 'FROM "tests_reporter" WHERE "tests_reporter"."id" = %s' % self .reporter .pk
264
+ )
265
+ article_raw_sql = (
266
+ 'SELECT "tests_article"."id", "tests_article"."pub_date", "tests_article"."headline", '
267
+ '"tests_article"."content", "tests_article"."reporter_id" '
268
+ 'FROM "tests_article" WHERE "tests_article"."id" = %s' % self .article .pk
269
+ )
270
+ obj = CaptureQueries (auto_call_func = True )
271
+
272
+ @obj
273
+ def _ () -> None :
274
+ list (Reporter .objects .raw (reporter_raw_sql ))
275
+ list (Article .objects .raw (article_raw_sql ))
276
+
277
+ data = obj .queries_log [0 ]['sql' ]
278
+ assert data == (reporter_raw_sql ), data
279
+
280
+ data = obj .queries_log [1 ]['sql' ]
281
+ assert data == (article_raw_sql ), data
282
+
283
+ def test_without_requests (self ) -> None :
284
+ @CaptureQueries (advanced_verb = True , auto_call_func = True )
285
+ def func () -> None :
286
+ pass # no have requests
287
+
233
288
234
289
@pytest .mark .django_db (transaction = True )
235
290
class TestContextManagerCaptureQueries (BasicTestsFor3ChoicesOfCaptureQueries ):
@@ -238,26 +293,50 @@ class TestContextManagerCaptureQueries(BasicTestsFor3ChoicesOfCaptureQueries):
238
293
def call_capture_queries (self , ** kwargs : Any ) -> CaptureQueries :
239
294
with slow_down_execute (0.1 ): # noqa: SIM117
240
295
with CaptureQueries (** kwargs ) as obj :
241
- obj .current_iteration = 1 # XXX(Ars): Временный хак для тестов, позже надо переработать
296
+ obj .current_iteration = 1 # XXX(Ars): A temporary hack only for tests, reworked later
242
297
_select (self .reporter .pk , self .article .pk )
243
298
return obj
244
299
245
- # @pytest.mark.filterwarnings("ignore::UserWarning") # warn не отображается, и не вызывает ошибки
246
- # @pytest.mark.filterwarnings('default::UserWarning') # warn отображается, и не вызывает ошибки
300
+ # @pytest.mark.filterwarnings("ignore::UserWarning") # warn not show, and not raise exc
301
+ # @pytest.mark.filterwarnings('default::UserWarning') # warn show, and not raise exc
247
302
def test_param__number_runs (self ) -> None :
248
303
with pytest .raises (
249
304
UserWarning ,
250
305
match = (
251
- 'При использовании : CaptureQueries как контекстного менеджера, '
252
- 'параметр number_runs > 1 не используеться .'
306
+ 'When using : CaptureQueries as a context manager, '
307
+ ' the number_runs > 1 parameter is not used .'
253
308
),
254
309
):
255
310
self .call_capture_queries (number_runs = 3 )
256
311
312
+ def test_execute_raw_sql (self ) -> None :
313
+ reporter_raw_sql = (
314
+ 'SELECT "tests_reporter"."id", "tests_reporter"."full_name" '
315
+ 'FROM "tests_reporter" WHERE "tests_reporter"."id" = %s' % self .reporter .pk
316
+ )
317
+ article_raw_sql = (
318
+ 'SELECT "tests_article"."id", "tests_article"."pub_date", "tests_article"."headline", '
319
+ '"tests_article"."content", "tests_article"."reporter_id" '
320
+ 'FROM "tests_article" WHERE "tests_article"."id" = %s' % self .article .pk
321
+ )
322
+ with CaptureQueries () as obj :
323
+ list (Reporter .objects .raw (reporter_raw_sql ))
324
+ list (Article .objects .raw (article_raw_sql ))
325
+
326
+ data = obj .queries_log [0 ]['sql' ]
327
+ assert data == (reporter_raw_sql ), data
328
+
329
+ data = obj .queries_log [1 ]['sql' ]
330
+ assert data == (article_raw_sql ), data
331
+
332
+ def test_without_requests (self ) -> None :
333
+ with CaptureQueries (advanced_verb = True ):
334
+ pass # no have requests
335
+
257
336
258
337
@pytest .mark .django_db (transaction = True )
259
338
class TestOutputCaptureQueries :
260
- """Обязательно должен быть передан аргумент -s при запуске pytest, для вывода output"""
339
+ """The -s argument must be passed when running py test to output output"""
261
340
262
341
# @pytest.mark.usefixtures('_debug_true')
263
342
def test_capture_queries_loop (self , intercept_output : StringIO ) -> None :
@@ -271,7 +350,7 @@ def test_capture_queries_loop(self, intercept_output: StringIO) -> None:
271
350
assert re .match (
272
351
f'\n \n Tests count: 100 | Total queries count: 200 | Total execution time: { ANYNUM } s | Median time one test is: { ANYNUM } s\n ' , # noqa: E501
273
352
output ,
274
- ), 'incorrect output'
353
+ ), f 'incorrect output = { output } '
275
354
276
355
# @pytest.mark.usefixtures('_debug_true')
277
356
def test_capture_queries_decorator (self , intercept_output : StringIO ) -> None :
@@ -286,7 +365,7 @@ def _() -> None:
286
365
assert re .match (
287
366
f'\n \n Tests count: 100 | Total queries count: 200 | Total execution time: { ANYNUM } s | Median time one test is: { ANYNUM } s\n ' , # noqa: E501
288
367
output ,
289
- ), 'incorrect output'
368
+ ), f 'incorrect output = { output } '
290
369
291
370
# @pytest.mark.usefixtures('_debug_true')
292
371
def test_capture_queries_context_manager (self , intercept_output : StringIO ) -> None :
@@ -296,9 +375,9 @@ def test_capture_queries_context_manager(self, intercept_output: StringIO) -> No
296
375
output = intercept_output .getvalue ()
297
376
298
377
assert re .match (
299
- f'Queries count: 2 | Execution time: { ANYNUM } s' ,
378
+ f'\n Queries count: 2 | Execution time: { ANYNUM } s | Vendor: sqlite \n \n ' ,
300
379
output ,
301
- ), 'incorrect output'
380
+ ), f 'incorrect output = { output } '
302
381
303
382
304
383
@pytest .mark .django_db (transaction = True )
0 commit comments