def test_close_invokes_connection_close(self, _uuid, _type, connect): """Ensure close calls connection.close""" sess = session.Session('pgsql://foo@bar:9999/baz') close_mock = mock.Mock() sess._conn.close = close_mock sess.close() close_mock.assert_called_once_with()
def test_exit_invokes_cleanup(self): cleanup = mock.Mock() with mock.patch.multiple('queries.session.Session', _cleanup=cleanup, _connect=mock.Mock(), _get_cursor=mock.Mock(), _autocommit=mock.Mock()): with session.Session(self.URI): pass self.assertTrue(cleanup.called)
def test_del_invokes_cleanup(self): cleanup = mock.Mock() with mock.patch.multiple('queries.session.Session', _cleanup=cleanup, _connect=mock.Mock(), _get_cursor=mock.Mock(), _autocommit=mock.Mock()): obj = session.Session(self.URI) del obj cleanup.assert_called_once_with()
def setUp(self, register_uuid, register_type, connect): self._connect = connect self._connect.reset = mock.Mock() self._reg_type = register_type self._reg_uuid = register_uuid self.uri = 'pgsql://[email protected]:5432/queries' self.pid = hashlib.md5(self.uri.encode('utf-8')).digest() if self.pid in pool.Pools: del pool.Pools[self.pid] self.client = session.Session(self.uri)
def setUp(self, uri_to_kwargs, register_uuid, register_type, connect): self.conn = mock.Mock() self.conn.autocommit = False self.conn.closed = False self.conn.cursor = mock.Mock() self.conn.isexecuting = mock.Mock(return_value=False) self.conn.reset = mock.Mock() self.conn.status = psycopg2.extensions.STATUS_BEGIN self.psycopg2_connect = connect self.psycopg2_connect.return_value = self.conn self.psycopg2_register_type = register_type self.psycopg2_register_uuid = register_uuid self.uri_to_kwargs = uri_to_kwargs self.uri_to_kwargs.return_value = {'host': 'localhost', 'port': 5432, 'user': '******', 'password': '******', 'dbname': 'foo'} self.obj = session.Session(self.URI, pool_max_size=100)
def test_context_manager_creation(self, _uuid, _type, _connect): """Ensure context manager returns self""" with session.Session(self.uri) as conn: self.assertIsInstance(conn, session.Session)
def test_close_raises_when_closed(self, _uuid, _type, _conn): """Ensure Session._cursor is None after close""" sess = session.Session('pgsql://foo@bar:9999/baz') sess.close() self.assertRaises(AssertionError, sess.close)
def test_close_sets_cursor_to_none(self, _uuid, _type, connect): """Ensure Session._cursor is None after close""" sess = session.Session('pgsql://foo@bar:9999/baz') sess.close() self.assertIsNone(sess._cursor)
def test_context_manager_cleanup(self, _uuid, _type, _connect): """Ensure context manager cleans up after self""" with mock.patch('queries.session.Session._cleanup') as cleanup: with session.Session(self.uri): pass cleanup.assert_called_with()