Skip to content

Commit 1e0731f

Browse files
author
Alan Christie
committed
fix: Adds get_jobs_replaced() to decoder
1 parent ffcceb2 commit 1e0731f

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

decoder/decoder.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ class TextEncoding(enum.Enum):
5454
JINJA2_3_0 = 1 # Encoding that complies with Jinja2 v3.0.x
5555

5656

57+
def get_job_key(collection: str, job: str) -> str:
58+
"""Returns the job Key, a string formed from "<collection>|<job>."""
59+
return f"{collection}|{job}"
60+
61+
5762
def validate_manifest_schema(manifest: Dict[str, Any]) -> Optional[str]:
5863
"""Checks the Job Definition Manifest (a preloaded job-definition dictionary)
5964
against the built-in schema. If there's an error the error text is
@@ -298,6 +303,22 @@ def get_environment_assets(job_definition: Dict[str, Any]) -> List[Dict[str, str
298303
return env_assets
299304

300305

306+
def get_jobs_replaced(job_definition: Dict[str, Any]) -> Optional[List[str]]:
307+
"""Given a Job Definition this function returns the Jobs it replaces.
308+
The returned list is a list of jobs identified by collection and
309+
job delimited with '|', e.g. string like "test-collection|test-job".
310+
"""
311+
replaces_list: List[Dict[str, str]] = job_definition.get("replaces", [])
312+
if not replaces_list:
313+
return None
314+
replaced: Set[str] = set()
315+
for replaces in replaces_list:
316+
r_collection: str = replaces["collection"]
317+
r_job: str = replaces["job"]
318+
replaced.add(get_job_key(r_collection, r_job))
319+
return list(replaced)
320+
321+
301322
def decode(
302323
template_text: str,
303324
variable_map: Optional[Dict[str, str]],

tests/test_get_jobs_replaced.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Tests for the decoder's get_jobs_replaced() 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_jobs_replaced_when_none():
12+
# Arrange
13+
job_definition: Dict = {}
14+
15+
# Act
16+
replaced = decoder.get_jobs_replaced(job_definition)
17+
18+
# Assert
19+
assert replaced is None
20+
21+
22+
def test_get_jobs_replaced():
23+
# Arrange
24+
collection: str = "collection-x"
25+
job: str = "job-x"
26+
job_definition: Dict = {
27+
"replaces": [
28+
{"collection": "c-1", "job": "j-1"},
29+
{"collection": "c-1", "job": "j-2"},
30+
]
31+
}
32+
33+
# Act
34+
replaced = decoder.get_jobs_replaced(job_definition)
35+
36+
# Assert
37+
assert replaced
38+
assert len(replaced) == 2
39+
assert "c-1|j-1" in replaced
40+
assert "c-1|j-2" in replaced

0 commit comments

Comments
 (0)