示例#1
0
    def test_wrapper(self):
        with self.connect() as connection:
            with connection.cursor() as cursor:
                inputs = [
                    None,
                    unicode_(b'*', encoding='utf-8'),
                    unicode_(b'*', encoding='utf-8') * 4000,
                ]
                if self.nchars_supported:  # pragma: nobranch
                    inputs.extend([
                        unicode_(b'\xe3\x83\x9b', encoding='utf-8'),
                        unicode_(b' \xe3\x83\x9b ', encoding='utf-8'),
                        unicode_(b'\xe3\x83\x9b', encoding='utf-8') * 4000,
                    ])
                    if self.use_utf16 and self.UCS4_SUPPORTED:  # pragma: nobranch
                        inputs.extend([
                            unichr_(127802),
                            unichr_(127802) * 2000,
                        ])
                for value in inputs:
                    wrapper = ctds.SqlNVarChar(value)
                    self.assertEqual(
                        wrapper.size,
                        self.nvarchar_width(value) if value is not None else 1)
                    row = self.parameter_type(cursor, wrapper)
                    self.assertEqual(
                        row.Type, '{0}varchar'.format(
                            'n' if self.nchars_supported else '')
                        if value is not None else None)
                    self.assertEqual(row.Value, value)
                    self.assertEqual(row.MaxLength,
                                     (self.nvarchar_width(value) *
                                      (1 + int(self.nchars_supported)))
                                     if value is not None else None)

                    self.assertEqual(
                        repr(wrapper),
                        'ctds.SqlNVarChar({0!r}, size={1})'.format(
                            value if PY3 else
                            (value.encode('utf-8')
                             if value is not None else value), wrapper.size))
                    self.assertEqual(repr(wrapper), str(wrapper))

                for expected in (unicode_(b'*', encoding='utf-8') * 54321, ):
                    cursor.execute('SELECT :0', (ctds.SqlNVarChar(expected), ))
                    actual = cursor.fetchone()[0]
                    self.assertEqual(len(expected), len(actual))
                    self.assertEqual(expected, actual)
 def test_wrapper(self):
     with self.connect() as connection:
         with connection.cursor() as cursor:
             inputs = [
                 None,
                 unicode_(b'*', encoding='utf-8'),
                 unicode_(b'*', encoding='utf-8') * 4000,
             ]
             if self.nchars_supported:  # pragma: nobranch
                 inputs.extend([
                     unicode_(b'\xe3\x83\x9b', encoding='utf-8'),
                     unicode_(b' \xe3\x83\x9b ', encoding='utf-8'),
                     unicode_(b'\xe3\x83\x9b', encoding='utf-8') * 4000,
                 ])
                 if self.use_utf16:
                     inputs.extend([
                         unichr_(127802),
                         unichr_(127802) * 2000,
                     ])
             for value in inputs:
                 wrapper = ctds.SqlNVarChar(value)
                 self.assertEqual(
                     wrapper.size,
                     self.nvarchar_width(value) if value is not None else 1)
                 row = self.parameter_type(cursor, wrapper)
                 self.assertEqual(
                     row.Type, '{0}varchar'.format(
                         'n' if self.nchars_supported else '')
                     if value is not None else None)
                 self.assertEqual(row.Value, value)
                 self.assertEqual(row.MaxLength,
                                  (self.nvarchar_width(value) *
                                   (1 + int(self.nchars_supported)))
                                  if value is not None else None)
示例#3
0
    def test_size(self):
        with self.connect() as connection:
            with connection.cursor() as cursor:
                # The parameter_type method does not work with NVARCHAR(MAX) and
                # will fail with "Operand type clash: nvarchar(max) is incompatible with sql_variant"
                # Therefore, limit input sizes to 4000 or less.
                inputs = [
                    (None, 14),
                    (unicode_(b'*', encoding='utf-8'), 4000),
                    (unicode_(b'*', encoding='utf-8') * 4000, 4000),
                ]
                if self.nchars_supported:  # pragma: nobranch
                    inputs.extend([
                        (unicode_(b'\xe3\x83\x9b', encoding='utf-8'), 4000),
                        (unicode_(b'\xe3\x83\x9b', encoding='utf-8') * 4000,
                         4000),
                    ])
                for value, size in inputs:
                    wrapper = ctds.SqlNVarChar(value, size=size)
                    self.assertEqual(wrapper.size, size)
                    row = self.parameter_type(cursor, wrapper)
                    self.assertEqual(
                        row.Type, '{0}varchar'.format(
                            'n' if self.nchars_supported else '')
                        if value is not None else None)
                    if value is not None:
                        self.assertEqual(len(row.Value), min(size, len(value)))
                    self.assertEqual(
                        row.Value, value[:size] if value is not None else None)
                    self.assertEqual(
                        row.MaxLength,
                        size * (1 + int(self.nchars_supported))
                        if value is not None else None)

                # Manually check NVARCHAR(MAX) types due to sql_variant limitation.
                value = unicode_(
                    b'\xe3\x83\x9b' if self.nchars_supported else b'&',
                    encoding='utf-8') * 4001
                wrapper = ctds.SqlNVarChar(value)
                cursor.execute('SELECT DATALENGTH(:0)', (wrapper, ))
                row = cursor.fetchone()
                self.assertEqual(row[0],
                                 len(value) * (1 + int(self.nchars_supported)))
示例#4
0
    def test_nvarchar(self):
        with self.connect() as connection:
            with connection.cursor() as cursor:
                inputs = [
                    None,
                    unicode_(''),
                    unicode_(' '),
                    unicode_('one'),
                ]
                if self.nchars_supported:  # pragma: nobranch
                    inputs.extend([
                        unicode_(b'hola \xc5\x93 \xe3\x83\x9b',
                                 encoding='utf-8'),
                        unicode_(b'\xe3\x83\x9b', encoding='utf-8') * 4000
                    ])
                    if self.use_utf16 and self.UCS4_SUPPORTED:  # pragma: nobranch
                        inputs.extend([
                            unicode_('multi-byte utf-16 chars') +
                            unichr_(0x10000) + unichr_(0x1f33a),
                        ])
                for value in inputs:
                    for size in (None, 1, 3, 500):
                        kwargs = {}
                        if size is not None:
                            kwargs['size'] = size
                            expected_size = size
                        else:
                            expected_size = 1 if value is None else max(
                                1, self.nvarchar_width(value))

                        nvarchar = ctds.SqlNVarChar(value, **kwargs)
                        self.assertEqual(nvarchar.size, expected_size)
                        self.assertEqual(
                            nvarchar.tdstype, ctds.NVARCHAR
                            if self.nchars_supported else ctds.VARCHAR)

                        cursor.execute(
                            '''
                            SELECT :0
                            ''', (nvarchar, ))

                        # $future: fix this once supported by FreeTDS
                        # Currently FreeTDS (really the db-lib API) will
                        # turn empty string to NULL
                        if value == '' and self.use_sp_executesql:
                            value = None
                        self.assertEqual(
                            [tuple(row) for row in cursor.fetchall()],
                            [(value
                              if value is None else value[0:expected_size], )])