예제 #1
0
 def test_precision(self):
     with self.connect() as connection:
         with connection.cursor() as cursor:
             for value, precision in (
                 (1, 1),
                 (1234, 4),
                 ('12.34', 4),
                 (12.34, 4),
                 ('123.4567', 14),
                 (123.4567, 14),
                 ('1234567890.123456789', 38),
                 (1234567890.123456789, 38),
             ):
                 wrapper = ctds.SqlDecimal(value, precision=precision)
                 self.assertEqual(id(value), id(wrapper.value))
                 self.assertEqual(wrapper.size, -1)
                 row = self.parameter_type(cursor, wrapper)
                 self.assertEqual(row.Type,
                                  'decimal' if value is not None else None)
                 self.assertEqual(row.Precision, precision)
                 self.assertEqual(row.Scale, 0)
                 self.assertEqual(
                     row.Value,
                     # scale defaults to 0, which will truncate any fractional part.
                     Decimal(str(round(float(value), 0))))
                 self.assertEqual(row.Precision, precision)
                 self.assertEqual(row.Scale, 0)
예제 #2
0
    def test_decimal_output(self):
        with self.connect() as connection:
            with connection.cursor() as cursor:
                sproc = self.test_decimal_output.__name__
                with self.stored_procedure(
                        cursor, sproc, '''
                        @pString VARCHAR(MAX),
                        @pDecimalOut DECIMAL(15,5) OUTPUT
                    AS
                        SET @pDecimalOut = CONVERT(DECIMAL(15,5), @pString);
                    '''):
                    for value in ('1230.456', '123456789.1234', '123',
                                  '1234567890.123456789123456789', None):
                        inputs = (
                            value,
                            ctds.Parameter(ctds.SqlDecimal(value,
                                                           precision=15,
                                                           scale=5),
                                           output=True),
                        )

                        outputs = cursor.callproc(sproc, inputs)
                        self.assertEqual(id(outputs[0]), id(inputs[0]))
                        if outputs[0] is not None:
                            self.assertEqual(
                                Decimal(outputs[0]).quantize(
                                    Decimal('.00001')), outputs[1])
                        else:
                            self.assertEqual(None, outputs[1])
예제 #3
0
    def test_decimal(self):
        with self.connect() as connection:
            with connection.cursor() as cursor:
                for value, kwargs, expected in (
                    (1.234, {
                        'scale': 1
                    }, Decimal('1.2')),
                    (1234.123456789, {
                        'scale': 4
                    }, Decimal('1234.1234')),
                    (None, {}, None),
                ):
                    decimal = ctds.SqlDecimal(value, **kwargs)
                    self.assertEqual(id(decimal.value), id(value))
                    self.assertEqual(decimal.size, -1)
                    self.assertEqual(decimal.tdstype, ctds.DECIMAL)

                    cursor.execute(
                        '''
                        SELECT :0, CONVERT(VARCHAR(50), :0)
                        ''', (decimal, ))
                    self.assertEqual(
                        [tuple(row) for row in cursor.fetchall()],
                        [(expected,
                          None if expected is None else str(expected))])
예제 #4
0
 def test_precision_valueerror(self):
     for precision in (0, 39):
         try:
             ctds.SqlDecimal(None, precision=precision)
         except ValueError as ex:
             self.assertEqual(str(ex),
                              'invalid precision: {0}'.format(precision))
         else:
             self.fail('ctds.SqlDecimal did not fail as expected'
                       )  # pragma: nocover
예제 #5
0
 def test_scale_valueerror(self):
     for precision, scale in (
         (38, -1),
         (10, 11),
     ):
         try:
             ctds.SqlDecimal(None, precision=precision, scale=scale)
         except ValueError as ex:
             self.assertEqual(str(ex), 'invalid scale: {0}'.format(scale))
         else:
             self.fail('ctds.SqlDecimal did not fail as expected'
                       )  # pragma: nocover
예제 #6
0
 def test_wrapper(self):
     with self.connect() as connection:
         with connection.cursor() as cursor:
             for value in ('12.34', int_(1234), long_(1234),
                           unicode_('1234'), Decimal('12.34'), 12.34, None):
                 wrapper = ctds.SqlDecimal(value)
                 self.assertEqual(id(value), id(wrapper.value))
                 self.assertEqual(wrapper.size, -1)
                 row = self.parameter_type(cursor, wrapper)
                 self.assertEqual(row.Type,
                                  'decimal' if value is not None else None)
                 self.assertEqual(
                     row.Value,
                     # scale defaults to 0, which will truncate any fractional part.
                     Decimal(str(round(float(value), 0)))
                     if value is not None else None)
                 self.assertEqual(row.Precision,
                                  18 if value is not None else None)
                 self.assertEqual(row.Scale,
                                  0 if value is not None else None)
예제 #7
0
 def test_scale(self):
     with self.connect() as connection:
         with connection.cursor() as cursor:
             for value, scale in (
                 (1234, 0),
                 ('12.34', 2),
                 (12.34, 2),
                 ('.1234567', 15),
                 (.1234567, 15),
             ):
                 wrapper = ctds.SqlDecimal(value, precision=38, scale=scale)
                 self.assertEqual(id(value), id(wrapper.value))
                 self.assertEqual(wrapper.size, -1)
                 row = self.parameter_type(cursor, wrapper)
                 self.assertEqual(row.Type, 'decimal')
                 self.assertEqual(row.Precision, 38)
                 self.assertEqual(row.Scale, scale)
                 self.assertEqual(row.Value,
                                  Decimal(str(round(float(value), scale))))
                 self.assertEqual(row.Precision, 38)
                 self.assertEqual(row.Scale, scale)