Skip to content

Commit b9a9ffc

Browse files
authored
Merge pull request #197 from mschoettle/mschoettle/move-fixtures
Move fixtures into conftest
2 parents 9a2eec1 + 748143a commit b9a9ffc

316 files changed

Lines changed: 646 additions & 694 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 112 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,112 @@
1-
# -*- coding: utf-8 _*_
2-
pytest_plugins = ["fhir.resources.R4B.tests.fixtures"]
1+
import hashlib
2+
import io
3+
import os
4+
import pathlib
5+
import shutil
6+
import sys
7+
import tempfile
8+
import zipfile
9+
from os.path import dirname
10+
11+
import pytest
12+
from fhir_core.types import (
13+
Base64BinaryType,
14+
DateTimeType,
15+
DateType,
16+
InstantType,
17+
TimeType,
18+
UriType,
19+
UrlType,
20+
)
21+
from pydantic import BaseModel, Field
22+
23+
from tests import patch_r4b_test
24+
25+
patch_r4b_test.apply()
26+
27+
EXAMPLE_RESOURCES_URL = (
28+
"https://github.com/nazrulworld/hl7-archives/raw/"
29+
"0.4.0/FHIR/R4B/"
30+
"4.3.0-examples-json.zip"
31+
)
32+
ROOT_PATH = dirname(dirname(dirname(dirname(dirname(os.path.abspath(__file__))))))
33+
CACHE_PATH = os.path.join(ROOT_PATH, ".cache", "R4B")
34+
35+
36+
def download_and_store(url, path):
37+
""" """
38+
import requests
39+
40+
try:
41+
sys.stdout.write("Attempting to download from {0}\n".format(url))
42+
ret = requests.get(url)
43+
except requests.HTTPError as exc: # pragma: no cover
44+
raise LookupError("Failed to download. Full error: {0!s}".format(exc))
45+
else:
46+
if not ret.ok: # pragma: no cover
47+
raise Exception("Failed to download {0}".format(url))
48+
with io.open(path, "wb") as handle:
49+
for chunk in ret.iter_content():
50+
handle.write(chunk)
51+
sys.stdout.write(
52+
"Download has been completed, now saved to {0}\n".format(path)
53+
)
54+
55+
56+
def expand(self, local): # pragma: no cover
57+
"""Expand the ZIP file at the given path to the cache directory."""
58+
path = os.path.join(self.cache, local)
59+
assert os.path.exists(path)
60+
import zipfile # import here as we can bypass its use with a manual unzip
61+
62+
with zipfile.ZipFile(path) as z:
63+
z.extractall(self.cache)
64+
65+
66+
@pytest.fixture(scope="session")
67+
def base_settings():
68+
if not os.path.exists(CACHE_PATH): # pragma: no cover
69+
os.makedirs(CACHE_PATH)
70+
71+
settings = {}
72+
73+
example_data_file_uri = EXAMPLE_RESOURCES_URL
74+
75+
example_data_file_id = hashlib.md5(example_data_file_uri.encode()).hexdigest()
76+
77+
example_data_file_location = os.path.join(
78+
CACHE_PATH, (example_data_file_id + ".zip")
79+
)
80+
81+
if not os.path.exists(example_data_file_location):
82+
download_and_store(example_data_file_uri, example_data_file_location)
83+
84+
temp_data_dir = tempfile.mkdtemp()
85+
# extract all files from archive and put into temp dir
86+
with zipfile.ZipFile(example_data_file_location) as z:
87+
z.extractall(temp_data_dir)
88+
89+
zip_dir_name = pathlib.Path(EXAMPLE_RESOURCES_URL).name[:-4]
90+
if "FHIR_UNITTEST_DATADIR" not in os.environ:
91+
os.environ.setdefault(
92+
"FHIR_UNITTEST_DATADIR", os.path.join(temp_data_dir, zip_dir_name)
93+
)
94+
95+
settings["unittest_data_dir"] = pathlib.Path(os.environ["FHIR_UNITTEST_DATADIR"])
96+
97+
yield settings
98+
99+
os.environ.pop("FHIR_UNITTEST_DATADIR")
100+
shutil.rmtree(temp_data_dir)
101+
102+
103+
class ExternalValidatorModel(BaseModel):
104+
"""This model is used to validate datetime objects against in the tests"""
105+
106+
valueDate: DateType = Field(None, title="Date")
107+
valueTime: TimeType = Field(None, title="Time")
108+
valueDateTime: DateTimeType = Field(None, title="DateTime")
109+
valueInstant: InstantType = Field(None, title="Instant")
110+
valueUri: UriType = Field(None, title="Uri")
111+
valueUrl: UrlType = Field(None, title="Url")
112+
valueBase64Binary: Base64BinaryType = Field(None, title="Base64Binary")

