Пример #1
0
 def test_enforce_timezone_returns_naive_datetime_with_utc_timezone(self):
     instance = fields.DateTimeField()
     value = datetime.datetime(2000, 1, 1, 10, 0, tzinfo=timezone.UTC())
     self.assertEqual(
         instance.enforce_timezone(value),
         datetime.datetime(2000, 1, 1, 10, 0)
     )
Пример #2
0
    def test_to_internal_value_returns_datetime_in_iso8601(self):
        instance = fields.DateTimeField()

        self.assertEqual(
            instance.to_internal_value('2000-01-01 10:00'),
            datetime.datetime(2000, 1, 1, 10, 0)
        )
Пример #3
0
 def test_to_representation_returns_value_in_iso8601(self):
     instance = fields.DateTimeField()
     value = datetime.datetime(2000, 1, 1, 10, 0, tzinfo=timezone.UTC())
     self.assertEqual(
         instance.to_representation(value),
         '2000-01-01T10:00:00Z'
     )
Пример #4
0
 def test_to_representation_with_none_output_format_returns_value(self):
     instance = fields.DateTimeField(format=None)
     self.assertEqual(
         instance.to_representation('2000-01-01T10:00:00Z'),
         '2000-01-01T10:00:00Z'
     )
Пример #5
0
 def test_to_representation_returns_none_for_none_type(self):
     instance = fields.DateTimeField()
     self.assertIsNone(instance.to_representation(None))
Пример #6
0
 def test_to_representation_returns_none_for_empty_string(self):
     instance = fields.DateTimeField()
     self.assertIsNone(instance.to_representation(''))
Пример #7
0
    def test_to_internal_value_with_format_raises_exc_for_invalid_value(self):
        instance = fields.DateTimeField(input_formats=("%Y-%m-%d %H:%M",))

        with self.assertRaises(ValidationError):
            instance.to_internal_value('2000-99-99 10:00')
Пример #8
0
    def test_to_internal_value_with_format_raises_exc_for_a_wrong_type(self):
        instance = fields.DateTimeField(input_formats=("%Y-%m-%d %H:%M",))

        with self.assertRaises(ValidationError):
            instance.to_internal_value(None)
Пример #9
0
 def test_to_internal_value_with_format_returns_datetime(self):
     instance = fields.DateTimeField(input_formats=("%Y-%m-%d %H:%M", ))
     self.assertEqual(
         instance.to_internal_value('2000-01-01 10:00'),
         datetime.datetime(2000, 1, 1, 10, 0)
     )
Пример #10
0
 def test_enforce_timezone_returns_naive_datetime(self):
     instance = fields.DateTimeField()
     value = datetime.datetime(2000, 1, 1, 10, 0)
     self.assertEqual(instance.enforce_timezone(value), value)
Пример #11
0
    def test_to_internal_value_raises_validation_error_with_a_wrong_type(self):
        instance = fields.DateTimeField()

        with self.assertRaises(ValidationError):
            instance.to_internal_value(None)
Пример #12
0
    def test_to_internal_value_raises_validation_error_for_date_type(self):
        instance = fields.DateTimeField()

        with self.assertRaises(ValidationError):
            instance.to_internal_value(datetime.date(2000, 1, 1))
Пример #13
0
 def test_to_internal_value_returns_datetime_with_enforce_datetime(self):
     instance = fields.DateTimeField()
     self.assertEqual(
         instance.to_internal_value(datetime.datetime(2000, 1, 1)),
         datetime.datetime(2000, 1, 1)
     )
Пример #14
0
 def test_enforce_timezone_returns_aware_datetime_with_utc_timezone(self):
     instance = fields.DateTimeField(default_timezone=timezone.UTC())
     self.assertEqual(
         instance.enforce_timezone(datetime.datetime(2000, 1, 1, 10, 0)),
         datetime.datetime(2000, 1, 1, 10, 0, tzinfo=timezone.UTC())
     )
Пример #15
0
    def test_to_internal_value_raises_validation_error_for_invalid_value(self):
        instance = fields.DateTimeField()

        with self.assertRaises(ValidationError):
            instance.to_internal_value('2000-99-99 10:00')
Пример #16
0
 def test_to_representation_returns_value_in_custom_format(self):
     instance = fields.DateTimeField(format="%Y-%m-%d %H:%M")
     self.assertEqual(
         instance.to_representation(datetime.datetime(2000, 1, 1, 10, 0)),
         '2000-01-01 10:00'
     )
Пример #17
0
    def test_run_validation_raises_validation_error_for_a_wrong_value(self):
        instance = fields.DateTimeField()

        with self.assertRaises(ValidationError):
            instance.run_validation('value')