Ejemplo n.º 1
0
    def test_async(self):
        uxdb.connect(database='foo', host='baz', async=1)
        self.assertDsnEqual(self.args[0], 'dbname=foo host=baz')
        self.assertEqual(self.args[1], None)
        self.assert_(self.args[2])

        uxdb.connect("dbname=foo host=baz", async=True)
        self.assertDsnEqual(self.args[0], 'dbname=foo host=baz')
        self.assertEqual(self.args[1], None)
        self.assert_(self.args[2])
Ejemplo n.º 2
0
    def test_pickle_connection_error(self):
        # segfaults on psycopg 2.5.1 - see ticket #170
        try:
            uxdb.connect('dbname=nosuchdatabasemate')
        except uxdb.Error as 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)
Ejemplo n.º 3
0
    def test_factory(self):
        def f(dsn, async_=False):
            pass

        uxdb.connect(database='foo', host='baz', connection_factory=f)
        self.assertDsnEqual(self.args[0], 'dbname=foo host=baz')
        self.assertEqual(self.args[1], f)
        self.assertEqual(self.args[2], False)

        uxdb.connect("dbname=foo host=baz", connection_factory=f)
        self.assertDsnEqual(self.args[0], 'dbname=foo host=baz')
        self.assertEqual(self.args[1], f)
        self.assertEqual(self.args[2], False)
Ejemplo n.º 4
0
    def test_escape(self):
        uxdb.connect(database='hello world')
        self.assertEqual(self.args[0], "dbname='hello world'")

        uxdb.connect(database=r'back\slash')
        self.assertEqual(self.args[0], r"dbname=back\\slash")

        uxdb.connect(database="quo'te")
        self.assertEqual(self.args[0], r"dbname=quo\'te")

        uxdb.connect(database="with\ttab")
        self.assertEqual(self.args[0], "dbname='with\ttab'")

        uxdb.connect(database=r"\every thing'")
        self.assertEqual(self.args[0], r"dbname='\\every thing\''")
Ejemplo n.º 5
0
 def test_async_connection_error_message(self):
     try:
         cnn = uxdb.connect('dbname=thisdatabasedoesntexist', async_=True)
         self.wait(cnn)
     except uxdb.Error as e:
         self.assertNotEqual(str(e), "asynchronous connection failed",
                             "connection error reason lost")
     else:
         self.fail("no exception raised")
Ejemplo n.º 6
0
 def _connect(self, key=None):
     """Create a new connection and assign it to 'key' if not None."""
     conn = uxdb.connect(*self._args, **self._kwargs)
     if key is not None:
         self._used[key] = conn
         self._rused[id(conn)] = key
     else:
         self._pool.append(conn)
     return conn
Ejemplo n.º 7
0
def test_suite():
    # If connection to test db fails, bail out early.
    import uxdb
    try:
        cnn = uxdb.connect(dsn)
    except Exception as e:
        print("Failed connection to test db:", e.__class__.__name__, e)
        print("Please set env vars 'uxdb_TESTDB*' to valid values.")
        sys.exit(1)
    else:
        cnn.close()

    suite = unittest.TestSuite()
    suite.addTest(test_async.test_suite())
    if sys.version_info[:2] < (3, 6):
        suite.addTest(test_async_keyword.test_suite())
    suite.addTest(test_bugX000.test_suite())
    suite.addTest(test_bug_gc.test_suite())
    suite.addTest(test_cancel.test_suite())
    suite.addTest(test_connection.test_suite())
    suite.addTest(test_copy.test_suite())
    suite.addTest(test_cursor.test_suite())
    suite.addTest(test_dates.test_suite())
    suite.addTest(test_errcodes.test_suite())
    suite.addTest(test_errors.test_suite())
    suite.addTest(test_extras_dictcursor.test_suite())
    suite.addTest(test_fast_executemany.test_suite())
    suite.addTest(test_green.test_suite())
    suite.addTest(test_ipaddress.test_suite())
    suite.addTest(test_lobject.test_suite())
    suite.addTest(test_module.test_suite())
    suite.addTest(test_notify.test_suite())
    suite.addTest(test_uxdb_dbapi20.test_suite())
    suite.addTest(test_quote.test_suite())
    suite.addTest(test_replication.test_suite())
    suite.addTest(test_sql.test_suite())
    suite.addTest(test_transaction.test_suite())
    suite.addTest(test_types_basic.test_suite())
    suite.addTest(test_types_extras.test_suite())
    suite.addTest(test_with.test_suite())
    return suite
