Initial commit
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
import logging
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from pcdigitizer import enable_logging
|
||||
|
||||
TEST_DIR: Path = Path(__file__).resolve().parent
|
||||
TMP_DIR: Path = TEST_DIR / "tmp"
|
||||
|
||||
|
||||
@pytest.fixture(scope="session", autouse=True)
|
||||
def turn_on_logging() -> None:
|
||||
"""Enable loguru logging at DEBUG level for the entire test session."""
|
||||
enable_logging(logging.DEBUG)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def test_dir() -> Path:
|
||||
"""Return the absolute path to the tests/ directory."""
|
||||
return TEST_DIR
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def tmp_dir() -> Path:
|
||||
"""Return the path to the shared temporary output directory."""
|
||||
TMP_DIR.mkdir(parents=True, exist_ok=True)
|
||||
return TMP_DIR
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_session(test_dir):
|
||||
"""A fake requests.Session that returns saved fixture files."""
|
||||
|
||||
class FakeResponse:
|
||||
def __init__(self, content: bytes, status_code: int = 200):
|
||||
self.content = content
|
||||
self.status_code = status_code
|
||||
|
||||
class FakeSession:
|
||||
def get(self, url):
|
||||
if "heading/Dissociation" in url:
|
||||
fixture = test_dir / "files" / "dissociation-constants-1.json"
|
||||
return FakeResponse(fixture.read_bytes())
|
||||
return FakeResponse(b'{"Fault": "not found"}', status_code=404)
|
||||
|
||||
return FakeSession()
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,33 @@
|
||||
import polars as pl
|
||||
|
||||
from pcdigitizer import Annotation, GetAnnotationPage
|
||||
|
||||
|
||||
def test_process_dissociation_data(test_dir, mock_session):
|
||||
"""Makes request for the first page of the `"Dissociation Constants"` and
|
||||
processes the data into a polars DataFrame.
|
||||
|
||||
Makes the following request:
|
||||
[pubchem.ncbi.nlm.nih.gov/rest/pug_view/annotations/heading/Dissociation%20Constants/JSON?page=1](https://pubchem.ncbi.nlm.nih.gov/rest/pug_view/annotations/heading/Dissociation%20Constants/JSON?page=1).
|
||||
"""
|
||||
task = GetAnnotationPage()
|
||||
df = task.do(
|
||||
item=1, annotation=Annotation.DISSOCIATION_CONSTANTS, session=mock_session
|
||||
)
|
||||
|
||||
# TODO: Need to implement actual tests
|
||||
assert not df.is_empty()
|
||||
assert df.columns == [
|
||||
"cid",
|
||||
"sid",
|
||||
"pclid",
|
||||
"pka_label",
|
||||
"pka_value",
|
||||
"temperature_C",
|
||||
"comment",
|
||||
]
|
||||
assert df.schema["pka_value"] == pl.Float64
|
||||
assert df.schema["cid"] == pl.Int64
|
||||
|
||||
csv_path = test_dir / "tmp" / "test_process_dissociation_data.csv"
|
||||
df.write_csv(csv_path)
|
||||
@@ -0,0 +1,48 @@
|
||||
import polars as pl
|
||||
|
||||
from pcdigitizer import Annotation, PubChemAPI
|
||||
|
||||
|
||||
def test_source_df():
|
||||
df = PubChemAPI.get_sources()
|
||||
|
||||
columns = df.columns
|
||||
assert len(columns) == 23
|
||||
assert columns[0] == "Source Name"
|
||||
assert columns[-3] == "License URL"
|
||||
|
||||
source_name = "Hazardous Substances Data Bank"
|
||||
source_count = df.select(
|
||||
(pl.col("Source Name").str.contains(source_name)).sum()
|
||||
).item()
|
||||
assert source_count == 1
|
||||
|
||||
|
||||
def test_source_annotations():
|
||||
annotations = PubChemAPI.get_source_annotations(
|
||||
"Hazardous Substances Data Bank (HSDB)"
|
||||
)
|
||||
assert len(annotations["Compound"]) == 246
|
||||
|
||||
|
||||
def test_annotations():
|
||||
annotations = PubChemAPI.get_annotations()
|
||||
annotation_types = sorted(annotations.keys())
|
||||
assert annotation_types == [
|
||||
"Assay",
|
||||
"Cell",
|
||||
"Compound",
|
||||
"Element",
|
||||
"Gene",
|
||||
"Pathway",
|
||||
"Protein",
|
||||
"Taxonomy",
|
||||
]
|
||||
assert "Dissociation Constants" in annotations["Compound"]
|
||||
|
||||
|
||||
def test_get_data(mock_session):
|
||||
data = PubChemAPI.get_data(
|
||||
Annotation.DISSOCIATION_CONSTANTS, 1, session=mock_session
|
||||
)
|
||||
assert len(data) == 1000
|
||||
@@ -0,0 +1,2 @@
|
||||
*
|
||||
!.gitignore
|
||||
Reference in New Issue
Block a user