sqlalchemy-dqlite 0.1.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,50 @@
1
+ name: Publish to PyPI
2
+
3
+ on: push
4
+
5
+ jobs:
6
+ build:
7
+ name: Build distribution
8
+ runs-on: ubuntu-latest
9
+ steps:
10
+ - uses: actions/checkout@v6
11
+ with:
12
+ persist-credentials: false
13
+ - name: Set up Python
14
+ uses: actions/setup-python@v6
15
+ with:
16
+ python-version: "3.x"
17
+ - name: Install pypa/build
18
+ run: >-
19
+ python3 -m
20
+ pip install
21
+ build
22
+ --user
23
+ - name: Build a binary wheel and a source tarball
24
+ run: python3 -m build
25
+ - name: Store the distribution packages
26
+ uses: actions/upload-artifact@v5
27
+ with:
28
+ name: python-package-distributions
29
+ path: dist/
30
+
31
+ publish-to-pypi:
32
+ name: >-
33
+ Publish to PyPI
34
+ if: startsWith(github.ref, 'refs/tags/') # only publish to PyPI on tag pushes
35
+ needs:
36
+ - build
37
+ runs-on: ubuntu-latest
38
+ environment:
39
+ name: pypi
40
+ url: https://pypi.org/p/sqlalchemy-dqlite
41
+ permissions:
42
+ id-token: write
43
+ steps:
44
+ - name: Download all the dists
45
+ uses: actions/download-artifact@v6
46
+ with:
47
+ name: python-package-distributions
48
+ path: dist/
49
+ - name: Publish to PyPI
50
+ uses: pypa/gh-action-pypi-publish@release/v1
@@ -0,0 +1,46 @@
1
+ # Python
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+ *.so
6
+ .Python
7
+ build/
8
+ develop-eggs/
9
+ dist/
10
+ downloads/
11
+ eggs/
12
+ .eggs/
13
+ lib/
14
+ lib64/
15
+ parts/
16
+ sdist/
17
+ var/
18
+ wheels/
19
+ *.egg-info/
20
+ .installed.cfg
21
+ *.egg
22
+
23
+ # Virtual environments
24
+ .venv/
25
+ venv/
26
+ ENV/
27
+
28
+ # IDE
29
+ .idea/
30
+ .vscode/
31
+ *.swp
32
+ *.swo
33
+
34
+ # Testing
35
+ .pytest_cache/
36
+ .coverage
37
+ htmlcov/
38
+ .tox/
39
+ .nox/
40
+
41
+ # mypy
42
+ .mypy_cache/
43
+
44
+ # Distribution
45
+ dist/
46
+ build/
@@ -0,0 +1,90 @@
1
+ # Development Guide
2
+
3
+ ## Prerequisites
4
+
5
+ - Python 3.13+
6
+ - [uv](https://github.com/astral-sh/uv) - Fast Python package manager
7
+ - Docker (for integration tests)
8
+
9
+ ## Setup
10
+
11
+ Start by also cloning [dqlite-wire](https://github.com/letsdiscodev/python-dqlite-wire),
12
+ [dqlite-client](https://github.com/letsdiscodev/python-dqlite-client)
13
+ and [dqlite-dbapi](https://github.com/letsdiscodev/python-dqlite-dbapi).
14
+
15
+ ```bash
16
+ # Install uv (if not already installed)
17
+ curl -LsSf https://astral.sh/uv/install.sh | sh
18
+
19
+ # Create virtual environment and install dependencies
20
+ uv venv --python 3.13
21
+ uv pip install -e "../python-dqlite-wire" -e "../python-dqlite-client" -e "../python-dqlite-dbapi" -e ".[dev]"
22
+ ```
23
+
24
+ ## Development Tools
25
+
26
+ | Tool | Purpose | Command |
27
+ |------|---------|---------|
28
+ | **pytest** | Testing framework | `pytest` |
29
+ | **ruff** | Linter (replaces flake8, isort, etc.) | `ruff check` |
30
+ | **ruff format** | Code formatter (replaces black) | `ruff format` |
31
+ | **mypy** | Static type checker | `mypy src` |
32
+
33
+ ## Running Tests
34
+
35
+ ```bash
36
+ # Run unit tests only
37
+ .venv/bin/pytest tests/ --ignore=tests/integration
38
+
39
+ # Run all tests (requires Docker cluster)
40
+ cd ../dqlite-test-cluster && docker compose up -d
41
+ .venv/bin/pytest tests/
42
+ ```
43
+
44
+ ## Linting & Formatting
45
+
46
+ ```bash
47
+ # Lint
48
+ .venv/bin/ruff check src tests
49
+
50
+ # Auto-fix lint issues
51
+ .venv/bin/ruff check --fix src tests
52
+
53
+ # Format
54
+ .venv/bin/ruff format src tests
55
+ ```
56
+
57
+ ## Type Checking
58
+
59
+ ```bash
60
+ .venv/bin/mypy src
61
+ ```
62
+
63
+ ## Pre-commit Workflow
64
+
65
+ ```bash
66
+ .venv/bin/ruff format src tests
67
+ .venv/bin/ruff check --fix src tests
68
+ .venv/bin/mypy src
69
+ .venv/bin/pytest tests/ --ignore=tests/integration
70
+ ```
71
+
72
+ ## SQLAlchemy URL Format
73
+
74
+ ```
75
+ # Sync
76
+ dqlite://host:port/database
77
+
78
+ # Async
79
+ dqlite+aio://host:port/database
80
+ ```
81
+
82
+ ## Dialect Registration
83
+
84
+ The dialects are registered via entry points in `pyproject.toml`:
85
+
86
+ ```toml
87
+ [project.entry-points."sqlalchemy.dialects"]
88
+ dqlite = "sqlalchemydqlite:DqliteDialect"
89
+ "dqlite.aio" = "sqlalchemydqlite.aio:DqliteDialect_aio"
90
+ ```
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Antoine Leclair and Greg Sadetsky
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,76 @@
1
+ Metadata-Version: 2.4
2
+ Name: sqlalchemy-dqlite
3
+ Version: 0.1.1
4
+ Summary: SQLAlchemy 2.0 dialect for dqlite distributed SQLite
5
+ Project-URL: Homepage, https://github.com/antoineleclair/sqlalchemy-dqlite
6
+ Project-URL: Repository, https://github.com/antoineleclair/sqlalchemy-dqlite
7
+ Project-URL: Issues, https://github.com/antoineleclair/sqlalchemy-dqlite/issues
8
+ Author-email: Antoine Leclair <antoineleclair@gmail.com>
9
+ License-Expression: MIT
10
+ License-File: LICENSE.md
11
+ Keywords: database,distributed,dqlite,orm,sqlalchemy,sqlite
12
+ Classifier: Development Status :: 3 - Alpha
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: License :: OSI Approved :: MIT License
15
+ Classifier: Operating System :: OS Independent
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Programming Language :: Python :: 3.13
18
+ Classifier: Topic :: Database
19
+ Classifier: Topic :: Database :: Database Engines/Servers
20
+ Classifier: Topic :: Database :: Front-Ends
21
+ Classifier: Typing :: Typed
22
+ Requires-Python: >=3.13
23
+ Requires-Dist: dqlite-dbapi>=0.1.0
24
+ Requires-Dist: sqlalchemy>=2.0
25
+ Provides-Extra: dev
26
+ Requires-Dist: mypy>=1.0; extra == 'dev'
27
+ Requires-Dist: pytest-asyncio>=0.23; extra == 'dev'
28
+ Requires-Dist: pytest-cov>=4.0; extra == 'dev'
29
+ Requires-Dist: pytest>=8.0; extra == 'dev'
30
+ Requires-Dist: ruff>=0.4; extra == 'dev'
31
+ Description-Content-Type: text/markdown
32
+
33
+ # sqlalchemy-dqlite
34
+
35
+ SQLAlchemy 2.0 dialect for [dqlite](https://dqlite.io/).
36
+
37
+ ## Installation
38
+
39
+ ```bash
40
+ pip install sqlalchemy-dqlite
41
+ ```
42
+
43
+ ## Usage
44
+
45
+ ```python
46
+ from sqlalchemy import create_engine, text
47
+
48
+ # Sync
49
+ engine = create_engine("dqlite://localhost:9001/mydb")
50
+ with engine.connect() as conn:
51
+ result = conn.execute(text("SELECT 1"))
52
+ print(result.fetchone())
53
+
54
+ # Async
55
+ from sqlalchemy.ext.asyncio import create_async_engine
56
+
57
+ async_engine = create_async_engine("dqlite+aio://localhost:9001/mydb")
58
+ async with async_engine.connect() as conn:
59
+ result = await conn.execute(text("SELECT 1"))
60
+ print(result.fetchone())
61
+ ```
62
+
63
+ ## URL Format
64
+
65
+ ```
66
+ dqlite://host:port/database
67
+ dqlite+aio://host:port/database
68
+ ```
69
+
70
+ ## Development
71
+
72
+ See [DEVELOPMENT.md](DEVELOPMENT.md) for setup and contribution guidelines.
73
+
74
+ ## License
75
+
76
+ MIT
@@ -0,0 +1,44 @@
1
+ # sqlalchemy-dqlite
2
+
3
+ SQLAlchemy 2.0 dialect for [dqlite](https://dqlite.io/).
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pip install sqlalchemy-dqlite
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```python
14
+ from sqlalchemy import create_engine, text
15
+
16
+ # Sync
17
+ engine = create_engine("dqlite://localhost:9001/mydb")
18
+ with engine.connect() as conn:
19
+ result = conn.execute(text("SELECT 1"))
20
+ print(result.fetchone())
21
+
22
+ # Async
23
+ from sqlalchemy.ext.asyncio import create_async_engine
24
+
25
+ async_engine = create_async_engine("dqlite+aio://localhost:9001/mydb")
26
+ async with async_engine.connect() as conn:
27
+ result = await conn.execute(text("SELECT 1"))
28
+ print(result.fetchone())
29
+ ```
30
+
31
+ ## URL Format
32
+
33
+ ```
34
+ dqlite://host:port/database
35
+ dqlite+aio://host:port/database
36
+ ```
37
+
38
+ ## Development
39
+
40
+ See [DEVELOPMENT.md](DEVELOPMENT.md) for setup and contribution guidelines.
41
+
42
+ ## License
43
+
44
+ MIT
@@ -0,0 +1,67 @@
1
+ [build-system]
2
+ requires = ["hatchling"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "sqlalchemy-dqlite"
7
+ version = "0.1.1"
8
+ description = "SQLAlchemy 2.0 dialect for dqlite distributed SQLite"
9
+ readme = "README.md"
10
+ requires-python = ">=3.13"
11
+ license = "MIT"
12
+ authors = [{ name = "Antoine Leclair", email = "antoineleclair@gmail.com" }]
13
+ keywords = ["dqlite", "sqlite", "distributed", "database", "sqlalchemy", "orm"]
14
+ classifiers = [
15
+ "Development Status :: 3 - Alpha",
16
+ "Intended Audience :: Developers",
17
+ "License :: OSI Approved :: MIT License",
18
+ "Operating System :: OS Independent",
19
+ "Programming Language :: Python :: 3",
20
+ "Programming Language :: Python :: 3.13",
21
+ "Topic :: Database",
22
+ "Topic :: Database :: Database Engines/Servers",
23
+ "Topic :: Database :: Front-Ends",
24
+ "Typing :: Typed",
25
+ ]
26
+ dependencies = ["dqlite-dbapi>=0.1.0", "sqlalchemy>=2.0"]
27
+
28
+ [project.urls]
29
+ Homepage = "https://github.com/antoineleclair/sqlalchemy-dqlite"
30
+ Repository = "https://github.com/antoineleclair/sqlalchemy-dqlite"
31
+ Issues = "https://github.com/antoineleclair/sqlalchemy-dqlite/issues"
32
+
33
+ [project.optional-dependencies]
34
+ dev = ["pytest>=8.0", "pytest-cov>=4.0", "pytest-asyncio>=0.23", "mypy>=1.0", "ruff>=0.4"]
35
+
36
+ [project.entry-points."sqlalchemy.dialects"]
37
+ dqlite = "sqlalchemydqlite:DqliteDialect"
38
+ "dqlite.aio" = "sqlalchemydqlite.aio:DqliteDialect_aio"
39
+
40
+ [tool.hatch.build.targets.wheel]
41
+ packages = ["src/sqlalchemydqlite"]
42
+
43
+ [tool.pytest.ini_options]
44
+ testpaths = ["tests"]
45
+ pythonpath = ["src"]
46
+ asyncio_mode = "auto"
47
+ asyncio_default_fixture_loop_scope = "function"
48
+
49
+ [tool.mypy]
50
+ strict = true
51
+ python_version = "3.13"
52
+
53
+ [tool.ruff]
54
+ target-version = "py313"
55
+ line-length = 100
56
+ src = ["src", "tests"]
57
+
58
+ [tool.ruff.lint]
59
+ select = ["E", "F", "I", "UP", "B", "SIM"]
60
+
61
+ [tool.ruff.lint.isort]
62
+ known-first-party = ["sqlalchemydqlite", "dqlitedbapi", "dqliteclient", "dqlitewire"]
63
+
64
+ [tool.ruff.format]
65
+ quote-style = "double"
66
+ indent-style = "space"
67
+ docstring-code-format = true
@@ -0,0 +1,7 @@
1
+ """SQLAlchemy 2.0 dialect for dqlite."""
2
+
3
+ from sqlalchemydqlite.base import DqliteDialect
4
+
5
+ __all__ = ["DqliteDialect"]
6
+
7
+ __version__ = "0.1.1"
@@ -0,0 +1,57 @@
1
+ """Async dqlite dialect for SQLAlchemy."""
2
+
3
+ from typing import Any
4
+
5
+ from sqlalchemy import pool
6
+ from sqlalchemy.dialects.sqlite.base import SQLiteDialect
7
+ from sqlalchemy.engine import URL
8
+ from sqlalchemy.pool import AsyncAdaptedQueuePool
9
+
10
+
11
+ class DqliteDialect_aio(SQLiteDialect): # noqa: N801
12
+ """Async SQLAlchemy dialect for dqlite.
13
+
14
+ Use with SQLAlchemy's async engine:
15
+ create_async_engine("dqlite+aio://host:port/database")
16
+ """
17
+
18
+ name = "dqlite"
19
+ driver = "dqlitedbapi_aio"
20
+ is_async = True
21
+
22
+ # dqlite uses qmark parameter style
23
+ paramstyle = "qmark"
24
+
25
+ @classmethod
26
+ def get_pool_class(cls, url: URL) -> type[pool.Pool]:
27
+ return AsyncAdaptedQueuePool
28
+
29
+ @classmethod
30
+ def import_dbapi(cls) -> Any:
31
+ from dqlitedbapi import aio
32
+
33
+ return aio
34
+
35
+ def create_connect_args(self, url: URL) -> tuple[list[Any], dict[str, Any]]:
36
+ """Create connection arguments from URL.
37
+
38
+ URL format: dqlite+aio://host:port/database
39
+ """
40
+ host = url.host or "localhost"
41
+ port = url.port or 9001
42
+ database = url.database or "default"
43
+
44
+ address = f"{host}:{port}"
45
+
46
+ return [], {
47
+ "address": address,
48
+ "database": database,
49
+ }
50
+
51
+ def get_driver_connection(self, connection: Any) -> Any:
52
+ """Return the driver-level connection."""
53
+ return connection
54
+
55
+
56
+ # Register the dialect
57
+ dialect = DqliteDialect_aio
@@ -0,0 +1,122 @@
1
+ """Base dqlite dialect for SQLAlchemy."""
2
+
3
+ from typing import Any
4
+
5
+ from sqlalchemy import pool
6
+ from sqlalchemy.dialects.sqlite.base import SQLiteDialect
7
+ from sqlalchemy.engine import URL
8
+ from sqlalchemy.engine.interfaces import DBAPIConnection, IsolationLevel
9
+
10
+
11
+ class DqliteDialect(SQLiteDialect):
12
+ """SQLAlchemy dialect for dqlite.
13
+
14
+ Inherits from SQLite dialect since dqlite is compatible with SQLite.
15
+ """
16
+
17
+ name = "dqlite"
18
+ driver = "dqlitedbapi"
19
+
20
+ # dqlite uses qmark parameter style
21
+ paramstyle = "qmark"
22
+
23
+ # Enable SQLAlchemy statement caching
24
+ supports_statement_cache = True
25
+
26
+ # Default to NullPool since dqlite handles connection pooling internally
27
+ @classmethod
28
+ def get_pool_class(cls, url: URL) -> type[pool.Pool]:
29
+ return pool.NullPool
30
+
31
+ @classmethod
32
+ def import_dbapi(cls) -> Any:
33
+ import dqlitedbapi
34
+
35
+ return dqlitedbapi
36
+
37
+ def create_connect_args(self, url: URL) -> tuple[list[Any], dict[str, Any]]:
38
+ """Create connection arguments from URL.
39
+
40
+ URL format: dqlite://host:port/database
41
+ """
42
+ host = url.host or "localhost"
43
+ port = url.port or 9001
44
+ database = url.database or "default"
45
+
46
+ address = f"{host}:{port}"
47
+
48
+ return [], {
49
+ "address": address,
50
+ "database": database,
51
+ }
52
+
53
+ def get_isolation_level(self, dbapi_connection: DBAPIConnection) -> IsolationLevel:
54
+ """Return the isolation level.
55
+
56
+ dqlite doesn't support PRAGMA read_uncommitted, so we return
57
+ SERIALIZABLE as the default isolation level.
58
+ """
59
+ return "SERIALIZABLE"
60
+
61
+ def set_isolation_level(self, dbapi_connection: DBAPIConnection, level: str | None) -> None:
62
+ """Set isolation level.
63
+
64
+ dqlite doesn't support changing isolation levels via PRAGMA,
65
+ so this is a no-op. dqlite uses SERIALIZABLE isolation by default.
66
+ """
67
+ pass
68
+
69
+ def do_rollback(self, dbapi_connection: DBAPIConnection) -> None:
70
+ """Rollback the current transaction.
71
+
72
+ dqlite throws an error if we try to rollback when no transaction
73
+ is active, so we catch and ignore that specific error.
74
+ """
75
+ try:
76
+ dbapi_connection.rollback()
77
+ except Exception as e:
78
+ # Ignore "no transaction is active" errors
79
+ if "no transaction is active" not in str(e):
80
+ raise
81
+
82
+ def do_commit(self, dbapi_connection: DBAPIConnection) -> None:
83
+ """Commit the current transaction.
84
+
85
+ dqlite throws an error if we try to commit when no transaction
86
+ is active, so we catch and ignore that specific error.
87
+ """
88
+ try:
89
+ dbapi_connection.commit()
90
+ except Exception as e:
91
+ # Ignore "no transaction is active" errors
92
+ if "no transaction is active" not in str(e):
93
+ raise
94
+
95
+ def do_ping(self, dbapi_connection: Any) -> bool:
96
+ """Check if the connection is still alive."""
97
+ try:
98
+ cursor = dbapi_connection.cursor()
99
+ cursor.execute("SELECT 1")
100
+ cursor.close()
101
+ return True
102
+ except Exception:
103
+ return False
104
+
105
+ def _get_server_version_info(self, connection: Any) -> tuple[int, ...]:
106
+ """Return the server version as a tuple.
107
+
108
+ dqlite uses SQLite internally, so we return SQLite version.
109
+ """
110
+ cursor = connection.connection.dbapi_connection.cursor()
111
+ cursor.execute("SELECT sqlite_version()")
112
+ row = cursor.fetchone()
113
+ cursor.close()
114
+
115
+ if row:
116
+ version_str = row[0]
117
+ return tuple(int(x) for x in version_str.split("."))
118
+ return (3, 0, 0)
119
+
120
+
121
+ # Register the dialect
122
+ dialect = DqliteDialect
File without changes
@@ -0,0 +1,45 @@
1
+ """SQLAlchemy test suite requirements for dqlite dialect."""
2
+
3
+ from sqlalchemy.testing.requirements import SuiteRequirements
4
+
5
+
6
+ class Requirements(SuiteRequirements):
7
+ """Test suite requirements for dqlite dialect.
8
+
9
+ Override requirements that dqlite doesn't support.
10
+ """
11
+
12
+ @property
13
+ def datetime_literals(self) -> bool:
14
+ """dqlite/SQLite doesn't have native datetime literals."""
15
+ return False
16
+
17
+ @property
18
+ def time_microseconds(self) -> bool:
19
+ """SQLite stores time as text without microseconds."""
20
+ return False
21
+
22
+ @property
23
+ def datetime_historic(self) -> bool:
24
+ """SQLite date range limitation."""
25
+ return False
26
+
27
+ @property
28
+ def unicode_ddl(self) -> bool:
29
+ """SQLite supports unicode in DDL."""
30
+ return True
31
+
32
+ @property
33
+ def savepoints(self) -> bool:
34
+ """dqlite supports savepoints."""
35
+ return True
36
+
37
+ @property
38
+ def two_phase_transactions(self) -> bool:
39
+ """dqlite doesn't support two-phase transactions."""
40
+ return False
41
+
42
+ @property
43
+ def temp_table_reflection(self) -> bool:
44
+ """SQLite supports temp table reflection."""
45
+ return True
File without changes
@@ -0,0 +1,15 @@
1
+ """Pytest configuration for sqlalchemy-dqlite tests."""
2
+
3
+ import os
4
+
5
+ import pytest
6
+
7
+
8
+ def pytest_configure(config: pytest.Config) -> None:
9
+ config.addinivalue_line("markers", "integration: marks tests as requiring dqlite cluster")
10
+
11
+
12
+ @pytest.fixture
13
+ def cluster_address() -> str:
14
+ """Get the test cluster address."""
15
+ return os.environ.get("DQLITE_TEST_CLUSTER", "localhost:9001")
File without changes
@@ -0,0 +1,17 @@
1
+ """Integration test fixtures for sqlalchemy-dqlite."""
2
+
3
+ import pytest
4
+
5
+
6
+ @pytest.fixture
7
+ def engine_url(cluster_address: str) -> str:
8
+ """Get SQLAlchemy engine URL."""
9
+ host, port = cluster_address.split(":")
10
+ return f"dqlite://{host}:{port}/test"
11
+
12
+
13
+ @pytest.fixture
14
+ def async_engine_url(cluster_address: str) -> str:
15
+ """Get async SQLAlchemy engine URL."""
16
+ host, port = cluster_address.split(":")
17
+ return f"dqlite+aio://{host}:{port}/test"
@@ -0,0 +1,28 @@
1
+ """Integration tests for sync engine used inside an async context."""
2
+
3
+ import asyncio
4
+
5
+ import pytest
6
+ from sqlalchemy import create_engine, text
7
+
8
+
9
+ @pytest.mark.integration
10
+ class TestSyncEngineInAsyncContext:
11
+ def test_sync_engine_inside_running_loop(self, engine_url: str) -> None:
12
+ """Sync engine must work when called from inside a running event loop.
13
+
14
+ This simulates the scenario where a sync SQLAlchemy engine is used
15
+ during app startup inside an async server like uvicorn.
16
+ """
17
+
18
+ async def _run_inside_loop() -> str:
19
+ engine = create_engine(engine_url)
20
+ with engine.connect() as conn:
21
+ result = conn.execute(text("SELECT 'ok'"))
22
+ row = result.fetchone()
23
+ engine.dispose()
24
+ assert row is not None
25
+ return str(row[0])
26
+
27
+ result = asyncio.run(_run_inside_loop())
28
+ assert result == "ok"
@@ -0,0 +1,278 @@
1
+ """Integration tests for ORM operations."""
2
+
3
+ import datetime
4
+
5
+ import pytest
6
+ from sqlalchemy import (
7
+ BigInteger,
8
+ Boolean,
9
+ Column,
10
+ DateTime,
11
+ Float,
12
+ Integer,
13
+ LargeBinary,
14
+ String,
15
+ Text,
16
+ create_engine,
17
+ text,
18
+ )
19
+ from sqlalchemy.orm import Session, declarative_base
20
+
21
+ Base = declarative_base()
22
+
23
+
24
+ class User(Base): # type: ignore[valid-type,misc]
25
+ __tablename__ = "users"
26
+
27
+ id = Column(Integer, primary_key=True)
28
+ name = Column(String(100))
29
+ email = Column(String(100))
30
+
31
+
32
+ class UnicodeTest(Base): # type: ignore[valid-type,misc]
33
+ __tablename__ = "unicode_test"
34
+
35
+ id = Column(Integer, primary_key=True)
36
+ content = Column(Text)
37
+
38
+
39
+ class BlobTest(Base): # type: ignore[valid-type,misc]
40
+ __tablename__ = "blob_test"
41
+
42
+ id = Column(Integer, primary_key=True)
43
+ data = Column(LargeBinary)
44
+
45
+
46
+ class NumericTest(Base): # type: ignore[valid-type,misc]
47
+ __tablename__ = "numeric_test"
48
+
49
+ id = Column(Integer, primary_key=True)
50
+ int_val = Column(Integer)
51
+ bigint_val = Column(BigInteger)
52
+ float_val = Column(Float)
53
+ bool_val = Column(Boolean)
54
+
55
+
56
+ class DateTimeTest(Base): # type: ignore[valid-type,misc]
57
+ __tablename__ = "datetime_test"
58
+
59
+ id = Column(Integer, primary_key=True)
60
+ created_at = Column(DateTime)
61
+ updated_at = Column(DateTime, nullable=True)
62
+
63
+
64
+ @pytest.mark.integration
65
+ class TestORMOperations:
66
+ def test_create_engine(self, engine_url: str) -> None:
67
+ engine = create_engine(engine_url)
68
+ assert engine is not None
69
+ engine.dispose()
70
+
71
+ def test_raw_sql(self, engine_url: str) -> None:
72
+ engine = create_engine(engine_url)
73
+
74
+ with engine.connect() as conn:
75
+ result = conn.execute(text("SELECT 1"))
76
+ row = result.fetchone()
77
+ assert row is not None
78
+ assert row[0] == 1
79
+
80
+ engine.dispose()
81
+
82
+ def test_create_table_and_insert(self, engine_url: str) -> None:
83
+ engine = create_engine(engine_url)
84
+
85
+ # Create tables
86
+ Base.metadata.create_all(engine)
87
+
88
+ # Insert data
89
+ with Session(engine) as session:
90
+ user = User(name="Alice", email="alice@example.com")
91
+ session.add(user)
92
+ session.commit()
93
+
94
+ # Query data
95
+ users = session.query(User).filter_by(name="Alice").all()
96
+ assert len(users) == 1
97
+ assert users[0].email == "alice@example.com"
98
+
99
+ # Cleanup
100
+ Base.metadata.drop_all(engine)
101
+ engine.dispose()
102
+
103
+ def test_unicode_text(self, engine_url: str) -> None:
104
+ """Test Unicode text handling including emojis, CJK, RTL."""
105
+ engine = create_engine(engine_url)
106
+ Base.metadata.create_all(engine)
107
+
108
+ unicode_values = [
109
+ # Emojis (4-byte UTF-8)
110
+ "Hello 🎉 World",
111
+ "🎉🎊🎁🎂",
112
+ # CJK characters
113
+ "中文测试",
114
+ "日本語テスト",
115
+ "한국어 테스트",
116
+ # RTL languages
117
+ "العربية",
118
+ "עברית",
119
+ # Mixed scripts
120
+ "Hello 世界 🌍",
121
+ # Combining characters
122
+ "café résumé naïve",
123
+ ]
124
+
125
+ with Session(engine) as session:
126
+ for val in unicode_values:
127
+ # Insert
128
+ record = UnicodeTest(content=val)
129
+ session.add(record)
130
+ session.commit()
131
+
132
+ # Query and verify
133
+ result = session.query(UnicodeTest).filter_by(content=val).first()
134
+ assert result is not None, f"Failed to find: {repr(val)}"
135
+ assert result.content == val, f"Mismatch for: {repr(val)}"
136
+
137
+ # Cleanup
138
+ session.delete(result)
139
+ session.commit()
140
+
141
+ Base.metadata.drop_all(engine)
142
+ engine.dispose()
143
+
144
+ def test_binary_blob(self, engine_url: str) -> None:
145
+ """Test binary blob handling including null bytes."""
146
+ engine = create_engine(engine_url)
147
+ Base.metadata.create_all(engine)
148
+
149
+ blob_values = [
150
+ b"simple",
151
+ b"\x00\x01\x02\x03", # Null bytes
152
+ b"\xff\xfe\xfd", # High bytes
153
+ bytes(range(256)), # All byte values
154
+ ]
155
+
156
+ with Session(engine) as session:
157
+ for val in blob_values:
158
+ # Insert
159
+ record = BlobTest(data=val)
160
+ session.add(record)
161
+ session.commit()
162
+
163
+ # Query and verify
164
+ result = session.query(BlobTest).order_by(BlobTest.id.desc()).first()
165
+ assert result is not None
166
+ assert result.data == val, f"Mismatch for blob: {repr(val)}"
167
+
168
+ Base.metadata.drop_all(engine)
169
+ engine.dispose()
170
+
171
+ def test_numeric_types(self, engine_url: str) -> None:
172
+ """Test integer, bigint, float, and boolean types.
173
+
174
+ Note: dqlite has a known limitation where BOOLEAN NULL values are
175
+ returned as False (type BOOLEAN with value 0) instead of NULL.
176
+ This is because dqlite returns the column's declared type even for
177
+ NULL values, and 0 is indistinguishable from NULL for BOOLEAN columns.
178
+ """
179
+ engine = create_engine(engine_url)
180
+ Base.metadata.create_all(engine)
181
+
182
+ test_cases = [
183
+ # (int, bigint, float, bool)
184
+ (0, 0, 0.0, False),
185
+ (1, 1, 1.0, True),
186
+ (-1, -1, -1.0, False),
187
+ (2147483647, 9223372036854775807, 3.14159265358979, True),
188
+ (-2147483648, -9223372036854775808, -3.14159265358979, False),
189
+ ]
190
+
191
+ with Session(engine) as session:
192
+ for int_val, bigint_val, float_val, bool_val in test_cases:
193
+ record = NumericTest(
194
+ int_val=int_val,
195
+ bigint_val=bigint_val,
196
+ float_val=float_val,
197
+ bool_val=bool_val,
198
+ )
199
+ session.add(record)
200
+ session.commit()
201
+
202
+ # Query and verify
203
+ result = session.query(NumericTest).order_by(NumericTest.id.desc()).first()
204
+ assert result is not None
205
+ assert result.int_val == int_val
206
+ assert result.bigint_val == bigint_val
207
+ if float_val is not None:
208
+ assert abs(result.float_val - float_val) < 1e-9
209
+ assert result.bool_val == bool_val
210
+
211
+ Base.metadata.drop_all(engine)
212
+ engine.dispose()
213
+
214
+ def test_datetime_types(self, engine_url: str) -> None:
215
+ """Test DateTime column type.
216
+
217
+ Note: dqlite has a known limitation where DATETIME NULL values are
218
+ returned as empty string instead of NULL. This causes SQLAlchemy's
219
+ DateTime processor to fail when parsing. Avoid using NULL datetime
220
+ values with dqlite - use a sentinel value if needed.
221
+ """
222
+ engine = create_engine(engine_url)
223
+ Base.metadata.create_all(engine)
224
+
225
+ test_dates = [
226
+ datetime.datetime(2024, 1, 15, 10, 30, 45),
227
+ datetime.datetime(1970, 1, 1, 0, 0, 0), # Unix epoch
228
+ datetime.datetime(2038, 1, 19, 3, 14, 7), # Near Y2038
229
+ datetime.datetime.now().replace(microsecond=0),
230
+ ]
231
+
232
+ with Session(engine) as session:
233
+ for dt in test_dates:
234
+ # Note: We set updated_at to a value, not NULL, due to dqlite limitation
235
+ record = DateTimeTest(created_at=dt, updated_at=dt)
236
+ session.add(record)
237
+ session.commit()
238
+
239
+ # Query and verify
240
+ result = session.query(DateTimeTest).order_by(DateTimeTest.id.desc()).first()
241
+ assert result is not None
242
+
243
+ # SQLite stores datetime as text, compare with second precision
244
+ assert result.created_at.year == dt.year
245
+ assert result.created_at.month == dt.month
246
+ assert result.created_at.day == dt.day
247
+ assert result.created_at.hour == dt.hour
248
+ assert result.created_at.minute == dt.minute
249
+ assert result.created_at.second == dt.second
250
+
251
+ Base.metadata.drop_all(engine)
252
+ engine.dispose()
253
+
254
+ def test_null_handling(self, engine_url: str) -> None:
255
+ """Test NULL values across different column types."""
256
+ engine = create_engine(engine_url)
257
+ Base.metadata.create_all(engine)
258
+
259
+ with Session(engine) as session:
260
+ # Insert record with all nullable fields as NULL
261
+ record = NumericTest(
262
+ int_val=None,
263
+ bigint_val=None,
264
+ float_val=None,
265
+ bool_val=None,
266
+ )
267
+ session.add(record)
268
+ session.commit()
269
+
270
+ result = session.query(NumericTest).first()
271
+ assert result is not None
272
+ assert result.int_val is None
273
+ assert result.bigint_val is None
274
+ assert result.float_val is None
275
+ assert result.bool_val is None
276
+
277
+ Base.metadata.drop_all(engine)
278
+ engine.dispose()
@@ -0,0 +1,94 @@
1
+ """Tests for dqlite dialect."""
2
+
3
+ from sqlalchemy.engine import URL
4
+
5
+ from sqlalchemydqlite import DqliteDialect
6
+ from sqlalchemydqlite.aio import DqliteDialect_aio
7
+
8
+
9
+ class TestDqliteDialect:
10
+ def test_dialect_name(self) -> None:
11
+ dialect = DqliteDialect()
12
+ assert dialect.name == "dqlite"
13
+
14
+ def test_dialect_driver(self) -> None:
15
+ dialect = DqliteDialect()
16
+ assert dialect.driver == "dqlitedbapi"
17
+
18
+ def test_paramstyle(self) -> None:
19
+ dialect = DqliteDialect()
20
+ assert dialect.paramstyle == "qmark"
21
+
22
+ def test_import_dbapi(self) -> None:
23
+ dbapi = DqliteDialect.import_dbapi()
24
+ assert hasattr(dbapi, "connect")
25
+ assert hasattr(dbapi, "apilevel")
26
+ assert dbapi.apilevel == "2.0"
27
+
28
+ def test_create_connect_args_default(self) -> None:
29
+ dialect = DqliteDialect()
30
+ url = URL.create("dqlite")
31
+
32
+ args, kwargs = dialect.create_connect_args(url)
33
+
34
+ assert args == []
35
+ assert kwargs["address"] == "localhost:9001"
36
+ assert kwargs["database"] == "default"
37
+
38
+ def test_create_connect_args_custom(self) -> None:
39
+ dialect = DqliteDialect()
40
+ url = URL.create("dqlite", host="node1", port=9002, database="mydb")
41
+
42
+ args, kwargs = dialect.create_connect_args(url)
43
+
44
+ assert kwargs["address"] == "node1:9002"
45
+ assert kwargs["database"] == "mydb"
46
+
47
+
48
+ class TestDqliteDialectAio:
49
+ def test_dialect_name(self) -> None:
50
+ dialect = DqliteDialect_aio()
51
+ assert dialect.name == "dqlite"
52
+
53
+ def test_dialect_is_async(self) -> None:
54
+ dialect = DqliteDialect_aio()
55
+ assert dialect.is_async is True
56
+
57
+ def test_import_dbapi(self) -> None:
58
+ dbapi = DqliteDialect_aio.import_dbapi()
59
+ assert hasattr(dbapi, "aconnect")
60
+
61
+ def test_import_dbapi_has_paramstyle(self) -> None:
62
+ """Async dbapi module must expose paramstyle for SQLAlchemy dialect init."""
63
+ dbapi = DqliteDialect_aio.import_dbapi()
64
+ assert dbapi.paramstyle == "qmark"
65
+
66
+ def test_import_dbapi_has_module_attributes(self) -> None:
67
+ """Async dbapi module must expose PEP 249 attributes for SQLAlchemy."""
68
+ dbapi = DqliteDialect_aio.import_dbapi()
69
+ assert dbapi.apilevel == "2.0"
70
+ assert dbapi.threadsafety == 1
71
+
72
+ def test_create_async_engine(self) -> None:
73
+ """create_async_engine must not raise during dialect initialization."""
74
+ from sqlalchemy.ext.asyncio import create_async_engine
75
+
76
+ engine = create_async_engine("dqlite+aio://localhost:19001/test")
77
+ assert engine.dialect.name == "dqlite"
78
+ assert engine.dialect.driver == "dqlitedbapi_aio"
79
+
80
+
81
+ class TestURLParsing:
82
+ def test_parse_basic_url(self) -> None:
83
+ url = URL.create("dqlite", host="localhost", port=9001, database="test")
84
+ assert url.host == "localhost"
85
+ assert url.port == 9001
86
+ assert url.database == "test"
87
+
88
+ def test_url_string_format(self) -> None:
89
+ url = URL.create("dqlite", host="node1", port=9001, database="mydb")
90
+ assert str(url) == "dqlite://node1:9001/mydb"
91
+
92
+ def test_aio_url_string_format(self) -> None:
93
+ url = URL.create("dqlite+aio", host="node1", port=9001, database="mydb")
94
+ assert str(url) == "dqlite+aio://node1:9001/mydb"