Exemplo n.º 1
0
class TestDecimalField(unittest.TestCase):
    def setUp(self):
        self.field = DecimalField()

    def test_should_convert_to_string_when_storing(self):
        value = self.field.to_mongo(Decimal('3.14'))
        assert value == '3.14'
        value = self.field.to_mongo(Decimal('3.1415926535897932384'))
        assert value == '3.1415926535897932384'

    def test_should_restore_to_decimal(self):
        value = self.field.from_mongo('3.14')
        assert value == Decimal('3.14')
        value = self.field.from_mongo('3.1415926535897932384')
        assert value == Decimal('3.1415926535897932384')

    def test_validate_non_compatible_decimals(self):
        wrong_values = [
            'string',
            '1/2',
        ]
        for value in wrong_values:
            self.assertRaises(ValidationError, self.field.validate, value)

    def test_validate_regular_url(self):
        valid_urls = [
            1,
            50.51,
            Decimal('1'),
            Decimal('50.51'),
        ]
        for url in valid_urls:
            self.assertIsNone(self.field.validate(url))
Exemplo n.º 2
0
class TestDecimalField(unittest.TestCase):
    def setUp(self):
        self.field = DecimalField()

    def test_should_convert_to_string_when_storing(self):
        value = self.field.to_mongo(Decimal('3.14'))
        assert value == '3.14'
        value = self.field.to_mongo(Decimal('3.1415926535897932384'))
        assert value == '3.1415926535897932384'

    def test_should_restore_to_decimal(self):
        value = self.field.from_mongo('3.14')
        assert value == Decimal('3.14')
        value = self.field.from_mongo('3.1415926535897932384')
        assert value == Decimal('3.1415926535897932384')

    def test_validate_non_compatible_decimals(self):
        wrong_values = [
            'string',
            '1/2',
        ]
        for value in wrong_values:
            self.assertRaises(ValidationError, self.field.validate, value)

    def test_validate_regular_url(self):
        valid_urls = [
            1,
            50.51,
            Decimal('1'),
            Decimal('50.51'),
        ]
        for url in valid_urls:
            self.assertIsNone(self.field.validate(url))