mirror of
https://github.com/psycopg/psycopg2.git
synced 2025-01-31 09:24:07 +03:00
Added a couple of files used for tests.
This commit is contained in:
parent
23866bc35d
commit
1fddaa8562
45
sandbox/misc_dbapi_test.py
Normal file
45
sandbox/misc_dbapi_test.py
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
"""
|
||||||
|
Test if the arguments object can be used with both positional and keyword
|
||||||
|
arguments.
|
||||||
|
"""
|
||||||
|
|
||||||
|
class O(object):
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwds):
|
||||||
|
self.args = args
|
||||||
|
self.kwds = kwds
|
||||||
|
|
||||||
|
def __getitem__(self, k):
|
||||||
|
if isinstance(k, int):
|
||||||
|
return self.args[k]
|
||||||
|
else:
|
||||||
|
return self.kwds[k]
|
||||||
|
|
||||||
|
o = O('R%', second='S%')
|
||||||
|
|
||||||
|
print o[0]
|
||||||
|
print o['second']
|
||||||
|
|
||||||
|
|
||||||
|
#-------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
import psycopg2 as dbapi
|
||||||
|
|
||||||
|
|
||||||
|
conn = dbapi.connect(database='test')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
cursor = conn.cursor()
|
||||||
|
cursor.execute("""
|
||||||
|
|
||||||
|
SELECT * FROM location_pretty
|
||||||
|
WHERE keyname LIKE %s OR keyname LIKE %(second)s
|
||||||
|
|
||||||
|
""", (o,))
|
||||||
|
|
||||||
|
for row in cursor:
|
||||||
|
print row
|
||||||
|
|
||||||
|
|
44
sandbox/trigger-commit-fail.py
Normal file
44
sandbox/trigger-commit-fail.py
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
import psycopg2
|
||||||
|
import traceback
|
||||||
|
|
||||||
|
# Change the table here to something the user can create tables in ...
|
||||||
|
db = psycopg2.connect('dbname=test')
|
||||||
|
|
||||||
|
cursor = db.cursor()
|
||||||
|
|
||||||
|
print 'Creating tables and sample data'
|
||||||
|
|
||||||
|
cursor.execute('''
|
||||||
|
CREATE TEMPORARY TABLE foo (
|
||||||
|
id int PRIMARY KEY
|
||||||
|
)''')
|
||||||
|
cursor.execute('''
|
||||||
|
CREATE TEMPORARY TABLE bar (
|
||||||
|
id int PRIMARY KEY,
|
||||||
|
foo_id int,
|
||||||
|
CONSTRAINT bar_foo_fk FOREIGN KEY (foo_id) REFERENCES foo(id) DEFERRABLE
|
||||||
|
)''')
|
||||||
|
cursor.execute('INSERT INTO foo VALUES (1)')
|
||||||
|
cursor.execute('INSERT INTO bar VALUES (1, 1)')
|
||||||
|
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
print 'Deferring constraint and breaking referential integrity'
|
||||||
|
cursor.execute('SET CONSTRAINTS bar_foo_fk DEFERRED')
|
||||||
|
cursor.execute('UPDATE bar SET foo_id = 42 WHERE id = 1')
|
||||||
|
|
||||||
|
print 'Committing (this should fail)'
|
||||||
|
try:
|
||||||
|
db.commit()
|
||||||
|
except:
|
||||||
|
traceback.print_exc()
|
||||||
|
|
||||||
|
print 'Rolling back connection'
|
||||||
|
db.rollback()
|
||||||
|
|
||||||
|
print 'Running a trivial query'
|
||||||
|
try:
|
||||||
|
cursor.execute('SELECT TRUE')
|
||||||
|
except:
|
||||||
|
traceback.print_exc()
|
||||||
|
print 'db.closed:', db.closed
|
Loading…
Reference in New Issue
Block a user