Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
3 changes: 1 addition & 2 deletions .config/pre-commit-config.yaml → .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ repos:
hooks:
# Run the linter.
- id: ruff
args: [--config, .config/ruff.toml, --fix]
args: [ --fix ]
# Run the formatter.
- id: ruff-format
args: [--config, .config/ruff.toml]
- repo: https://github.com/astral-sh/uv-pre-commit
rev: 0.6.5
hooks:
Expand Down
4 changes: 3 additions & 1 deletion documentation/mkdocs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ plugins:
nav:
- Home: index.md
- API Documentation:
- Database: $src/hdx.database.database.*
- Database:
- $src/hdx.database.Database
- $src/hdx.database.DatabaseError
- PostgreSQL specific: $src/hdx.database.postgresql.*
34 changes: 34 additions & 0 deletions hatch.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Build

[build.targets.wheel]
packages = ["src/hdx"]

[build.hooks.vcs]
version-file = "src/hdx/database/_version.py"

# Versioning

[version]
source = "vcs"

[version.raw-options]
local_scheme = "no-local-version"
version_scheme = "python-simplified-semver"

# Tests

[envs.hatch-test]
features = ["test"]

[[envs.hatch-test.matrix]]
python = ["3.12"]

[envs.hatch-test.scripts]
run = """
pytest --rootdir=. --junitxml=test-results.xml --cov --no-cov-on-fail \
--cov-report=lcov --cov-report=term-missing
"""

[envs.hatch-static-analysis]
config-path = "none"
dependencies = ["ruff==0.9.10"]
48 changes: 1 addition & 47 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,52 +48,6 @@ Homepage = "https://github.com/OCHA-DAP/hdx-python-database"
[project.optional-dependencies]
sshtunnel = ["sshtunnel"]
postgresql = ["psycopg[binary]"]
test = ["pytest", "pytest-cov"]
test = ["sshtunnel", "psycopg[binary]", "pytest", "pytest-cov"]
dev = ["pre-commit"]
docs = ["mkapi"]


#########
# Hatch #
#########

# Build

[tool.hatch.build.targets.wheel]
packages = ["src/hdx"]

[tool.hatch.build.hooks.vcs]
version-file = "src/hdx/database/_version.py"

# Versioning

[tool.hatch.version]
source = "vcs"

[tool.hatch.version.raw-options]
local_scheme = "no-local-version"
version_scheme = "python-simplified-semver"

# Tests

[tool.hatch.envs.hatch-test]
features = ["sshtunnel", "postgresql", "test"]

[[tool.hatch.envs.hatch-test.matrix]]
python = ["3.12"]

[tool.hatch.envs.hatch-test.scripts]
run = """
pytest -c .config/pytest.ini --rootdir=. --junitxml=test-results.xml \
--cov --cov-config=.config/coveragerc --no-cov-on-fail \
--cov-report=lcov --cov-report=term-missing
"""

[tool.hatch.envs.hatch-static-analysis]
dependencies = ["ruff==0.9.10"]

