Skip to content

Commit

Permalink
fix instance comparison of extensions (Draegerwerk#310)
Browse files Browse the repository at this point in the history
<!--- Provide a general summary of your changes in the title above -->
<!--- Link the corresponding issues after you created the pull request
-->

## Types of changes
<!--- What types of changes does your code introduce? Put an `x` in all
the boxes that apply: -->
- [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:
<!--- Go over all the following points, and put an `x` in all the boxes
that apply. -->
<!--- If you're unsure about any of these, don't hesitate to ask. We're
here to help! -->
- [x] I have updated the [changelog](../CHANGELOG.md) accordingly.
- [x] I have added tests to cover my changes.
  • Loading branch information
leon1995 authored Jan 5, 2024
1 parent f6df527 commit bbdf111
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion src/sdc11073/xml_types/xml_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
30 changes: 29 additions & 1 deletion tests/test_pmtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ext:Extension xmlns:ext="http://standards.ieee.org/downloads/11073/11073-10207-2017/extension"
Expand All @@ -356,3 +355,32 @@ def test_mixed_content_is_ignored(self):
inst1 = xml_structure.ExtensionLocalValue([xml1])
inst2 = xml_structure.ExtensionLocalValue([xml2])
self.assertEqual(inst1, inst2)

def test_operators_of_extension_local_value(self):
xml1 = fromstring(b"""
<ext:Extension xmlns:ext="__ExtensionPoint__">
<foo someattr="somevalue"/>
</ext:Extension>
""") # noqa: S320
xml2 = fromstring(b"""
<ext:Extension xmlns:ext="__ExtensionPoint__">
<foo someattr="somevalue"/>
</ext:Extension>
""") # 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"""
<ext:Extension xmlns:ext="__ExtensionPoint__">
<bar anotherattr="differentvalue"/>
</ext:Extension>
""") # noqa: S320
inst2 = xml_structure.ExtensionLocalValue([xml2])
self.assertNotEqual(inst1, inst2)
self.assertFalse(inst1 == inst2)
self.assertTrue(inst1 != inst2)

0 comments on commit bbdf111

Please sign in to comment.