Ejemplo n.º 8
0
    def tearDown(self):
        uxdb._connect = self._connect_orig

    def test_there_has_to_be_something(self):
        self.assertRaises(TypeError, uxdb.connect)
        self.assertRaises(TypeError,
                          uxdb.connect,
                          connection_factory=lambda dsn, async=False: None)
        self.assertRaises(TypeError, uxdb.connect, async=True)

    def test_factory(self):
        def f(dsn, async=False):
            pass

        uxdb.connect(database='foo', host='baz', connection_factory=f)
        self.assertDsnEqual(self.args[0], 'dbname=foo host=baz')
        self.assertEqual(self.args[1], f)
        self.assertEqual(self.args[2], False)

        uxdb.connect("dbname=foo host=baz", connection_factory=f)
        self.assertDsnEqual(self.args[0], 'dbname=foo host=baz')
        self.assertEqual(self.args[1], f)
        self.assertEqual(self.args[2], False)

    def test_async(self):
        uxdb.connect(database='foo', host='baz', async=1)
        self.assertDsnEqual(self.args[0], 'dbname=foo host=baz')
        self.assertEqual(self.args[1], None)
        self.assert_(self.args[2])
Ejemplo n.º 9
0
 def test_generic_keywords(self):
     uxdb.connect(options='stuff')
     self.assertEqual(self.args[0], 'options=stuff')
Ejemplo n.º 10
0
    def test_supported_keywords(self):
        uxdb.connect(database='foo')
        self.assertEqual(self.args[0], 'dbname=foo')
        uxdb.connect(user='******')
        self.assertEqual(self.args[0], 'user=postgres')
        uxdb.connect(password='******')
        self.assertEqual(self.args[0], 'password=secret')
        uxdb.connect(port=5432)
        self.assertEqual(self.args[0], 'port=5432')
        uxdb.connect(sslmode='require')
        self.assertEqual(self.args[0], 'sslmode=require')

        uxdb.connect(database='foo',
                     user='******',
                     password='******',
                     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)
Ejemplo n.º 11
0
 def test_dsn(self):
     uxdb.connect('dbname=blah host=y')
     self.assertEqual(self.args[0], 'dbname=blah host=y')
     self.assertEqual(self.args[1], None)
     self.assertEqual(self.args[2], False)
Ejemplo n.º 12
0
 def test_no_keywords(self):
     uxdb.connect('')
     self.assertEqual(self.args[0], '')
     self.assertEqual(self.args[1], None)
     self.assertEqual(self.args[2], False)
Ejemplo n.º 13
0
    def test_params_merging(self):
        uxdb.connect('dbname=foo', database='bar')
        self.assertEqual(self.args[0], 'dbname=bar')

        uxdb.connect('dbname=foo', user='******')
        self.assertDsnEqual(self.args[0], 'dbname=foo user=postgres')
Ejemplo n.º 14
0
 def test_empty_param(self):
     uxdb.connect(database='sony', password='')
     self.assertDsnEqual(self.args[0], "dbname=sony password=''")
Ejemplo n.º 15
0
 def test_int_port_param(self):
     uxdb.connect(database='sony', port=6543)
     dsn = " %s " % self.args[0]
     self.assert_(" dbname=sony " in dsn, dsn)
     self.assert_(" port=6543 " in dsn, dsn)
Ejemplo n.º 16
0
 def connect(self):
     return uxdb.connect(dsn=dsn)