Skip to content

Commit

Permalink
feat: migration from attrs to pydantic (#49) (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
holtgrewe authored Nov 1, 2023
1 parent 062cbe8 commit 2c9e402
Show file tree
Hide file tree
Showing 47 changed files with 10,045 additions and 219 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@ lint: flake8 isort-check black-check
.PHONY: test
test:
TZ=UTC LC_ALL=C pytest .

.PHONY: test-update-snapshots
test-update-snapshots:
TZ=UTC LC_ALL=C pytest --snapshot-update .
5 changes: 1 addition & 4 deletions requirements/base.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
# Base requirements

# Attrs, helpful for removing boilerplate, cattr is for serialization/deserializaton.
attrs >=21.2.0
cattrs >=1.7.1
# Easy date parsing (e.g., from API).
python-dateutil >=2.8.1,<3.0

# pydantic: typed models and validation
# pydantic: typed models and validation
pydantic >=2,<3

# toml parsing if python <3.11
Expand Down
5 changes: 3 additions & 2 deletions requirements/test.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
-r base.txt

# Multi-environment testing
tox ==4.11.3
# While pydantic is great in the production code, for just bundling
# things, attrs is still better.
attrs >=23

# Wonderful py.test for testing
pytest >=3.6.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
],
"sodar_uuid": "0b96111f-00c9-4ed2-a4a2-ec2ec3322066",
"owner": null,
"date_created": "2023-06-26T17:15:13+00:00",
"date_modified": "2023-08-02T14:34:05+00:00",
"date_created": "2023-06-26T17:15:13+02:00",
"date_modified": "2023-08-02T14:34:05+02:00",
"project": "062b8838-453f-4cf3-817d-a5ec76546462",
"case": null,
"state": "draft",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"parent": null,
"title": "Test Category",
"type": "CATEGORY",
"description": "None",
"description": null,
"readme": "",
"public_guest_access": false,
"archive": false
Expand All @@ -14,7 +14,7 @@
"parent": "4071c7f5769447f69dd9d2834d4b0164",
"title": "Test Project",
"type": "PROJECT",
"description": "None",
"description": null,
"readme": "",
"public_guest_access": false,
"archive": false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"parent": null,
"title": "Test Category",
"type": "CATEGORY",
"description": "None",
"description": null,
"readme": "",
"public_guest_access": false,
"archive": false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{'sodar_uuid': 'e211747f-2a50-4a65-b192-c96bc2e111fa', 'date_created': '2023-10-31T08:15:15+01:00', 'date_modified': '2023-10-31T08:15:15+01:00', 'project': '062b8838-453f-4cf3-817d-a5ec76546462', 'title': 'my title', 'description': 'None', 'release': 'GRCh37', 'fields': ['pathogenicity', 'notes']}
{
"sodar_uuid": "e211747f-2a50-4a65-b192-c96bc2e111fa",
"date_created": "2023-10-31T08:15:15+00:00",
"date_modified": "2023-10-31T08:15:15+00:00",
"date_created": "2023-10-31T08:15:15+01:00",
"date_modified": "2023-10-31T08:15:15+01:00",
"project": "062b8838-453f-4cf3-817d-a5ec76546462",
"title": "my title",
"description": "None",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"sodar_uuid": "e211747f-2a50-4a65-b192-c96bc2e111fa",
"date_created": "2023-10-31T08:15:15+00:00",
"date_modified": "2023-10-31T08:15:15+00:00",
"date_created": "2023-10-31T08:15:15+01:00",
"date_modified": "2023-10-31T08:15:15+01:00",
"project": "062b8838-453f-4cf3-817d-a5ec76546462",
"title": "my title",
"description": "None",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"sodar_uuid": "e211747f-2a50-4a65-b192-c96bc2e111fa",
"date_created": "2023-10-31T08:15:15+00:00",
"date_modified": "2023-10-31T08:15:15+00:00",
"date_created": "2023-10-31T08:15:15+01:00",
"date_modified": "2023-10-31T08:15:15+01:00",
"project": "062b8838-453f-4cf3-817d-a5ec76546462",
"title": "my title",
"description": "None",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"sodar_uuid": "4a3a96ae-ddd8-4e9f-897f-5fbbadfedad5",
"date_created": "2023-10-31T09:02:26+00:00",
"date_modified": "2023-10-31T09:02:26+00:00",
"date_created": "2023-10-31T09:02:26+01:00",
"date_modified": "2023-10-31T09:02:26+01:00",
"varannoset": "0a30d59b-7257-43d1-8964-2a2ac50af7f2",
"release": "GRCh37",
"chromosome": "1",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"sodar_uuid": "4a3a96ae-ddd8-4e9f-897f-5fbbadfedad5",
"date_created": "2023-10-31T09:02:26+00:00",
"date_modified": "2023-10-31T09:02:26+00:00",
"date_created": "2023-10-31T09:02:26+01:00",
"date_modified": "2023-10-31T09:02:26+01:00",
"varannoset": "0a30d59b-7257-43d1-8964-2a2ac50af7f2",
"release": "GRCh37",
"chromosome": "1",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"sodar_uuid": "4a3a96ae-ddd8-4e9f-897f-5fbbadfedad5",
"date_created": "2023-10-31T09:02:26+00:00",
"date_modified": "2023-10-31T09:02:26+00:00",
"date_created": "2023-10-31T09:02:26+01:00",
"date_modified": "2023-10-31T09:02:26+01:00",
"varannoset": "0a30d59b-7257-43d1-8964-2a2ac50af7f2",
"release": "GRCh37",
"chromosome": "1",
Expand Down
97 changes: 97 additions & 0 deletions tests/cli/test_importer.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
"""Test CLI for importer API."""

import glob
import json
import types
import typing
import uuid

import pytest
from pytest_mock import MockerFixture
from pytest_snapshot.plugin import Snapshot
from requests_mock import ANY
from requests_mock.mocker import Mocker as RequestsMocker
from typer.testing import CliRunner

from tests.conftest import FakeFs
from varfish_cli.cli import app
from varfish_cli.cli.importer import cli_caseimportinfo_create
from varfish_cli.config import CommonOptions


@pytest.fixture
Expand Down Expand Up @@ -78,3 +83,95 @@ def test_varannoset_list_one_element(

assert result.exit_code == 0, result.output
snapshot.assert_match(result.output, "result_output")


def test_caseimportinfo_create_raw_func_call(
fake_fs_configured: FakeFs,
requests_mock: RequestsMocker,
fake_conn: typing.Tuple[str, str],
caseimportinfo_list_result_two_elements: typing.List[typing.Any],
snapshot: Snapshot,
mocker: MockerFixture,
):
"""Test through function call and not runner.
We create a stubbed out replay of the requests communication and only check
results against snapshots. This is not much more than a smoke test or a
change test.
"""
fake_fs_configured.fs.add_real_directory("tests/data/importer")

mocker.patch("varfish_cli.config.open", fake_fs_configured.open_, create=True)
mocker.patch("varfish_cli.config.os", fake_fs_configured.os)

project_uuid = "5b1d876d-5ea9-426f-9f8f-f82b18830e16"
host, token = fake_conn
m_any = requests_mock.register_uri(
ANY, ANY, request_headers={"Authorization": f"Token {token}"}, json=[]
)
m_case_import_info = requests_mock.post(
f"{host}/importer/api/case-import-info/{project_uuid}/",
request_headers={"Authorization": f"Token {token}"},
json=caseimportinfo_list_result_two_elements[0],
)
m_case_bam_qc_file = requests_mock.post(
f"{host}/importer/api/bam-qc-file/8adbf84e-adb2-4c5d-9b5a-2a8194307b79/",
request_headers={"Authorization": f"Token {token}"},
json=caseimportinfo_list_result_two_elements[0]["bam_qc_files"][0],
)
m_case_variant_set_import_info_create = requests_mock.post(
f"{host}/importer/api/variant-set-import-info/8adbf84e-adb2-4c5d-9b5a-2a8194307b79/",
request_headers={"Authorization": f"Token {token}"},
json=caseimportinfo_list_result_two_elements[0]["variant_sets"][0],
)
m_case_genotype_file = requests_mock.post(
f"{host}/importer/api/genotype-file/104f900b-fa9c-4bd5-a119-35544293fe7f/",
request_headers={"Authorization": f"Token {token}"},
json=caseimportinfo_list_result_two_elements[0]["variant_sets"][0]["genotype_files"][0],
)
m_case_db_info_file = requests_mock.post(
f"{host}/importer/api/database-info-file/104f900b-fa9c-4bd5-a119-35544293fe7f/",
request_headers={"Authorization": f"Token {token}"},
json=caseimportinfo_list_result_two_elements[0]["variant_sets"][0]["db_info_files"][0],
)
m_case_variant_set_import_info_update = requests_mock.put(
(
f"{host}/importer/api/variant-set-import-info/8adbf84e-adb2-4c5d-9b5a-2a8194307b79/"
"104f900b-fa9c-4bd5-a119-35544293fe7f/"
),
request_headers={"Authorization": f"Token {token}"},
json=caseimportinfo_list_result_two_elements[0]["variant_sets"][0],
)
m_case_import_info_update = requests_mock.put(
(
f"{host}/importer/api/case-import-info/5b1d876d-5ea9-426f-9f8f-f82b18830e16/"
"8adbf84e-adb2-4c5d-9b5a-2a8194307b79/"
),
request_headers={"Authorization": f"Token {token}"},
json=caseimportinfo_list_result_two_elements[0],
)
ctx = types.SimpleNamespace(
obj=CommonOptions(
verbose=True,
verify_ssl=False,
config=None,
varfish_server_url=host,
varfish_api_token=token,
)
)
cli_caseimportinfo_create(
ctx=ctx,
project_uuid=project_uuid,
paths=list(sorted(glob.glob("tests/data/importer/*"))),
)

assert m_any.call_count == 10
assert m_case_import_info.call_count == 1
assert m_case_bam_qc_file.call_count == 1
assert m_case_variant_set_import_info_create.call_count == 2
assert m_case_genotype_file.call_count == 2
assert m_case_db_info_file.call_count == 2
assert m_case_variant_set_import_info_update.call_count == 2
assert m_case_import_info_update.call_count == 1

mocker.stopall()
Loading

0 comments on commit 2c9e402

Please sign in to comment.