[Python-checkins] python/nondist/sandbox/decimal test_money.py, NONE, 1.1

facundobatista at users.sourceforge.net facundobatista at users.sourceforge.net
Wed Jun 30 20:05:32 EDT 2004


Update of /cvsroot/python/python/nondist/sandbox/decimal
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv25552

Added Files:
	test_money.py 
Log Message:
Draft tests for Money class.

--- NEW FILE: test_money.py ---
''' Unit tests for Money
'''

__copyright__ = 'Python Software Foundation'
__author__ = 'Facundo Batista'
__version__ = 0, 1

import unittest
from money import *
import Decimal


class SonOfMoney(Money):
    '''Subclass of Money to change its visual elements.'''
    decimalSeparator = '#'
    currencySymbol = 'AR$'
    thousandSeparator = ';'
    decimal_positions = 2
    currSymbolStyle = OUTSIDE
    minusStyle = DASH


class TestRepresentations(unittest.TestCase):
    '''Unit test for string representations of Money.'''

    def test_string_with_decimals(self):
        '''String with decimals.'''
        m = Money('23.72')
        self.assertEqual(str(m), '$23.72')
        som = SonOfMoney('23.72')
        self.assertEqual(str(som), 'AR$23#72')
        
    def test_int(self):
        '''Integer.'''
        m = Money(15)
        self.assertEqual(str(m), '$15.00')
        som = SonOfMoney(15)
        self.assertEqual(str(som), 'AR$15#00')

    def test_thousand_separator(self):
        '''Thousand separator.'''
        m = Money(1234567)
        self.assertEqual(str(m), '$1234567.00')
        som = SonOfMoney(1234567)
        self.assertEqual(str(som), 'AR$1;234;567#00')

    def test_negative(self):
        '''Negative with values set inheritating.'''
        m = Money('-23.72')
        self.assertEqual(str(m), '$-23.72')
        som = SonOfMoney('-23.72')
        self.assertEqual(str(som), 'AR$-23#72')

    def test_negative_currSymbol_inside(self):
        '''Negative with currency symbol inside.'''
        m = Money('-23.72')
        som = SonOfMoney('-23.72')
        m.currSymbolStyle = INSIDE
        som.currSymbolStyle = INSIDE
        self.assertEqual(str(m), '-$23.72')
        self.assertEqual(str(som), '-AR$23#72')
        
    def test_negative_minus_parenth(self):
        '''Negative with parentheses instead of dash.'''
        m = Money('-23.72')
        som = SonOfMoney('-23.72')
        m.minusStyle = PARENTHESES
        som.minusStyle = PARENTHESES
        self.assertEqual(str(m), '$(23.72)')
        self.assertEqual(str(som), 'AR$(23#72)')

    def test_negative_inside_parenth(self):
        '''Negative with parentheses and currency symbol inside.'''
        m = Money('-23.72')
        som = SonOfMoney('-23.72')
        m.currSymbolStyle = INSIDE
        som.currSymbolStyle = INSIDE
        m.minusStyle = PARENTHESES
        som.minusStyle = PARENTHESES
        self.assertEqual(str(m), '($23.72)')
        self.assertEqual(str(som), '(AR$23#72)')
        
    def test__repr__(self):
        '''Test representation.'''
        m = Money('25.172')
        self.assertEqual(repr(m), "Money('25.172')")

class TestBehaviour(unittest.TestCase):
    '''Unit test for example behaviours.'''

    def test_Mark_McEahern(self):
        '''Generic test case provided by Mark to comp.lang.python.'''
	cost = Money('5.99')
	percentDiscount = Decimal.Decimal('0.1')
	months = 3
	subTotal = cost * months
	discount = subTotal * percentDiscount
	total = subTotal - discount
	self.assertEqual(total, Money('16.17'))

if __name__ == '__main__':
    unittest.main()



def test_main(verbose=None):
    from test import test_sets
    test_support.run_unittest(
        TestRepresentations,
        TestBehaviour,
    )
    test_support.run_doctest(test_sets, verbose)

if __name__ == "__main__":
    test_main(verbose=True)




More information about the Python-checkins mailing list