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 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 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()