def test_wildcard_with_siblings(self): before = { 'a': 'value', 'b': { 'key': 'value', 'another': 'filterme' }, 'c': { 'another': 'dontfilter' } } after = { 'a': 'value', 'b': { 'key': 'value' }, 'c': { 'another': 'dontfilter' } } spec = { True: True, 'b': { 'key': True } } self.assertDictEqual(filter_dict(before, spec), after)
def test_transform_key_and_value(self): before = {'a': 'change_me', 'b': 'filter_me', 'c': 'keep_me'} after = {'newkey': 'change_me_changed', 'c': 'keep_me'} spec = { 'a': TransformKeyAndValue(lambda x: ('newkey', x + '_changed')), 'c': True, } self.assertDictEqual(filter_dict(before, spec), after)
def make_expected_sdk3_project(**kwargs): # This is the union of all expected folder structures. expected = { 'pebble-jshintrc': True, 'package.json': True, 'src': { 'c': { 'lib.c': True, 'main.c': True }, 'rocky': { 'index.js': True }, 'pkjs': { 'index.js': True }, 'common': { 'shared.js': True }, 'resources': { 'fonts': {}, 'images': { 'package_image.png': True }, 'data': {}, } }, 'include': { 'lib.h': True }, 'worker_src': { 'c': { 'worker.c': True, } }, 'wscript': True, 'resources': { 'fonts': {}, 'images': { 'image.png': True }, 'data': {}, } } # The spec excludes items from modifies the 'expected' folder structure, determining # what will actually be expected when the test is run. spec = { 'worker_src': False, 'include': False, 'src': { 'c': { 'main.c': True, } }, True: True } spec.update(kwargs) return filter_dict(expected, spec)
def test_wildcard_values(self): """ Test that 'True:True: simply copies a dictionary """ before = { 'a': 1, 'b': {'c': 2} } after = before spec = {True: True} self.assertDictEqual(filter_dict(before, spec), after)
def test_value_callback(self): """ Test that we can use a callback to transform a dict's values """ before = { 'numbers': [1, 5, 'a'], } after = { 'numbers': [2, 10, 'aa'], } spec = {'numbers': TransformValue(lambda v: [x * 2 for x in v])} self.assertDictEqual(filter_dict(before, spec), after)
def test_filter_sub_keys(self): """ Test that we can filter keys in lower levels of a dict """ before = { 'sublevel': { 'key': 'value', 'filter_me': 'filter_me' }, 'filter_me': 'filter_me' } after = {'sublevel': {'key': 'value'}} spec = {'sublevel': {'key': True}} self.assertDictEqual(filter_dict(before, spec), after)
def test_value_callback(self): """ Test that we can use a callback to transform a dict's values """ before = { 'numbers': [1, 5, 'a'], } after = { 'numbers': [2, 10, 'aa'], } spec = { 'numbers': TransformValue(lambda v: [x * 2 for x in v]) } self.assertDictEqual(filter_dict(before, spec), after)
def test_wildcard_with_callback(self): """ Test that transformations are applied for wildcard keys""" before = { 'a': 1, 'b': 2 } after = { 'a': 2, 'b': 3 } spec = {True: TransformValue(lambda x: x + 1)} self.assertDictEqual(filter_dict(before, spec), after)
def npm_search(request): try: query = request.GET['q'] except KeyError: return {'packages': []} search = requests.get('http://node-modules.com/search.json', {'q': query}).json() data = {'packages': [filter_dict(package, PACKAGE_SPEC) for package in search]} send_td_event('cloudpebble_package_search', data={ 'data': { 'query': query } }, request=request) return data
def test_wildcard_omission_with_false_value(self): """ Test that a False value can be used to exclude something from a wildcard. """ before = { 'a': 'value', 'b': 'value', 'c': 'filter_me', 'd': 'filter_me' } after = { 'a': 'value', 'b': 'value', } spec = {True: True, 'c': False, 'd': False} self.assertDictEqual(filter_dict(before, spec), after)
def make_expected_sdk3_project(**kwargs): # This is the union of all expected folder structures. expected = { 'pebble-jshintrc': True, 'package.json': True, 'src': { 'c': {'lib.c': True, 'main.c': True}, 'rocky': {'index.js': True}, 'pkjs': {'index.js': True}, 'common': {'shared.js': True}, 'resources': { 'fonts': {}, 'images': { 'package_image.png': True }, 'data': {}, } }, 'include': { 'lib.h': True }, 'worker_src': { 'c': { 'worker.c': True, } }, 'wscript': True, 'resources': { 'fonts': {}, 'images': { 'image.png': True }, 'data': {}, } } # The spec excludes items from modifies the 'expected' folder structure, determining # what will actually be expected when the test is run. spec = { 'worker_src': False, 'include': False, 'src': { 'c': { 'main.c': True, } }, True: True } spec.update(kwargs) return filter_dict(expected, spec)
def test_transform_key_and_value(self): before = { 'a': 'change_me', 'b': 'filter_me', 'c': 'keep_me' } after = { 'newkey': 'change_me_changed', 'c': 'keep_me' } spec = { 'a': TransformKeyAndValue(lambda x: ('newkey', x + '_changed')), 'c': True, } self.assertDictEqual(filter_dict(before, spec), after)
def test_wildcard_multilevel(self): """ Test that dicts inside wildcard keys are filtered """ before = { 'a': { 'filter_me': 'filter_me' }, 'b': { 'key': 'value', 'filter_me': 'filter_me' }, 'c': 'value' } after = {'a': {}, 'b': {'key': 'value'}, 'c': 'value'} spec = {True: {'key': True}} self.assertDictEqual(filter_dict(before, spec), after)
def npm_info(request): query = request.GET['q'] try: package = requests.get('http://node-modules.com/package/%s.json' % urllib.quote(query)).json() except ValueError: raise Http404("Package not found") data = {'package': filter_dict(package, PACKAGE_SPEC)} send_td_event('cloudpebble_package_get_info', data={'data': { 'query': query }}, request=request) return data
def test_wildcard_multilevel(self): """ Test that dicts inside wildcard keys are filtered """ before = { 'a': {'filter_me': 'filter_me'}, 'b': {'key': 'value', 'filter_me': 'filter_me'}, 'c': 'value' } after = { 'a': {}, 'b': {'key': 'value'}, 'c': 'value' } spec = {True: { 'key': True }} self.assertDictEqual(filter_dict(before, spec), after)
def npm_search(request): try: query = request.GET['q'] except KeyError: return {'packages': []} search = requests.get('http://node-modules.com/search.json', { 'q': query }).json() data = { 'packages': [filter_dict(package, PACKAGE_SPEC) for package in search] } send_td_event('cloudpebble_package_search', data={'data': { 'query': query }}, request=request) return data
def npm_info(request): query = request.GET['q'] try: package = requests.get('http://node-modules.com/package/%s.json' % urllib.quote(query)).json() except ValueError: raise Http404("Package not found") data = { 'package': filter_dict(package, PACKAGE_SPEC) } send_td_event('cloudpebble_package_get_info', data={ 'data': { 'query': query } }, request=request) return data
def test_wildcard_omission_with_false_value(self): """ Test that a False value can be used to exclude something from a wildcard. """ before = { 'a': 'value', 'b': 'value', 'c': 'filter_me', 'd': 'filter_me' } after = { 'a': 'value', 'b': 'value', } spec = { True: True, 'c': False, 'd': False } self.assertDictEqual(filter_dict(before, spec), after)
def test_wildcard_with_siblings(self): before = { 'a': 'value', 'b': { 'key': 'value', 'another': 'filterme' }, 'c': { 'another': 'dontfilter' } } after = { 'a': 'value', 'b': { 'key': 'value' }, 'c': { 'another': 'dontfilter' } } spec = {True: True, 'b': {'key': True}} self.assertDictEqual(filter_dict(before, spec), after)
def test_rename(self): before = {'a': 'thing'} after = {'b': 'thing'} spec = {'a': 'b'} self.assertDictEqual(filter_dict(before, spec), after)
def test_filter_toplevel_keys(self): """ Test that we can filter keys at the top level of a dict """ before = {'key': 'value', 'filter_me': 'filter_me'} after = {'key': 'value'} spec = {'key': True} self.assertDictEqual(filter_dict(before, spec), after)
def test_invalid_dictionary_type(self): """ Test that the function fails if the 'dictionary' is not a Mappable """ with self.assertRaises(ValueError): print filter_dict('a_string', {})
def test_invalid_spec_type(self): """ Test that the function fails if the spec is not a Mappable """ with self.assertRaises(ValueError): print filter_dict({}, 'a_string')
def test_rename(self): """ Check that string values in the spec can rename a key. """ before = {'a': 'thing'} after = {'b': 'thing'} spec = {'a': 'b'} self.assertDictEqual(filter_dict(before, spec), after)
def test_wildcard_values(self): """ Test that 'True:True: simply copies a dictionary """ before = {'a': 1, 'b': {'c': 2}} after = before spec = {True: True} self.assertDictEqual(filter_dict(before, spec), after)
def test_wildcard_with_callback(self): """ Test that transformations are applied for wildcard keys""" before = {'a': 1, 'b': 2} after = {'a': 2, 'b': 3} spec = {True: TransformValue(lambda x: x + 1)} self.assertDictEqual(filter_dict(before, spec), after)