-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/mx 1537 extract voxco data (#97)
# Added - extract voxco data # Changes - consolidate mocked drop connector into one general mock --------- Signed-off-by: erichesse <[email protected]> Signed-off-by: erichesse <[email protected]> Co-authored-by: Kamran Ali <[email protected]>
- Loading branch information
1 parent
f439f25
commit 3669319
Showing
21 changed files
with
226 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,6 +47,9 @@ | |
{ | ||
"identifier": "report-server" | ||
}, | ||
{ | ||
"identifier": "voxco" | ||
}, | ||
{ | ||
"identifier": "wikidata" | ||
} | ||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from mex.drop import DropApiConnector | ||
from mex.voxco.model import VoxcoVariable | ||
|
||
|
||
def extract_voxco_variables() -> dict[str, list[VoxcoVariable]]: | ||
"""Extract voxco variables by loading data from mex-drop source json file. | ||
Returns: | ||
lists of voxco variables by json file name | ||
""" | ||
connector = DropApiConnector.get() | ||
files = connector.list_files("voxco") | ||
data = { | ||
file_name: connector.get_file("voxco", file_name) | ||
for file_name in files | ||
if "test_" not in file_name | ||
} | ||
return { | ||
file_name: [VoxcoVariable.model_validate(item) for item in file_rows["value"]] | ||
for file_name, file_rows in data.items() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from pathlib import Path | ||
from typing import Any | ||
|
||
from mex.common.cli import entrypoint | ||
from mex.common.models import ( | ||
ExtractedPrimarySource, | ||
ExtractedResource, | ||
) | ||
from mex.common.primary_source.transform import ( | ||
get_primary_sources_by_name, | ||
) | ||
from mex.mapping.extract import extract_mapping_data | ||
from mex.pipeline import asset, run_job_in_process | ||
from mex.sinks import load | ||
from mex.voxco.extract import extract_voxco_variables | ||
from mex.voxco.model import VoxcoVariable | ||
from mex.voxco.settings import VoxcoSettings | ||
|
||
|
||
@asset(group_name="voxco", deps=["extracted_primary_source_mex"]) | ||
def extracted_primary_source_voxco( | ||
extracted_primary_sources: list[ExtractedPrimarySource], | ||
) -> ExtractedPrimarySource: | ||
"""Load and return voxco primary source.""" | ||
(extracted_primary_source,) = get_primary_sources_by_name( | ||
extracted_primary_sources, "voxco" | ||
) | ||
load([extracted_primary_source]) | ||
|
||
return extracted_primary_source | ||
|
||
|
||
@asset(group_name="voxco") | ||
def voxco_sources() -> dict[str, list[VoxcoVariable]]: | ||
"""Extract voxco variables by json file names.""" | ||
return extract_voxco_variables() | ||
|
||
|
||
@asset(group_name="voxco") | ||
def voxco_resource_mappings() -> list[dict[str, Any]]: | ||
"""Extract voxco resource mappings.""" | ||
settings = VoxcoSettings.get() | ||
return [ | ||
extract_mapping_data(file, ExtractedResource) | ||
for file in Path(settings.mapping_path).glob("resource_*.yaml") | ||
] | ||
|
||
|
||
@entrypoint(VoxcoSettings) | ||
def run() -> None: | ||
"""Run the voxco extractor job in-process.""" | ||
run_job_in_process("voxco") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from pydantic import Field | ||
|
||
from mex.common.models import BaseModel | ||
|
||
|
||
class VoxcoVariable(BaseModel): | ||
"""Model class for Voxco Variable.""" | ||
|
||
Id: int | ||
DataType: str | ||
Type: str | ||
QuestionText: str = Field(min_length=0) | ||
Choices: list[str] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from pydantic import Field | ||
from pydantic_settings import SettingsConfigDict | ||
|
||
from mex.common.types import AssetsPath | ||
from mex.settings import Settings | ||
|
||
|
||
class VoxcoSettings(Settings): | ||
"""Settings for the Voxco extractor.""" | ||
|
||
model_config = SettingsConfigDict(env_prefix="voxco_") | ||
|
||
mapping_path: AssetsPath = Field( | ||
AssetsPath("mappings/__final__/voxco"), | ||
description=( | ||
"Path to the directory with the voxco mapping files containing the " | ||
"default values, absolute path or relative to `assets_dir`." | ||
), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import json | ||
from pathlib import Path | ||
from unittest.mock import MagicMock | ||
|
||
import pytest | ||
import requests | ||
from pytest import MonkeyPatch | ||
|
||
from mex.drop import DropApiConnector | ||
|
||
|
||
@pytest.fixture | ||
def mocked_drop(monkeypatch: MonkeyPatch) -> None: | ||
"""Mock the drop api connector to return dummy data.""" | ||
monkeypatch.setattr( | ||
DropApiConnector, | ||
"__init__", | ||
lambda self: setattr(self, "session", MagicMock(spec=requests.Session)), | ||
) | ||
monkeypatch.setattr( | ||
DropApiConnector, | ||
"list_files", | ||
lambda _, x_system: [ | ||
path.stem | ||
for path in ( | ||
Path(__file__).parents[2] | ||
/ "tests" | ||
/ x_system.replace("-", "_") | ||
/ "test_data" | ||
).rglob("*.json") | ||
], | ||
) | ||
|
||
def get_file_mocked(self, x_system: str, file_id: str): | ||
with open( | ||
( | ||
Path(__file__).parents[2] | ||
/ "tests" | ||
/ x_system.replace("-", "_") | ||
/ "test_data" | ||
/ file_id | ||
).with_suffix(".json") | ||
) as handle: | ||
return json.load(handle) | ||
|
||
monkeypatch.setattr( | ||
DropApiConnector, | ||
"get_file", | ||
get_file_mocked, | ||
) |
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"Count": 3, | ||
"value": [ | ||
{ | ||
"Choices": [ | ||
"@{Code=1; Text=Januar; Image=; HasOpenEnd=False; Visible=True; Default=False}", | ||
"@{Code=1; Text=Februar; Image=; HasOpenEnd=False; Visible=True; Default=False}" | ||
], | ||
"DataType": "Text", | ||
"HasOpenEnd": false, | ||
"Id": 50614, | ||
"MaxAnswers": 1, | ||
"MaxLength": 2, | ||
"Name": "MONAT", | ||
"QuestionId": 33302, | ||
"QuestionName": "MONAT", | ||
"QuestionText": "Monat ", | ||
"Text": "", | ||
"Type": "Discrete" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import pytest | ||
|
||
from mex.voxco.extract import extract_voxco_variables | ||
|
||
|
||
@pytest.mark.usefixtures( | ||
"mocked_drop", | ||
) | ||
def test_extract_voxco_variables() -> None: | ||
sources = extract_voxco_variables() | ||
expected = { | ||
"Id": 50614, | ||
"DataType": "Text", | ||
"Type": "Discrete", | ||
"QuestionText": "Monat", | ||
"Choices": [ | ||
"@{Code=1; Text=Januar; Image=; HasOpenEnd=False; Visible=True; Default=False}", | ||
"@{Code=1; Text=Februar; Image=; HasOpenEnd=False; Visible=True; Default=False}", | ||
], | ||
} | ||
assert sources["voxco_data"][0].model_dump() == expected |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import pytest | ||
|
||
from mex.pipeline import run_job_in_process | ||
|
||
|
||
@pytest.mark.usefixtures( | ||
"mocked_drop", | ||
) | ||
def test_job() -> None: | ||
result = run_job_in_process("voxco") | ||
assert result.success |