@@ -153,7 +153,7 @@ async def test_prepare_10_stmt_lru(self):
153
153
154
154
stmts = []
155
155
for i in range(iter_max):
156
- s = await self.con.prepare (query.format(i))
156
+ s = await self.con._prepare (query.format(i), use_cache=True )
157
157
self.assertEqual(await s.fetchval(), i)
158
158
stmts.append(s)
159
159
@@ -207,7 +207,7 @@ async def test_prepare_11_stmt_gc(self):
207
207
# The prepared statement that we'll create will be GCed
208
208
# right await. However, its state should be still in
209
209
# in the statements LRU cache.
210
- await self.con.prepare ('select 1')
210
+ await self.con._prepare ('select 1', use_cache=True )
211
211
gc.collect()
212
212
213
213
self.assertEqual(len(cache), 1)
@@ -224,12 +224,12 @@ async def test_prepare_12_stmt_gc(self):
224
224
self.assertEqual(len(cache), 0)
225
225
self.assertEqual(len(self.con._stmts_to_close), 0)
226
226
227
- stmt = await self.con.prepare ('select 100000000')
227
+ stmt = await self.con._prepare ('select 100000000', use_cache=True )
228
228
self.assertEqual(len(cache), 1)
229
229
self.assertEqual(len(self.con._stmts_to_close), 0)
230
230
231
231
for i in range(cache_max):
232
- await self.con.prepare ('select {}'.format(i))
232
+ await self.con._prepare ('select {}'.format(i), use_cache=True )
233
233
234
234
self.assertEqual(len(cache), cache_max)
235
235
self.assertEqual(len(self.con._stmts_to_close), 0)
@@ -293,7 +293,7 @@ async def test_prepare_15_stmt_gc_cache_disabled(self):
293
293
# Disable cache
294
294
cache.set_max_size(0)
295
295
296
- stmt = await self.con.prepare ('select 100000000')
296
+ stmt = await self.con._prepare ('select 100000000', use_cache=True )
297
297
self.assertEqual(len(cache), 0)
298
298
self.assertEqual(len(self.con._stmts_to_close), 0)
299
299
@@ -305,7 +305,7 @@ async def test_prepare_15_stmt_gc_cache_disabled(self):
305
305
self.assertEqual(len(self.con._stmts_to_close), 1)
306
306
307
307
# Next "prepare" call will trigger a cleanup
308
- stmt = await self.con.prepare ('select 1')
308
+ stmt = await self.con._prepare ('select 1', use_cache=True )
309
309
self.assertEqual(len(cache), 0)
310
310
self.assertEqual(len(self.con._stmts_to_close), 0)
311
311
@@ -468,25 +468,25 @@ async def test_prepare_24_max_lifetime(self):
468
468
self.assertEqual(cache.get_max_lifetime(), 142)
469
469
cache.set_max_lifetime(1)
470
470
471
- s = await self.con.prepare ('SELECT 1')
471
+ s = await self.con._prepare ('SELECT 1', use_cache=True )
472
472
state = s._state
473
473
474
- s = await self.con.prepare ('SELECT 1')
474
+ s = await self.con._prepare ('SELECT 1', use_cache=True )
475
475
self.assertIs(s._state, state)
476
476
477
- s = await self.con.prepare ('SELECT 1')
477
+ s = await self.con._prepare ('SELECT 1', use_cache=True )
478
478
self.assertIs(s._state, state)
479
479
480
480
await asyncio.sleep(1, loop=self.loop)
481
481
482
- s = await self.con.prepare ('SELECT 1')
482
+ s = await self.con._prepare ('SELECT 1', use_cache=True )
483
483
self.assertIsNot(s._state, state)
484
484
485
485
@tb.with_connection_options(max_cached_statement_lifetime=0.5)
486
486
async def test_prepare_25_max_lifetime_reset(self):
487
487
cache = self.con._stmt_cache
488
488
489
- s = await self.con.prepare ('SELECT 1')
489
+ s = await self.con._prepare ('SELECT 1', use_cache=True )
490
490
state = s._state
491
491
492
492
# Disable max_lifetime
@@ -495,20 +495,20 @@ async def test_prepare_25_max_lifetime_reset(self):
495
495
await asyncio.sleep(1, loop=self.loop)
496
496
497
497
# The statement should still be cached (as we disabled the timeout).
498
- s = await self.con.prepare ('SELECT 1')
498
+ s = await self.con._prepare ('SELECT 1', use_cache=True )
499
499
self.assertIs(s._state, state)
500
500
501
501
@tb.with_connection_options(max_cached_statement_lifetime=0.5)
502
502
async def test_prepare_26_max_lifetime_max_size(self):
503
503
cache = self.con._stmt_cache
504
504
505
- s = await self.con.prepare ('SELECT 1')
505
+ s = await self.con._prepare ('SELECT 1', use_cache=True )
506
506
state = s._state
507
507
508
508
# Disable max_lifetime
509
509
cache.set_max_size(0)
510
510
511
- s = await self.con.prepare ('SELECT 1')
511
+ s = await self.con._prepare ('SELECT 1', use_cache=True )
512
512
self.assertIsNot(s._state, state)
513
513
514
514
# Check that nothing crashes after the initial timeout
@@ -518,12 +518,12 @@ async def test_prepare_26_max_lifetime_max_size(self):
518
518
async def test_prepare_27_max_cacheable_statement_size(self):
519
519
cache = self.con._stmt_cache
520
520
521
- await self.con.prepare ('SELECT 1')
521
+ await self.con._prepare ('SELECT 1', use_cache=True )
522
522
self.assertEqual(len(cache), 1)
523
523
524
524
# Test that long and explicitly created prepared statements
525
525
# are not cached.
526
- await self.con.prepare ("SELECT \'" + "a" * 50 + "\'")
526
+ await self.con._prepare ("SELECT \'" + "a" * 50 + "\'", use_cache=True )
527
527
self.assertEqual(len(cache), 1)
528
528
529
529
# Test that implicitly created long prepared statements
@@ -532,7 +532,7 @@ async def test_prepare_27_max_cacheable_statement_size(self):
532
532
self.assertEqual(len(cache), 1)
533
533
534
534
# Test that short prepared statements can still be cached.
535
- await self.con.prepare ('SELECT 2')
535
+ await self.con._prepare ('SELECT 2', use_cache=True )
536
536
self.assertEqual(len(cache), 2)
537
537
538
538
async def test_prepare_28_max_args(self):
@@ -593,3 +593,10 @@ async def test_prepare_31_pgbouncer_note(self):
593
593
self.assertTrue('pgbouncer' in e.hint)
594
594
else:
595
595
self.fail('InvalidSQLStatementNameError not raised')
596
+
597
+ async def test_prepare_does_not_use_cache(self):
598
+ cache = self.con._stmt_cache
599
+
600
+ # prepare with disabled cache
601
+ await self.con.prepare('select 1')
602
+ self.assertEqual(len(cache), 0)
0 commit comments