def test_default(self): dt = datetime.strptime('2013-10-07T22:58:40', '%Y-%m-%dT%H:%M:%S') field = DatetimeField(required=True, default=dt) field.validate() self.assertEqual(field.to_python(), dt) self.assertEqual(field.to_native(), '2013-10-07T22:58:40') field = DatetimeField(required=True, default='2013-10-07T22:58:40') field.validate() self.assertEqual(field.to_python(), dt) self.assertEqual(field.to_native(), '2013-10-07T22:58:40')
def test_set_value(self): dt = datetime.strptime('2013-10-07T22:58:40', '%Y-%m-%dT%H:%M:%S') field = DatetimeField(required=True) field.set_value(dt) field.validate() self.assertIsInstance(field.to_python(), datetime) self.assertEqual(field.to_python(), dt) self.assertEqual(field.to_native(), '2013-10-07T22:58:40')
def test_formats(self): field = DatetimeField(required=True, formats=['%d.%m.%Y %H:%M:%S']) field.set_value('07.10.2013 20:15:23') field.validate() self.assertEqual( field.to_python(), datetime.strptime('2013-10-07T20:15:23', '%Y-%m-%dT%H:%M:%S')) self.assertEqual(field.to_native(), '07.10.2013 20:15:23')
def test_set_value_string(self): field = DatetimeField(required=True) field.set_value('2013-10-07T20:15:23') field.validate() self.assertEqual( field.to_python(), datetime.strptime('2013-10-07T20:15:23', '%Y-%m-%dT%H:%M:%S')) self.assertEqual(field.to_native(), '2013-10-07T20:15:23')
class TestFlatSerializer(Serializer): _type = TypeField('test_object') id = IntegerField(required=True, identity=True) name = StringField(required=True) street = StringField(required=False, on_null=HIDE_FIELD) uuid_var = UUIDField(required=True) maxmin = IntegerField(max_value=10, min_value=6, required=True) datetime_var = DatetimeField(required=True) date_var = DateField(required=True) time_var = TimeField(required=True) haus = StringField(required=True, map_field='house') url = UrlField(required=True, base='http://www.base.com', default='api') action = StringField(required=False, action_field=True)
def test_hide_on_null(self): field = DatetimeField(required=False, on_null=HIDE_FIELD) self.assertRaises(IgnoreField, field.to_native) self.assertIsNone(field.to_python())
def test_validate_raises(self): field = DatetimeField(required=True) field.set_value('datetime') self.assertRaises(SerializerFieldValueError, field.validate)
def test_serialize_to(self): field = DatetimeField(required=True, formats=['%d.%m.%Y %H:%M:%S'], serialize_to='%Y-%m-%dT%H:%M:%S.%f') field.set_value('07.10.2013 20:15:23') field.validate() self.assertEqual(field.to_python(), datetime.strptime('2013-10-07T20:15:23', '%Y-%m-%dT%H:%M:%S')) self.assertEqual(field.to_native(), '2013-10-07T20:15:23.000000')
def test_set_value_string(self): field = DatetimeField(required=True) field.set_value('2013-10-07T20:15:23') field.validate() self.assertEqual(field.to_python(), datetime.strptime('2013-10-07T20:15:23', '%Y-%m-%dT%H:%M:%S')) self.assertEqual(field.to_native(), '2013-10-07T20:15:23')