Skip to content

Commit

Permalink
Merge pull request #59 from TheJacksonLaboratory/G3-75-all-geneweaver…
Browse files Browse the repository at this point in the history
…-packages-need-to-upgrade-pydantic-2-0

G3-75: Updating dependencies
  • Loading branch information
bergsalex authored Jul 12, 2024
2 parents 65626b5 + 9d14267 commit 604a5fb
Show file tree
Hide file tree
Showing 11 changed files with 356 additions and 224 deletions.
471 changes: 286 additions & 185 deletions poetry.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "geneweaver-db"
version = "0.5.0a11"
version = "0.5.0a12"
description = "Database Interaction Services for GeneWeaver"
authors = ["Jax Computational Sciences <[email protected]>"]
readme = "README.md"
Expand All @@ -13,7 +13,7 @@ packages = [

[tool.poetry.dependencies]
python = "^3.9"
geneweaver-core = ">=0.10.0a0,<1.0.0"
geneweaver-core = ">=0.10.0a3,<1.0.0"
psycopg = {version = "3.1.18", extras = ["binary"]}

[tool.poetry.group.dev.dependencies]
Expand Down
2 changes: 1 addition & 1 deletion src/geneweaver/db/aio/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ async def add(
:return: The ID of the added project
"""
await cursor.execute(
*project_query.add(user_id=user_id, starred=starred, **project.dict())
*project_query.add(user_id=user_id, starred=starred, **project.model_dump())
)

return await cursor.fetchone()
Expand Down
2 changes: 1 addition & 1 deletion src/geneweaver/db/aio/publication.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ async def add(cursor: AsyncCursor, publication: PublicationInfo) -> Optional[row
:return: optional row using `.fetchone()`
"""
await cursor.execute(*publication_query.add(**publication.dict()))
await cursor.execute(*publication_query.add(**publication.model_dump()))
return await cursor.fetchone()


Expand Down
54 changes: 28 additions & 26 deletions src/geneweaver/db/core/settings_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@
"""

# ruff: noqa: N805, ANN101, ANN401
from typing import Any, Dict, Optional
from typing import Optional

from pydantic import BaseSettings, PostgresDsn, validator
from pydantic import PostgresDsn, field_validator, model_validator
from pydantic_settings import BaseSettings, SettingsConfigDict
from typing_extensions import Self, Type


class Settings(BaseSettings):
"""Settings class for the GeneWeaver Database module."""

DEBUG_MODE = False
DEBUG_MODE: bool = False

CONNECTION_SCHEME: str = "postgresql"

Expand All @@ -29,32 +31,32 @@ class Settings(BaseSettings):
PORT: int = 5432
URI: Optional[str] = None

@validator("SERVER", pre=True)
def replace_localhost(cls, v: str) -> str:
"""Replace localhost with 127.0.0.1."""
@field_validator("SERVER", mode="after")
@classmethod
def name_must_contain_space(cls: Type["Settings"], v: str) -> str:
"""Ensure that the server name is not 'localhost'."""
if v == "localhost":
return "127.0.0.1"
return v

@validator("URI", pre=True)
def assemble_db_connection(cls, v: Optional[str], values: Dict[str, Any]) -> str:
"""Build the database connection string, unless one is provided."""
if isinstance(v, str):
return v
return str(
PostgresDsn.build(
scheme=values.get("CONNECTION_SCHEME"),
user=values.get("USERNAME"),
password=values.get("PASSWORD"),
host=values.get("SERVER"),
port=str(values.get("PORT")),
path=f"/{values.get('NAME') or ''}",
@model_validator(mode="after")
def assemble_db_connection(self) -> Self:
"""Build the database connection string."""
if isinstance(self.URI, str):
self.URI = str(PostgresDsn(self.URI))
else:
self.URI = str(
PostgresDsn.build(
scheme=self.CONNECTION_SCHEME,
username=self.USERNAME,
password=self.PASSWORD,
host=self.SERVER,
port=self.PORT,
path=f"{self.NAME or ''}",
)
)
)
return self

class Config:
"""Configuration for the BaseSettings class."""

env_prefix = "GWDB_"
env_file = ".env"
case_sensitive = True
model_config = SettingsConfigDict(
env_prefix="GWDB_", env_file=".env", case_sensitive=True
)
2 changes: 1 addition & 1 deletion src/geneweaver/db/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def add(
"""
cursor.execute(
*project_query.add(user_id=user_id, starred=starred, **project.dict())
*project_query.add(user_id=user_id, starred=starred, **project.model_dump())
)

return cursor.fetchone()
Expand Down
2 changes: 1 addition & 1 deletion src/geneweaver/db/publication.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def add(cursor: Cursor, publication: PublicationInfo) -> Optional[rows.Row]:
:return: optional row using `.fetchone()`
"""
cursor.execute(*publication_query.add(**publication.dict()))
cursor.execute(*publication_query.add(**publication.model_dump()))
return cursor.fetchone()


Expand Down
9 changes: 7 additions & 2 deletions tests/unit/core/test_settings_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from geneweaver.db.core.settings_class import Settings
from pydantic import BaseModel, PostgresDsn
from pydantic.networks import MultiHostUrl


class PostgresDsnExample(BaseModel):
Expand Down Expand Up @@ -42,7 +43,9 @@ def test_settings_class_has_expected_attributes():
parsed = PostgresDsnExample(db=settings.URI)
assert parsed is not None, "URI should be parsable as a PostgresDsn"
assert parsed.db is not None, "URI should be parsable as a PostgresDsn"
assert isinstance(parsed.db, PostgresDsn), "URI should be parsable as a PostgresDsn"
assert isinstance(
parsed.db, MultiHostUrl
), "URI should be parsable as a PostgresDsn"

# "localhost" should be replaced with 127.0.0.1
assert (
Expand Down Expand Up @@ -81,7 +84,9 @@ def test_settings_class_can_directly_set_database_uri():
parsed = PostgresDsnExample(db=settings.URI)
assert parsed is not None, "URI should be parsable as a PostgresDsn"
assert parsed.db is not None, "URI should be parsable as a PostgresDsn"
assert isinstance(parsed.db, PostgresDsn), "URI should be parsable as a PostgresDsn"
assert isinstance(
parsed.db, MultiHostUrl
), "URI should be parsable as a PostgresDsn"

assert (
str(settings.URI) == "postgresql://other_admin@non_localhost/"
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/publication/test_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
abstract="Abstract 1",
journal="Journal 1",
pubmed_id="12345678",
pages=1,
pages="1",
month="Dec",
year=2021,
)
Expand Down
16 changes: 14 additions & 2 deletions tests/unit/query/threshold/test_set_geneset_threshold.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@
)
def test_set_geneset_threshold(geneset_id, score_type, threshold):
"""Test the set_geneset_threshold function for both async and sync."""
# Threshold_low values are not allowed for score types other than correlation and
# effect
if (
score_type not in [ScoreType.CORRELATION, ScoreType.EFFECT]
and "threshold_low" in threshold
):
del threshold["threshold_low"]

geneset_score_type = GenesetScoreType(score_type=score_type, **threshold)
sql, params = set_geneset_threshold(geneset_id, geneset_score_type)

Expand Down Expand Up @@ -105,11 +113,15 @@ def test_set_geneset_threshold_error(geneset_id, score_type, threshold):
"""Test the set_geneset_threshold function for an error."""
geneset_id = 0
geneset_score_type = GenesetScoreType(
score_type=score_type,
threshold_low=threshold["threshold_low"],
score_type=ScoreType.CORRELATION,
threshold=threshold["threshold"],
)

# We patch the values back to their invalid state since pydantic will now
# complain on initialization. We still want to check that this is caught.
geneset_score_type.score_type = score_type
geneset_score_type.threshold_low = threshold["threshold_low"]

error_str = (
"geneset_score_type.threshold must be larger "
"than geneset_score_type.threshold_low"
Expand Down
16 changes: 14 additions & 2 deletions tests/unit/query/threshold/test_set_geneset_value_threshold.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@
)
def test_set_geneset_threshold(geneset_id, score_type, threshold):
"""Test the set_geneset_threshold function for both async and sync."""
# Threshold_low values are not allowed for score types other than correlation and
# effect
if (
score_type not in [ScoreType.CORRELATION, ScoreType.EFFECT]
and "threshold_low" in threshold
):
del threshold["threshold_low"]

geneset_score_type = GenesetScoreType(score_type=score_type, **threshold)
sql, params = set_geneset_value_threshold(geneset_id, geneset_score_type)

Expand Down Expand Up @@ -103,11 +111,15 @@ def test_set_geneset_threshold_error(geneset_id, score_type, threshold):
"""Test the set_geneset_threshold function for an error."""
geneset_id = 0
geneset_score_type = GenesetScoreType(
score_type=score_type,
threshold_low=threshold["threshold_low"],
score_type=ScoreType.CORRELATION,
threshold=threshold["threshold"],
)

# We patch the values back to their invalid state since pydantic will now
# complain on initialization. We still want to check that this is caught.
geneset_score_type.score_type = score_type
geneset_score_type.threshold_low = threshold["threshold_low"]

error_str = (
"geneset_score_type.threshold must be larger "
"than geneset_score_type.threshold_low"
Expand Down

0 comments on commit 604a5fb

Please sign in to comment.