Skip to content

Commit

Permalink
WIP: Conversion to SQLAlchemy v2
Browse files Browse the repository at this point in the history
  • Loading branch information
tony committed May 29, 2023
1 parent cd83087 commit f096ea0
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 105 deletions.
2 changes: 2 additions & 0 deletions src/cihai/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import typing as t
from cihai._internal.config_reader import ConfigReader
from cihai.data.unihan.dataset import Unihan

from unihan_etl.util import merge_dict

from . import exc, extend
Expand Down Expand Up @@ -71,6 +72,7 @@ class Cihai:
default_config: "UntypedDict" = DEFAULT_CONFIG
config: "ConfigDict"
unihan: Unihan
sql: Database

def __init__(
self,
Expand Down
10 changes: 7 additions & 3 deletions src/cihai/data/unihan/bootstrap.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import typing as t

import sqlalchemy.sql.schema
import sqlalchemy
from sqlalchemy import Column, String, Table

from unihan_etl import process as unihan
Expand All @@ -11,6 +12,7 @@


def bootstrap_unihan(
engine: sqlalchemy.Engine,
metadata: sqlalchemy.sql.schema.MetaData,
options: t.Optional[t.Dict[str, object]] = None,
) -> None:
Expand All @@ -24,9 +26,11 @@ def bootstrap_unihan(
p.download()
data = p.export()
table = create_unihan_table(UNIHAN_FIELDS, metadata)
metadata.create_all()
assert metadata.bind is not None
metadata.bind.execute(table.insert(), data)

metadata.create_all(engine)
with engine.connect() as conn:
conn.execute(table.insert().values(data))
conn.commit()


TABLE_NAME = "Unihan"
Expand Down
4 changes: 3 additions & 1 deletion src/cihai/data/unihan/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ def bootstrap(self, options: Optional[Dict[str, object]] = None) -> None:
if options is None:
options = {}

bootstrap.bootstrap_unihan(self.sql.metadata, options=options)
bootstrap.bootstrap_unihan(
engine=self.sql.engine, metadata=self.sql.metadata, options=options
)
self.sql.reflect_db() # automap new table created during bootstrap

def lookup_char(self, char: str) -> "Query[Unihan]":
Expand Down
3 changes: 1 addition & 2 deletions src/cihai/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ def __init__(self, config: ConfigDict) -> None:
self.engine = create_engine(config["database"]["url"])

self.metadata = MetaData()
self.metadata.bind = self.engine
self.reflect_db()

self.session = Session(self.engine)
Expand All @@ -42,6 +41,6 @@ def reflect_db(self) -> None:
This is available as a method so the database can be reflected
outside initialization (such bootstrapping unihan during CLI usage).
"""
self.metadata.reflect(views=True, extend_existing=True)
self.metadata.reflect(bind=self.engine, views=True, extend_existing=True)
self.base = automap_base(metadata=self.metadata)
self.base.prepare()
12 changes: 12 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import pytest

import sqlalchemy

from cihai.data.unihan.constants import UNIHAN_FILES


Expand Down Expand Up @@ -52,3 +54,13 @@ def unihan_options(
@pytest.fixture(scope="function")
def tmpdb_file(tmpdir: str) -> str:
return tmpdir.join("test.db")


@pytest.fixture(scope="session")
def engine():
return sqlalchemy.create_engine("sqlite:///")


@pytest.fixture(scope="session")
def metadata() -> sqlalchemy.MetaData:
return sqlalchemy.MetaData()
2 changes: 1 addition & 1 deletion tests/data/unihan/test_bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def test_reflect_db(
) -> None:
c = Cihai(config={"database": {"url": f"sqlite:///{tmpdb_file}"}})
assert not c.unihan.is_bootstrapped
bootstrap.bootstrap_unihan(c.sql.metadata, unihan_options)
bootstrap.bootstrap_unihan(c.sql.engine, c.sql.metadata, unihan_options)
assert not hasattr(c.sql.base.classes, "Unihan")
c.unihan.sql.reflect_db()
assert hasattr(c.sql.base.classes, "Unihan")
9 changes: 7 additions & 2 deletions tests/test_cihai.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""
import typing as t
import sqlalchemy

import cihai
from cihai.constants import UNIHAN_CONFIG
Expand Down Expand Up @@ -43,10 +44,14 @@ def test_yaml_config_and_override(test_config_file: str) -> None:


def test_unihan_options(
unihan_options: t.Dict[str, object], test_config_file: str
unihan_options: t.Dict[str, object],
engine: sqlalchemy.Engine,
test_config_file: str,
) -> None:
app = Cihai.from_file(test_config_file)
bootstrap.bootstrap_unihan(app.sql.metadata, unihan_options)
bootstrap.bootstrap_unihan(
engine=engine, metadata=app.sql.metadata, options=unihan_options
)
assert "Unihan" in app.sql.metadata.tables
assert app.sql.metadata.tables["Unihan"].columns
assert set(app.sql.metadata.tables["Unihan"].columns.keys()) == set(
Expand Down

0 comments on commit f096ea0

Please sign in to comment.