From bbdf111d1935c29f5d8421392d134be7bb9d036e Mon Sep 17 00:00:00 2001 From: Leon Date: Fri, 5 Jan 2024 12:59:28 +0100 Subject: [PATCH] fix instance comparison of extensions (#310) ## Types of changes - [x] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) ## Checklist: - [x] I have updated the [changelog](../CHANGELOG.md) accordingly. - [x] I have added tests to cover my changes. --- CHANGELOG.md | 6 +++++ src/sdc11073/xml_types/xml_structure.py | 5 ++++- tests/test_pmtypes.py | 30 ++++++++++++++++++++++++- 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d0b86518..cd7f248b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to the sdc11073 module will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] + +### Fixed + +- fixed a bug where comparing `ExtensionLocalValue` would fail when using the `!=` operator [#305](https://github.com/Draegerwerk/sdc11073/issues/305) + ## [2.0.0a7] - 2024-01-04 ### Added diff --git a/src/sdc11073/xml_types/xml_structure.py b/src/sdc11073/xml_types/xml_structure.py index fd87ce40..959035f5 100644 --- a/src/sdc11073/xml_types/xml_structure.py +++ b/src/sdc11073/xml_types/xml_structure.py @@ -826,7 +826,7 @@ def update_xml_value(self, instance: Any, node: xml_utils.LxmlElement): def _compare_extension(left: xml_utils.LxmlElement, right: xml_utils.LxmlElement) -> bool: - # xml comparison + # SDPi R0019 and R0020 forbid the usage of XML mixed content or xml schema qname type in extensions try: if left.tag != right.tag: # compare expanded names return False @@ -860,6 +860,9 @@ def __eq__(self, other: Sequence) -> bool: return False return all(self.__class__.compare_method(left, right) for left, right in zip(self, other)) + def __ne__(self, other): + return not self == other + class ExtensionNodeProperty(_ElementBase): """Represents an ext:Extension Element that contains 0...n child elements of any kind. diff --git a/tests/test_pmtypes.py b/tests/test_pmtypes.py index b27aa5dc..10b87c36 100644 --- a/tests/test_pmtypes.py +++ b/tests/test_pmtypes.py @@ -338,7 +338,6 @@ def my_generator(): inst3.ExtExtension = my_generator() self.assertEqual(inst2.ExtExtension, inst3.ExtExtension) - def test_mixed_content_is_ignored(self): xml1 = fromstring(b""" + + + """) # noqa: S320 + xml2 = fromstring(b""" + + + + """) # noqa: S320 + inst1 = xml_structure.ExtensionLocalValue([xml1]) + inst2 = xml_structure.ExtensionLocalValue([xml2]) + self.assertEqual(inst1, inst2) + self.assertTrue(inst1 == inst1) + self.assertFalse(inst1 != inst1) + self.assertTrue(inst1 == inst2) + self.assertFalse(inst1 != inst2) + + xml2 = fromstring(b""" + + + + """) # noqa: S320 + inst2 = xml_structure.ExtensionLocalValue([xml2]) + self.assertNotEqual(inst1, inst2) + self.assertFalse(inst1 == inst2) + self.assertTrue(inst1 != inst2)