|
21 | 21 | import pkg_resources |
22 | 22 | import pytest |
23 | 23 | import random |
24 | | -import unittest |
| 24 | +import time |
25 | 25 | from unittest import mock |
26 | 26 |
|
27 | 27 | import sqlalchemy |
@@ -1579,45 +1579,32 @@ def test_user_agent(self): |
1579 | 1579 | ) |
1580 | 1580 |
|
1581 | 1581 |
|
1582 | | -class ExecutionOptionsTest(fixtures.TestBase, unittest.TestCase): |
| 1582 | +class ExecutionOptionsReadOnlyTest(fixtures.TestBase): |
1583 | 1583 | """ |
1584 | 1584 | Check that `execution_options()` method correctly |
1585 | 1585 | sets parameters on the underlying DB API connection. |
1586 | 1586 | """ |
1587 | 1587 |
|
1588 | | - @classmethod |
1589 | | - def setUpClass(cls): |
1590 | | - cls._engine = create_engine(get_db_url(), pool_size=1) |
1591 | | - cls._metadata = MetaData(bind=cls._engine) |
| 1588 | + def setUp(self): |
| 1589 | + self._engine = create_engine(get_db_url(), pool_size=1) |
| 1590 | + metadata = MetaData(bind=self._engine) |
1592 | 1591 |
|
1593 | | - cls._table = Table( |
| 1592 | + self._table = Table( |
1594 | 1593 | "execution_options", |
1595 | | - cls._metadata, |
| 1594 | + metadata, |
1596 | 1595 | Column("opt_id", Integer, primary_key=True), |
1597 | 1596 | Column("opt_name", String(16), nullable=False), |
1598 | 1597 | ) |
1599 | 1598 |
|
1600 | | - cls._metadata.create_all(cls._engine) |
| 1599 | + metadata.create_all(self._engine) |
1601 | 1600 |
|
1602 | 1601 | def test_read_only(self): |
1603 | 1602 | with self._engine.connect().execution_options(read_only=True) as connection: |
1604 | 1603 | connection.execute(select(["*"], from_obj=self._table)).fetchall() |
1605 | 1604 | assert connection.connection.read_only is True |
1606 | 1605 |
|
1607 | | - def test_staleness(self): |
1608 | | - with self._engine.connect().execution_options( |
1609 | | - read_only=True, staleness={"exact_staleness": datetime.timedelta(seconds=5)} |
1610 | | - ) as connection: |
1611 | | - connection.execute(select(["*"], from_obj=self._table)).fetchall() |
1612 | | - assert connection.connection.staleness == { |
1613 | | - "exact_staleness": datetime.timedelta(seconds=5) |
1614 | | - } |
1615 | | - |
1616 | | - with self._engine.connect() as connection: |
1617 | | - assert connection.connection.staleness is None |
1618 | | - |
1619 | 1606 | with self._engine.connect() as connection: |
1620 | | - del connection.staleness |
| 1607 | + assert connection.connection.read_only is False |
1621 | 1608 |
|
1622 | 1609 |
|
1623 | 1610 | class LimitOffsetTest(fixtures.TestBase): |
@@ -1645,6 +1632,43 @@ def test_offset_only(self): |
1645 | 1632 | list(connection.execute(self._table.select().offset(offset)).fetchall()) |
1646 | 1633 |
|
1647 | 1634 |
|
| 1635 | +class ExecutionOptionsStalenessTest(fixtures.TestBase): |
| 1636 | + """ |
| 1637 | + Check that `execution_options()` method correctly |
| 1638 | + sets parameters on the underlying DB API connection. |
| 1639 | + """ |
| 1640 | + |
| 1641 | + def setUp(self): |
| 1642 | + self._engine = create_engine(get_db_url(), pool_size=1) |
| 1643 | + metadata = MetaData(bind=self._engine) |
| 1644 | + |
| 1645 | + self._table = Table( |
| 1646 | + "execution_options", |
| 1647 | + metadata, |
| 1648 | + Column("opt_id", Integer, primary_key=True), |
| 1649 | + Column("opt_name", String(16), nullable=False), |
| 1650 | + ) |
| 1651 | + |
| 1652 | + metadata.create_all(self._engine) |
| 1653 | + time.sleep(1) |
| 1654 | + |
| 1655 | + def test_staleness(self): |
| 1656 | + with self._engine.connect().execution_options( |
| 1657 | + read_only=True, staleness={"exact_staleness": datetime.timedelta(seconds=1)} |
| 1658 | + ) as connection: |
| 1659 | + connection.execute(select(["*"], from_obj=self._table)).fetchall() |
| 1660 | + assert connection.connection.staleness == { |
| 1661 | + "exact_staleness": datetime.timedelta(seconds=1) |
| 1662 | + } |
| 1663 | + |
| 1664 | + with self._engine.connect() as connection: |
| 1665 | + assert connection.connection.staleness == {} |
| 1666 | + |
| 1667 | + engine = create_engine("sqlite:///database") |
| 1668 | + with engine.connect() as connection: |
| 1669 | + pass |
| 1670 | + |
| 1671 | + |
1648 | 1672 | class TemporaryTableTest(fixtures.TestBase): |
1649 | 1673 | """ |
1650 | 1674 | Check that temporary tables raise an error on creation. |
|
0 commit comments