def test_error_strict(self): fields = { FieldName("version", alias="@version"): StringField(), FieldName("servers", alias="server"): [{ FieldName("type", alias="@type"): StringField(), "host": StringField(), "port": IntegerField() }] } serializer = SerializerFactory.create(None, fields, error_strict=False) raw_data = { "@version": "3.0", "server": { "id": "main", "@type": "main", "host": "127.0.0.1", "port": '80' } } load_data = serializer.load(raw_data) self.assertEqual(len(serializer.errors), 0) self.assertEqual( load_data, { "version": "3.0", "servers": [{ "type": "main", "host": "127.0.0.1", "port": 80 }] })
class SimpleTestConfig(BaseConfig): fields = { FieldName("version", alias="@version"): StringField(), FieldName("servers", alias="server"): [{ FieldName("type", alias="@type"): StringField(), "host": StringField(), "port": IntegerField() }] } loader_class = Mock()
class Config1(ReadWriteConfig): config_file = "a" loader_class = MemFileLoader.build(mem=mem) fields = { FieldName("version", alias="@version"): StringField(), }
def test_string(self): field = StringField() self.assertEqual(field.load('abc'), u'abc') with self.assertRaises(ValidationError): field.load('') field = StringField(min_length=4) with self.assertRaises(ValidationError): field.load('abc') field = StringField(allow_blank=True, min_length=4) self.assertEqual(field.load(None), u'') self.assertEqual(field.load('abcd'), 'abcd') with self.assertRaises(ValidationError): field.load('abc') field = StringField(allow_blank=True) self.assertEqual(field.load(None), u'') self.assertEqual(field.load('abcd'), u'abcd') self.assertEqual(field.load('abc'), u'abc')