def test_cast_date_to_string(self):
     self.assertEqual(
         cast_to_string(datetime.date(2019, 8, 28),
                        DateType(),
                        options=BASE_OPTIONS),
         '2019-08-28',
     )
 def test_cast_row_to_string(self):
     self.assertEqual(
         cast_to_string(
             Row(
                 a=collections.OrderedDict([('value', None), ('b', {
                     'c': 7
                 })]),
                 b=None,
                 c=True,
                 d=5.2,
             ),
             StructType([
                 StructField(
                     'a',
                     MapType(
                         StringType(),
                         MapType(StringType(), LongType(), True),
                         True,
                     ),
                     True,
                 ),
                 StructField('b', LongType(), True),
                 StructField('c', BooleanType(), True),
                 StructField('d', DoubleType(), True),
             ]),
             options=BASE_OPTIONS,
         ),
         '[[value ->, b -> [c -> 7]],, true, 5.2]',
     )
 def test_cast_array_to_string(self):
     self.assertEqual(
         cast_to_string(
             [[[1, None, 2], []]],
             ArrayType(ArrayType(ArrayType(IntegerType()))),
             options=BASE_OPTIONS,
         ),
         '[[[1,, 2], []]]',
     )
 def test_cast_timestamp_to_string(self):
     self.assertEqual(
         cast_to_string(
             datetime.datetime(2019, 8, 28, 13, 5, 0),
             TimestampType(),
             options=BASE_OPTIONS,
         ),
         '2019-08-28 13:05:00',
     )
 def test_cast_map_to_string(self):
     self.assertEqual(
         cast_to_string(
             {
                 True:
                 collections.OrderedDict([('one', 1), ('nothing', None),
                                          ('three', 3)])
             },
             MapType(BooleanType(), MapType(StringType(), IntegerType())),
             options=BASE_OPTIONS,
         ),
         '[true -> [one -> 1, nothing ->, three -> 3]]',
     )
Esempio n. 6
0
 def preformat_cell(self, value, field):
     if value is None:
         value = self.nullValue
     else:
         value = cast_to_string(value,
                                from_type=field.dataType,
                                options=self.options)
     if self.ignoreLeadingWhiteSpace:
         value = value.rstrip()
     if self.ignoreTrailingWhiteSpace:
         value = value.lstrip()
     if value == '':
         return self.emptyValue
     return value
 def test_cast_null_to_string(self):
     self.assertEqual(
         cast_to_string(None, NullType(), options=BASE_OPTIONS), 'null')