|
6 | 6 | import itertools
|
7 | 7 | import os
|
8 | 8 | import re
|
| 9 | +import sqlite3 |
9 | 10 | from typing import MutableMapping
|
10 | 11 | from unittest.mock import MagicMock, patch
|
11 | 12 |
|
@@ -1529,6 +1530,67 @@ async def test_result_named_access(database_url):
|
1529 | 1530 | assert result.completed is True
|
1530 | 1531 |
|
1531 | 1532 |
|
| 1533 | +@pytest.mark.parametrize("database_url", DATABASE_URLS) |
| 1534 | +@async_adapter |
| 1535 | +async def test_mapping_property_interface(database_url): |
| 1536 | + """ |
| 1537 | + Test that all connections implement interface with `_mapping` property |
| 1538 | + """ |
| 1539 | + async with Database(database_url) as database: |
| 1540 | + query = notes.select() |
| 1541 | + single_result = await database.fetch_one(query=query) |
| 1542 | + assert single_result._mapping["text"] == "example1" |
| 1543 | + assert single_result._mapping["completed"] is True |
| 1544 | + |
| 1545 | + list_result = await database.fetch_all(query=query) |
| 1546 | + assert list_result[0]._mapping["text"] == "example1" |
| 1547 | + assert list_result[0]._mapping["completed"] is True |
| 1548 | + |
| 1549 | + |
| 1550 | +@async_adapter |
| 1551 | +async def test_should_not_maintain_ref_when_no_cache_param(): |
| 1552 | + async with Database("sqlite:///file::memory:", uri=True) as database: |
| 1553 | + query = sqlalchemy.schema.CreateTable(notes) |
| 1554 | + await database.execute(query) |
| 1555 | + |
| 1556 | + query = notes.insert() |
| 1557 | + values = {"text": "example1", "completed": True} |
| 1558 | + with pytest.raises(sqlite3.OperationalError): |
| 1559 | + await database.execute(query, values) |
| 1560 | + |
| 1561 | + |
| 1562 | +@async_adapter |
| 1563 | +async def test_should_maintain_ref_when_cache_param(): |
| 1564 | + async with Database("sqlite:///file::memory:?cache=shared", uri=True) as database: |
| 1565 | + query = sqlalchemy.schema.CreateTable(notes) |
| 1566 | + await database.execute(query) |
| 1567 | + |
| 1568 | + query = notes.insert() |
| 1569 | + values = {"text": "example1", "completed": True} |
| 1570 | + await database.execute(query, values) |
| 1571 | + |
| 1572 | + query = notes.select().where(notes.c.text == "example1") |
| 1573 | + result = await database.fetch_one(query=query) |
| 1574 | + assert result.text == "example1" |
| 1575 | + assert result.completed is True |
| 1576 | + |
| 1577 | + |
| 1578 | +@async_adapter |
| 1579 | +async def test_should_remove_ref_on_disconnect(): |
| 1580 | + async with Database("sqlite:///file::memory:?cache=shared", uri=True) as database: |
| 1581 | + query = sqlalchemy.schema.CreateTable(notes) |
| 1582 | + await database.execute(query) |
| 1583 | + |
| 1584 | + query = notes.insert() |
| 1585 | + values = {"text": "example1", "completed": True} |
| 1586 | + await database.execute(query, values) |
| 1587 | + |
| 1588 | + async with Database("sqlite:///file::memory:?cache=shared", uri=True) as database: |
| 1589 | + query = notes.select() |
| 1590 | + with pytest.raises(sqlite3.OperationalError): |
| 1591 | + await database.fetch_all(query=query) |
| 1592 | + |
| 1593 | + |
1532 | 1594 | @pytest.mark.parametrize("database_url", DATABASE_URLS)
|
1533 | 1595 | @async_adapter
|
1534 | 1596 | async def test_mapping_property_interface(database_url):
|
|
0 commit comments