Skip to content

Commit 5343fd9

Browse files
author
Alan Christie
committed
feat: Add get_job_doc_url() function
1 parent f9161b6 commit 5343fd9

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed

decoder/decoder.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,60 @@ def validate_job_schema(job_definition: Dict[str, Any]) -> Optional[str]:
8181
return None
8282

8383

84+
def get_job_doc_url(job: str, job_definition: Dict[str, Any], manifest_url: str) -> str:
85+
"""Returns the Job's documentation URL for a specific Job using the JOb's
86+
'doc-url' and manifest URL. The job manifest is expected to
87+
have been validated, and we expect to have been given one job structure
88+
from the job's definition, not the whole job definition file.
89+
"""
90+
assert job
91+
assert isinstance(job_definition, dict)
92+
assert manifest_url
93+
94+
# The response depends on the value of 'doc-url'.
95+
# 1. If there is no doc-url the URL is auto-generated
96+
# from the manifest URL, job collection and job name.
97+
# 2. If the doc-url begins 'https://' then this is taken as the doc-url
98+
# 3. If the doc-url does not begin https:// then is
99+
# assumed to be relative to the manifest directory URL
100+
101+
# A typical manifest URl will look like this...
102+
#
103+
# https://raw.githubusercontent.com/InformaticsMatters/
104+
# virtual-screening/main/data-manager/manifest-virtual-screening.yaml
105+
#
106+
# The base for an auto-generated doc-url (if we need it)
107+
# will be: -
108+
#
109+
# https://raw.githubusercontent.com/InformaticsMatters/
110+
# virtual-screening/main/data-manager/docs
111+
112+
collection: str = job_definition["collection"]
113+
doc_url: Optional[str] = job_definition.get("doc-url", None)
114+
115+
# If doc-url starts 'https://' just return it
116+
if doc_url and doc_url.startswith("https://"):
117+
return doc_url
118+
119+
# If the doc-url is set (it's not https://)
120+
# so we assume is simply relative to the 'docs' directory
121+
# where the manifest is found.
122+
manifest_directory_url, _ = os.path.split(manifest_url)
123+
if doc_url and not doc_url.startswith("https://"):
124+
# doc-url defined but does not start 'https://'
125+
doc_url = f"{manifest_directory_url}/docs/{doc_url}"
126+
elif doc_url is None:
127+
# No doc-url.
128+
# The
129+
doc_url = f"{manifest_directory_url}/docs/{collection}/{job}.md"
130+
else:
131+
# How did we get here?
132+
assert False
133+
134+
assert doc_url
135+
return doc_url
136+
137+
84138
def decode(
85139
template_text: str,
86140
variable_map: Optional[Dict[str, str]],

tests/test_get_job_doc_url.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Tests for the decoder's get_job_doc_url() function.
2+
from typing import Dict
3+
4+
import pytest
5+
6+
pytestmark = pytest.mark.unit
7+
8+
from decoder import decoder
9+
10+
11+
def test_get_job_doc_url_with_fq_doc_url():
12+
# Arrange
13+
job: str = "job-x"
14+
job_definition: Dict = {
15+
"collection": "collection-x",
16+
"doc-url": "https://example.com/docs/doc.md",
17+
}
18+
manifest_url: str = (
19+
"https://raw.githubusercontent.com/"
20+
"InformaticsMatters/virtual-screening/main/data-manager/"
21+
"manifest-virtual-screening.yaml"
22+
)
23+
expected_doc_url: str = "https://example.com/docs/doc.md"
24+
25+
# Act
26+
doc_url = decoder.get_job_doc_url(job, job_definition, manifest_url)
27+
28+
# Assert
29+
assert doc_url
30+
assert doc_url == expected_doc_url
31+
32+
33+
def test_get_job_doc_url_with_partial_doc_url():
34+
# Arrange
35+
job: str = "job-x"
36+
job_definition: Dict = {"collection": "collection-x", "doc-url": "special/doc.md"}
37+
manifest_url: str = (
38+
"https://raw.githubusercontent.com/"
39+
"InformaticsMatters/virtual-screening/main/data-manager/"
40+
"manifest-virtual-screening.yaml"
41+
)
42+
expected_doc_url: str = (
43+
"https://raw.githubusercontent.com/"
44+
"InformaticsMatters/virtual-screening/main/data-manager/docs/"
45+
"special/doc.md"
46+
)
47+
48+
# Act
49+
doc_url = decoder.get_job_doc_url(job, job_definition, manifest_url)
50+
51+
# Assert
52+
assert doc_url
53+
assert doc_url == expected_doc_url
54+
55+
56+
def test_get_job_doc_url_with_no_doc_url():
57+
# Arrange
58+
job: str = "job-x"
59+
job_definition: Dict = {"collection": "collection-x"}
60+
manifest_url: str = (
61+
"https://raw.githubusercontent.com/"
62+
"InformaticsMatters/virtual-screening/main/data-manager/"
63+
"manifest-virtual-screening.yaml"
64+
)
65+
expected_doc_url: str = (
66+
"https://raw.githubusercontent.com/"
67+
"InformaticsMatters/virtual-screening/main/data-manager/docs/"
68+
"collection-x/job-x.md"
69+
)
70+
71+
# Act
72+
doc_url = decoder.get_job_doc_url(job, job_definition, manifest_url)
73+
74+
# Assert
75+
assert doc_url
76+
assert doc_url == expected_doc_url

0 commit comments

Comments
 (0)