Skip to content

Commit 1008ca9

Browse files
committed
Python: Add psycopg2.pool tests
1 parent d5e0298 commit 1008ca9

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

python/ql/test/library-tests/frameworks/psycopg2/ConceptsTest.expected

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import python
2+
import experimental.meta.ConceptsTest
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Examples using psycopg2 connection pools.
2+
3+
import psycopg2
4+
from psycopg2.pool import SimpleConnectionPool, AbstractConnectionPool
5+
6+
7+
DSN = "dbname=test user=test password=test host=localhost port=5432"
8+
9+
10+
def run_simple_pool_query():
11+
pool = SimpleConnectionPool(1, 4, dsn=DSN)
12+
try:
13+
conn = pool.getconn()
14+
try:
15+
cur = conn.cursor()
16+
try:
17+
# Simple, parameterless query
18+
cur.execute("SELECT 1") # $ getSql="SELECT 1"
19+
_ = cur.fetchall() if hasattr(cur, "fetchall") else None # $ threatModelSource[database]=cur.fetchall()
20+
finally:
21+
cur.close()
22+
finally:
23+
pool.putconn(conn)
24+
finally:
25+
pool.closeall()
26+
27+
28+
class LocalPool(AbstractConnectionPool):
29+
pass
30+
31+
32+
def run_custom_pool_query():
33+
pool = LocalPool(1, 3, dsn=DSN)
34+
try:
35+
conn = pool.getconn()
36+
try:
37+
cur = conn.cursor()
38+
try:
39+
cur.execute("SELECT 2") # $ getSql="SELECT 2"
40+
_ = cur.fetchone() if hasattr(cur, "fetchone") else None # $ threatModelSource[database]=cur.fetchone()
41+
finally:
42+
cur.close()
43+
finally:
44+
pool.putconn(conn)
45+
finally:
46+
pool.closeall()

0 commit comments

Comments
 (0)