def testConnectDSN(self): dsn = "dbname='%(database)s' user='******' host='%(host)s' port=%(port)s " if "password" in db_connect: dsn += "password='******' " if not "host" in db_connect: # use unix socket directory and extension (see libpq-envars) db_connect["host"] = os.path.dirname(db_connect["unix_sock"]) db_connect["port"] = os.path.splitext(db_connect["unix_sock"])[1][1:] dbapi.connect(dsn % db_connect)
def testConnectEnv(self): import os if not "host" in db_connect: # use unix socket directory and extension (see libpq-envars) db_connect["host"] = os.path.dirname(db_connect["unix_sock"]) db_connect["port"] = os.path.splitext(db_connect["unix_sock"])[1][1:] os.environ["PGHOST"] = db_connect["host"] os.environ["PGPORT"] = db_connect["port"] os.environ["PGUSER"] = db_connect["user"] if "password" in db_connect: os.environ["PGPASSWORD"] = db_connect["password"] dbapi.connect("")
def testSsl(self): data = db_connect.copy() data["ssl"] = True if PRE_26: self.assertRaises(dbapi.InterfaceError, dbapi.connect, **data) else: db = dbapi.connect(**data) db.close()
def testNotify(self): try: db = dbapi.connect(**db_connect) self.assertEqual(db.notifies, []) cursor = db.cursor() cursor.execute("LISTEN test") cursor.execute("NOTIFY test") db.commit() cursor.execute("VALUES (1, 2), (3, 4), (5, 6)") self.assertEqual(len(db.notifies), 1) self.assertEqual(db.notifies[0][1], "test") finally: cursor.close() db.close()
def testNotify(self): try: db = dbapi.connect(**db_connect) self.assert_(db.notifies == []) with closing(db.cursor()) as cursor: cursor.execute("LISTEN test") cursor.execute("NOTIFY test") db.commit() cursor.execute("VALUES (1, 2), (3, 4), (5, 6)") self.assert_(len(db.notifies) == 1) self.assert_(db.notifies[0][1] == "test") finally: db.close()
def setUp(self): self.db = dbapi.connect(**db_connect) try: cursor = self.db.cursor() try: cursor = self.db.cursor() cursor.execute("DROP TABLE t1") except dbapi.DatabaseError: e = exc_info()[1] # the only acceptable error is: self.assertEqual( e.args[1], b('42P01'), # table does not exist "incorrect error for drop table") self.db.rollback() cursor.execute( "CREATE TEMPORARY TABLE t1 (f1 int primary key, " "f2 int not null, f3 varchar(50) null)") finally: cursor.close()
def setUp(self): self.db = dbapi.connect(**db_connect) try: cursor = self.db.cursor() try: cursor = self.db.cursor() cursor.execute("DROP TABLE t1") except dbapi.DatabaseError: e = exc_info()[1] # the only acceptable error is: self.assertEqual( e.args[1], b('42P01'), # table does not exist "incorrect error for drop table") self.db.rollback() cursor.execute("CREATE TEMPORARY TABLE t1 (f1 int primary key, " "f2 int not null, f3 varchar(50) null)") finally: cursor.close()
def setUp(self): self.db = dbapi.connect(**db_connect) filterwarnings("ignore", "DB-API extension cursor.next()") filterwarnings("ignore", "DB-API extension cursor.__iter__()") self.db.paramstyle = 'format' try: cursor = self.db.cursor() try: cursor.execute("DROP TABLE t1") except dbapi.DatabaseError: e = exc_info()[1] # the only acceptable error is: self.assertEqual(e.args[1], b('42P01')) # table does not exist self.db.rollback() cursor.execute("CREATE TEMPORARY TABLE t1 (f1 int primary key, " "f2 bigint not null, f3 varchar(50) null)") finally: cursor.close() self.db.commit()
def setUp(self): self.db = dbapi.connect(**db_connect) filterwarnings("ignore", "DB-API extension cursor.next()") filterwarnings("ignore", "DB-API extension cursor.__iter__()") self.db.paramstyle = 'format' try: cursor = self.db.cursor() try: cursor.execute("DROP TABLE t1") except dbapi.DatabaseError: e = exc_info()[1] # the only acceptable error is: self.assertEqual(e.args[1], b('42P01')) # table does not exist self.db.rollback() cursor.execute( "CREATE TEMPORARY TABLE t1 (f1 int primary key, " "f2 bigint not null, f3 varchar(50) null)") finally: cursor.close() self.db.commit()
import unittest import threading from pg8000 import dbapi from .connection_settings import db_connect from pg8000.six import u, b from sys import exc_info db = dbapi.connect(**db_connect) from warnings import filterwarnings # Tests relating to the basic operation of the database driver, driven by the # pg8000 custom interface. class Tests(unittest.TestCase): def setUp(self): filterwarnings("ignore", "DB-API extension cursor.next()") filterwarnings("ignore", "DB-API extension cursor.__iter__()") db.paramstyle = 'format' try: cursor = db.cursor() try: cursor.execute("DROP TABLE t1") except dbapi.DatabaseError: e = exc_info()[1] # the only acceptable error is: self.assertEqual(e.args[1], b('42P01')) # table does not exist db.rollback() cursor.execute("CREATE TEMPORARY TABLE t1 (f1 int primary key, " "f2 bigint not null, f3 varchar(50) null)") finally:
def setUp(self): self.db = dbapi.connect(**db_connect) self.cursor = self.db.cursor()
def testServerVersion(self): with closing(dbapi.connect(**db_connect)) as db: self.assertRegexpMatches(db.server_version, r'\d{1,2}\.\d(\.\d)?')
from __future__ import with_statement import unittest from contextlib import closing from pg8000 import dbapi from .connection_settings import db_connect from StringIO import StringIO db = dbapi.connect(**db_connect) class Tests(unittest.TestCase): def setUp(self): with closing(db.cursor()) as cursor: try: cursor.execute("DROP TABLE t1") except dbapi.DatabaseError, e: # the only acceptable error is: self.assert_(e.args[1] == "42P01", "incorrect error for drop table") # table does not exist cursor.execute("CREATE TEMPORARY TABLE t1 (f1 int primary key, f2 int not null, f3 varchar(50) null)") def testCopyToWithTable(self): with closing(db.cursor()) as cursor: cursor.execute("INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", (1, 1, 1)) cursor.execute("INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", (2, 2, 2)) cursor.execute("INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", (3, 3, 3)) stream = StringIO() cursor.copy_to(stream, "t1") self.assert_(stream.getvalue() == "1\t1\t1\n2\t2\t2\n3\t3\t3\n") self.assert_(cursor.rowcount == 3)