Path: blob/main/py-polars/tests/unit/io/database/conftest.py
6939 views
from __future__ import annotations12import sqlite33from datetime import date4from typing import TYPE_CHECKING56import pytest78if TYPE_CHECKING:9from pathlib import Path101112@pytest.fixture13def tmp_sqlite_db(tmp_path: Path) -> Path:14test_db = tmp_path / "test.db"15test_db.unlink(missing_ok=True)1617def convert_date(val: bytes) -> date:18"""Convert ISO 8601 date to datetime.date object."""19return date.fromisoformat(val.decode())2021# NOTE: at the time of writing adbc/connectorx have weak SQLite support (poor or22# no bool/date/datetime dtypes, for example) and there is a bug in connectorx that23# causes float rounding < py 3.11, hence we are only testing/storing simple values24# in this test db for now. as support improves, we can add/test additional dtypes).25sqlite3.register_converter("date", convert_date)26conn = sqlite3.connect(test_db)2728# ┌─────┬───────┬───────┬────────────┐29# │ id ┆ name ┆ value ┆ date │30# │ --- ┆ --- ┆ --- ┆ --- │31# │ i64 ┆ str ┆ f64 ┆ date │32# ╞═════╪═══════╪═══════╪════════════╡33# │ 1 ┆ misc ┆ 100.0 ┆ 2020-01-01 │34# │ 2 ┆ other ┆ -99.0 ┆ 2021-12-31 │35# └─────┴───────┴───────┴────────────┘36conn.executescript(37"""38CREATE TABLE IF NOT EXISTS test_data (39id INTEGER PRIMARY KEY,40name TEXT NOT NULL,41value FLOAT,42date DATE43);44REPLACE INTO test_data(name,value,date)45VALUES ('misc',100.0,'2020-01-01'),46('other',-99.5,'2021-12-31');47"""48)49conn.close()50return test_db515253@pytest.fixture54def tmp_sqlite_inference_db(tmp_path: Path) -> Path:55test_db = tmp_path / "test_inference.db"56test_db.unlink(missing_ok=True)57conn = sqlite3.connect(test_db)58conn.executescript(59"""60CREATE TABLE IF NOT EXISTS test_data (name TEXT, value FLOAT);61REPLACE INTO test_data(name,value) VALUES (NULL,NULL), ('foo',0);62"""63)64conn.close()65return test_db666768