whenever-sqlalchemy 0.1.0b1__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.
- whenever_sqlalchemy-0.1.0b1/CHANGELOG.md +11 -0
- whenever_sqlalchemy-0.1.0b1/LICENSE +21 -0
- whenever_sqlalchemy-0.1.0b1/MANIFEST.in +6 -0
- whenever_sqlalchemy-0.1.0b1/PKG-INFO +122 -0
- whenever_sqlalchemy-0.1.0b1/README.md +81 -0
- whenever_sqlalchemy-0.1.0b1/pyproject.toml +77 -0
- whenever_sqlalchemy-0.1.0b1/setup.cfg +4 -0
- whenever_sqlalchemy-0.1.0b1/src/whenever_sqlalchemy/__init__.py +126 -0
- whenever_sqlalchemy-0.1.0b1/src/whenever_sqlalchemy/py.typed +0 -0
- whenever_sqlalchemy-0.1.0b1/src/whenever_sqlalchemy.egg-info/PKG-INFO +122 -0
- whenever_sqlalchemy-0.1.0b1/src/whenever_sqlalchemy.egg-info/SOURCES.txt +14 -0
- whenever_sqlalchemy-0.1.0b1/src/whenever_sqlalchemy.egg-info/dependency_links.txt +1 -0
- whenever_sqlalchemy-0.1.0b1/src/whenever_sqlalchemy.egg-info/requires.txt +12 -0
- whenever_sqlalchemy-0.1.0b1/src/whenever_sqlalchemy.egg-info/top_level.txt +1 -0
- whenever_sqlalchemy-0.1.0b1/tests/__init__.py +0 -0
- whenever_sqlalchemy-0.1.0b1/tests/test_whenever_sqlalchemy.py +356 -0
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.1.0 (unreleased)
|
|
4
|
+
|
|
5
|
+
Initial release.
|
|
6
|
+
|
|
7
|
+
- `InstantType` — maps `Instant` to ``TIMESTAMP WITH TIME ZONE``
|
|
8
|
+
- `PlainDateTimeType` — maps `PlainDateTime` to ``TIMESTAMP``
|
|
9
|
+
- `DateType` — maps `Date` to ``DATE``
|
|
10
|
+
- `TimeType` — maps `Time` to ``TIME``
|
|
11
|
+
- `type_annotation_map` — ready-to-use dict for SQLAlchemy 2.x `Mapped[]` annotations
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 - 2023 Arie Bovenberg
|
|
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,122 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: whenever-sqlalchemy
|
|
3
|
+
Version: 0.1.0b1
|
|
4
|
+
Summary: SQLAlchemy column types for the whenever datetime library
|
|
5
|
+
Author-email: Arie Bovenberg <a.c.bovenberg@gmail.com>
|
|
6
|
+
Maintainer-email: Arie Bovenberg <a.c.bovenberg@gmail.com>
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
Project-URL: Repository, https://github.com/ariebovenberg/whenever-sqlalchemy
|
|
9
|
+
Project-URL: Issues, https://github.com/ariebovenberg/whenever/issues
|
|
10
|
+
Project-URL: Changelog, https://github.com/ariebovenberg/whenever/blob/main/whenever_sqlalchemy/CHANGELOG.md
|
|
11
|
+
Keywords: whenever,sqlalchemy,datetime,date,time,orm
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
21
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
22
|
+
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
|
23
|
+
Classifier: Typing :: Typed
|
|
24
|
+
Classifier: Topic :: Database
|
|
25
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
26
|
+
Requires-Python: >=3.10
|
|
27
|
+
Description-Content-Type: text/markdown
|
|
28
|
+
License-File: LICENSE
|
|
29
|
+
Requires-Dist: whenever>=0.10.0b1
|
|
30
|
+
Requires-Dist: sqlalchemy>=1.4
|
|
31
|
+
Provides-Extra: dev
|
|
32
|
+
Requires-Dist: pytest<9,>=7; extra == "dev"
|
|
33
|
+
Requires-Dist: pytest-cov<8,>=7; extra == "dev"
|
|
34
|
+
Requires-Dist: black<26,>=25; extra == "dev"
|
|
35
|
+
Requires-Dist: isort<7,>=6; extra == "dev"
|
|
36
|
+
Requires-Dist: flake8<8,>=6; extra == "dev"
|
|
37
|
+
Requires-Dist: mypy<1.20,>=1.19; extra == "dev"
|
|
38
|
+
Requires-Dist: build; extra == "dev"
|
|
39
|
+
Requires-Dist: twine>=6.2.0; extra == "dev"
|
|
40
|
+
Dynamic: license-file
|
|
41
|
+
|
|
42
|
+
# whenever-sqlalchemy
|
|
43
|
+
|
|
44
|
+
SQLAlchemy column types for the [whenever](https://github.com/ariebovenberg/whenever) datetime library.
|
|
45
|
+
|
|
46
|
+
## Installation
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
pip install whenever-sqlalchemy
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Type mapping
|
|
53
|
+
|
|
54
|
+
| whenever type | SQL type |
|
|
55
|
+
|------------------|-----------------------------|
|
|
56
|
+
| `Instant` | `TIMESTAMP WITH TIME ZONE` |
|
|
57
|
+
| `PlainDateTime` | `TIMESTAMP` (no timezone) |
|
|
58
|
+
| `Date` | `DATE` |
|
|
59
|
+
| `Time` | `TIME` |
|
|
60
|
+
|
|
61
|
+
## Usage
|
|
62
|
+
|
|
63
|
+
### SQLAlchemy 2.x — `Mapped[]` style (recommended)
|
|
64
|
+
|
|
65
|
+
Register `type_annotation_map` on your `DeclarativeBase` once, then use
|
|
66
|
+
`Mapped[Instant]` exactly like you would use `Mapped[datetime]`:
|
|
67
|
+
|
|
68
|
+
```python
|
|
69
|
+
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
|
|
70
|
+
from whenever import Date, Instant, PlainDateTime, Time
|
|
71
|
+
from whenever_sqlalchemy import type_annotation_map
|
|
72
|
+
|
|
73
|
+
class Base(DeclarativeBase):
|
|
74
|
+
type_annotation_map = type_annotation_map # merge with your own if needed
|
|
75
|
+
|
|
76
|
+
class Event(Base):
|
|
77
|
+
__tablename__ = "events"
|
|
78
|
+
id: Mapped[int] = mapped_column(primary_key=True)
|
|
79
|
+
timestamp: Mapped[Instant]
|
|
80
|
+
scheduled: Mapped[PlainDateTime | None]
|
|
81
|
+
date: Mapped[Date | None]
|
|
82
|
+
time: Mapped[Time | None]
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### SQLAlchemy 1.4 / 2.x — `Column()` style
|
|
86
|
+
|
|
87
|
+
```python
|
|
88
|
+
from sqlalchemy import Column, Integer
|
|
89
|
+
from whenever_sqlalchemy import DateType, InstantType, PlainDateTimeType, TimeType
|
|
90
|
+
|
|
91
|
+
class Event(Base):
|
|
92
|
+
__tablename__ = "events"
|
|
93
|
+
id = Column(Integer, primary_key=True)
|
|
94
|
+
timestamp = Column(InstantType)
|
|
95
|
+
scheduled = Column(PlainDateTimeType, nullable=True)
|
|
96
|
+
date = Column(DateType, nullable=True)
|
|
97
|
+
time = Column(TimeType, nullable=True)
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## Notes
|
|
101
|
+
|
|
102
|
+
**Nanoseconds** — `whenever` types support nanosecond precision; SQL
|
|
103
|
+
`TIMESTAMP` columns store at most microsecond precision. Values are
|
|
104
|
+
truncated (not rounded) to microseconds on write, matching `to_stdlib()`.
|
|
105
|
+
|
|
106
|
+
**`server_default`** — `server_default=func.now()` works as expected: the
|
|
107
|
+
server sets the value and SQLAlchemy fetches it back, converting it to the
|
|
108
|
+
appropriate `whenever` type.
|
|
109
|
+
|
|
110
|
+
**Dialect notes**
|
|
111
|
+
|
|
112
|
+
- **PostgreSQL**: uses `TIMESTAMPTZ` natively; full roundtrip fidelity.
|
|
113
|
+
- **SQLite**: stores datetimes as text and drops timezone info. `InstantType`
|
|
114
|
+
reattaches UTC tzinfo on retrieval (the stored value is UTC).
|
|
115
|
+
- **MySQL / MariaDB / SQL Server**: similar to SQLite for timezone columns;
|
|
116
|
+
the UTC-reattach workaround covers these too.
|
|
117
|
+
|
|
118
|
+
## Compatibility
|
|
119
|
+
|
|
120
|
+
- Python 3.10+
|
|
121
|
+
- whenever 0.10+
|
|
122
|
+
- SQLAlchemy 1.4+ (`Column()` style); SQLAlchemy 2.0+ (`Mapped[]` style)
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# whenever-sqlalchemy
|
|
2
|
+
|
|
3
|
+
SQLAlchemy column types for the [whenever](https://github.com/ariebovenberg/whenever) datetime library.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install whenever-sqlalchemy
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Type mapping
|
|
12
|
+
|
|
13
|
+
| whenever type | SQL type |
|
|
14
|
+
|------------------|-----------------------------|
|
|
15
|
+
| `Instant` | `TIMESTAMP WITH TIME ZONE` |
|
|
16
|
+
| `PlainDateTime` | `TIMESTAMP` (no timezone) |
|
|
17
|
+
| `Date` | `DATE` |
|
|
18
|
+
| `Time` | `TIME` |
|
|
19
|
+
|
|
20
|
+
## Usage
|
|
21
|
+
|
|
22
|
+
### SQLAlchemy 2.x — `Mapped[]` style (recommended)
|
|
23
|
+
|
|
24
|
+
Register `type_annotation_map` on your `DeclarativeBase` once, then use
|
|
25
|
+
`Mapped[Instant]` exactly like you would use `Mapped[datetime]`:
|
|
26
|
+
|
|
27
|
+
```python
|
|
28
|
+
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
|
|
29
|
+
from whenever import Date, Instant, PlainDateTime, Time
|
|
30
|
+
from whenever_sqlalchemy import type_annotation_map
|
|
31
|
+
|
|
32
|
+
class Base(DeclarativeBase):
|
|
33
|
+
type_annotation_map = type_annotation_map # merge with your own if needed
|
|
34
|
+
|
|
35
|
+
class Event(Base):
|
|
36
|
+
__tablename__ = "events"
|
|
37
|
+
id: Mapped[int] = mapped_column(primary_key=True)
|
|
38
|
+
timestamp: Mapped[Instant]
|
|
39
|
+
scheduled: Mapped[PlainDateTime | None]
|
|
40
|
+
date: Mapped[Date | None]
|
|
41
|
+
time: Mapped[Time | None]
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### SQLAlchemy 1.4 / 2.x — `Column()` style
|
|
45
|
+
|
|
46
|
+
```python
|
|
47
|
+
from sqlalchemy import Column, Integer
|
|
48
|
+
from whenever_sqlalchemy import DateType, InstantType, PlainDateTimeType, TimeType
|
|
49
|
+
|
|
50
|
+
class Event(Base):
|
|
51
|
+
__tablename__ = "events"
|
|
52
|
+
id = Column(Integer, primary_key=True)
|
|
53
|
+
timestamp = Column(InstantType)
|
|
54
|
+
scheduled = Column(PlainDateTimeType, nullable=True)
|
|
55
|
+
date = Column(DateType, nullable=True)
|
|
56
|
+
time = Column(TimeType, nullable=True)
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Notes
|
|
60
|
+
|
|
61
|
+
**Nanoseconds** — `whenever` types support nanosecond precision; SQL
|
|
62
|
+
`TIMESTAMP` columns store at most microsecond precision. Values are
|
|
63
|
+
truncated (not rounded) to microseconds on write, matching `to_stdlib()`.
|
|
64
|
+
|
|
65
|
+
**`server_default`** — `server_default=func.now()` works as expected: the
|
|
66
|
+
server sets the value and SQLAlchemy fetches it back, converting it to the
|
|
67
|
+
appropriate `whenever` type.
|
|
68
|
+
|
|
69
|
+
**Dialect notes**
|
|
70
|
+
|
|
71
|
+
- **PostgreSQL**: uses `TIMESTAMPTZ` natively; full roundtrip fidelity.
|
|
72
|
+
- **SQLite**: stores datetimes as text and drops timezone info. `InstantType`
|
|
73
|
+
reattaches UTC tzinfo on retrieval (the stored value is UTC).
|
|
74
|
+
- **MySQL / MariaDB / SQL Server**: similar to SQLite for timezone columns;
|
|
75
|
+
the UTC-reattach workaround covers these too.
|
|
76
|
+
|
|
77
|
+
## Compatibility
|
|
78
|
+
|
|
79
|
+
- Python 3.10+
|
|
80
|
+
- whenever 0.10+
|
|
81
|
+
- SQLAlchemy 1.4+ (`Column()` style); SQLAlchemy 2.0+ (`Mapped[]` style)
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "whenever-sqlalchemy"
|
|
3
|
+
version = "0.1.0b1"
|
|
4
|
+
authors = [
|
|
5
|
+
{name = "Arie Bovenberg", email = "a.c.bovenberg@gmail.com"},
|
|
6
|
+
]
|
|
7
|
+
maintainers = [
|
|
8
|
+
{name = "Arie Bovenberg", email = "a.c.bovenberg@gmail.com"},
|
|
9
|
+
]
|
|
10
|
+
readme = "README.md"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
description = "SQLAlchemy column types for the whenever datetime library"
|
|
13
|
+
requires-python = ">=3.10"
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Development Status :: 4 - Beta",
|
|
16
|
+
"Intended Audience :: Developers",
|
|
17
|
+
"Operating System :: OS Independent",
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"Programming Language :: Python :: 3.10",
|
|
20
|
+
"Programming Language :: Python :: 3.11",
|
|
21
|
+
"Programming Language :: Python :: 3.12",
|
|
22
|
+
"Programming Language :: Python :: 3.13",
|
|
23
|
+
"Programming Language :: Python :: 3.14",
|
|
24
|
+
"Programming Language :: Python :: Implementation :: CPython",
|
|
25
|
+
"Programming Language :: Python :: Implementation :: PyPy",
|
|
26
|
+
"Typing :: Typed",
|
|
27
|
+
"Topic :: Database",
|
|
28
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
29
|
+
]
|
|
30
|
+
dependencies = [
|
|
31
|
+
"whenever>=0.10.0b1",
|
|
32
|
+
"sqlalchemy>=1.4",
|
|
33
|
+
]
|
|
34
|
+
keywords = ["whenever", "sqlalchemy", "datetime", "date", "time", "orm"]
|
|
35
|
+
|
|
36
|
+
[project.urls]
|
|
37
|
+
Repository = "https://github.com/ariebovenberg/whenever-sqlalchemy"
|
|
38
|
+
Issues = "https://github.com/ariebovenberg/whenever/issues"
|
|
39
|
+
Changelog = "https://github.com/ariebovenberg/whenever/blob/main/whenever_sqlalchemy/CHANGELOG.md"
|
|
40
|
+
|
|
41
|
+
[project.optional-dependencies]
|
|
42
|
+
dev = [
|
|
43
|
+
"pytest>=7,<9",
|
|
44
|
+
"pytest-cov>=7,<8",
|
|
45
|
+
"black>=25,<26",
|
|
46
|
+
"isort>=6,<7",
|
|
47
|
+
"flake8>=6,<8",
|
|
48
|
+
"mypy>=1.19,<1.20",
|
|
49
|
+
"build",
|
|
50
|
+
"twine>=6.2.0",
|
|
51
|
+
]
|
|
52
|
+
|
|
53
|
+
[tool.setuptools.packages.find]
|
|
54
|
+
where = ["src"]
|
|
55
|
+
|
|
56
|
+
[build-system]
|
|
57
|
+
requires = ["setuptools>=61", "wheel"]
|
|
58
|
+
build-backend = "setuptools.build_meta"
|
|
59
|
+
|
|
60
|
+
[tool.black]
|
|
61
|
+
line-length = 79
|
|
62
|
+
include = '\.pyi?$'
|
|
63
|
+
|
|
64
|
+
[tool.isort]
|
|
65
|
+
profile = 'black'
|
|
66
|
+
line_length = 79
|
|
67
|
+
combine_as_imports = true
|
|
68
|
+
|
|
69
|
+
[tool.mypy]
|
|
70
|
+
warn_unused_ignores = true
|
|
71
|
+
strict = true
|
|
72
|
+
disallow_untyped_defs = true
|
|
73
|
+
|
|
74
|
+
[[tool.mypy.overrides]]
|
|
75
|
+
module = ["tests.*"]
|
|
76
|
+
disable_error_code = ["no-untyped-def"]
|
|
77
|
+
check_untyped_defs = false
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"""SQLAlchemy column types for ``whenever``."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import datetime as _stdlib
|
|
6
|
+
|
|
7
|
+
import sqlalchemy as _sa
|
|
8
|
+
from sqlalchemy.types import TypeDecorator
|
|
9
|
+
from whenever import Date, Instant, PlainDateTime, Time
|
|
10
|
+
|
|
11
|
+
_UTC = _stdlib.timezone.utc
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class InstantType(TypeDecorator): # type: ignore[type-arg]
|
|
15
|
+
"""SQLAlchemy column type for :class:`~whenever.Instant`.
|
|
16
|
+
|
|
17
|
+
Stored as ``TIMESTAMP WITH TIME ZONE`` (``TIMESTAMPTZ`` on PostgreSQL).
|
|
18
|
+
Nanoseconds are truncated to microseconds on storage.
|
|
19
|
+
|
|
20
|
+
On databases that do not preserve timezone information (e.g. SQLite),
|
|
21
|
+
the stored UTC value is returned correctly.
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
impl = _sa.DateTime(timezone=True)
|
|
25
|
+
cache_ok = True
|
|
26
|
+
|
|
27
|
+
def process_bind_param(
|
|
28
|
+
self, value: Instant | None, dialect: object
|
|
29
|
+
) -> _stdlib.datetime | None:
|
|
30
|
+
if value is None:
|
|
31
|
+
return None
|
|
32
|
+
return value.to_stdlib()
|
|
33
|
+
|
|
34
|
+
def process_result_value(
|
|
35
|
+
self, value: _stdlib.datetime | None, dialect: object
|
|
36
|
+
) -> Instant | None:
|
|
37
|
+
if value is None:
|
|
38
|
+
return None
|
|
39
|
+
# SQLite (and other backends without native tz support) returns naive
|
|
40
|
+
# datetimes for TIMESTAMP WITH TIME ZONE columns. We stored UTC, so
|
|
41
|
+
# we can safely attach UTC tzinfo on retrieval.
|
|
42
|
+
if value.tzinfo is None:
|
|
43
|
+
value = value.replace(tzinfo=_UTC)
|
|
44
|
+
return Instant(value)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class PlainDateTimeType(TypeDecorator): # type: ignore[type-arg]
|
|
48
|
+
"""SQLAlchemy column type for :class:`~whenever.PlainDateTime`.
|
|
49
|
+
|
|
50
|
+
Stored as ``TIMESTAMP`` (without timezone).
|
|
51
|
+
Nanoseconds are truncated to microseconds on storage.
|
|
52
|
+
"""
|
|
53
|
+
|
|
54
|
+
impl = _sa.DateTime(timezone=False)
|
|
55
|
+
cache_ok = True
|
|
56
|
+
|
|
57
|
+
def process_bind_param(
|
|
58
|
+
self, value: PlainDateTime | None, dialect: object
|
|
59
|
+
) -> _stdlib.datetime | None:
|
|
60
|
+
if value is None:
|
|
61
|
+
return None
|
|
62
|
+
return value.to_stdlib()
|
|
63
|
+
|
|
64
|
+
def process_result_value(
|
|
65
|
+
self, value: _stdlib.datetime | None, dialect: object
|
|
66
|
+
) -> PlainDateTime | None:
|
|
67
|
+
if value is None:
|
|
68
|
+
return None
|
|
69
|
+
return PlainDateTime(value)
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
class DateType(TypeDecorator): # type: ignore[type-arg]
|
|
73
|
+
"""SQLAlchemy column type for :class:`~whenever.Date`.
|
|
74
|
+
|
|
75
|
+
Stored as ``DATE``.
|
|
76
|
+
"""
|
|
77
|
+
|
|
78
|
+
impl = _sa.Date()
|
|
79
|
+
cache_ok = True
|
|
80
|
+
|
|
81
|
+
def process_bind_param(
|
|
82
|
+
self, value: Date | None, dialect: object
|
|
83
|
+
) -> _stdlib.date | None:
|
|
84
|
+
if value is None:
|
|
85
|
+
return None
|
|
86
|
+
return value.to_stdlib()
|
|
87
|
+
|
|
88
|
+
def process_result_value(
|
|
89
|
+
self, value: _stdlib.date | None, dialect: object
|
|
90
|
+
) -> Date | None:
|
|
91
|
+
if value is None:
|
|
92
|
+
return None
|
|
93
|
+
return Date(value)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
class TimeType(TypeDecorator): # type: ignore[type-arg]
|
|
97
|
+
"""SQLAlchemy column type for :class:`~whenever.Time`.
|
|
98
|
+
|
|
99
|
+
Stored as ``TIME``.
|
|
100
|
+
Nanoseconds are truncated to microseconds on storage.
|
|
101
|
+
"""
|
|
102
|
+
|
|
103
|
+
impl = _sa.Time()
|
|
104
|
+
cache_ok = True
|
|
105
|
+
|
|
106
|
+
def process_bind_param(
|
|
107
|
+
self, value: Time | None, dialect: object
|
|
108
|
+
) -> _stdlib.time | None:
|
|
109
|
+
if value is None:
|
|
110
|
+
return None
|
|
111
|
+
return value.to_stdlib()
|
|
112
|
+
|
|
113
|
+
def process_result_value(
|
|
114
|
+
self, value: _stdlib.time | None, dialect: object
|
|
115
|
+
) -> Time | None:
|
|
116
|
+
if value is None:
|
|
117
|
+
return None
|
|
118
|
+
return Time(value)
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
type_annotation_map = {
|
|
122
|
+
Instant: InstantType(),
|
|
123
|
+
PlainDateTime: PlainDateTimeType(),
|
|
124
|
+
Date: DateType(),
|
|
125
|
+
Time: TimeType(),
|
|
126
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: whenever-sqlalchemy
|
|
3
|
+
Version: 0.1.0b1
|
|
4
|
+
Summary: SQLAlchemy column types for the whenever datetime library
|
|
5
|
+
Author-email: Arie Bovenberg <a.c.bovenberg@gmail.com>
|
|
6
|
+
Maintainer-email: Arie Bovenberg <a.c.bovenberg@gmail.com>
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
Project-URL: Repository, https://github.com/ariebovenberg/whenever-sqlalchemy
|
|
9
|
+
Project-URL: Issues, https://github.com/ariebovenberg/whenever/issues
|
|
10
|
+
Project-URL: Changelog, https://github.com/ariebovenberg/whenever/blob/main/whenever_sqlalchemy/CHANGELOG.md
|
|
11
|
+
Keywords: whenever,sqlalchemy,datetime,date,time,orm
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
21
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
22
|
+
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
|
23
|
+
Classifier: Typing :: Typed
|
|
24
|
+
Classifier: Topic :: Database
|
|
25
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
26
|
+
Requires-Python: >=3.10
|
|
27
|
+
Description-Content-Type: text/markdown
|
|
28
|
+
License-File: LICENSE
|
|
29
|
+
Requires-Dist: whenever>=0.10.0b1
|
|
30
|
+
Requires-Dist: sqlalchemy>=1.4
|
|
31
|
+
Provides-Extra: dev
|
|
32
|
+
Requires-Dist: pytest<9,>=7; extra == "dev"
|
|
33
|
+
Requires-Dist: pytest-cov<8,>=7; extra == "dev"
|
|
34
|
+
Requires-Dist: black<26,>=25; extra == "dev"
|
|
35
|
+
Requires-Dist: isort<7,>=6; extra == "dev"
|
|
36
|
+
Requires-Dist: flake8<8,>=6; extra == "dev"
|
|
37
|
+
Requires-Dist: mypy<1.20,>=1.19; extra == "dev"
|
|
38
|
+
Requires-Dist: build; extra == "dev"
|
|
39
|
+
Requires-Dist: twine>=6.2.0; extra == "dev"
|
|
40
|
+
Dynamic: license-file
|
|
41
|
+
|
|
42
|
+
# whenever-sqlalchemy
|
|
43
|
+
|
|
44
|
+
SQLAlchemy column types for the [whenever](https://github.com/ariebovenberg/whenever) datetime library.
|
|
45
|
+
|
|
46
|
+
## Installation
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
pip install whenever-sqlalchemy
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Type mapping
|
|
53
|
+
|
|
54
|
+
| whenever type | SQL type |
|
|
55
|
+
|------------------|-----------------------------|
|
|
56
|
+
| `Instant` | `TIMESTAMP WITH TIME ZONE` |
|
|
57
|
+
| `PlainDateTime` | `TIMESTAMP` (no timezone) |
|
|
58
|
+
| `Date` | `DATE` |
|
|
59
|
+
| `Time` | `TIME` |
|
|
60
|
+
|
|
61
|
+
## Usage
|
|
62
|
+
|
|
63
|
+
### SQLAlchemy 2.x — `Mapped[]` style (recommended)
|
|
64
|
+
|
|
65
|
+
Register `type_annotation_map` on your `DeclarativeBase` once, then use
|
|
66
|
+
`Mapped[Instant]` exactly like you would use `Mapped[datetime]`:
|
|
67
|
+
|
|
68
|
+
```python
|
|
69
|
+
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
|
|
70
|
+
from whenever import Date, Instant, PlainDateTime, Time
|
|
71
|
+
from whenever_sqlalchemy import type_annotation_map
|
|
72
|
+
|
|
73
|
+
class Base(DeclarativeBase):
|
|
74
|
+
type_annotation_map = type_annotation_map # merge with your own if needed
|
|
75
|
+
|
|
76
|
+
class Event(Base):
|
|
77
|
+
__tablename__ = "events"
|
|
78
|
+
id: Mapped[int] = mapped_column(primary_key=True)
|
|
79
|
+
timestamp: Mapped[Instant]
|
|
80
|
+
scheduled: Mapped[PlainDateTime | None]
|
|
81
|
+
date: Mapped[Date | None]
|
|
82
|
+
time: Mapped[Time | None]
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### SQLAlchemy 1.4 / 2.x — `Column()` style
|
|
86
|
+
|
|
87
|
+
```python
|
|
88
|
+
from sqlalchemy import Column, Integer
|
|
89
|
+
from whenever_sqlalchemy import DateType, InstantType, PlainDateTimeType, TimeType
|
|
90
|
+
|
|
91
|
+
class Event(Base):
|
|
92
|
+
__tablename__ = "events"
|
|
93
|
+
id = Column(Integer, primary_key=True)
|
|
94
|
+
timestamp = Column(InstantType)
|
|
95
|
+
scheduled = Column(PlainDateTimeType, nullable=True)
|
|
96
|
+
date = Column(DateType, nullable=True)
|
|
97
|
+
time = Column(TimeType, nullable=True)
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## Notes
|
|
101
|
+
|
|
102
|
+
**Nanoseconds** — `whenever` types support nanosecond precision; SQL
|
|
103
|
+
`TIMESTAMP` columns store at most microsecond precision. Values are
|
|
104
|
+
truncated (not rounded) to microseconds on write, matching `to_stdlib()`.
|
|
105
|
+
|
|
106
|
+
**`server_default`** — `server_default=func.now()` works as expected: the
|
|
107
|
+
server sets the value and SQLAlchemy fetches it back, converting it to the
|
|
108
|
+
appropriate `whenever` type.
|
|
109
|
+
|
|
110
|
+
**Dialect notes**
|
|
111
|
+
|
|
112
|
+
- **PostgreSQL**: uses `TIMESTAMPTZ` natively; full roundtrip fidelity.
|
|
113
|
+
- **SQLite**: stores datetimes as text and drops timezone info. `InstantType`
|
|
114
|
+
reattaches UTC tzinfo on retrieval (the stored value is UTC).
|
|
115
|
+
- **MySQL / MariaDB / SQL Server**: similar to SQLite for timezone columns;
|
|
116
|
+
the UTC-reattach workaround covers these too.
|
|
117
|
+
|
|
118
|
+
## Compatibility
|
|
119
|
+
|
|
120
|
+
- Python 3.10+
|
|
121
|
+
- whenever 0.10+
|
|
122
|
+
- SQLAlchemy 1.4+ (`Column()` style); SQLAlchemy 2.0+ (`Mapped[]` style)
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
CHANGELOG.md
|
|
2
|
+
LICENSE
|
|
3
|
+
MANIFEST.in
|
|
4
|
+
README.md
|
|
5
|
+
pyproject.toml
|
|
6
|
+
src/whenever_sqlalchemy/__init__.py
|
|
7
|
+
src/whenever_sqlalchemy/py.typed
|
|
8
|
+
src/whenever_sqlalchemy.egg-info/PKG-INFO
|
|
9
|
+
src/whenever_sqlalchemy.egg-info/SOURCES.txt
|
|
10
|
+
src/whenever_sqlalchemy.egg-info/dependency_links.txt
|
|
11
|
+
src/whenever_sqlalchemy.egg-info/requires.txt
|
|
12
|
+
src/whenever_sqlalchemy.egg-info/top_level.txt
|
|
13
|
+
tests/__init__.py
|
|
14
|
+
tests/test_whenever_sqlalchemy.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
whenever_sqlalchemy
|
|
File without changes
|
|
@@ -0,0 +1,356 @@
|
|
|
1
|
+
import datetime as stdlib_datetime
|
|
2
|
+
from typing import Optional
|
|
3
|
+
|
|
4
|
+
import pytest
|
|
5
|
+
from sqlalchemy import Column, Integer, create_engine, func
|
|
6
|
+
from sqlalchemy.orm import Session, declarative_base
|
|
7
|
+
from whenever import Date, Instant, PlainDateTime, Time
|
|
8
|
+
|
|
9
|
+
from whenever_sqlalchemy import (
|
|
10
|
+
DateType,
|
|
11
|
+
InstantType,
|
|
12
|
+
PlainDateTimeType,
|
|
13
|
+
TimeType,
|
|
14
|
+
type_annotation_map as whenever_type_annotation_map,
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
_UTC = stdlib_datetime.timezone.utc
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
# ── Unit tests (no database) ─────────────────────────────────────────────────
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class TestInstantType:
|
|
24
|
+
def test_bind_none(self):
|
|
25
|
+
assert InstantType().process_bind_param(None, None) is None
|
|
26
|
+
|
|
27
|
+
def test_result_none(self):
|
|
28
|
+
assert InstantType().process_result_value(None, None) is None
|
|
29
|
+
|
|
30
|
+
def test_bind(self):
|
|
31
|
+
instant = Instant.from_utc(2024, 6, 15, 12, 30, 0)
|
|
32
|
+
result = InstantType().process_bind_param(instant, None)
|
|
33
|
+
assert result == stdlib_datetime.datetime(
|
|
34
|
+
2024, 6, 15, 12, 30, tzinfo=_UTC
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
def test_result_aware(self):
|
|
38
|
+
dt = stdlib_datetime.datetime(2024, 6, 15, 12, 30, tzinfo=_UTC)
|
|
39
|
+
result = InstantType().process_result_value(dt, None)
|
|
40
|
+
assert result == Instant.from_utc(2024, 6, 15, 12, 30)
|
|
41
|
+
|
|
42
|
+
def test_result_naive_treated_as_utc(self):
|
|
43
|
+
# SQLite (and MySQL) drop tzinfo on retrieval; the stored value was
|
|
44
|
+
# UTC so we reattach UTC tzinfo.
|
|
45
|
+
dt = stdlib_datetime.datetime(2024, 6, 15, 12, 30)
|
|
46
|
+
result = InstantType().process_result_value(dt, None)
|
|
47
|
+
assert result == Instant.from_utc(2024, 6, 15, 12, 30)
|
|
48
|
+
|
|
49
|
+
def test_roundtrip(self):
|
|
50
|
+
instant = Instant.from_utc(2024, 6, 15, 12, 30, 45)
|
|
51
|
+
t = InstantType()
|
|
52
|
+
assert (
|
|
53
|
+
t.process_result_value(t.process_bind_param(instant, None), None)
|
|
54
|
+
== instant
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
def test_nanoseconds_truncated_to_microseconds(self):
|
|
58
|
+
# 500 ns < 1 µs, so it is truncated away
|
|
59
|
+
instant = Instant.from_utc(2024, 1, 1, nanosecond=1_500)
|
|
60
|
+
t = InstantType()
|
|
61
|
+
result = t.process_result_value(
|
|
62
|
+
t.process_bind_param(instant, None), None
|
|
63
|
+
)
|
|
64
|
+
assert result == Instant.from_utc(2024, 1, 1, nanosecond=1_000)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
class TestPlainDateTimeType:
|
|
68
|
+
def test_bind_none(self):
|
|
69
|
+
assert PlainDateTimeType().process_bind_param(None, None) is None
|
|
70
|
+
|
|
71
|
+
def test_result_none(self):
|
|
72
|
+
assert PlainDateTimeType().process_result_value(None, None) is None
|
|
73
|
+
|
|
74
|
+
def test_bind(self):
|
|
75
|
+
pdt = PlainDateTime(2024, 6, 15, 12, 30, 0)
|
|
76
|
+
result = PlainDateTimeType().process_bind_param(pdt, None)
|
|
77
|
+
assert result == stdlib_datetime.datetime(2024, 6, 15, 12, 30, 0)
|
|
78
|
+
assert result.tzinfo is None
|
|
79
|
+
|
|
80
|
+
def test_result(self):
|
|
81
|
+
dt = stdlib_datetime.datetime(2024, 6, 15, 12, 30, 0)
|
|
82
|
+
result = PlainDateTimeType().process_result_value(dt, None)
|
|
83
|
+
assert result == PlainDateTime(2024, 6, 15, 12, 30, 0)
|
|
84
|
+
|
|
85
|
+
def test_roundtrip(self):
|
|
86
|
+
pdt = PlainDateTime(2024, 6, 15, 12, 30, 45)
|
|
87
|
+
t = PlainDateTimeType()
|
|
88
|
+
assert (
|
|
89
|
+
t.process_result_value(t.process_bind_param(pdt, None), None)
|
|
90
|
+
== pdt
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
def test_nanoseconds_truncated_to_microseconds(self):
|
|
94
|
+
pdt = PlainDateTime(2024, 1, 1, nanosecond=1_500)
|
|
95
|
+
t = PlainDateTimeType()
|
|
96
|
+
result = t.process_result_value(t.process_bind_param(pdt, None), None)
|
|
97
|
+
assert result == PlainDateTime(2024, 1, 1, nanosecond=1_000)
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
class TestDateType:
|
|
101
|
+
def test_bind_none(self):
|
|
102
|
+
assert DateType().process_bind_param(None, None) is None
|
|
103
|
+
|
|
104
|
+
def test_result_none(self):
|
|
105
|
+
assert DateType().process_result_value(None, None) is None
|
|
106
|
+
|
|
107
|
+
def test_bind(self):
|
|
108
|
+
d = Date(2024, 6, 15)
|
|
109
|
+
result = DateType().process_bind_param(d, None)
|
|
110
|
+
assert result == stdlib_datetime.date(2024, 6, 15)
|
|
111
|
+
|
|
112
|
+
def test_result(self):
|
|
113
|
+
d = stdlib_datetime.date(2024, 6, 15)
|
|
114
|
+
result = DateType().process_result_value(d, None)
|
|
115
|
+
assert result == Date(2024, 6, 15)
|
|
116
|
+
|
|
117
|
+
def test_roundtrip(self):
|
|
118
|
+
d = Date(2024, 6, 15)
|
|
119
|
+
t = DateType()
|
|
120
|
+
assert t.process_result_value(t.process_bind_param(d, None), None) == d
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
class TestTimeType:
|
|
124
|
+
def test_bind_none(self):
|
|
125
|
+
assert TimeType().process_bind_param(None, None) is None
|
|
126
|
+
|
|
127
|
+
def test_result_none(self):
|
|
128
|
+
assert TimeType().process_result_value(None, None) is None
|
|
129
|
+
|
|
130
|
+
def test_bind(self):
|
|
131
|
+
t = Time(12, 30, 45)
|
|
132
|
+
result = TimeType().process_bind_param(t, None)
|
|
133
|
+
assert result == stdlib_datetime.time(12, 30, 45)
|
|
134
|
+
|
|
135
|
+
def test_result(self):
|
|
136
|
+
t = stdlib_datetime.time(12, 30, 45)
|
|
137
|
+
result = TimeType().process_result_value(t, None)
|
|
138
|
+
assert result == Time(12, 30, 45)
|
|
139
|
+
|
|
140
|
+
def test_roundtrip(self):
|
|
141
|
+
t = Time(12, 30, 45)
|
|
142
|
+
typ = TimeType()
|
|
143
|
+
assert (
|
|
144
|
+
typ.process_result_value(typ.process_bind_param(t, None), None)
|
|
145
|
+
== t
|
|
146
|
+
)
|
|
147
|
+
|
|
148
|
+
def test_nanoseconds_truncated_to_microseconds(self):
|
|
149
|
+
t = Time(12, 30, 45, nanosecond=1_500)
|
|
150
|
+
typ = TimeType()
|
|
151
|
+
result = typ.process_result_value(
|
|
152
|
+
typ.process_bind_param(t, None), None
|
|
153
|
+
)
|
|
154
|
+
assert result == Time(12, 30, 45, nanosecond=1_000)
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
# ── Integration tests (SQLite in-memory, full ORM roundtrip) ─────────────────
|
|
158
|
+
#
|
|
159
|
+
# Two model definitions cover the two supported SQLAlchemy usage styles:
|
|
160
|
+
# - SA1.4 / SA2 Core style: explicit Column() declarations
|
|
161
|
+
# - SA2 Mapped[] style: type-annotated attributes with type_annotation_map
|
|
162
|
+
|
|
163
|
+
# ── SA1.4-compatible Core style ──────────────────────────────────────────────
|
|
164
|
+
|
|
165
|
+
_CoreBase = declarative_base()
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
class _CoreModel(_CoreBase): # type: ignore[valid-type, misc]
|
|
169
|
+
__tablename__ = "core_model"
|
|
170
|
+
id = Column(Integer, primary_key=True, autoincrement=True)
|
|
171
|
+
instant = Column(InstantType, nullable=True)
|
|
172
|
+
plain_dt = Column(PlainDateTimeType, nullable=True)
|
|
173
|
+
date = Column(DateType, nullable=True)
|
|
174
|
+
time = Column(TimeType, nullable=True)
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
class _ServerDefaultModel(_CoreBase): # type: ignore[valid-type, misc]
|
|
178
|
+
__tablename__ = "server_default_model"
|
|
179
|
+
id = Column(Integer, primary_key=True, autoincrement=True)
|
|
180
|
+
# server_default bypasses process_bind_param;
|
|
181
|
+
# process_result_value still runs
|
|
182
|
+
instant = Column(InstantType, server_default=func.current_timestamp())
|
|
183
|
+
plain_dt = Column(
|
|
184
|
+
PlainDateTimeType, server_default=func.current_timestamp()
|
|
185
|
+
)
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
@pytest.fixture
|
|
189
|
+
def core_session():
|
|
190
|
+
engine = create_engine("sqlite:///:memory:")
|
|
191
|
+
_CoreBase.metadata.create_all(engine)
|
|
192
|
+
with Session(engine) as session:
|
|
193
|
+
yield session
|
|
194
|
+
_CoreBase.metadata.drop_all(engine)
|
|
195
|
+
engine.dispose()
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
class TestSQLiteRoundtrip:
|
|
199
|
+
def test_instant(self, core_session):
|
|
200
|
+
value = Instant.from_utc(2024, 6, 15, 12, 30, 45)
|
|
201
|
+
core_session.add(_CoreModel(instant=value))
|
|
202
|
+
core_session.commit()
|
|
203
|
+
core_session.expire_all()
|
|
204
|
+
result = core_session.query(_CoreModel).one()
|
|
205
|
+
assert result.instant == value
|
|
206
|
+
|
|
207
|
+
def test_plain_datetime(self, core_session):
|
|
208
|
+
value = PlainDateTime(2024, 6, 15, 12, 30, 45)
|
|
209
|
+
core_session.add(_CoreModel(plain_dt=value))
|
|
210
|
+
core_session.commit()
|
|
211
|
+
core_session.expire_all()
|
|
212
|
+
result = core_session.query(_CoreModel).one()
|
|
213
|
+
assert result.plain_dt == value
|
|
214
|
+
|
|
215
|
+
def test_date(self, core_session):
|
|
216
|
+
value = Date(2024, 6, 15)
|
|
217
|
+
core_session.add(_CoreModel(date=value))
|
|
218
|
+
core_session.commit()
|
|
219
|
+
core_session.expire_all()
|
|
220
|
+
result = core_session.query(_CoreModel).one()
|
|
221
|
+
assert result.date == value
|
|
222
|
+
|
|
223
|
+
def test_time(self, core_session):
|
|
224
|
+
value = Time(12, 30, 45)
|
|
225
|
+
core_session.add(_CoreModel(time=value))
|
|
226
|
+
core_session.commit()
|
|
227
|
+
core_session.expire_all()
|
|
228
|
+
result = core_session.query(_CoreModel).one()
|
|
229
|
+
assert result.time == value
|
|
230
|
+
|
|
231
|
+
def test_null_values(self, core_session):
|
|
232
|
+
core_session.add(_CoreModel())
|
|
233
|
+
core_session.commit()
|
|
234
|
+
core_session.expire_all()
|
|
235
|
+
result = core_session.query(_CoreModel).one()
|
|
236
|
+
assert result.instant is None
|
|
237
|
+
assert result.plain_dt is None
|
|
238
|
+
assert result.date is None
|
|
239
|
+
assert result.time is None
|
|
240
|
+
|
|
241
|
+
def test_all_types_together(self, core_session):
|
|
242
|
+
instant = Instant.from_utc(2024, 6, 15, 12, 30, 45)
|
|
243
|
+
plain_dt = PlainDateTime(2023, 12, 31, 23, 59, 59)
|
|
244
|
+
date = Date(2024, 1, 1)
|
|
245
|
+
time = Time(8, 0, 0)
|
|
246
|
+
core_session.add(
|
|
247
|
+
_CoreModel(
|
|
248
|
+
instant=instant, plain_dt=plain_dt, date=date, time=time
|
|
249
|
+
)
|
|
250
|
+
)
|
|
251
|
+
core_session.commit()
|
|
252
|
+
core_session.expire_all()
|
|
253
|
+
result = core_session.query(_CoreModel).one()
|
|
254
|
+
assert result.instant == instant
|
|
255
|
+
assert result.plain_dt == plain_dt
|
|
256
|
+
assert result.date == date
|
|
257
|
+
assert result.time == time
|
|
258
|
+
|
|
259
|
+
def test_server_default_instant(self, core_session):
|
|
260
|
+
# The DB sets the value; process_result_value must convert it back.
|
|
261
|
+
# SQLite's current_timestamp() returns a naive string; InstantType
|
|
262
|
+
# reattaches UTC, so the result must be a valid Instant.
|
|
263
|
+
core_session.add(_ServerDefaultModel())
|
|
264
|
+
core_session.commit()
|
|
265
|
+
core_session.expire_all()
|
|
266
|
+
result = core_session.query(_ServerDefaultModel).one()
|
|
267
|
+
assert isinstance(result.instant, Instant)
|
|
268
|
+
|
|
269
|
+
def test_server_default_plain_datetime(self, core_session):
|
|
270
|
+
core_session.add(_ServerDefaultModel())
|
|
271
|
+
core_session.commit()
|
|
272
|
+
core_session.expire_all()
|
|
273
|
+
result = core_session.query(_ServerDefaultModel).one()
|
|
274
|
+
assert isinstance(result.plain_dt, PlainDateTime)
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
# ── SA2 Mapped[] style ───────────────────────────────────────────────────────
|
|
278
|
+
|
|
279
|
+
try:
|
|
280
|
+
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
|
|
281
|
+
|
|
282
|
+
class _MappedBase(DeclarativeBase):
|
|
283
|
+
type_annotation_map = whenever_type_annotation_map
|
|
284
|
+
|
|
285
|
+
class _MappedModel(_MappedBase):
|
|
286
|
+
__tablename__ = "mapped_model"
|
|
287
|
+
id: Mapped[int] = mapped_column(
|
|
288
|
+
Integer, primary_key=True, autoincrement=True
|
|
289
|
+
)
|
|
290
|
+
instant: Mapped[Optional[Instant]]
|
|
291
|
+
plain_dt: Mapped[Optional[PlainDateTime]]
|
|
292
|
+
date: Mapped[Optional[Date]]
|
|
293
|
+
time: Mapped[Optional[Time]]
|
|
294
|
+
|
|
295
|
+
_SA2_AVAILABLE = True
|
|
296
|
+
except ImportError:
|
|
297
|
+
_SA2_AVAILABLE = False
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
@pytest.fixture
|
|
301
|
+
def mapped_session():
|
|
302
|
+
if not _SA2_AVAILABLE:
|
|
303
|
+
pytest.skip("SQLAlchemy 2.x required for Mapped[] style")
|
|
304
|
+
engine = create_engine("sqlite:///:memory:")
|
|
305
|
+
_MappedBase.metadata.create_all(engine)
|
|
306
|
+
with Session(engine) as session:
|
|
307
|
+
yield session
|
|
308
|
+
_MappedBase.metadata.drop_all(engine)
|
|
309
|
+
engine.dispose()
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
@pytest.mark.skipif(not _SA2_AVAILABLE, reason="SQLAlchemy 2.x required")
|
|
313
|
+
class TestMappedAnnotations:
|
|
314
|
+
"""Verify Mapped[WheneverType] works identically to Mapped[datetime]."""
|
|
315
|
+
|
|
316
|
+
def test_instant(self, mapped_session):
|
|
317
|
+
value = Instant.from_utc(2024, 6, 15, 12, 30, 45)
|
|
318
|
+
mapped_session.add(_MappedModel(instant=value))
|
|
319
|
+
mapped_session.commit()
|
|
320
|
+
mapped_session.expire_all()
|
|
321
|
+
result = mapped_session.query(_MappedModel).one()
|
|
322
|
+
assert result.instant == value
|
|
323
|
+
|
|
324
|
+
def test_plain_datetime(self, mapped_session):
|
|
325
|
+
value = PlainDateTime(2024, 6, 15, 12, 30, 45)
|
|
326
|
+
mapped_session.add(_MappedModel(plain_dt=value))
|
|
327
|
+
mapped_session.commit()
|
|
328
|
+
mapped_session.expire_all()
|
|
329
|
+
result = mapped_session.query(_MappedModel).one()
|
|
330
|
+
assert result.plain_dt == value
|
|
331
|
+
|
|
332
|
+
def test_date(self, mapped_session):
|
|
333
|
+
value = Date(2024, 6, 15)
|
|
334
|
+
mapped_session.add(_MappedModel(date=value))
|
|
335
|
+
mapped_session.commit()
|
|
336
|
+
mapped_session.expire_all()
|
|
337
|
+
result = mapped_session.query(_MappedModel).one()
|
|
338
|
+
assert result.date == value
|
|
339
|
+
|
|
340
|
+
def test_time(self, mapped_session):
|
|
341
|
+
value = Time(12, 30, 45)
|
|
342
|
+
mapped_session.add(_MappedModel(time=value))
|
|
343
|
+
mapped_session.commit()
|
|
344
|
+
mapped_session.expire_all()
|
|
345
|
+
result = mapped_session.query(_MappedModel).one()
|
|
346
|
+
assert result.time == value
|
|
347
|
+
|
|
348
|
+
def test_nullable(self, mapped_session):
|
|
349
|
+
mapped_session.add(_MappedModel())
|
|
350
|
+
mapped_session.commit()
|
|
351
|
+
mapped_session.expire_all()
|
|
352
|
+
result = mapped_session.query(_MappedModel).one()
|
|
353
|
+
assert result.instant is None
|
|
354
|
+
assert result.plain_dt is None
|
|
355
|
+
assert result.date is None
|
|
356
|
+
assert result.time is None
|