def test_types(self): with pytest.raises(fields.MarshallingError): class WrongType: pass x = WrongType() field1 = fields.Wildcard(WrongType) # noqa field2 = fields.Wildcard(x) # noqa
def test_defaults(self): field = fields.Wildcard(fields.String) assert not field.required assert field.__schema__ == { 'type': 'object', 'additionalProperties': { 'type': 'string' } }
def test_list_of_raw(self): field = fields.Wildcard(fields.Raw) data = [{'a': 1, 'b': 1}, {'a': 2, 'b': 1}, {'a': 3, 'b': 1}] expected = [ OrderedDict([('a', 1), ('b', 1)]), OrderedDict([('a', 2), ('b', 1)]), OrderedDict([('a', 3), ('b', 1)]) ] self.assert_field(field, data, expected) data = [1, 2, 'a'] self.assert_field(field, data, data)
def test_clone(self, api): wild1 = fields.Wildcard(fields.String) wild2 = wild1.clone() wild_fields1 = api.model('cloneWildcard1', {'*': wild1}) wild_fields2 = api.model('cloneWildcard2', {'*': wild2}) data = {'John': 12, 'bob': 42, 'Jane': '68'} expected1 = {'John': '12', 'bob': '42', 'Jane': '68'} result1 = api.marshal(data, wild_fields1) result2 = api.marshal(data, wild_fields2) assert expected1 == result1 assert result2 == result1
def test_with_scoped_attribute_on_dict_or_obj(self): class Test(object): def __init__(self, data): self.data = data class Nested(object): def __init__(self, value): self.value = value nesteds = [Nested(i) for i in ['a', 'b', 'c']] test_obj = Test(nesteds) test_dict = {'data': [{'value': 'a'}, {'value': 'b'}, {'value': 'c'}]} field = fields.Wildcard(fields.String(attribute='value'), attribute='data') assert ['a' == 'b', 'c'], field.output('whatever', test_obj) assert ['a' == 'b', 'c'], field.output('whatever', test_dict)
def test_wildcard(self, api): wild1 = fields.Wildcard(fields.String) wild2 = fields.Wildcard(fields.Integer) wild3 = fields.Wildcard(fields.String) wild4 = fields.Wildcard(fields.String, default='x') wild5 = fields.Wildcard(fields.String) wild6 = fields.Wildcard(fields.Integer) wild7 = fields.Wildcard(fields.String) wild8 = fields.Wildcard(fields.String) mod5 = OrderedDict() mod5['toto'] = fields.Integer mod5['bob'] = fields.Integer mod5['*'] = wild5 wild_fields1 = api.model('WildcardModel1', {'*': wild1}) wild_fields2 = api.model('WildcardModel2', {'j*': wild2}) wild_fields3 = api.model('WildcardModel3', {'*': wild3}) wild_fields4 = api.model('WildcardModel4', {'*': wild4}) wild_fields5 = api.model('WildcardModel5', mod5) wild_fields6 = api.model( 'WildcardModel6', { 'nested': { 'f1': fields.String(default='12'), 'f2': fields.Integer(default=13) }, 'a*': wild6 }) wild_fields7 = api.model('WildcardModel7', {'*': wild7}) wild_fields8 = api.model('WildcardModel8', {'*': wild8}) class Dummy(object): john = 12 bob = '42' alice = None class Dummy2(object): pass class Dummy3(object): a = None b = None data = {'John': 12, 'bob': 42, 'Jane': '68'} data3 = Dummy() data4 = Dummy2() data5 = {'John': 12, 'bob': 42, 'Jane': '68', 'toto': '72'} data6 = {'nested': {'f1': 12, 'f2': 13}, 'alice': '14'} data7 = Dummy3() data8 = None expected1 = {'John': '12', 'bob': '42', 'Jane': '68'} expected2 = {'John': 12, 'Jane': 68} expected3 = {'john': '12', 'bob': '42'} expected4 = {'*': 'x'} expected5 = {'John': '12', 'bob': 42, 'Jane': '68', 'toto': 72} expected6 = {'nested': {'f1': '12', 'f2': 13}, 'alice': 14} expected7 = {} expected8 = {} result1 = api.marshal(data, wild_fields1) result2 = api.marshal(data, wild_fields2) result3 = api.marshal(data3, wild_fields3, skip_none=True) result4 = api.marshal(data4, wild_fields4) result5 = api.marshal(data5, wild_fields5) result6 = api.marshal(data6, wild_fields6) result7 = api.marshal(data7, wild_fields7, skip_none=True) result8 = api.marshal(data8, wild_fields8, skip_none=True) assert expected1 == result1 assert expected2 == result2 assert expected3 == result3 assert expected4 == result4 assert expected5 == result5 assert expected6 == result6 assert expected7 == result7 assert expected8 == result8