|
| 1 | +import unittest |
| 2 | +from unittest.mock import ( |
| 3 | + PropertyMock, |
| 4 | + call, |
| 5 | + patch, |
| 6 | +) |
| 7 | + |
| 8 | +import aiopg |
| 9 | +from aiopg import ( |
| 10 | + Connection, |
| 11 | + Cursor, |
| 12 | +) |
| 13 | +from psycopg2 import ( |
| 14 | + IntegrityError, |
| 15 | + OperationalError, |
| 16 | +) |
| 17 | + |
| 18 | +from minos.common import ( |
| 19 | + AiopgDatabaseClient, |
| 20 | + DatabaseLock, |
| 21 | + IntegrityException, |
| 22 | + UnableToConnectException, |
| 23 | +) |
| 24 | +from minos.common.testing import ( |
| 25 | + PostgresAsyncTestCase, |
| 26 | +) |
| 27 | +from tests.utils import ( |
| 28 | + CommonTestCase, |
| 29 | +) |
| 30 | + |
| 31 | + |
| 32 | +# noinspection SqlNoDataSourceInspection |
| 33 | +class TestAiopgDatabaseClient(CommonTestCase, PostgresAsyncTestCase): |
| 34 | + def setUp(self): |
| 35 | + super().setUp() |
| 36 | + self.sql = "SELECT * FROM information_schema.tables" |
| 37 | + |
| 38 | + def test_constructor(self): |
| 39 | + client = AiopgDatabaseClient("foo") |
| 40 | + self.assertEqual("foo", client.database) |
| 41 | + self.assertEqual("postgres", client.user) |
| 42 | + self.assertEqual("", client.password) |
| 43 | + self.assertEqual("localhost", client.host) |
| 44 | + self.assertEqual(5432, client.port) |
| 45 | + |
| 46 | + def test_from_config(self): |
| 47 | + default_database = self.config.get_default_database() |
| 48 | + client = AiopgDatabaseClient.from_config(self.config) |
| 49 | + self.assertEqual(default_database["database"], client.database) |
| 50 | + self.assertEqual(default_database["user"], client.user) |
| 51 | + self.assertEqual(default_database["password"], client.password) |
| 52 | + self.assertEqual(default_database["host"], client.host) |
| 53 | + self.assertEqual(default_database["port"], client.port) |
| 54 | + |
| 55 | + async def test_is_valid_true(self): |
| 56 | + async with AiopgDatabaseClient.from_config(self.config) as client: |
| 57 | + self.assertTrue(client.is_valid()) |
| 58 | + |
| 59 | + async def test_is_valid_false_not_setup(self): |
| 60 | + client = AiopgDatabaseClient.from_config(self.config) |
| 61 | + self.assertFalse(await client.is_valid()) |
| 62 | + |
| 63 | + async def test_is_valid_false_operational_error(self): |
| 64 | + async with AiopgDatabaseClient.from_config(self.config) as client: |
| 65 | + with patch.object(Connection, "isolation_level", new_callable=PropertyMock, side_effect=OperationalError): |
| 66 | + self.assertFalse(await client.is_valid()) |
| 67 | + |
| 68 | + async def test_is_valid_false_closed(self): |
| 69 | + async with AiopgDatabaseClient.from_config(self.config) as client: |
| 70 | + with patch.object(Connection, "closed", new_callable=PropertyMock, return_valud=False): |
| 71 | + self.assertFalse(await client.is_valid()) |
| 72 | + |
| 73 | + async def test_connection(self): |
| 74 | + client = AiopgDatabaseClient.from_config(self.config) |
| 75 | + self.assertIsNone(client.connection) |
| 76 | + async with client: |
| 77 | + self.assertIsInstance(client.connection, Connection) |
| 78 | + self.assertIsNone(client.connection) |
| 79 | + |
| 80 | + async def test_connection_raises(self): |
| 81 | + with patch.object(aiopg, "connect", new_callable=PropertyMock, side_effect=OperationalError): |
| 82 | + with self.assertRaises(UnableToConnectException): |
| 83 | + async with AiopgDatabaseClient.from_config(self.config): |
| 84 | + pass |
| 85 | + |
| 86 | + async def test_notifications(self): |
| 87 | + async with AiopgDatabaseClient.from_config(self.config) as client: |
| 88 | + self.assertEqual(client.connection.notifies, client.notifications) |
| 89 | + |
| 90 | + async def test_cursor(self): |
| 91 | + client = AiopgDatabaseClient.from_config(self.config) |
| 92 | + self.assertIsNone(client.cursor) |
| 93 | + async with client: |
| 94 | + self.assertIsNone(client.cursor) |
| 95 | + await client.execute("SELECT * FROM information_schema.tables") |
| 96 | + self.assertIsInstance(client.cursor, Cursor) |
| 97 | + |
| 98 | + self.assertIsNone(client.cursor) |
| 99 | + |
| 100 | + async def test_cursor_reset(self): |
| 101 | + client = AiopgDatabaseClient.from_config(self.config) |
| 102 | + async with client: |
| 103 | + await client.execute("SELECT * FROM information_schema.tables") |
| 104 | + self.assertIsInstance(client.cursor, Cursor) |
| 105 | + await client.reset() |
| 106 | + self.assertIsNone(client.cursor) |
| 107 | + |
| 108 | + async def test_lock(self): |
| 109 | + client = AiopgDatabaseClient.from_config(self.config) |
| 110 | + self.assertIsNone(client.lock) |
| 111 | + async with client: |
| 112 | + self.assertIsNone(client.lock) |
| 113 | + await client.execute(self.sql, lock="foo") |
| 114 | + self.assertIsInstance(client.lock, DatabaseLock) |
| 115 | + |
| 116 | + self.assertIsNone(client.lock) |
| 117 | + |
| 118 | + async def test_lock_reset(self): |
| 119 | + async with AiopgDatabaseClient.from_config(self.config) as client: |
| 120 | + await client.execute(self.sql, lock="foo") |
| 121 | + self.assertIsInstance(client.lock, DatabaseLock) |
| 122 | + await client.reset() |
| 123 | + self.assertIsNone(client.lock) |
| 124 | + |
| 125 | + async def test_execute(self): |
| 126 | + async with AiopgDatabaseClient.from_config(self.config) as client: |
| 127 | + with patch.object(Cursor, "execute") as execute_mock: |
| 128 | + await client.execute(self.sql) |
| 129 | + self.assertEqual( |
| 130 | + [call(operation=self.sql, parameters=None, timeout=None)], |
| 131 | + execute_mock.call_args_list, |
| 132 | + ) |
| 133 | + |
| 134 | + async def test_execute_with_lock(self): |
| 135 | + with patch.object(DatabaseLock, "__aenter__") as enter_lock_mock: |
| 136 | + with patch.object(DatabaseLock, "__aexit__") as exit_lock_mock: |
| 137 | + async with AiopgDatabaseClient.from_config(self.config) as client: |
| 138 | + await client.execute(self.sql, lock="foo") |
| 139 | + self.assertEqual(1, enter_lock_mock.call_count) |
| 140 | + self.assertEqual(0, exit_lock_mock.call_count) |
| 141 | + enter_lock_mock.reset_mock() |
| 142 | + exit_lock_mock.reset_mock() |
| 143 | + self.assertEqual(0, enter_lock_mock.call_count) |
| 144 | + self.assertEqual(1, exit_lock_mock.call_count) |
| 145 | + |
| 146 | + async def test_execute_with_lock_multiple(self): |
| 147 | + async with AiopgDatabaseClient.from_config(self.config) as client: |
| 148 | + await client.execute(self.sql, lock="foo") |
| 149 | + await client.execute(self.sql, lock="foo") |
| 150 | + with self.assertRaises(ValueError): |
| 151 | + await client.execute(self.sql, lock="bar") |
| 152 | + |
| 153 | + async def test_execute_raises_integrity(self): |
| 154 | + async with AiopgDatabaseClient.from_config(self.config) as client: |
| 155 | + with patch.object(Cursor, "execute", side_effect=IntegrityError): |
| 156 | + with self.assertRaises(IntegrityException): |
| 157 | + await client.execute(self.sql) |
| 158 | + |
| 159 | + async def test_fetch_one(self): |
| 160 | + async with AiopgDatabaseClient.from_config(self.config) as client: |
| 161 | + await client.execute(self.sql) |
| 162 | + observed = await client.fetch_one() |
| 163 | + self.assertIsInstance(observed, tuple) |
| 164 | + |
| 165 | + async def test_fetch_all(self): |
| 166 | + async with AiopgDatabaseClient.from_config(self.config) as client: |
| 167 | + await client.execute(self.sql) |
| 168 | + observed = [value async for value in client.fetch_all()] |
| 169 | + |
| 170 | + self.assertGreater(len(observed), 0) |
| 171 | + for obs in observed: |
| 172 | + self.assertIsInstance(obs, tuple) |
| 173 | + |
| 174 | + |
| 175 | +if __name__ == "__main__": |
| 176 | + unittest.main() |
0 commit comments