sharpy icon indicating copy to clipboard operation
sharpy copied to clipboard

TypeError

Open jaivikram opened this issue 14 years ago • 3 comments

I was running the included tests and got

File "/code/appenv/src/sharpy/sharpy/product.py", line 477, in charge each_amount = Decimal(each_amount) File "/usr/lib/python2.6/decimal.py", line 649, in new "First convert the float to a string") TypeError: Cannot convert float to Decimal. First convert the float to a string

*I understand the error and fixed it for running my tests. Just sharing it so that fix becomes a part of the repo.

jaivikram avatar Dec 17 '11 07:12 jaivikram

I think the real problem is in the file

/code/appenv/src/sharpy/tests/product_tests.py

The line https://github.com/Saaspire/sharpy/blob/master/tests/product_tests.py#L477 the method signature is

def assert_charged(self, code, each_amount, quantity=None, description=None):

and within the method the variable 'each_amount' is converted to decimal using

Decimal(each_amount)

which is correct if each_amount is already a string or a decimal or an int

however, that line fails if each_amount is a float

and the test methods from line line 513 call it will different type of arguments for the variable each_amount - viz int, float, decimal

@clear_users
def test_add_charge(self):
    self.assert_charged(code='TEST-CHARGE', each_amount=1, quantity=1)

@clear_users
def test_add_float_charge(self):
    self.assert_charged(code='TEST-CHARGE', each_amount=2.3, quantity=2)

@clear_users
def test_add_float_charge(self):
    self.assert_charged(code='TEST-CHARGE', each_amount=2.3, quantity=2)

@clear_users
def test_add_decimal_charge(self):
    self.assert_charged(code='TEST-CHARGE', each_amount=Decimal('2.3'), quantity=3)

and the actual test assert_charge fails when the argument is of type float

I think within the method "assert_charge" adding the following two lines in the begining should fix everything:

if isinstance(each_amount, (float,)):
    each_amount = Decimal( "%.2f" % each_amount )

what do you think sean?

jaivikram avatar Dec 17 '11 07:12 jaivikram

hmmm....

the same thing is done at

File "/code/appenv/src/sharpy/tests/product_tests.py", line 595, in test_add_one_time_invoice_with_description 'each_amount': Decimal(5.23),

a direct float->decimal conversion and it is dawning to me that all these tests must have been running just fine for you. Which ultimately leads me to think more on why is happening with me only.

I am running Py2.6.5 . Which version did you run the tests on?

jaivikram avatar Dec 17 '11 07:12 jaivikram

The tests had been run against Python 2.7 where float -> decimal conversions are handled properly (docs).

If you send a pull request with the fixes to get the tests running properly under Python 2.6 I'll happily accept it. Otherwise, thanks for the report and I'll get to it when I can :)

SeanOC avatar Dec 17 '11 16:12 SeanOC