def test_valid(self): dt = datetime.datetime(2016, 1, 1).replace(tzinfo=datetime.timezone.utc) self.assertEqual( datetime_to_cdms_datetime(dt), '/Date(1451606400000)/' )
def populate_data(service, data={}, guid=None): """ Returns a dict of cdms data to be used as return value of mocked cdms api calls. This data will always include: - ModifiedOn - CreatedOn - {service}Id: id of the record which are autogenerated if not specified. datatime values are converted to cdms ones automatically. Examples of how to use it: 1. with defaults: populate_data('MyService'): will return: { 'ModifiedOn': ..., 'CreatedOn': ..., 'MyServiceId': 'cdms-pk' } 2. with overriding values: poplate_data( 'MyService', data={ 'CreatedOn': datetime.now(), 'ExtraField': 'extra' }, guid='my-id' ) will return: { 'ModifiedOn': ..., 'CreatedOn': ..., # cdms equivalent of the one provided 'MyServiceId': 'my-id', 'ExtraField'': 'extra' } """ _data = dict(data) _data['ModifiedOn'] = _data.get('ModifiedOn', timezone.now()) _data['CreatedOn'] = _data.get('CreatedOn', timezone.now()) id_key = '{0}Id'.format(service) _data[id_key] = guid or _data.get(id_key, 'cdms-pk') for k, v in _data.items(): if isinstance(v, datetime.datetime): _data[k] = datetime_to_cdms_datetime(v) return _data
def test_None(self): self.assertEqual(datetime_to_cdms_datetime(None), None)