[tool.hatch.envs.hatch-static-analysis.scripts]
format-check = ["ruff format --config .config/ruff.toml --check --diff {args:.}",]
format-fix = ["ruff format --config .config/ruff.toml {args:.}",]
lint-check = ["ruff check --config .config/ruff.toml {args:.}",]
lint-fix = ["ruff check --config .config/ruff.toml --fix {args:.}",]
2 changes: 1 addition & 1 deletion .config/pytest.ini → pytest.ini
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[pytest]
pythonpath = ../src
pythonpath = src
addopts = "--color=yes"
log_cli = 1
55 changes: 23 additions & 32 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,83 +1,78 @@
# This file was autogenerated by uv via the following command:
# uv pip compile pyproject.toml --resolver=backtracking --all-extras -o requirements.txt
astdoc==1.1.5
astdoc==1.2.1
# via mkapi
babel==2.17.0
# via mkdocs-material
backrefs==5.8
# via mkdocs-material
bcrypt==4.3.0
# via paramiko
certifi==2025.1.31
certifi==2025.4.26
# via requests
cffi==1.17.1
# via
# cryptography
# pynacl
cfgv==3.4.0
# via pre-commit
charset-normalizer==3.4.1
charset-normalizer==3.4.2
# via requests
click==8.1.8
# via mkdocs
colorama==0.4.6
# via mkdocs-material
coverage==7.7.0
coverage==7.8.0
# via pytest-cov
cryptography==44.0.2
cryptography==44.0.3
# via paramiko
distlib==0.3.9
# via virtualenv
filelock==3.18.0
# via virtualenv
ghp-import==2.1.0
# via mkdocs
greenlet==3.1.1
greenlet==3.2.1
# via sqlalchemy
identify==2.6.9
identify==2.6.10
# via pre-commit
idna==3.10
# via requests
iniconfig==2.0.0
iniconfig==2.1.0
# via pytest
jinja2==3.1.6
# via
# mkapi
# mkdocs
# mkdocs-material
markdown==3.7
markdown==3.8
# via
# astdoc
# mkdocs
# mkdocs-material
# pymdown-extensions
markdown-it-py==3.0.0
# via rich
markupsafe==3.0.2
# via
# jinja2
# mkdocs
mdurl==0.1.2
# via markdown-it-py
mergedeep==1.3.4
# via
# mkdocs
# mkdocs-get-deps
mkapi==4.1.3
mkapi==4.3.2
# via hdx-python-database (pyproject.toml)
mkdocs==1.6.1
# via
# mkapi
# mkdocs-material
mkdocs-get-deps==0.2.0
# via mkdocs
mkdocs-material==9.6.8
mkdocs-material==9.6.12
# via mkapi
mkdocs-material-extensions==1.3.1
# via mkdocs-material
nodeenv==1.9.1
# via pre-commit
packaging==24.2
packaging==25.0
# via
# mkdocs
# pytest
Expand All @@ -87,33 +82,31 @@ paramiko==3.5.1
# via sshtunnel
pathspec==0.12.1
# via mkdocs
platformdirs==4.3.6
platformdirs==4.3.7
# via
# mkdocs-get-deps
# virtualenv
pluggy==1.5.0
# via pytest
pre-commit==4.1.0
pre-commit==4.2.0
# via hdx-python-database (pyproject.toml)
psycopg==3.2.6
psycopg==3.2.7
# via hdx-python-database (pyproject.toml)
psycopg-binary==3.2.6
psycopg-binary==3.2.7
# via psycopg
pycparser==2.22
# via cffi
pygments==2.19.1
# via
# mkdocs-material
# rich
pymdown-extensions==10.14.3
# via mkdocs-material
pymdown-extensions==10.15
# via mkdocs-material
pynacl==1.5.0
# via paramiko
pytest==8.3.5
# via
# hdx-python-database (pyproject.toml)
# pytest-cov
pytest-cov==6.0.0
pytest-cov==6.1.1
# via hdx-python-database (pyproject.toml)
python-dateutil==2.9.0.post0
# via ghp-import
Expand All @@ -128,21 +121,19 @@ pyyaml-env-tag==0.1
# via mkdocs
requests==2.32.3
# via mkdocs-material
rich==13.9.4
# via mkapi
six==1.17.0
# via python-dateutil
sqlalchemy==2.0.39
sqlalchemy==2.0.40
# via hdx-python-database (pyproject.toml)
sshtunnel==0.4.0
# via hdx-python-database (pyproject.toml)
typing-extensions==4.12.2
typing-extensions==4.13.2
# via
# psycopg
# sqlalchemy
urllib3==2.3.0
urllib3==2.4.0
# via requests
virtualenv==20.29.3
virtualenv==20.31.1
# via pre-commit
watchdog==6.0.0
# via mkdocs
1 change: 0 additions & 1 deletion .config/ruff.toml → ruff.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
line-length = 79
exclude = ["_version.py"]

