-
Notifications
You must be signed in to change notification settings - Fork 3
/
conftest.py
executable file
·168 lines (119 loc) · 4.45 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
"""
Read more about conftest.py under:
- https://docs.pytest.org/en/stable/fixture.html
- https://docs.pytest.org/en/stable/writing_plugins.html
"""
import requests
from pathlib import Path
import os
import re
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
import pytest as pt
import fhirpy
import fhirpack as fp
import tests as ts
pytest_plugins = ["docker_compose"]
# this is an example fixture with session scope,
# for all packages, with autouse enabled and a finalizer
# this fixture can be used by any test in the repository
@pt.fixture(scope="session", autouse=True)
def globalSessionFixture(request):
ts.debug("global session confSetup")
def globalSessionFixtureFin():
ts.debug("global session confTeardown")
pass
request.addfinalizer(globalSessionFixtureFin)
@pt.fixture(scope="session")
def fhirServerDocker(session_scoped_container_getter):
"""Wait for the api from my_api_service to become responsive"""
session = requests.Session()
retries = Retry(total=5, backoff_factor=0.1, status_forcelist=[500, 502, 503, 504])
session.mount("http://", HTTPAdapter(max_retries=retries))
service = session_scoped_container_getter.get("r4").network_info[0]
api_url = "http://%s:%s/" % (service.hostname, service.host_port)
assert session.get(api_url)
return session, api_url
@pt.fixture(scope="session")
def packUnconnected(request):
ts.debug("global session pack")
# session, apiUrl = remoteFhirServer
# client = fhirpy.SyncFHIRClient(f"{apiUrl}hapi-fhir-jpaserver/fhir")
def globalSessionFixtureFin():
ts.debug("global session pack Teardown")
pass
request.addfinalizer(globalSessionFixtureFin)
pack = fp.PACK(unconnected=True)
return pack
@pt.fixture(scope="session")
def packEnv(request):
ts.debug("global session pack")
# session, apiUrl = remoteFhirServer
# client = fhirpy.SyncFHIRClient(f"{apiUrl}hapi-fhir-jpaserver/fhir")
def globalSessionFixtureFin():
ts.debug("global session pack Teardown")
pass
request.addfinalizer(globalSessionFixtureFin)
pack = fp.PACK(envFile=".env.example")
return pack
@pt.fixture(scope="session")
def packDocker(request, fhirServerDocker):
ts.debug("global session localPACK")
session, apiUrl = fhirServerDocker
def globalSessionFixtureFin():
ts.debug("global session confTeardown")
pass
request.addfinalizer(globalSessionFixtureFin)
return fp.PACK(f"{apiUrl}hapi-fhir-jpaserver/fhir")
@pt.fixture(scope="function")
def functionData(request):
# behaves differently on linux and mac, src. prepended on mac
# moduleName = request.module.__name__
installationPath = os.path.dirname(fp.__file__)
installationPath = str(Path(installationPath).parent.absolute())
testSessionPath = str(request.session.path)
# ./fhirpack
testPath = str(request.fspath)
# ./fhirpack/src/fhirpack/utils_test.py
testPath = testPath.split(f"{installationPath}/")
# ['', '/src/fhirpack/utils_test.py']
if len(testPath) == 1:
testPath = testPath[0].split(testSessionPath)
testRelativePath = (
testPath[1]
.replace("src/", "")
.replace("/", ".")
.replace("_test", "")
.replace("test_", "")
.replace(".py", "")
) # fhirpack.utils
# tests.cli
testedFunctionName = request.function.__name__
testedFunctionName = testedFunctionName.split("_")[1]
# valuesForKeys
# run
# main
# guessBufferMIMEType
if testRelativePath:
libraryDataPrefix = f"all"
packageDataPrefix = f"fhirpack"
functionDataPrefix = f"{testRelativePath}.{testedFunctionName}"
# tests.cli.main
# fhirpack.utils.guessBufferMIMEType
# fhirpack.utils.valuesForKeys
else:
packageDataPrefix = f"tests"
testDataFiles = []
# pt.set_trace()
availableDataFiles = sorted(os.listdir(ts.TEST_DATA_DIR))
for f in availableDataFiles:
if re.match(f"{functionDataPrefix}.[a-zA-Z]+.[0-9]+..?", f):
testDataFiles.append(f)
elif re.match(f"{packageDataPrefix}.[a-zA-Z]+.[0-9]+..?", f):
testDataFiles.append(f)
elif re.match(f"{libraryDataPrefix}.[a-zA-Z]+.[0-9]+..?", f):
testDataFiles.append(f)
data = testDataFiles
indata = [e for e in data if ".in" in e]
outdata = [e for e in data if ".out" in e]
return {"in": indata, "out": outdata}