def test_mask_error_on_list_field(self): model = { 'nested': fields.List(fields.String) } with self.assertRaises(mask.MaskError): mask.apply(model, 'nested{notpossible}')
def test_mask_error_on_simple_fields(self): model = { 'name': fields.String, } with self.assertRaises(mask.MaskError): mask.apply(model, 'name{notpossible}')
def test_mask_error_on_simple_fields(self): model = { 'name': fields.String, } with pytest.raises(mask.MaskError): mask.apply(model, 'name{notpossible}')
def test_multiple_nested_api_fields(self): level_2 = {'nested_2': fields.Nested(person_fields)} level_1 = {'nested_1': fields.Nested(level_2)} root = {'nested': fields.Nested(level_1)} result = mask.apply(root, 'nested{nested_1{nested_2{name}}}') self.assertEqual(set(result.keys()), set(['nested'])) self.assertIsInstance(result['nested'], fields.Nested) self.assertEqual(set(result['nested'].nested.keys()), set(['nested_1'])) data = { 'nested': { 'nested_1': { 'nested_2': {'name': 'John', 'age': 42} } } } expected = { 'nested': { 'nested_1': { 'nested_2': {'name': 'John'} } } } self.assertDataEqual(marshal(data, result), expected) # Should leave th original mask untouched self.assertDataEqual(marshal(data, root), data)
def test_list_fields_with_nested(self): family_fields = {'members': fields.List(fields.Nested(person_fields))} result = mask.apply(family_fields, 'members{name}') assert set(result.keys()) == set(['members']) assert isinstance(result['members'], fields.List) assert isinstance(result['members'].container, fields.Nested) assert set(result['members'].container.nested.keys()) == set(['name']) data = { 'members': [ { 'name': 'John', 'age': 42 }, { 'name': 'Jane', 'age': 42 }, ] } expected = {'members': [{'name': 'John'}, {'name': 'Jane'}]} assert_data(marshal(data, result), expected) # Should leave th original mask untouched assert_data(marshal(data, family_fields), data)
def test_multiple_nested_api_fields(self): level_2 = {'nested_2': fields.Nested(person_fields)} level_1 = {'nested_1': fields.Nested(level_2)} root = {'nested': fields.Nested(level_1)} result = mask.apply(root, 'nested{nested_1{nested_2{name}}}') assert set(result.keys()) == set(['nested']) assert isinstance(result['nested'], fields.Nested) assert set(result['nested'].nested.keys()) == set(['nested_1']) data = { 'nested': { 'nested_1': { 'nested_2': { 'name': 'John', 'age': 42 } } } } expected = {'nested': {'nested_1': {'nested_2': {'name': 'John'}}}} assert_data(marshal(data, result), expected) # Should leave th original mask untouched assert_data(marshal(data, root), data)
def test_nested_api_fields(self): family_fields = { 'father': fields.Nested(person_fields), 'mother': fields.Nested(person_fields), } result = mask.apply(family_fields, 'father{name},mother{age}') assert set(result.keys()) == set(['father', 'mother']) assert isinstance(result['father'], fields.Nested) assert set(result['father'].nested.keys()) == set(['name']) assert isinstance(result['mother'], fields.Nested) assert set(result['mother'].nested.keys()) == set(['age']) data = { 'father': { 'name': 'John', 'age': 42 }, 'mother': { 'name': 'Jane', 'age': 42 }, } expected = {'father': {'name': 'John'}, 'mother': {'age': 42}} assert_data(marshal(data, result), expected) # Should leave th original mask untouched assert_data(marshal(data, family_fields), data)
def test_list_fields_with_nested_inherited(self): api = Api(self.app) person = api.model('Person', { 'name': fields.String, 'age': fields.Integer }) child = api.inherit('Child', person, { 'attr': fields.String }) family = api.model('Family', { 'children': fields.List(fields.Nested(child)) }) result = mask.apply(family.resolved, 'children{name,attr}') data = {'children': [ {'name': 'John', 'age': 5, 'attr': 'value-john'}, {'name': 'Jane', 'age': 42, 'attr': 'value-jane'}, ]} expected = {'children': [ {'name': 'John', 'attr': 'value-john'}, {'name': 'Jane', 'attr': 'value-jane'}, ]} self.assertDataEqual(marshal(data, result), expected) # Should leave th original mask untouched self.assertDataEqual(marshal(data, family), data)
def test_with_ordered_dict(self): data = OrderedDict({ 'integer': 42, 'string': 'a string', 'boolean': True, }) result = mask.apply(data, '{integer, string}') self.assertEqual(result, {'integer': 42, 'string': 'a string'})
def test_star_only(self): data = { 'integer': 42, 'string': 'a string', 'boolean': True, } result = mask.apply(data, '*') self.assertEqual(result, data)
def test_multiple_fields(self): data = { 'integer': 42, 'string': 'a string', 'boolean': True, } result = mask.apply(data, '{integer, string}') self.assertEqual(result, {'integer': 42, 'string': 'a string'})
def test_empty(self): data = { 'integer': 42, 'string': 'a string', 'boolean': True, } result = mask.apply(data, '{}') self.assertEqual(result, {})
def test_with_objects(self): data = DObject({ 'integer': 42, 'string': 'a string', 'boolean': True, }) result = mask.apply(data, '{integer, string}') assert result == {'integer': 42, 'string': 'a string'}
def test_single_field(self): data = { 'integer': 42, 'string': 'a string', 'boolean': True, } result = mask.apply(data, '{integer}') assert result == {'integer': 42}
def test_empty(self): data = { 'integer': 42, 'string': 'a string', 'boolean': True, } result = mask.apply(data, '{}') assert result == {}
def test_star_only(self): data = { 'integer': 42, 'string': 'a string', 'boolean': True, } result = mask.apply(data, '*') assert result == data
def test_with_ordered_dict(self): data = OrderedDict({ 'integer': 42, 'string': 'a string', 'boolean': True, }) result = mask.apply(data, '{integer, string}') assert result == {'integer': 42, 'string': 'a string'}
def test_multiple_fields(self): data = { 'integer': 42, 'string': 'a string', 'boolean': True, } result = mask.apply(data, '{integer, string}') assert result == {'integer': 42, 'string': 'a string'}
def test_nested_fields(self): data = { 'nested': { 'integer': 42, 'string': 'a string', 'boolean': True, } } result = mask.apply(data, '{nested{integer}}') self.assertEqual(result, {'nested': {'integer': 42}})
def test_nested_fields(self): data = { 'nested': { 'integer': 42, 'string': 'a string', 'boolean': True, } } result = mask.apply(data, '{nested{integer}}') assert result == {'nested': {'integer': 42}}
def test_nested_with_start(self): data = { 'nested': { 'integer': 42, 'string': 'a string', 'boolean': True, }, 'other': 'value', } result = mask.apply(data, '{nested{integer},*}') assert result == {'nested': {'integer': 42}, 'other': 'value'}
def test_nested_with_start(self): data = { 'nested': { 'integer': 42, 'string': 'a string', 'boolean': True, }, 'other': 'value', } result = mask.apply(data, '{nested{integer},*}') self.assertEqual(result, {'nested': {'integer': 42}, 'other': 'value'})
def test_nested_list_fields(self): data = { 'list': [{ 'integer': 42, 'string': 'a string', }, { 'integer': 404, 'string': 'another string', }] } result = mask.apply(data, '{list{integer}}') assert result == {'list': [{'integer': 42}, {'integer': 404}]}
def test_nested_list_fields(self): data = { 'list': [{ 'integer': 42, 'string': 'a string', }, { 'integer': 404, 'string': 'another string', }] } result = mask.apply(data, '{list{integer}}') self.assertEqual(result, {'list': [{'integer': 42}, {'integer': 404}]})
def test_list(self): data = [{ 'integer': 42, 'string': 'a string', 'boolean': True, }, { 'integer': 404, 'string': 'another string', 'boolean': False, }] result = mask.apply(data, '{integer, string}') self.assertEqual(result, [ {'integer': 42, 'string': 'a string'}, {'integer': 404, 'string': 'another string'} ])
def test_list_fields_with_raw(self): family_fields = { 'members': fields.List(fields.Raw) } result = mask.apply(family_fields, 'members{name}') data = {'members': [ {'name': 'John', 'age': 42}, {'name': 'Jane', 'age': 42}, ]} expected = {'members': [{'name': 'John'}, {'name': 'Jane'}]} self.assertDataEqual(marshal(data, result), expected) # Should leave th original mask untouched self.assertDataEqual(marshal(data, family_fields), data)
def test_nested_field(self): data = { 'integer': 42, 'string': 'a string', 'boolean': True, 'nested': { 'integer': 42, 'string': 'a string', 'boolean': True, } } result = mask.apply(data, '{nested}') assert result == {'nested': { 'integer': 42, 'string': 'a string', 'boolean': True, }}
def test_list_fields_with_simple_field(self): family_fields = { 'name': fields.String, 'members': fields.List(fields.String) } result = mask.apply(family_fields, 'members') assert set(result.keys()) == set(['members']) assert isinstance(result['members'], fields.List) assert isinstance(result['members'].container, fields.String) data = {'name': 'Doe', 'members': ['John', 'Jane']} expected = {'members': ['John', 'Jane']} assert_data(marshal(data, result), expected) # Should leave th original mask untouched assert_data(marshal(data, family_fields), data)
def test_raw_api_fields(self): family_fields = { 'father': fields.Raw, 'mother': fields.Raw, } result = mask.apply(family_fields, 'father{name},mother{age}') data = { 'father': {'name': 'John', 'age': 42}, 'mother': {'name': 'Jane', 'age': 42}, } expected = {'father': {'name': 'John'}, 'mother': {'age': 42}} self.assertDataEqual(marshal(data, result), expected) # Should leave th original mask untouched self.assertDataEqual(marshal(data, family_fields), data)
def test_list_fields_with_simple_field(self): family_fields = { 'name': fields.String, 'members': fields.List(fields.String) } result = mask.apply(family_fields, 'members') self.assertEqual(set(result.keys()), set(['members'])) self.assertIsInstance(result['members'], fields.List) self.assertIsInstance(result['members'].container, fields.String) data = {'name': 'Doe', 'members': ['John', 'Jane']} expected = {'members': ['John', 'Jane']} self.assertDataEqual(marshal(data, result), expected) # Should leave th original mask untouched self.assertDataEqual(marshal(data, family_fields), data)
def test_list(self): data = [{ 'integer': 42, 'string': 'a string', 'boolean': True, }, { 'integer': 404, 'string': 'another string', 'boolean': False, }] result = mask.apply(data, '{integer, string}') assert result == [{ 'integer': 42, 'string': 'a string' }, { 'integer': 404, 'string': 'another string' }]
def test_list_fields_with_nested_inherited(self, app): api = Api(app) person = api.model('Person', { 'name': fields.String, 'age': fields.Integer }) child = api.inherit('Child', person, {'attr': fields.String}) family = api.model('Family', {'children': fields.List(fields.Nested(child))}) result = mask.apply(family.resolved, 'children{name,attr}') data = { 'children': [ { 'name': 'John', 'age': 5, 'attr': 'value-john' }, { 'name': 'Jane', 'age': 42, 'attr': 'value-jane' }, ] } expected = { 'children': [ { 'name': 'John', 'attr': 'value-john' }, { 'name': 'Jane', 'attr': 'value-jane' }, ] } assert_data(marshal(data, result), expected) # Should leave th original mask untouched assert_data(marshal(data, family), data)
def test_nested_list(self): data = { 'integer': 42, 'list': [{ 'integer': 42, 'string': 'a string', }, { 'integer': 404, 'string': 'another string', }] } result = mask.apply(data, '{list}') assert result == {'list': [{ 'integer': 42, 'string': 'a string', }, { 'integer': 404, 'string': 'another string', }]}
def test_nested_field(self): data = { 'integer': 42, 'string': 'a string', 'boolean': True, 'nested': { 'integer': 42, 'string': 'a string', 'boolean': True, } } result = mask.apply(data, '{nested}') self.assertEqual(result, { 'nested': { 'integer': 42, 'string': 'a string', 'boolean': True, } })
def test_list_fields_with_nested(self): family_fields = { 'members': fields.List(fields.Nested(person_fields)) } result = mask.apply(family_fields, 'members{name}') self.assertEqual(set(result.keys()), set(['members'])) self.assertIsInstance(result['members'], fields.List) self.assertIsInstance(result['members'].container, fields.Nested) self.assertEqual(set(result['members'].container.nested.keys()), set(['name'])) data = {'members': [ {'name': 'John', 'age': 42}, {'name': 'Jane', 'age': 42}, ]} expected = {'members': [{'name': 'John'}, {'name': 'Jane'}]} self.assertDataEqual(marshal(data, result), expected) # Should leave th original mask untouched self.assertDataEqual(marshal(data, family_fields), data)
def test_list_fields_with_raw(self): family_fields = {'members': fields.List(fields.Raw)} result = mask.apply(family_fields, 'members{name}') data = { 'members': [ { 'name': 'John', 'age': 42 }, { 'name': 'Jane', 'age': 42 }, ] } expected = {'members': [{'name': 'John'}, {'name': 'Jane'}]} assert_data(marshal(data, result), expected) # Should leave th original mask untouched assert_data(marshal(data, family_fields), data)
def test_nested_api_fields(self): family_fields = { 'father': fields.Nested(person_fields), 'mother': fields.Nested(person_fields), } result = mask.apply(family_fields, 'father{name},mother{age}') self.assertEqual(set(result.keys()), set(['father', 'mother'])) self.assertIsInstance(result['father'], fields.Nested) self.assertEqual(set(result['father'].nested.keys()), set(['name'])) self.assertIsInstance(result['mother'], fields.Nested) self.assertEqual(set(result['mother'].nested.keys()), set(['age'])) data = { 'father': {'name': 'John', 'age': 42}, 'mother': {'name': 'Jane', 'age': 42}, } expected = {'father': {'name': 'John'}, 'mother': {'age': 42}} self.assertDataEqual(marshal(data, result), expected) # Should leave th original mask untouched self.assertDataEqual(marshal(data, family_fields), data)
def test_raw_api_fields(self): family_fields = { 'father': fields.Raw, 'mother': fields.Raw, } result = mask.apply(family_fields, 'father{name},mother{age}') data = { 'father': { 'name': 'John', 'age': 42 }, 'mother': { 'name': 'Jane', 'age': 42 }, } expected = {'father': {'name': 'John'}, 'mother': {'age': 42}} assert_data(marshal(data, result), expected) # Should leave th original mask untouched assert_data(marshal(data, family_fields), data)
def test_nested_list(self): data = { 'integer': 42, 'list': [{ 'integer': 42, 'string': 'a string', }, { 'integer': 404, 'string': 'another string', }] } result = mask.apply(data, '{list}') self.assertEqual( result, { 'list': [{ 'integer': 42, 'string': 'a string', }, { 'integer': 404, 'string': 'another string', }] })
def test_missing_nested_field_skipped(self): result = mask.apply({}, 'nested{integer}', skip=True) assert result == {}
def test_missing_field_none_by_default(self): result = mask.apply({}, '{integer}') assert result == {'integer': None}
def test_missing_field_none_by_default(self): result = mask.apply({}, '{integer}') self.assertEqual(result, {'integer': None})
def test_missing_nested_field_skipped(self): result = mask.apply({}, 'nested{integer}', skip=True) self.assertEqual(result, {})
def test_nested_fields_when_none(self): data = {'nested': None} result = mask.apply(data, '{nested{integer}}') assert result == {'nested': None}
def test_nested_fields_when_none(self): data = {'nested': None} result = mask.apply(data, '{nested{integer}}') self.assertEqual(result, {'nested': None})
def test_mask_error_on_list_field(self): model = {'nested': fields.List(fields.String)} with pytest.raises(mask.MaskError): mask.apply(model, 'nested{notpossible}')