@@ -41,14 +41,15 @@ def _select(reporter_id: UUID, article_id: UUID) -> None:
4141
4242
4343class BasicTestsFor3ChoicesOfCaptureQueries :
44- """Обязательно должен быть передан аргумент -s при запуске pytest, для вывода output """
44+ """"""
4545
4646 def setup_method (self , method : Callable [..., Any ]) -> None :
4747 self .reporter , self .article = request_to_db ()
4848
4949 def call_capture_queries (self , ** kwargs : Any ) -> CaptureQueries :
5050 raise NotImplementedError
5151
52+ # @pytest.mark.usefixtures('_debug_true')
5253 def test_basic_logic (self ) -> None :
5354 obj = self .call_capture_queries ()
5455
@@ -74,14 +75,14 @@ def test_basic_logic(self) -> None:
7475 data = obj .queries_log [0 ]['sql' ]
7576 assert obj .queries_log [0 ]['sql' ] == (
7677 '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
7879 ), data
7980
8081 data = obj .queries_log [1 ]['sql' ]
8182 assert obj .queries_log [1 ]['sql' ] == (
8283 'SELECT "tests_article"."id", "tests_article"."pub_date", "tests_article"."headline", '
8384 '"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
8586 ), data
8687
8788 with pytest .raises (KeyError , match = 'explain' ):
@@ -110,14 +111,14 @@ def test_param__explain(self) -> None:
110111 data = obj .queries_log [0 ]['sql' ]
111112 assert data == (
112113 '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
114115 ), data
115116
116117 data = obj .queries_log [1 ]['sql' ]
117118 assert data == (
118119 'SELECT "tests_article"."id", "tests_article"."pub_date", "tests_article"."headline", '
119120 '"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
121122 ), data
122123
123124 # data = obj.queries_log[0]['explain']
@@ -134,6 +135,7 @@ def test_param__connection(self) -> None:
134135 class FakeConnection :
135136 vendor = 'fake_vendor'
136137 queries_limit = 4
138+ ops = connection .ops
137139
138140 @contextmanager
139141 def execute_wrapper (
@@ -177,6 +179,31 @@ def test_param__number_runs(self) -> None:
177179 gt , lt = 0.08 , 0.13
178180 assert gt < data < lt , f'{ gt } < { data } < { lt } '
179181
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+
180207
181208@pytest .mark .django_db (transaction = True )
182209class TestDecoratorCaptureQueries (BasicTestsFor3ChoicesOfCaptureQueries ):
@@ -230,6 +257,34 @@ def test_param__auto_call_func(self) -> None:
230257 gt , lt = 0.08 , 0.13
231258 assert gt < data < lt , f'{ gt } < { data } < { lt } '
232259
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+
233288
234289@pytest .mark .django_db (transaction = True )
235290class TestContextManagerCaptureQueries (BasicTestsFor3ChoicesOfCaptureQueries ):
@@ -238,26 +293,50 @@ class TestContextManagerCaptureQueries(BasicTestsFor3ChoicesOfCaptureQueries):
238293 def call_capture_queries (self , ** kwargs : Any ) -> CaptureQueries :
239294 with slow_down_execute (0.1 ): # noqa: SIM117
240295 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
242297 _select (self .reporter .pk , self .article .pk )
243298 return obj
244299
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
247302 def test_param__number_runs (self ) -> None :
248303 with pytest .raises (
249304 UserWarning ,
250305 match = (
251- 'При использовании : CaptureQueries как контекстного менеджера, '
252- 'параметр number_runs > 1 не используеться .'
306+ 'When using : CaptureQueries as a context manager, '
307+ ' the number_runs > 1 parameter is not used .'
253308 ),
254309 ):
255310 self .call_capture_queries (number_runs = 3 )
256311
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+
257336
258337@pytest .mark .django_db (transaction = True )
259338class TestOutputCaptureQueries :
260- """Обязательно должен быть передан аргумент -s при запуске pytest, для вывода output"""
339+ """The -s argument must be passed when running py test to output output"""
261340
262341 # @pytest.mark.usefixtures('_debug_true')
263342 def test_capture_queries_loop (self , intercept_output : StringIO ) -> None :
@@ -271,7 +350,7 @@ def test_capture_queries_loop(self, intercept_output: StringIO) -> None:
271350 assert re .match (
272351 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
273352 output ,
274- ), 'incorrect output'
353+ ), f 'incorrect output = { output } '
275354
276355 # @pytest.mark.usefixtures('_debug_true')
277356 def test_capture_queries_decorator (self , intercept_output : StringIO ) -> None :
@@ -286,7 +365,7 @@ def _() -> None:
286365 assert re .match (
287366 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
288367 output ,
289- ), 'incorrect output'
368+ ), f 'incorrect output = { output } '
290369
291370 # @pytest.mark.usefixtures('_debug_true')
292371 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
296375 output = intercept_output .getvalue ()
297376
298377 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 ' ,
300379 output ,
301- ), 'incorrect output'
380+ ), f 'incorrect output = { output } '
302381
303382
304383@pytest .mark .django_db (transaction = True )
0 commit comments