Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/py-polars/tests/unit/io/database/conftest.py
6939 views
1
from __future__ import annotations
2
3
import sqlite3
4
from datetime import date
5
from typing import TYPE_CHECKING
6
7
import pytest
8
9
if TYPE_CHECKING:
10
from pathlib import Path
11
12
13
@pytest.fixture
14
def tmp_sqlite_db(tmp_path: Path) -> Path:
15
test_db = tmp_path / "test.db"
16
test_db.unlink(missing_ok=True)
17
18
def convert_date(val: bytes) -> date:
19
"""Convert ISO 8601 date to datetime.date object."""
20
return date.fromisoformat(val.decode())
21
22
# NOTE: at the time of writing adbc/connectorx have weak SQLite support (poor or
23
# no bool/date/datetime dtypes, for example) and there is a bug in connectorx that
24
# causes float rounding < py 3.11, hence we are only testing/storing simple values
25
# in this test db for now. as support improves, we can add/test additional dtypes).
26
sqlite3.register_converter("date", convert_date)
27
conn = sqlite3.connect(test_db)
28
29
# ┌─────┬───────┬───────┬────────────┐
30
# │ id ┆ name ┆ value ┆ date │
31
# │ --- ┆ --- ┆ --- ┆ --- │
32
# │ i64 ┆ str ┆ f64 ┆ date │
33
# ╞═════╪═══════╪═══════╪════════════╡
34
# │ 1 ┆ misc ┆ 100.0 ┆ 2020-01-01 │
35
# │ 2 ┆ other ┆ -99.0 ┆ 2021-12-31 │
36
# └─────┴───────┴───────┴────────────┘
37
conn.executescript(
38
"""
39
CREATE TABLE IF NOT EXISTS test_data (
40
id INTEGER PRIMARY KEY,
41
name TEXT NOT NULL,
42
value FLOAT,
43
date DATE
44
);
45
REPLACE INTO test_data(name,value,date)
46
VALUES ('misc',100.0,'2020-01-01'),
47
('other',-99.5,'2021-12-31');
48
"""
49
)
50
conn.close()
51
return test_db
52
53
54
@pytest.fixture
55
def tmp_sqlite_inference_db(tmp_path: Path) -> Path:
56
test_db = tmp_path / "test_inference.db"
57
test_db.unlink(missing_ok=True)
58
conn = sqlite3.connect(test_db)
59
conn.executescript(
60
"""
61
CREATE TABLE IF NOT EXISTS test_data (name TEXT, value FLOAT);
62
REPLACE INTO test_data(name,value) VALUES (NULL,NULL), ('foo',0);
63
"""
64
)
65
conn.close()
66
return test_db
67
68