Skip to content

Commit

Permalink
Merge pull request #101 from ecolabdata/feat/force-transform-success
Browse files Browse the repository at this point in the history
feat: Force catalog update
  • Loading branch information
streino authored Nov 5, 2024
2 parents 0440f97 + 1b73be7 commit 09a13c9
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 5 deletions.
2 changes: 2 additions & 0 deletions isomorphe/batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ class SuccessTransformBatchRecord(TransformBatchRecord):
result: bytes
info: str
log: TransformLog | None = None
# has_diff == False can happen when Transformation.always_apply
has_diff: bool = True


@dataclass(kw_only=True)
Expand Down
18 changes: 17 additions & 1 deletion isomorphe/migrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,18 @@ class TransformationParam:

@dataclass
class Transformation:
ALWAYS_APPLY_SUFFIX = "~always"

path: Path

@property
def name(self) -> str:
return self.path.stem

@property
def display_name(self) -> str:
return self.path.stem.removesuffix(Transformation.ALWAYS_APPLY_SUFFIX)

@cached_property
def params(self) -> list[TransformationParam]:
xslt = etree.parse(self.path, parser=None)
Expand All @@ -61,6 +67,14 @@ def params(self) -> list[TransformationParam]:
params.append(param_info)
return params

@property
def always_apply(self) -> bool:
"""
When true, Transformation expects never to be skipped, so only its only
states can be Success or Failure.
"""
return self.path.stem.endswith(Transformation.ALWAYS_APPLY_SUFFIX)

@property
def transform(self) -> etree.XSLT:
xslt = etree.parse(self.path, parser=None)
Expand Down Expand Up @@ -147,13 +161,15 @@ def transform(
transform_log = TransformLog(transformer.error_log)
result_str = xml_to_string(result)
original_str = xml_to_string(original)
if result_str != original_str:
has_diff = result_str != original_str
if has_diff or transformation.always_apply:
batch.add(
SuccessTransformBatchRecord(
**batch_record.__dict__,
result=result_str,
info=xml_to_string(info),
log=transform_log,
has_diff=has_diff,
)
)
else:
Expand Down
10 changes: 7 additions & 3 deletions isomorphe/templates/fragments/transform_job_status.html.j2
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,13 @@
rel="noopener">Voir le XML</a>
</td>
<td>
<a href="{{ url_for('transform_diff', job_id=job.id, uuid=record.uuid) }}"
target="_blank"
rel="noopener">Voir le diff</a>
{% if record.has_diff %}
<a href="{{ url_for('transform_diff', job_id=job.id, uuid=record.uuid) }}"
target="_blank"
rel="noopener">Voir le diff</a>
{% else %}
-
{% endif %}
</td>
<td>
{{ record | record_transform_log }}
Expand Down
2 changes: 1 addition & 1 deletion isomorphe/templates/select.html.j2
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
name="transformation">
<option value="" selected disabled hidden>Sélectionner une transformation</option>
{% for transformation in transformations %}
<option value="{{ transformation.name }}">{{ transformation.name }}</option>
<option value="{{ transformation.name }}">{{ transformation.display_name }}</option>
{% endfor %}
</select>
</div>
Expand Down
16 changes: 16 additions & 0 deletions isomorphe/transformations/default/noop~always.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Retourne le XML à l'identique.
-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:gmd="http://www.isotc211.org/2005/gmd"
exclude-result-prefixes="#all">

<!-- Do a copy of every nodes and attributes -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>
11 changes: 11 additions & 0 deletions tests/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,24 @@ def test_transform_error(migrator: Migrator):
assert results.transformation == "error"


def test_transform_noop_always(migrator: Migrator):
"""`noop~always` transform is always successful"""
results, selection = get_transform_results("noop~always", migrator)
assert len(results.skipped()) == 0
assert len(results.successes()) == len(selection)
assert len(results.failures()) == 0
assert results.transformation == "noop~always"
assert all([not r.has_diff for r in results.successes()])


def test_transform_change_language(migrator: Migrator, clean_md_fixtures: list[Fixture]):
"""`change-language` transform is always successful"""
results, selection = get_transform_results("change-language", migrator)
assert len(results.skipped()) == 0
assert len(results.successes()) == len(selection)
assert len(results.failures()) == 0
assert results.transformation == "change-language"
assert all([r.has_diff for r in results.successes()])


def test_transform_working_copy(migrator: Migrator):
Expand Down

0 comments on commit 09a13c9

Please sign in to comment.