Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
'contact': {
'phone': '082345234',
'address': {
'country': 'uk',
'postcode': 'sg1 3ab',
}
},
'signup_date': '2014-06-12T19:06:02'
}
serializer = UserSerializer()
result = serializer.marshal(data)
self.assertTrue(isinstance(result, User))
self.assertEqual(result.name, 'bob')
self.assertEqual(result.signup_date, datetime(2014, 6, 12, 19, 6, 2, tzinfo=Utc()))
contact_details = result.contact_details
self.assertTrue(isinstance(contact_details, ContactDetail))
self.assertEqual(contact_details.phone, '082345234')
address = result.contact_details.address
self.assertTrue(isinstance(address, Address))
self.assertEqual(address.country, 'uk')
self.assertEqual(address.postcode, 'sg1 3ab')
self.assertIsNone(result.id)
self.session.add(result)
self.session.commit()
self.assertIsNotNone(result.id)
def now():
"""Return datetime instance for current time in UTC.
"""
return datetime.datetime.utcnow().replace(tzinfo=iso8601.Utc())
user = Field(Nested(Inner))
status = Field(Integer())
updated_at = Field(DateTime())
cost = Field(Decimal(precision=2))
data = {'user': {
'name': 'Bob',
'email': 'bob@bobscaravans.com',
'alternative_emails': [
'bigbob@gmail.com',
'bobscaravansltd@btinternet.com'
],
'created_at': date(2014, 4, 8),
},
'status': 200,
'updated_at': datetime(2014, 4, 8, 6, 12, 43, tzinfo=Utc()),
'cost': decimal.Decimal("3.50"),
}
result = Outer().serialize(data)
self.assertEquals(result,
{'user': {
'name': 'Bob',
'email': 'bob@bobscaravans.com',
'alternative_emails': [
'bigbob@gmail.com',
'bobscaravansltd@btinternet.com'
],
'signup_date': '2014-04-08',
},
'status': 200,
def test_is_valid_datetime_pipe():
"""test piping data through is_valid_datetime.
"""
field = DateTime(name='test')
session = Session(field, 'test', {})
with pytest.raises(FieldInvalid):
is_valid_datetime(session)
session.data = '2016-02-29T12:00:12Z'
assert is_valid_datetime(session) == \
datetime(2016, 2, 29, 12, 0, 12, tzinfo=Utc())
session.data = '2015-06-29T08:00:12Z'
assert is_valid_datetime(session) == \
datetime(2015, 6, 29, 8, 0, 12, tzinfo=Utc())
def test_coercable_type_valid_value(self, type_registry, cls):
value = datetime(2012, 1, 1, 2, 3, tzinfo=iso8601.iso8601.Utc())
str_value = '2012-01-01T02:03:00+00:00'
instance = cls(bar=str_value)
data = type_registry.object_to_dict(instance, for_db=True)
assert data['bar'] == str_value
obj = type_registry.dict_to_object(data)
assert obj.bar == value
def datetime_or_none(dt):
"""Validate a datetime or None value."""
if dt is None:
return None
elif isinstance(dt, datetime.datetime):
if dt.utcoffset() is None:
# NOTE(danms): Legacy objects from sqlalchemy are stored in UTC,
# but are returned without a timezone attached.
# As a transitional aid, assume a tz-naive object is in UTC.
return dt.replace(tzinfo=iso8601.iso8601.Utc())
else:
return dt
raise ValueError(_("A datetime.datetime is required here"))
def issue_fields(issue):
result = {}
# Add basic fields
for field_name, dotted_field in as_is_fields.items():
result[field_name] = retrieve_dotnotation_field(issue, dotted_field)
# Add user transitions
for field_name, field_value in issue['__user_transitions'].items():
result[field_name] = field_value
# Calculate # of days in current status
result['days_in_current_status'] = (datetime.datetime.now(iso8601.Utc()) - parse_iso(issue['__transitions'][-1]['when'])).total_seconds() / (24 * 3600)
# Add character lenghts for some fields
description_field = retrieve_dotnotation_field(issue, 'fields.description')
summary_field = retrieve_dotnotation_field(issue, 'fields.summary')
result['description_length'] = len(description_field) if description_field else None
result['summary_length'] = len(summary_field) if summary_field else None
return result
def _get_local_remote_times(self, local, remote):
assert isinstance(local, File)
assert isinstance(remote, RemoteFile)
local_time = local.date_modified.replace(tzinfo=iso8601.iso8601.Utc())
# NOTE; waterbutler does NOT update time when a file or folder is RENAMED.
# thus cannot accurately determine when file/folder was renamed.
# thus, going to have to go with local is pretty much always newer.
# NOTE: based on above note, it is a better idea to go with the .locally_renamed idea.
remote_time = remote.last_modified
return local_time, remote_time