[lint]
Expand Down
8 changes: 2 additions & 6 deletions src/hdx/database/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,7 @@ def create_session(
engine = create_engine(db_uri, poolclass=NullPool, echo=False)
if reflect:
Base = automap_base(declarative_base=table_base)
Base.prepare(
autoload_with=engine, reflection_options={"views": True}
)
Base.prepare(autoload_with=engine, reflection_options={"views": True})
table_base = Base
else:
table_base.metadata.create_all(engine)
Expand All @@ -307,9 +305,7 @@ def recreate_schema(engine: Engine, schema_name: str = "public") -> bool:
DropSchema(schema_name, cascade=True, if_exists=True)
)
connection.commit()
connection.execute(
CreateSchema(schema_name, if_not_exists=True)
)
connection.execute(CreateSchema(schema_name, if_not_exists=True))
connection.commit()
return True
except SQLAlchemyError:
Expand Down
4 changes: 1 addition & 3 deletions src/hdx/database/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,5 @@ def camel_to_snake_case(string: str) -> str:
Returns:
str: String in snake case
"""
string = re.sub(
r"((?<=[a-z0-9])[A-Z]|(?!^)[A-Z](?=[a-z]))", r"_\1", string
)
string = re.sub(r"((?<=[a-z0-9])[A-Z]|(?!^)[A-Z](?=[a-z]))", r"_\1", string)
return string.lower().lstrip("_")
4 changes: 1 addition & 3 deletions tests/hdx/database/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ def prepare_fn():
now = datetime(2023, 10, 20, 22, 35, 55, tzinfo=timezone.utc)
rows = [{"test_date": now}]
dbdatabase.batch_populate(rows, DBTestDate)
dbtestdate = (
dbsession.execute(select(DBTestDate)).all()[1][0].test_date
)
dbtestdate = dbsession.execute(select(DBTestDate)).all()[1][0].test_date
assert dbtestdate == now

remove(dbpath)
Expand Down
16 changes: 4 additions & 12 deletions tests/hdx/database/test_dburi.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ class TestDBURI:
"dialect": "postgresql",
"driver": "psycopg",
}
connection_uri_pg = (
"postgresql+psycopg://myuser:mypass@myserver:1234/mydatabase"
)
connection_uri_pg = "postgresql+psycopg://myuser:mypass@myserver:1234/mydatabase"
params_pg_no_driver = {
"database": "mydatabase",
"host": "myserver",
Expand All @@ -37,9 +35,7 @@ class TestDBURI:
"dialect": "postgresql",
"driver": None,
}
connection_uri_pg_no_driver = (
"postgresql://myuser:mypass@myserver:1234/mydatabase"
)
connection_uri_pg_no_driver = "postgresql://myuser:mypass@myserver:1234/mydatabase"
params_sq_no_driver = {
"database": "mydatabase",
"host": "myserver",
Expand Down Expand Up @@ -67,19 +63,15 @@ def test_get_params_from_connection_uri(self):
include_driver=False,
)
assert result == TestDBURI.params_pg_no_driver
result = get_params_from_connection_uri(
TestDBURI.connection_uri_pg_no_driver
)
result = get_params_from_connection_uri(TestDBURI.connection_uri_pg_no_driver)
assert result == TestDBURI.params_pg_driver_none
result = get_params_from_connection_uri(TestDBURI.connection_uri_sq)
assert result == TestDBURI.params_sq_driver_none

def test_get_connection_uri(self):
result = get_connection_uri(**TestDBURI.params_pg)
assert result == TestDBURI.connection_uri_pg
result = get_connection_uri(
**TestDBURI.params_pg, include_driver=False
)
result = get_connection_uri(**TestDBURI.params_pg, include_driver=False)
assert result == TestDBURI.connection_uri_pg_no_driver
result = get_connection_uri(**TestDBURI.params_pg_no_driver)
assert result == TestDBURI.connection_uri_pg
Expand Down
4 changes: 1 addition & 3 deletions tests/hdx/database/test_ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ def test_get_session_ssh(
)
assert dbdatabase._base == NoTZBase

with Database(
ssh_host="mysshhost", ssh_port=25, **params
) as dbdatabase:
with Database(ssh_host="mysshhost", ssh_port=25, **params) as dbdatabase:
dbsession = dbdatabase.get_session()
assert (
str(dbsession.bind.engine.url)
Expand Down