fhir/resources/R4B/tests/fixtures.py

Lines changed: 0 additions & 127 deletions
This file was deleted.

fhir/resources/R4B/tests/test_account.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
Last updated: 2022-05-28T12:47:40.239+10:00
88
"""
99
from .. import account
10-
from .fixtures import ExternalValidatorModel # noqa: F401
10+
from .conftest import ExternalValidatorModel # noqa: F401
1111

1212

1313
def impl_account_1(inst):

fhir/resources/R4B/tests/test_activitydefinition.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
Last updated: 2022-05-28T12:47:40.239+10:00
88
"""
99
from .. import activitydefinition
10-
from .fixtures import ExternalValidatorModel # noqa: F401
10+
from .conftest import ExternalValidatorModel # noqa: F401
1111

1212

1313
def impl_activitydefinition_1(inst):

fhir/resources/R4B/tests/test_administrableproductdefinition.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
Last updated: 2022-05-28T12:47:40.239+10:00
88
"""
99
from .. import administrableproductdefinition
10-
from .fixtures import ExternalValidatorModel # noqa: F401
10+
from .conftest import ExternalValidatorModel # noqa: F401
1111

1212

1313
def impl_administrableproductdefinition_1(inst):

fhir/resources/R4B/tests/test_adverseevent.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
Last updated: 2022-05-28T12:47:40.239+10:00
88
"""
99
from .. import adverseevent
10-
from .fixtures import ExternalValidatorModel # noqa: F401
10+
from .conftest import ExternalValidatorModel # noqa: F401
1111

1212

1313
def impl_adverseevent_1(inst):

fhir/resources/R4B/tests/test_allergyintolerance.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
Last updated: 2022-05-28T12:47:40.239+10:00
88
"""
99
from .. import allergyintolerance
10-
from .fixtures import ExternalValidatorModel # noqa: F401
10+
from .conftest import ExternalValidatorModel # noqa: F401
1111

1212

1313
def impl_allergyintolerance_1(inst):

fhir/resources/R4B/tests/test_appointment.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
Last updated: 2022-05-28T12:47:40.239+10:00
88
"""
99
from .. import appointment
10-
from .fixtures import ExternalValidatorModel # noqa: F401
10+
from .conftest import ExternalValidatorModel # noqa: F401
1111

1212

1313
def impl_appointment_1(inst):

fhir/resources/R4B/tests/test_appointmentresponse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
Last updated: 2022-05-28T12:47:40.239+10:00
88
"""
99
from .. import appointmentresponse
10-
from .fixtures import ExternalValidatorModel # noqa: F401
10+
from .conftest import ExternalValidatorModel # noqa: F401
1111

1212

1313
def impl_appointmentresponse_1(inst):

fhir/resources/R4B/tests/test_auditevent.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
Last updated: 2022-05-28T12:47:40.239+10:00
88
"""
99
from .. import auditevent
10-
from .fixtures import ExternalValidatorModel # noqa: F401
10+
from .conftest import ExternalValidatorModel # noqa: F401
1111

1212

1313
def impl_auditevent_1(inst):

0 commit comments

Comments
 (0)