2011-11-18 00:18:36 +04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
# test_module.py - unit test for the module interface
|
|
|
|
#
|
|
|
|
# Copyright (C) 2011 Daniele Varrazzo <daniele.varrazzo@gmail.com>
|
|
|
|
#
|
|
|
|
# psycopg2 is free software: you can redistribute it and/or modify it
|
|
|
|
# under the terms of the GNU Lesser General Public License as published
|
|
|
|
# by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# In addition, as a special exception, the copyright holders give
|
|
|
|
# permission to link this program with the OpenSSL library (or with
|
|
|
|
# modified versions of OpenSSL that use the same license as OpenSSL),
|
|
|
|
# and distribute linked combinations including the two.
|
|
|
|
#
|
|
|
|
# You must obey the GNU Lesser General Public License in all respects for
|
|
|
|
# all of the code used other than OpenSSL.
|
|
|
|
#
|
|
|
|
# psycopg2 is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
|
|
|
# License for more details.
|
|
|
|
|
2014-08-26 06:42:34 +04:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
from subprocess import Popen
|
|
|
|
|
2013-03-17 20:41:15 +04:00
|
|
|
from testutils import unittest, skip_before_python, skip_before_postgres
|
2014-08-26 06:42:34 +04:00
|
|
|
from testutils import ConnectingTestCase, skip_copy_if_green, script_to_py3
|
2011-11-18 00:18:36 +04:00
|
|
|
|
|
|
|
import psycopg2
|
|
|
|
|
2016-03-03 18:07:38 +03:00
|
|
|
|
2011-11-18 00:18:36 +04:00
|
|
|
class ConnectTestCase(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.args = None
|
2016-03-03 18:07:38 +03:00
|
|
|
|
2011-11-18 00:18:36 +04:00
|
|
|
def conect_stub(dsn, connection_factory=None, async=False):
|
|
|
|
self.args = (dsn, connection_factory, async)
|
|
|
|
|
|
|
|
self._connect_orig = psycopg2._connect
|
2016-03-08 17:44:29 +03:00
|
|
|
psycopg2._connect = conect_stub
|
2011-11-18 00:18:36 +04:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
psycopg2._connect = self._connect_orig
|
|
|
|
|
2016-03-03 07:33:59 +03:00
|
|
|
def assertDsnEqual(self, dsn1, dsn2):
|
|
|
|
self.assertEqual(set(dsn1.split()), set(dsn2.split()))
|
|
|
|
|
2011-11-18 01:41:50 +04:00
|
|
|
def test_there_has_to_be_something(self):
|
2012-09-26 14:55:21 +04:00
|
|
|
self.assertRaises(TypeError, psycopg2.connect)
|
|
|
|
self.assertRaises(TypeError, psycopg2.connect,
|
2011-11-18 01:41:50 +04:00
|
|
|
connection_factory=lambda dsn, async=False: None)
|
2012-09-26 14:55:21 +04:00
|
|
|
self.assertRaises(TypeError, psycopg2.connect,
|
2011-11-18 01:41:50 +04:00
|
|
|
async=True)
|
|
|
|
|
2011-11-18 00:18:36 +04:00
|
|
|
def test_no_keywords(self):
|
|
|
|
psycopg2.connect('')
|
|
|
|
self.assertEqual(self.args[0], '')
|
|
|
|
self.assertEqual(self.args[1], None)
|
|
|
|
self.assertEqual(self.args[2], False)
|
|
|
|
|
|
|
|
def test_dsn(self):
|
2016-03-03 19:52:53 +03:00
|
|
|
psycopg2.connect('dbname=blah host=y')
|
|
|
|
self.assertEqual(self.args[0], 'dbname=blah host=y')
|
2011-11-18 00:18:36 +04:00
|
|
|
self.assertEqual(self.args[1], None)
|
|
|
|
self.assertEqual(self.args[2], False)
|
|
|
|
|
|
|
|
def test_supported_keywords(self):
|
|
|
|
psycopg2.connect(database='foo')
|
|
|
|
self.assertEqual(self.args[0], 'dbname=foo')
|
|
|
|
psycopg2.connect(user='postgres')
|
|
|
|
self.assertEqual(self.args[0], 'user=postgres')
|
|
|
|
psycopg2.connect(password='secret')
|
|
|
|
self.assertEqual(self.args[0], 'password=secret')
|
|
|
|
psycopg2.connect(port=5432)
|
|
|
|
self.assertEqual(self.args[0], 'port=5432')
|
|
|
|
psycopg2.connect(sslmode='require')
|
|
|
|
self.assertEqual(self.args[0], 'sslmode=require')
|
|
|
|
|
|
|
|
psycopg2.connect(database='foo',
|
|
|
|
user='postgres', password='secret', port=5432)
|
|
|
|
self.assert_('dbname=foo' in self.args[0])
|
|
|
|
self.assert_('user=postgres' in self.args[0])
|
|
|
|
self.assert_('password=secret' in self.args[0])
|
|
|
|
self.assert_('port=5432' in self.args[0])
|
|
|
|
self.assertEqual(len(self.args[0].split()), 4)
|
|
|
|
|
|
|
|
def test_generic_keywords(self):
|
2016-03-03 19:52:53 +03:00
|
|
|
psycopg2.connect(options='stuff')
|
|
|
|
self.assertEqual(self.args[0], 'options=stuff')
|
2011-11-18 00:18:36 +04:00
|
|
|
|
|
|
|
def test_factory(self):
|
|
|
|
def f(dsn, async=False):
|
|
|
|
pass
|
|
|
|
|
2016-03-03 19:52:53 +03:00
|
|
|
psycopg2.connect(database='foo', host='baz', connection_factory=f)
|
|
|
|
self.assertDsnEqual(self.args[0], 'dbname=foo host=baz')
|
2011-11-18 00:18:36 +04:00
|
|
|
self.assertEqual(self.args[1], f)
|
|
|
|
self.assertEqual(self.args[2], False)
|
|
|
|
|
2016-03-03 19:52:53 +03:00
|
|
|
psycopg2.connect("dbname=foo host=baz", connection_factory=f)
|
|
|
|
self.assertDsnEqual(self.args[0], 'dbname=foo host=baz')
|
2011-11-18 00:18:36 +04:00
|
|
|
self.assertEqual(self.args[1], f)
|
|
|
|
self.assertEqual(self.args[2], False)
|
|
|
|
|
|
|
|
def test_async(self):
|
2016-03-03 19:52:53 +03:00
|
|
|
psycopg2.connect(database='foo', host='baz', async=1)
|
|
|
|
self.assertDsnEqual(self.args[0], 'dbname=foo host=baz')
|
2011-11-18 00:18:36 +04:00
|
|
|
self.assertEqual(self.args[1], None)
|
|
|
|
self.assert_(self.args[2])
|
|
|
|
|
2016-03-03 19:52:53 +03:00
|
|
|
psycopg2.connect("dbname=foo host=baz", async=True)
|
|
|
|
self.assertDsnEqual(self.args[0], 'dbname=foo host=baz')
|
2011-11-18 00:18:36 +04:00
|
|
|
self.assertEqual(self.args[1], None)
|
|
|
|
self.assert_(self.args[2])
|
|
|
|
|
2015-10-27 14:54:10 +03:00
|
|
|
def test_int_port_param(self):
|
|
|
|
psycopg2.connect(database='sony', port=6543)
|
|
|
|
dsn = " %s " % self.args[0]
|
2016-12-24 02:18:22 +03:00
|
|
|
self.assert_(" dbname=sony " in dsn, dsn)
|
|
|
|
self.assert_(" port=6543 " in dsn, dsn)
|
2015-10-27 14:54:10 +03:00
|
|
|
|
2011-11-18 01:19:01 +04:00
|
|
|
def test_empty_param(self):
|
|
|
|
psycopg2.connect(database='sony', password='')
|
2016-03-03 07:33:59 +03:00
|
|
|
self.assertDsnEqual(self.args[0], "dbname=sony password=''")
|
2011-11-18 01:19:01 +04:00
|
|
|
|
|
|
|
def test_escape(self):
|
|
|
|
psycopg2.connect(database='hello world')
|
|
|
|
self.assertEqual(self.args[0], "dbname='hello world'")
|
|
|
|
|
|
|
|
psycopg2.connect(database=r'back\slash')
|
|
|
|
self.assertEqual(self.args[0], r"dbname=back\\slash")
|
|
|
|
|
|
|
|
psycopg2.connect(database="quo'te")
|
|
|
|
self.assertEqual(self.args[0], r"dbname=quo\'te")
|
|
|
|
|
|
|
|
psycopg2.connect(database="with\ttab")
|
|
|
|
self.assertEqual(self.args[0], "dbname='with\ttab'")
|
|
|
|
|
|
|
|
psycopg2.connect(database=r"\every thing'")
|
|
|
|
self.assertEqual(self.args[0], r"dbname='\\every thing\''")
|
|
|
|
|
2016-03-03 07:33:59 +03:00
|
|
|
def test_params_merging(self):
|
|
|
|
psycopg2.connect('dbname=foo', database='bar')
|
|
|
|
self.assertEqual(self.args[0], 'dbname=bar')
|
|
|
|
|
|
|
|
psycopg2.connect('dbname=foo', user='postgres')
|
|
|
|
self.assertDsnEqual(self.args[0], 'dbname=foo user=postgres')
|
2012-09-26 02:46:46 +04:00
|
|
|
|
2011-11-18 00:18:36 +04:00
|
|
|
|
2013-04-07 03:23:30 +04:00
|
|
|
class ExceptionsTestCase(ConnectingTestCase):
|
2012-01-14 21:34:09 +04:00
|
|
|
def test_attributes(self):
|
|
|
|
cur = self.conn.cursor()
|
2012-02-24 03:15:42 +04:00
|
|
|
try:
|
|
|
|
cur.execute("select * from nonexist")
|
|
|
|
except psycopg2.Error, exc:
|
|
|
|
e = exc
|
2012-01-14 21:34:09 +04:00
|
|
|
|
|
|
|
self.assertEqual(e.pgcode, '42P01')
|
|
|
|
self.assert_(e.pgerror)
|
|
|
|
self.assert_(e.cursor is cur)
|
|
|
|
|
2013-03-18 03:31:53 +04:00
|
|
|
def test_diagnostics_attributes(self):
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
try:
|
|
|
|
cur.execute("select * from nonexist")
|
|
|
|
except psycopg2.Error, exc:
|
|
|
|
e = exc
|
|
|
|
|
|
|
|
diag = e.diag
|
|
|
|
self.assert_(isinstance(diag, psycopg2.extensions.Diagnostics))
|
|
|
|
for attr in [
|
|
|
|
'column_name', 'constraint_name', 'context', 'datatype_name',
|
|
|
|
'internal_position', 'internal_query', 'message_detail',
|
|
|
|
'message_hint', 'message_primary', 'schema_name', 'severity',
|
|
|
|
'source_file', 'source_function', 'source_line', 'sqlstate',
|
|
|
|
'statement_position', 'table_name', ]:
|
|
|
|
v = getattr(diag, attr)
|
|
|
|
if v is not None:
|
|
|
|
self.assert_(isinstance(v, str))
|
|
|
|
|
|
|
|
def test_diagnostics_values(self):
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
try:
|
|
|
|
cur.execute("select * from nonexist")
|
|
|
|
except psycopg2.Error, exc:
|
|
|
|
e = exc
|
|
|
|
|
|
|
|
self.assertEqual(e.diag.sqlstate, '42P01')
|
|
|
|
self.assertEqual(e.diag.severity, 'ERROR')
|
|
|
|
|
2013-03-18 04:24:46 +04:00
|
|
|
def test_diagnostics_life(self):
|
|
|
|
import gc
|
|
|
|
from weakref import ref
|
|
|
|
|
|
|
|
def tmp():
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
try:
|
|
|
|
cur.execute("select * from nonexist")
|
|
|
|
except psycopg2.Error, exc:
|
|
|
|
return cur, exc
|
|
|
|
|
|
|
|
cur, e = tmp()
|
|
|
|
diag = e.diag
|
|
|
|
w = ref(cur)
|
|
|
|
|
|
|
|
del e, cur
|
|
|
|
gc.collect()
|
|
|
|
assert(w() is not None)
|
|
|
|
|
|
|
|
self.assertEqual(diag.sqlstate, '42P01')
|
|
|
|
|
|
|
|
del diag
|
2016-03-03 18:07:38 +03:00
|
|
|
gc.collect()
|
|
|
|
gc.collect()
|
2013-03-18 04:24:46 +04:00
|
|
|
assert(w() is None)
|
|
|
|
|
2013-03-18 14:06:07 +04:00
|
|
|
@skip_copy_if_green
|
2013-03-18 04:31:25 +04:00
|
|
|
def test_diagnostics_copy(self):
|
|
|
|
from StringIO import StringIO
|
|
|
|
f = StringIO()
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
try:
|
|
|
|
cur.copy_to(f, 'nonexist')
|
|
|
|
except psycopg2.Error, exc:
|
|
|
|
diag = exc.diag
|
|
|
|
|
|
|
|
self.assertEqual(diag.sqlstate, '42P01')
|
|
|
|
|
2013-03-20 03:33:12 +04:00
|
|
|
def test_diagnostics_independent(self):
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
try:
|
|
|
|
cur.execute("l'acqua e' poca e 'a papera nun galleggia")
|
|
|
|
except Exception, exc:
|
|
|
|
diag1 = exc.diag
|
|
|
|
|
|
|
|
self.conn.rollback()
|
|
|
|
|
|
|
|
try:
|
|
|
|
cur.execute("select level from water where ducks > 1")
|
|
|
|
except psycopg2.Error, exc:
|
|
|
|
diag2 = exc.diag
|
|
|
|
|
|
|
|
self.assertEqual(diag1.sqlstate, '42601')
|
|
|
|
self.assertEqual(diag2.sqlstate, '42P01')
|
|
|
|
|
|
|
|
def test_diagnostics_from_commit(self):
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
cur.execute("""
|
|
|
|
create temp table test_deferred (
|
|
|
|
data int primary key,
|
|
|
|
ref int references test_deferred (data)
|
|
|
|
deferrable initially deferred)
|
|
|
|
""")
|
|
|
|
cur.execute("insert into test_deferred values (1,2)")
|
|
|
|
try:
|
|
|
|
self.conn.commit()
|
|
|
|
except psycopg2.Error, exc:
|
|
|
|
e = exc
|
|
|
|
self.assertEqual(e.diag.sqlstate, '23503')
|
|
|
|
|
2013-03-17 20:41:15 +04:00
|
|
|
@skip_before_postgres(9, 3)
|
2013-03-18 03:31:53 +04:00
|
|
|
def test_9_3_diagnostics(self):
|
2013-03-17 20:41:15 +04:00
|
|
|
cur = self.conn.cursor()
|
|
|
|
cur.execute("""
|
|
|
|
create temp table test_exc (
|
|
|
|
data int constraint chk_eq1 check (data = 1)
|
|
|
|
)""")
|
|
|
|
try:
|
|
|
|
cur.execute("insert into test_exc values(2)")
|
|
|
|
except psycopg2.Error, exc:
|
|
|
|
e = exc
|
|
|
|
self.assertEqual(e.pgcode, '23514')
|
|
|
|
self.assertEqual(e.diag.schema_name[:7], "pg_temp")
|
|
|
|
self.assertEqual(e.diag.table_name, "test_exc")
|
|
|
|
self.assertEqual(e.diag.column_name, None)
|
|
|
|
self.assertEqual(e.diag.constraint_name, "chk_eq1")
|
|
|
|
self.assertEqual(e.diag.datatype_name, None)
|
|
|
|
|
2012-01-14 21:34:09 +04:00
|
|
|
@skip_before_python(2, 5)
|
|
|
|
def test_pickle(self):
|
|
|
|
import pickle
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
try:
|
|
|
|
cur.execute("select * from nonexist")
|
2012-02-24 03:15:42 +04:00
|
|
|
except psycopg2.Error, exc:
|
|
|
|
e = exc
|
2012-01-14 21:34:09 +04:00
|
|
|
|
|
|
|
e1 = pickle.loads(pickle.dumps(e))
|
|
|
|
|
|
|
|
self.assertEqual(e.pgerror, e1.pgerror)
|
|
|
|
self.assertEqual(e.pgcode, e1.pgcode)
|
|
|
|
self.assert_(e1.cursor is None)
|
|
|
|
|
2013-07-19 18:57:27 +04:00
|
|
|
@skip_before_python(2, 5)
|
|
|
|
def test_pickle_connection_error(self):
|
|
|
|
# segfaults on psycopg 2.5.1 - see ticket #170
|
|
|
|
import pickle
|
|
|
|
try:
|
|
|
|
psycopg2.connect('dbname=nosuchdatabasemate')
|
|
|
|
except psycopg2.Error, exc:
|
|
|
|
e = exc
|
|
|
|
|
|
|
|
e1 = pickle.loads(pickle.dumps(e))
|
|
|
|
|
|
|
|
self.assertEqual(e.pgerror, e1.pgerror)
|
|
|
|
self.assertEqual(e.pgcode, e1.pgcode)
|
|
|
|
self.assert_(e1.cursor is None)
|
|
|
|
|
2012-01-14 21:34:09 +04:00
|
|
|
|
2014-08-26 06:42:34 +04:00
|
|
|
class TestExtensionModule(unittest.TestCase):
|
|
|
|
def test_import_internal(self):
|
|
|
|
# check that the internal package can be imported "naked"
|
|
|
|
# we may break this property if there is a compelling reason to do so,
|
|
|
|
# however having it allows for some import juggling such as the one
|
|
|
|
# required in ticket #201.
|
|
|
|
pkgdir = os.path.dirname(psycopg2.__file__)
|
|
|
|
pardir = os.path.dirname(pkgdir)
|
|
|
|
self.assert_(pardir in sys.path)
|
|
|
|
script = ("""
|
|
|
|
import sys
|
|
|
|
sys.path.remove(%r)
|
|
|
|
sys.path.insert(0, %r)
|
|
|
|
import _psycopg
|
|
|
|
""" % (pardir, pkgdir))
|
|
|
|
|
|
|
|
proc = Popen([sys.executable, '-c', script_to_py3(script)])
|
|
|
|
proc.communicate()
|
|
|
|
self.assertEqual(0, proc.returncode)
|
|
|
|
|
|
|
|
|
2015-06-02 12:12:16 +03:00
|
|
|
class TestVersionDiscovery(unittest.TestCase):
|
|
|
|
def test_libpq_version(self):
|
|
|
|
self.assertTrue(type(psycopg2.__libpq_version__) is int)
|
|
|
|
try:
|
|
|
|
self.assertTrue(type(psycopg2.extensions.libpq_version()) is int)
|
2016-03-03 18:07:38 +03:00
|
|
|
except psycopg2.NotSupportedError:
|
2015-06-02 12:12:16 +03:00
|
|
|
self.assertTrue(psycopg2.__libpq_version__ < 90100)
|
|
|
|
|
|
|
|
|
2011-11-18 00:18:36 +04:00
|
|
|
def test_suite():
|
|
|
|
return unittest.TestLoader().loadTestsFromName(__name__)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|