Example #1
0
 def setUp(self):
     self.cclose = mock.Mock()
     self.connection = mock.Mock('psycopg2._psycopg.connection')
     self.connection.close = self.cclose
     self.pid = str(uuid.uuid4())
     self.session = mock.Mock('queries.session.Session')
     pool.add_connection(self.pid, self.session, self.connection)
Example #2
0
 def test_max_connection_reached_raises_exception(self):
     """Ensure that a ValueError is raised with too many connections"""
     for iteration in range(0, pool.MAX_CONNECTIONS):
         conn = 'conn%i' % iteration
         pool.add_connection(self.pid, self, conn)
     self.assertRaises(ValueError, pool.add_connection,
                       self.pid, self, 'ERROR')
Example #3
0
    def _connect(self):
        """Connect to PostgreSQL, either by reusing a connection from the pool
        if possible, or by creating the new connection.

        :rtype: psycopg2.connection

        """
        # Attempt to get a cached connection from the connection pool
        if self._use_pool and pool.has_idle_connection(self.pid):
            connection = pool.get_connection(self.pid)
            if connection:
                self._from_pool = True
                return connection

        # Create a new PostgreSQL connection
        kwargs = utils.uri_to_kwargs(self._uri)
        connection = self._psycopg2_connect(kwargs)

        # Add it to the pool, if pooling is enabled
        if self._use_pool:
            pool.add_connection(self.pid, self, connection)

        # Added in because psycopg2ct connects and leaves the connection in
        # a weird state: consts.STATUS_DATESTYLE, returning from
        # Connection._setup without setting the state as const.STATUS_OK
        if PYPY:
            connection.reset()

        # Register the custom data types
        self._register_unicode(connection)
        self._register_uuid(connection)

        return connection
Example #4
0
 def setUp(self):
     self.connection = mock.Mock('psycopg2._psycopg.connection')
     self.pid = str(uuid.uuid4())
     pool.add_connection(self.pid, self, self.connection)
Example #5
0
 def test_count_of_session_references(self):
     """Ensure a single session is only added once to the pool sessions"""
     for iteration in range(0, pool.MAX_CONNECTIONS):
         conn = 'conn%i' % iteration
         pool.add_connection(self.pid, self, conn)
     self.assertEqual(len(pool.Pools[self.pid].sessions), 1)