def test_description(self): con = self._connect() try: cur = con.cursor() self.executeDDL1(cur) self.assertEqual( cur.description, None, 'cursor.description should be none after executing a ' 'statement that can return no rows (such as DDL)') cur.execute('select name from %sbooze' % self.table_prefix) self.assertEqual( len(cur.description), 1, 'cursor.description describes too many columns') self.assertEqual( len(cur.description[0]), 7, 'cursor.description[x] tuples must have 7 elements') self.assertEqual( cur.description[0][0].lower(), b('name'), 'cursor.description[x][0] must return column name') self.assertEqual( cur.description[0][1], self.driver.STRING, 'cursor.description[x][1] must return column type. Got %r' % cur.description[0][1]) # Make sure self.description gets reset self.executeDDL2(cur) self.assertEqual( cur.description, None, 'cursor.description not being set to None when executing ' 'no-result statements (eg. DDL)') finally: con.close()
def testCopyFromWithError(self): try: cursor = self.db.cursor() stream = BytesIO(b("f1Xf2\n\n1XY1Y\n")) cursor.execute( "COPY t1 (f1, f2) FROM STDIN WITH DELIMITER 'X' CSV HEADER " "QUOTE AS 'Y' FORCE NOT NULL f1", stream=stream) self.assertTrue(False, "Should have raised an exception") except: args_dict = { 0: u('ERROR'), 1: u('22P02'), 2: u('invalid input syntax for integer: ""'), 3: u('COPY t1, line 2, column f1: ""'), 4: u('numutils.c'), 6: u('pg_atoi'), 7: u(''), 8: u('') } args = exc_info()[1].args for k, v in iteritems(args_dict): self.assertEqual(args[k], v) finally: cursor.close()
def testCopyFromWithError(self): try: cursor = self.db.cursor() stream = BytesIO(b("f1Xf2\n\n1XY1Y\n")) cursor.execute( "COPY t1 (f1, f2) FROM STDIN WITH DELIMITER 'X' CSV HEADER " "QUOTE AS 'Y' FORCE NOT NULL f1", stream=stream, ) self.assertTrue(False, "Should have raised an exception") except: args_dict = { 0: u("ERROR"), 1: u("22P02"), 2: u('invalid input syntax for integer: ""'), 3: u('COPY t1, line 2, column f1: ""'), 4: u("numutils.c"), 6: u("pg_atoi"), 7: u(""), 8: u(""), } args = exc_info()[1].args for k, v in iteritems(args_dict): self.assertEqual(args[k], v) finally: cursor.close()
def setUp(self): # Jython 2.5.3 doesn't have a time.tzset() so skip if not IS_JYTHON: os.environ['TZ'] = "UTC" time.tzset() try: c = db2.cursor() try: c = db2.cursor() c.execute("DROP TABLE t1") except pg8000.DatabaseError: e = exc_info()[1] # the only acceptable error is: self.assertEqual(e.args[1], b('42P01')) # table does not exist db2.rollback() c.execute( "CREATE TEMPORARY TABLE t1 " "(f1 int primary key, f2 int not null, f3 varchar(50) null)") c.execute("INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", (1, 1, None)) c.execute("INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", (2, 10, None)) c.execute("INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", (3, 100, None)) c.execute("INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", (4, 1000, None)) c.execute("INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", (5, 10000, None)) db2.commit() finally: c.close()
def testBinaryOutputMethods(self): methods = ( ("float8send", 22.2), ("timestamp_send", datetime.datetime(2001, 2, 3, 4, 5, 6, 789)), ("byteasend", pg8000.Binary(b("\x01\x02"))), ("interval_send", pg8000.Interval(1234567, 123, 123)), ) for method_out, value in methods: self.cursor.execute("SELECT %s(%%s) as f1" % method_out, (value,)) retval = self.cursor.fetchall() self.assertEqual(retval[0][0], self.db.make_params((value,))[0][2](value))
def testBinaryOutputMethods(self): methods = ( ("float8send", 22.2), ("timestamp_send", datetime.datetime(2001, 2, 3, 4, 5, 6, 789)), ("byteasend", pg8000.Binary(b("\x01\x02"))), ("interval_send", pg8000.Interval(1234567, 123, 123)),) for method_out, value in methods: self.cursor.execute("SELECT %s(%%s) as f1" % method_out, (value,)) retval = self.cursor.fetchall() self.assertEqual( retval[0][0], self.db.make_params((value,))[0][2](value))
def testCopyToWithQuery(self): try: cursor = self.db.cursor() stream = BytesIO() cursor.copy_to( stream, query="COPY (SELECT 1 as One, 2 as Two) TO STDOUT " "WITH DELIMITER 'X' CSV HEADER QUOTE AS 'Y' FORCE QUOTE Two") self.assertEqual(stream.getvalue(), b('oneXtwo\n1XY2Y\n')) self.assertEqual(cursor.rowcount, 1) self.db.rollback() finally: cursor.close()
def testNoDataErrorRecovery(self): for i in range(1, 4): try: try: cursor = self.db.cursor() cursor.execute("DROP TABLE t1") finally: cursor.close() except DatabaseError: e = exc_info()[1] # the only acceptable error is: self.assertEqual(e.args[1], b('42P01')) # table does not exist self.db.rollback()
def testCopyFromWithTable(self): try: cursor = self.db.cursor() stream = BytesIO(b("1\t1\t1\n2\t2\t2\n3\t3\t3\n")) cursor.execute("copy t1 from STDIN", stream=stream) self.assertEqual(cursor.rowcount, 3) cursor.execute("SELECT * FROM t1 ORDER BY f1") retval = cursor.fetchall() self.assertEqual(retval, ([1, 1, "1"], [2, 2, "2"], [3, 3, "3"])) self.db.rollback() finally: cursor.close()
def testCopyFromWithTable(self): try: cursor = self.db.cursor() stream = BytesIO(b("1\t1\t1\n2\t2\t2\n3\t3\t3\n")) cursor.copy_from(stream, "t1") self.assertEqual(cursor.rowcount, 3) cursor.execute("SELECT * FROM t1 ORDER BY f1") retval = cursor.fetchall() self.assertEqual(retval, ([1, 1, '1'], [2, 2, '2'], [3, 3, '3'])) self.db.rollback() finally: cursor.close()
def testCopyToWithQuery(self): try: cursor = self.db.cursor() stream = BytesIO() cursor.execute( "COPY (SELECT 1 as One, 2 as Two) TO STDOUT WITH DELIMITER " "'X' CSV HEADER QUOTE AS 'Y' FORCE QUOTE Two", stream=stream) self.assertEqual(stream.getvalue(), b('oneXtwo\n1XY2Y\n')) self.assertEqual(cursor.rowcount, 1) self.db.rollback() finally: cursor.close()
def testCopyFromWithTable(self): try: cursor = self.db.cursor() stream = BytesIO(b("1\t1\t1\n2\t2\t2\n3\t3\t3\n")) cursor.execute("copy t1 from STDIN", stream=stream) self.assertEqual(cursor.rowcount, 3) cursor.execute("SELECT * FROM t1 ORDER BY f1") retval = cursor.fetchall() self.assertEqual(retval, ([1, 1, '1'], [2, 2, '2'], [3, 3, '3'])) self.db.rollback() finally: cursor.close()
def testNoDataErrorRecovery(self): for i in range(1, 4): try: try: cursor = self.db.cursor() cursor.execute("DROP TABLE t1") finally: cursor.close() except pg8000.DatabaseError: e = exc_info()[1] # the only acceptable error is: self.assertEqual(e.args[1], b('42P01')) # table does not exist self.db.rollback()
def testCopyToWithQuery(self): try: cursor = self.db.cursor() stream = BytesIO() cursor.execute( "COPY (SELECT 1 as One, 2 as Two) TO STDOUT WITH DELIMITER " "'X' CSV HEADER QUOTE AS 'Y' FORCE QUOTE Two", stream=stream, ) self.assertEqual(stream.getvalue(), b("oneXtwo\n1XY2Y\n")) self.assertEqual(cursor.rowcount, 1) self.db.rollback() finally: cursor.close()
def testCopyToWithTable(self): try: cursor = self.db.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 = BytesIO() cursor.execute("copy t1 to stdout", stream=stream) self.assertEqual(stream.getvalue(), b("1\t1\t1\n2\t2\t2\n3\t3\t3\n")) self.assertEqual(cursor.rowcount, 3) self.db.commit() finally: cursor.close()
def setUp(self): self.db = pg8000.connect(**db_connect) try: cursor = self.db.cursor() try: cursor = self.db.cursor() cursor.execute("DROP TABLE t1") except pg8000.DatabaseError: e = exc_info()[1] # the only acceptable error is: self.assertEqual(e.args[1], b("42P01"), "incorrect error for drop table") # table does not exist 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 testCopyFromWithQuery(self): try: cursor = self.db.cursor() stream = BytesIO(b("f1Xf2\n1XY1Y\n")) cursor.copy_from( stream, query="COPY t1 (f1, f2) FROM STDIN WITH DELIMITER " "'X' CSV HEADER QUOTE AS 'Y' FORCE NOT NULL f1") self.assertEqual(cursor.rowcount, 1) cursor.execute("SELECT * FROM t1 ORDER BY f1") retval = cursor.fetchall() self.assertEqual(retval, ([1, 1, None],)) self.db.commit() finally: cursor.close()
def testCopyFromWithQuery(self): try: cursor = self.db.cursor() stream = BytesIO(b("f1Xf2\n1XY1Y\n")) cursor.execute( "COPY t1 (f1, f2) FROM STDIN WITH DELIMITER 'X' CSV HEADER " "QUOTE AS 'Y' FORCE NOT NULL f1", stream=stream) self.assertEqual(cursor.rowcount, 1) cursor.execute("SELECT * FROM t1 ORDER BY f1") retval = cursor.fetchall() self.assertEqual(retval, ([1, 1, None], )) self.db.commit() finally: cursor.close()
def setUp(self): try: cursor = db.cursor() try: cursor = 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") 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): 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: cursor.close() db.commit()
def setUp(self): self.db = pg8000.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 pg8000.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) # Jython 2.5.3 doesn't have a time.tzset() so skip if not IS_JYTHON: os.environ['TZ'] = "UTC" time.tzset() try: c = self.db.cursor() try: c = self.db.cursor() c.execute("DROP TABLE t1") except pg8000.DatabaseError: e = exc_info()[1] # the only acceptable error is: self.assertEqual(e.args[1], b('42P01')) # table does not exist self.db.rollback() c.execute( "CREATE TEMPORARY TABLE t1 " "(f1 int primary key, f2 int not null, f3 varchar(50) null)") c.execute( "INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", (1, 1, None)) c.execute( "INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", (2, 10, None)) c.execute( "INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", (3, 100, None)) c.execute( "INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", (4, 1000, None)) c.execute( "INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", (5, 10000, None)) self.db.commit() finally: c.close()
def test_Binary(self): self.driver.Binary(b('Something')) self.driver.Binary(b(''))
def test_timestamp_send_float(self): assert b('A\xbe\x19\xcf\x80\x00\x00\x00') == \ pg8000.core.timestamp_send_float( datetime.datetime(2016, 1, 2, 0, 0))
def testByteaRoundtrip(self): self.cursor.execute( "SELECT %s as f1", (pg8000.Binary(b("\x00\x01\x02\x03\x02\x01\x00")), )) retval = self.cursor.fetchall() self.assertEqual(retval[0][0], b("\x00\x01\x02\x03\x02\x01\x00"))
def testBinary(self): v = pg8000.Binary(b("\x00\x01\x02\x03\x02\x01\x00")) self.assertEqual(v, b("\x00\x01\x02\x03\x02\x01\x00")) self.assertTrue(isinstance(v, pg8000.BINARY))
def testByteaRoundtrip(self): self.cursor.execute("SELECT %s as f1", (pg8000.Binary(b("\x00\x01\x02\x03\x02\x01\x00")),)) retval = self.cursor.fetchall() self.assertEqual(retval[0][0], b("\x00\x01\x02\x03\x02\x01\x00"))
def testBinary(self): v = dbapi.Binary(b("\x00\x01\x02\x03\x02\x01\x00")) self.assertEqual(v, b("\x00\x01\x02\x03\x02\x01\x00")) self.assertTrue(isinstance(v, dbapi.BINARY))