Skip to content

Commit

Permalink
Ignore non-significant digits when computing differences between Deci…
Browse files Browse the repository at this point in the history
…mal fields
  • Loading branch information
spanezz committed Aug 1, 2022
1 parent fe2cbe6 commit 11cf909
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# New in version UNRELEASED

* Generate `dati_riepilogo` with properly set `natura` (#27)
* Ignore non-significant digits when computing differences between Decimal fields

# New in version 0.1.5

Expand Down
17 changes: 17 additions & 0 deletions a38/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,23 @@ def validate(self, validation, value):
"'{}' should be no more than {} digits long".format(xml_value, self.max_length))
return value

def diff(self, res: Diff, first: Optional[T], second: Optional[T]):
"""
Report to res if there are differences between values first and second
"""
first = self.clean_value(first)
second = self.clean_value(second)
has_first = self.has_value(first)
has_second = self.has_value(second)
if not has_first and not has_second:
return
elif has_first and not has_second:
res.add_only_first(self, first)
elif not has_first and has_second:
res.add_only_second(self, second)
elif first.quantize(self.quantize_sample) != second.quantize(self.quantize_sample):
res.add_different(self, first, second)


class StringField(ChoicesField[str]):
def __init__(self, length=None, min_length=None, max_length=None, **kw):
Expand Down
3 changes: 2 additions & 1 deletion tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,8 @@ def test_to_python(self):
def test_diff(self):
f = self.get_field()
self.assert_diff_empty(f, Decimal("1.0"), "1.0")
self.assert_field_diff(f, "1.0001", "1.0002")
self.assert_diff_empty(f, "1.0001", "1.0002")
self.assert_field_diff(f, "1.1", "1.2")

def test_xml(self):
f = self.get_field(null=True)
Expand Down

0 comments on commit 11cf909

Please sign in to comment.