Esempio n. 1
0
 def test_as_string(self):
     self.assertEqual(fields.as_string(None), 'None')
     self.assertEqual(fields.as_string(42), '42')
     self.assertEqual(fields.as_string(3.141592), '3.141592')
     self.assertEqual(fields.as_string('Álvaro'), 'Álvaro')
     self.assertEqual(fields.as_string('Álvaro'.encode('utf-8')),
                      'Álvaro'.encode('utf-8'))
Esempio n. 2
0
 def test_as_string(self):
     self.assertEqual(fields.as_string(None), 'None')
     self.assertEqual(fields.as_string(42), '42')
     self.assertEqual(fields.as_string(3.141592), '3.141592')
     self.assertEqual(fields.as_string('Álvaro'), 'Álvaro')
     self.assertEqual(fields.as_string('Álvaro'.encode('utf-8')),
                      'Álvaro'.encode('utf-8'))
Esempio n. 3
0
    def test_as_string(self):
        self.assertEqual(fields.as_string(None), "None")
        self.assertEqual(fields.as_string(42), "42")
        self.assertEqual(fields.as_string(3.141592), "3.141592")
        self.assertEqual(fields.as_string("Álvaro"), "Álvaro")

        with self.assertRaises(ValueError) as exception_context:
            fields.as_string("Álvaro".encode("utf-8"))
        self.assertEqual(exception_context.exception.args[0], "Binary is not supported")
Esempio n. 4
0
    def process_row(self, row):
        for index, value in enumerate(row):
            if index in self._skip:
                continue
            self.check_type(index, value)

            value_length = len(fields.as_string(value))
            self.min_sizes[index] = min(self.min_sizes[index], value_length)
            self.max_sizes[index] = max(self.max_sizes[index], value_length)

            if self.choices[index] is not None:
                if len(self.choices[index]) > self.max_choices:
                    self.choices[index] = None
                else:
                    self.choices[index].add(value)
Esempio n. 5
0
    def test_as_string(self):
        self.assertEqual(fields.as_string(None), 'None')
        self.assertEqual(fields.as_string(42), '42')
        self.assertEqual(fields.as_string(3.141592), '3.141592')
        self.assertEqual(fields.as_string('Álvaro'), 'Álvaro')

        with self.assertRaises(ValueError) as exception_context:
            fields.as_string('Álvaro'.encode('utf-8'))
        self.assertEqual(exception_context.exception.args[0],
                         'Binary is not supported')
Esempio n. 6
0
    def deserialize(cls, value: Date, *args, **kwargs) -> date:
        if isinstance(value, datetime):
            return value.date()

        if isinstance(value, date):
            return value

        if isinstance(value, (int, float)):
            return datetime.fromtimestamp(value).date()

        value = fields.Field.deserialize(value)
        as_str: str = fields.as_string(value)
        as_str = as_str[:
                        10]  # in ISO format datetime, discard chars after date
        for date_format in cls.INPUT_FORMATS:
            try:
                dt_object = datetime.strptime(as_str, date_format)
            except ValueError:
                continue
            return dt_object.date()

        raise ValueError(f"Cannot parse value as date: {value}")