|
| 1 | +from contextlib import suppress |
| 2 | + |
| 3 | +import pytest |
| 4 | +import pytest_asyncio |
| 5 | +import ydb |
| 6 | + |
| 7 | +import ydb_dbapi as dbapi |
| 8 | + |
| 9 | + |
| 10 | +class BaseDBApiTestSuit: |
| 11 | + async def _test_isolation_level_read_only( |
| 12 | + self, |
| 13 | + connection: dbapi.Connection, |
| 14 | + isolation_level: str, |
| 15 | + read_only: bool, |
| 16 | + ): |
| 17 | + await connection.cursor().execute( |
| 18 | + "CREATE TABLE foo(id Int64 NOT NULL, PRIMARY KEY (id))" |
| 19 | + ) |
| 20 | + connection.set_isolation_level(isolation_level) |
| 21 | + |
| 22 | + cursor = connection.cursor() |
| 23 | + |
| 24 | + await connection.begin() |
| 25 | + |
| 26 | + query = "UPSERT INTO foo(id) VALUES (1)" |
| 27 | + if read_only: |
| 28 | + with pytest.raises(dbapi.DatabaseError): |
| 29 | + await cursor.execute(query) |
| 30 | + else: |
| 31 | + await cursor.execute(query) |
| 32 | + |
| 33 | + await connection.rollback() |
| 34 | + |
| 35 | + await connection.cursor().execute("DROP TABLE foo") |
| 36 | + await connection.cursor().close() |
| 37 | + |
| 38 | + async def _test_connection(self, connection: dbapi.Connection): |
| 39 | + await connection.commit() |
| 40 | + await connection.rollback() |
| 41 | + |
| 42 | + cur = connection.cursor() |
| 43 | + with suppress(dbapi.DatabaseError): |
| 44 | + await cur.execute("DROP TABLE foo") |
| 45 | + |
| 46 | + assert not await connection.check_exists("/local/foo") |
| 47 | + with pytest.raises(dbapi.ProgrammingError): |
| 48 | + await connection.describe("/local/foo") |
| 49 | + |
| 50 | + await cur.execute( |
| 51 | + "CREATE TABLE foo(id Int64 NOT NULL, PRIMARY KEY (id))" |
| 52 | + ) |
| 53 | + |
| 54 | + assert await connection.check_exists("/local/foo") |
| 55 | + |
| 56 | + col = (await connection.describe("/local/foo")).columns[0] |
| 57 | + assert col.name == "id" |
| 58 | + assert col.type == ydb.PrimitiveType.Int64 |
| 59 | + |
| 60 | + await cur.execute("DROP TABLE foo") |
| 61 | + await cur.close() |
| 62 | + |
| 63 | + async def _test_cursor_raw_query(self, connection: dbapi.Connection): |
| 64 | + cur = connection.cursor() |
| 65 | + assert cur |
| 66 | + |
| 67 | + with suppress(dbapi.DatabaseError): |
| 68 | + await cur.execute("DROP TABLE test") |
| 69 | + |
| 70 | + await cur.execute( |
| 71 | + "CREATE TABLE test(id Int64 NOT NULL, text Utf8, PRIMARY KEY (id))" |
| 72 | + ) |
| 73 | + |
| 74 | + await cur.execute( |
| 75 | + """ |
| 76 | + DECLARE $data AS List<Struct<id:Int64, text: Utf8>>; |
| 77 | +
|
| 78 | + INSERT INTO test SELECT id, text FROM AS_TABLE($data); |
| 79 | + """, |
| 80 | + { |
| 81 | + "$data": ydb.TypedValue( |
| 82 | + [ |
| 83 | + {"id": 17, "text": "seventeen"}, |
| 84 | + {"id": 21, "text": "twenty one"}, |
| 85 | + ], |
| 86 | + ydb.ListType( |
| 87 | + ydb.StructType() |
| 88 | + .add_member("id", ydb.PrimitiveType.Int64) |
| 89 | + .add_member("text", ydb.PrimitiveType.Utf8) |
| 90 | + ), |
| 91 | + ) |
| 92 | + }, |
| 93 | + ) |
| 94 | + |
| 95 | + await cur.execute("DROP TABLE test") |
| 96 | + |
| 97 | + await cur.close() |
| 98 | + |
| 99 | + async def _test_errors(self, connection: dbapi.Connection): |
| 100 | + with pytest.raises(dbapi.InterfaceError): |
| 101 | + await dbapi.connect("localhost:2136", database="/local666") |
| 102 | + |
| 103 | + cur = connection.cursor() |
| 104 | + |
| 105 | + with suppress(dbapi.DatabaseError): |
| 106 | + await cur.execute("DROP TABLE test") |
| 107 | + |
| 108 | + with pytest.raises(dbapi.DataError): |
| 109 | + await cur.execute("SELECT 18446744073709551616") |
| 110 | + |
| 111 | + with pytest.raises(dbapi.DataError): |
| 112 | + await cur.execute("SELECT * FROM 拉屎") |
| 113 | + |
| 114 | + with pytest.raises(dbapi.DataError): |
| 115 | + await cur.execute("SELECT floor(5 / 2)") |
| 116 | + |
| 117 | + with pytest.raises(dbapi.ProgrammingError): |
| 118 | + await cur.execute("SELECT * FROM test") |
| 119 | + |
| 120 | + await cur.execute("CREATE TABLE test(id Int64, PRIMARY KEY (id))") |
| 121 | + |
| 122 | + await cur.execute("INSERT INTO test(id) VALUES(1)") |
| 123 | + with pytest.raises(dbapi.IntegrityError): |
| 124 | + await cur.execute("INSERT INTO test(id) VALUES(1)") |
| 125 | + |
| 126 | + await cur.execute("DROP TABLE test") |
| 127 | + await cur.close() |
| 128 | + |
| 129 | + |
| 130 | +class TestAsyncConnection(BaseDBApiTestSuit): |
| 131 | + @pytest_asyncio.fixture |
| 132 | + async def connection(self, endpoint, database): |
| 133 | + host, port = endpoint.split(":") |
| 134 | + conn = await dbapi.connect(host=host, port=port, database=database) |
| 135 | + try: |
| 136 | + yield conn |
| 137 | + finally: |
| 138 | + await conn.close() |
| 139 | + |
| 140 | + @pytest.mark.asyncio |
| 141 | + @pytest.mark.parametrize( |
| 142 | + "isolation_level, read_only", |
| 143 | + [ |
| 144 | + (dbapi.IsolationLevel.SERIALIZABLE, False), |
| 145 | + (dbapi.IsolationLevel.AUTOCOMMIT, False), |
| 146 | + # (dbapi.IsolationLevel.ONLINE_READONLY, True), |
| 147 | + # (dbapi.IsolationLevel.ONLINE_READONLY_INCONSISTENT, True), |
| 148 | + # (dbapi.IsolationLevel.STALE_READONLY, True), |
| 149 | + # (dbapi.IsolationLevel.SNAPSHOT_READONLY, True), |
| 150 | + ], |
| 151 | + ) |
| 152 | + async def test_isolation_level_read_only( |
| 153 | + self, |
| 154 | + isolation_level: str, |
| 155 | + read_only: bool, |
| 156 | + connection: dbapi.Connection, |
| 157 | + ): |
| 158 | + await self._test_isolation_level_read_only( |
| 159 | + connection, isolation_level, read_only |
| 160 | + ) |
| 161 | + |
| 162 | + @pytest.mark.asyncio |
| 163 | + async def test_connection(self, connection: dbapi.Connection): |
| 164 | + await self._test_connection(connection) |
| 165 | + |
| 166 | + @pytest.mark.asyncio |
| 167 | + async def test_cursor_raw_query(self, connection: dbapi.Connection): |
| 168 | + await self._test_cursor_raw_query(connection) |
| 169 | + |
| 170 | + @pytest.mark.asyncio |
| 171 | + async def test_errors(self, connection: dbapi.Connection): |
| 172 | + await self._test_errors(connection) |
0 commit comments