2008-01-19 06:32:42 +03:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2010-02-13 01:34:53 +03:00
|
|
|
import unittest
|
2008-01-19 06:32:42 +03:00
|
|
|
import psycopg2
|
2010-04-21 21:40:05 +04:00
|
|
|
import psycopg2.extensions
|
2008-01-19 06:32:42 +03:00
|
|
|
import tests
|
|
|
|
|
|
|
|
class ConnectionTests(unittest.TestCase):
|
|
|
|
|
|
|
|
def connect(self):
|
2008-04-21 03:12:21 +04:00
|
|
|
return psycopg2.connect(tests.dsn)
|
2008-01-19 06:32:42 +03:00
|
|
|
|
|
|
|
def test_closed_attribute(self):
|
|
|
|
conn = self.connect()
|
|
|
|
self.assertEqual(conn.closed, False)
|
|
|
|
conn.close()
|
|
|
|
self.assertEqual(conn.closed, True)
|
|
|
|
|
|
|
|
def test_cursor_closed_attribute(self):
|
|
|
|
conn = self.connect()
|
|
|
|
curs = conn.cursor()
|
|
|
|
self.assertEqual(curs.closed, False)
|
|
|
|
curs.close()
|
|
|
|
self.assertEqual(curs.closed, True)
|
|
|
|
|
|
|
|
# Closing the connection closes the cursor:
|
|
|
|
curs = conn.cursor()
|
|
|
|
conn.close()
|
|
|
|
self.assertEqual(curs.closed, True)
|
|
|
|
|
2009-08-09 18:19:08 +04:00
|
|
|
def test_reset(self):
|
|
|
|
conn = self.connect()
|
|
|
|
# switch isolation level, then reset
|
|
|
|
level = conn.isolation_level
|
|
|
|
conn.set_isolation_level(0)
|
|
|
|
self.assertEqual(conn.isolation_level, 0)
|
|
|
|
conn.reset()
|
|
|
|
# now the isolation level should be equal to saved one
|
|
|
|
self.assertEqual(conn.isolation_level, level)
|
|
|
|
|
2010-04-20 20:52:05 +04:00
|
|
|
def test_notices(self):
|
|
|
|
conn = self.connect()
|
|
|
|
cur = conn.cursor()
|
|
|
|
cur.execute("create temp table chatty (id serial primary key);")
|
|
|
|
self.assertEqual("CREATE TABLE", cur.statusmessage)
|
|
|
|
self.assert_(conn.notices)
|
|
|
|
conn.close()
|
2008-01-19 06:32:42 +03:00
|
|
|
|
2010-04-21 16:22:09 +04:00
|
|
|
def test_server_version(self):
|
|
|
|
conn = self.connect()
|
|
|
|
self.assert_(conn.server_version)
|
|
|
|
|
2010-04-21 21:40:05 +04:00
|
|
|
def test_protocol_version(self):
|
|
|
|
conn = self.connect()
|
|
|
|
self.assert_(conn.protocol_version in (2,3), conn.protocol_version)
|
|
|
|
|
|
|
|
def test_isolation_level(self):
|
|
|
|
conn = self.connect()
|
|
|
|
self.assertEqual(
|
|
|
|
conn.isolation_level,
|
|
|
|
psycopg2.extensions.ISOLATION_LEVEL_READ_COMMITTED)
|
|
|
|
|
|
|
|
def test_encoding(self):
|
|
|
|
conn = self.connect()
|
|
|
|
self.assert_(conn.encoding in psycopg2.extensions.encodings)
|
|
|
|
|
2008-01-19 06:32:42 +03:00
|
|
|
def test_suite():
|
|
|
|
return unittest.TestLoader().loadTestsFromName(__name__)
|
|
|
|
|
2010-02-13 01:34:53 +03:00
|
|
|
if __name__ == "__main__":
|
2010-04-21 16:22:09 +04:00
|
|
|
unittest.main()
|