|
6 | 6 | class TestPostgreSQLClient: |
7 | 7 |
|
8 | 8 | @pytest.fixture |
9 | | - def mock_pg_conn(self, mocker): |
10 | | - return mocker.patch('psycopg.connect') |
11 | | - |
12 | | - @pytest.fixture |
13 | | - def test_instance(self): |
| 9 | + def test_instance(self, mocker): |
| 10 | + mocker.patch('psycopg_pool.ConnectionPool.open') |
| 11 | + mocker.patch('psycopg_pool.ConnectionPool.close') |
14 | 12 | return PostgreSQLClient('test_host', 'test_port', 'test_db_name', |
15 | 13 | 'test_user', 'test_password') |
16 | 14 |
|
17 | | - def test_connect(self, mock_pg_conn, test_instance): |
18 | | - test_instance.connect() |
19 | | - mock_pg_conn.assert_called_once_with(host='test_host', |
20 | | - port='test_port', |
21 | | - dbname='test_db_name', |
22 | | - user='test_user', |
23 | | - password='test_password') |
| 15 | + def test_init(self, test_instance): |
| 16 | + assert test_instance.pool.conninfo == ( |
| 17 | + 'postgresql://test_user:test_password@test_host:test_port/' + |
| 18 | + 'test_db_name') |
| 19 | + assert test_instance.pool._opened is False |
| 20 | + assert test_instance.pool.min_size == 1 |
| 21 | + assert test_instance.pool.max_size == 1 |
24 | 22 |
|
25 | | - def test_execute_query(self, mock_pg_conn, test_instance, mocker): |
| 23 | + def test_init_with_kwargs(self): |
| 24 | + test_instance = PostgreSQLClient( |
| 25 | + 'test_host', 'test_port', 'test_db_name', 'test_user', |
| 26 | + 'test_password', min_size=5, max_size=10) |
| 27 | + assert test_instance.pool.conninfo == ( |
| 28 | + 'postgresql://test_user:test_password@test_host:test_port/' + |
| 29 | + 'test_db_name') |
| 30 | + assert test_instance.pool._opened is False |
| 31 | + assert test_instance.pool.min_size == 5 |
| 32 | + assert test_instance.pool.max_size == 10 |
| 33 | + |
| 34 | + def test_connect(self, test_instance): |
26 | 35 | test_instance.connect() |
| 36 | + test_instance.pool.open.assert_called_once() |
27 | 37 |
|
28 | | - mock_cursor = mocker.MagicMock() |
29 | | - mock_cursor.execute.return_value = mock_cursor |
30 | | - mock_cursor.fetchall.return_value = [(1, 2, 3), ('a', 'b', 'c')] |
31 | | - test_instance.conn.cursor.return_value = mock_cursor |
| 38 | + def test_connect_with_exception(self): |
| 39 | + test_instance = PostgreSQLClient( |
| 40 | + 'test_host', 'test_port', 'test_db_name', 'test_user', |
| 41 | + 'test_password', timeout=1.0) |
32 | 42 |
|
33 | | - assert test_instance.execute_query( |
34 | | - 'test query') == [(1, 2, 3), ('a', 'b', 'c')] |
35 | | - mock_cursor.execute.assert_called_once_with('test query') |
36 | | - mock_cursor.close.assert_called_once() |
| 43 | + with pytest.raises(PostgreSQLClientError): |
| 44 | + test_instance.connect() |
37 | 45 |
|
38 | | - def test_execute_query_with_exception( |
39 | | - self, mock_pg_conn, test_instance, mocker): |
| 46 | + def test_execute_query(self, test_instance, mocker): |
40 | 47 | test_instance.connect() |
41 | 48 |
|
42 | | - mock_cursor = mocker.MagicMock() |
43 | | - mock_cursor.execute.side_effect = Exception() |
44 | | - test_instance.conn.cursor.return_value = mock_cursor |
| 49 | + mock_conn = mocker.MagicMock() |
| 50 | + mock_conn.execute.side_effect = Exception() |
| 51 | + mock_conn_context = mocker.MagicMock() |
| 52 | + mock_conn_context.__enter__.return_value = mock_conn |
| 53 | + mocker.patch('psycopg_pool.ConnectionPool.connection', |
| 54 | + return_value=mock_conn_context) |
45 | 55 |
|
46 | 56 | with pytest.raises(PostgreSQLClientError): |
47 | 57 | test_instance.execute_query('test query') |
48 | 58 |
|
49 | | - test_instance.conn.rollback.assert_called_once() |
50 | | - mock_cursor.close.assert_called_once() |
| 59 | + mock_conn.rollback.assert_called_once() |
51 | 60 |
|
52 | | - def test_close_connection(self, mock_pg_conn, test_instance): |
| 61 | + def test_close_connection(self, test_instance): |
53 | 62 | test_instance.connect() |
54 | 63 | test_instance.close_connection() |
55 | | - test_instance.conn.close.assert_called_once() |
| 64 | + test_instance.pool.close.assert_called_once() |
0 commit comments