Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_to_struct_datetime():
class Event(models.Base):
start = fields.DateTimeField()
event = Event()
event.start = '2013-05-06 12:03:34'
pattern = {
'start': '2013-05-06T12:03:34'
}
assert pattern == event.to_struct()
def test_datetime_field_is_none():
"""If field nullable, dateutil raises error"""
datetime_field = fields.DateTimeField()
assert datetime_field.parse_value(None) is None
default_name = "John Doe"
default_height = 1.70
default_hobbies = ["eating", "reading"]
default_last_ate = datetime.time()
default_birthday = datetime.date.today()
default_time_of_death = datetime.datetime.now()
class Person(models.Base):
name = fields.StringField(default=default_name)
age = fields.IntField(default=default_age)
height = fields.FloatField(default=default_height)
job = fields.EmbeddedField(Job, default=default_job)
hobbies = fields.ListField(items_types=str, default=default_hobbies)
last_ate = fields.TimeField(default=default_last_ate)
birthday = fields.DateField(default=default_birthday)
time_of_death = fields.DateTimeField(default=default_time_of_death)
p = Person()
assert p.name == default_name
assert p.age == default_age
assert p.height == default_height
assert p.hobbies == default_hobbies
assert p.job == default_job
assert p.last_ate == default_last_ate
assert p.birthday == default_birthday
assert p.time_of_death == default_time_of_death
def test_datetime_field_to_struct_with_tz():
field = fields.DateTimeField()
tt = datetime.datetime(2014, 5, 7, 12, 45, 56, tzinfo=_TestCet())
assert '2014-05-07T12:45:56+02:00' == field.to_struct(tt)
tt = datetime.datetime(2014, 5, 7, 12, 45, 56, tzinfo=_TestUtc())
assert '2014-05-07T12:45:56+00:00' == field.to_struct(tt)
def test_datetime_field_parse_value():
field = fields.DateTimeField()
assert (
datetime.datetime(2014, 4, 21, 12, 45, 56) ==
field.parse_value('2014-04-21T12:45:56')
)
assert (
datetime.datetime(
2014, 4, 21, 12, 45, 56, tzinfo=tzoffset(None, 7200)) ==
field.parse_value('2014-04-21T12:45:56+02:00')
)
with pytest.raises(ValueError):
field.parse_value('not a datetime')
def test_schema_for_list_and_primitives(self):
class Event(models.Base):
time = fields.TimeField()
date = fields.DateField()
end = fields.DateTimeField()
class Person(models.Base):
names = fields.ListField([str, int, Event])
schema = Person.to_json_schema()
pattern = get_fixture('schema_with_list.json')
self.assertTrue(compare_schemas(pattern, schema))
def __init__(self, str_format=None, *args, **kwargs):
"""Init.
:param str str_format: Format to cast datetime to (if `None` - casting
to ISO 8601 format).
"""
self.str_format = str_format
super(DateTimeField, self).__init__(*args, **kwargs)