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)
Esempio n. 2
0
 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)
Esempio n. 3
0
 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)
Esempio n. 4
0
 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)
Esempio n. 5
0
 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)
Esempio n. 6
0
 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)
Esempio n. 7
0
 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)
Esempio n. 8
0
 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)
Esempio n. 9
0
 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)
Esempio n. 10
0
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
Esempio n. 11
0
 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)
Esempio n. 13
0
 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)
Esempio n. 14
0
 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)
Esempio n. 15
0
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
Esempio n. 16
0
 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)
Esempio n. 17
0
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
Esempio n. 18
0
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
Esempio n. 19
0
 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)
Esempio n. 20
0
 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)
Esempio n. 21
0
 def test_rename(self):
     before = {'a': 'thing'}
     after = {'b': 'thing'}
     spec = {'a': 'b'}
     self.assertDictEqual(filter_dict(before, spec), after)
Esempio n. 22
0
 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)
Esempio n. 23
0
 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', {})
Esempio n. 24
0
 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')
Esempio n. 25
0
 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', {})
Esempio n. 26
0
 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)
Esempio n. 27
0
 def test_rename(self):
     before = {'a': 'thing'}
     after = {'b': 'thing'}
     spec = {'a': 'b'}
     self.assertDictEqual(filter_dict(before, spec), after)
Esempio n. 28
0
 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)
Esempio n. 29
0
 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)
Esempio n. 30
0
 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)
Esempio n. 31
0
 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)
Esempio n. 32
0
 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')