Commit 7da6cb94 authored by Yusei Tahara's avatar Yusei Tahara

Add decimal alignment option to quantity divergence tester.


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@26263 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 9e094092
......@@ -59,9 +59,9 @@ class QuantityDivergenceTester(PropertyDivergenceTester):
, PropertySheet.CategoryCore
, PropertySheet.DublinCore
, PropertySheet.DivergenceTester
, PropertySheet.DecimalOption
)
def explain(self, simulation_movement):
"""
This method returns a list of messages that contains
......@@ -109,6 +109,31 @@ class QuantityDivergenceTester(PropertyDivergenceTester):
message.decision_title = repr(d_quantity)
if delivery_ratio == 0 and quantity > 0:
return [message]
if d_quantity != quantity + d_error:
if not self.compare(d_quantity, quantity+d_error):
return [message]
return []
def compare(self, x, y):
if self.isDecimalAlignmentEnabled():
from decimal import (Decimal, ROUND_DOWN, ROUND_UP, ROUND_CEILING,
ROUND_FLOOR, ROUND_HALF_DOWN, ROUND_HALF_EVEN,
ROUND_HALF_UP)
# Python2.4 did not support ROUND_05UP yet.
rounding_option_dict = {'ROUND_DOWN':ROUND_DOWN,
'ROUND_UP':ROUND_UP,
'ROUND_CEILING':ROUND_CEILING,
'ROUND_FLOOR':ROUND_FLOOR,
'ROUND_HALF_DOWN':ROUND_HALF_DOWN,
'ROUND_HALF_EVEN':ROUND_HALF_EVEN,
'ROUND_HALF_UP':ROUND_HALF_UP}
rounding_option = rounding_option_dict.get(self.getDecimalRoundingOption(),
ROUND_DOWN)
return (Decimal(str(x)).quantize(Decimal(self.getDecimalExponent()),
rounding=rounding_option)
==
Decimal(str(y)).quantize(Decimal(self.getDecimalExponent()),
rounding=rounding_option))
else:
return x==y
##############################################################################
#
# Copyright (c) 2009 Nexedi KK, Nexedi SA and Contributors. All Rights Reserved.
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################
class DecimalOption:
"""
This property sheet provides properties which are required when using
python's decimal module.
"""
_properties = (
{ 'id' : 'decimal_alignment_enabled',
'description' : 'True if use decimal alignment.',
'type' : 'boolean',
'mode' : 'w' },
{ 'id' : 'decimal_rounding_option',
'description' : 'Rounding option.',
'type' : 'string',
'mode' : 'w' },
{ 'id' : 'decimal_exponent',
'description' : 'A example of decimal exponent. This value is used when rounds a number to a fixed exponent.',
'type' : 'string',
'mode' : 'w' },
)
......@@ -338,6 +338,32 @@ class TestDivergenceTester(TestPackingListMixin, ERP5TypeTestCase):
sequence_list.addSequenceString(sequence_string)
sequence_list.play(self, quiet=self.quiet)
def test_QuantityDivergenceTesterCompareMethod(self):
rule = self.portal.portal_rules.newContent(portal_type='Delivery Rule')
divergence_tester = rule.newContent(portal_type='Quantity Divergence Tester')
self.assert_(not divergence_tester.isDecimalAlignmentEnabled())
self.assertEqual(divergence_tester.compare(3.0, 3.001), False)
self.assertEqual(divergence_tester.compare(3.0, 3.0), True)
divergence_tester.setDecimalAlignmentEnabled(True)
divergence_tester.setDecimalRoundingOption('ROUND_DOWN')
divergence_tester.setDecimalExponent('0.01')
self.assertEqual(divergence_tester.compare(3.0, 3.001), True)
self.assertEqual(divergence_tester.compare(3.0, 3.0), True)
divergence_tester.setDecimalExponent('0.001')
self.assertEqual(divergence_tester.compare(3.0, 3.001), False)
divergence_tester.setDecimalRoundingOption('ROUND_UP')
divergence_tester.setDecimalExponent('0.01')
self.assertEqual(divergence_tester.compare(3.0, 3.001), False)
divergence_tester.setDecimalRoundingOption('ROUND_HALF_UP')
divergence_tester.setDecimalExponent('0.01')
self.assertEqual(divergence_tester.compare(3.0, 3.001), True)
def test_suite():
suite = unittest.TestSuite()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment