def test_blacklist(self): """Cache gets populated and results get filtered""" # Cache is populated and highest value is filtered blacklist = [objects.BlacklistEntry(None, None, '3.0')] self.assertFalse('blacklist' in self.udict._cache) self.assertEqual(self.udict._filter(blacklist, newest_only=True), 'dos') self.assertEqual(len(self.udict._cache['blacklist']), 1) self.assertEqual(self.udict._cache['blacklist'][('3.0', '==')], set(['3.0'])) self.assertEqual(self.udict._filter(blacklist), { '2.0': 'dos', '1.0': 'uno' }) # Another entry is cached, result is the same blacklist.append(objects.BlacklistEntry(None, None, '1.0')) self.assertEqual(self.udict._filter(blacklist, newest_only=True), 'dos') self.assertEqual(len(self.udict._cache['blacklist']), 2) self.assertEqual(self.udict._cache['blacklist'][('1.0', '==')], set(['1.0'])) self.assertEqual(self.udict._filter(blacklist), {'2.0': 'dos'}) # An equivalent entry is added, cache is the same blacklist.append(objects.BlacklistEntry(None, 'number', '3.0', '==')) self.assertEqual(self.udict._filter(blacklist, newest_only=True), 'dos') self.assertEqual(len(self.udict._cache['blacklist']), 2) self.assertEqual(self.udict._filter(blacklist), {'2.0': 'dos'})
def test_op_none(self): """Operator is required when version is supplied""" with self.assertRaises(AttributeError): objects.BlacklistEntry('parser', 'json', '>=') entry = objects.BlacklistEntry('parser', 'json') self.assertEqual(entry.type, 'parser') self.assertEqual(entry.name, 'json') self.assertEqual(entry.version, None) self.assertEqual(entry.operator, '==')
def test_op_ver(self): """Operator and version can be swapped""" entry = objects.BlacklistEntry('parser', 'json', '>=', '1.0') self.assertEqual(entry.type, 'parser') self.assertEqual(entry.name, 'json') self.assertEqual(entry.version, '1.0') self.assertEqual(entry.operator, '>=')
def test_blacklist_by_name_version(self): """A name and version is blacklisted, so blacklist is passed to child for type""" blacklist = [objects.BlacklistEntry('parser', 'json', '1.0')] self.assertEqual(self.tdict._filter(blacklist), self.expected) self.mock_plugin_json._filter.assert_called_with(blacklist, newest_only=False) self.mock_plugin_xml._filter.assert_called_with([], newest_only=False)
def test_blacklist_by_name(self): """Entire name is blacklisted, so it's not called or included""" blacklist = [objects.BlacklistEntry(None, 'json')] del self.expected['json'] self.assertEqual(self.tdict._filter(blacklist), self.expected) self.mock_plugin_json._filter.assert_not_called() self.mock_plugin_xml._filter.assert_called_with([], newest_only=False)
def test_no_op(self): """Operator defaults to '=='""" entry = objects.BlacklistEntry('parser', 'json', '1.0') self.assertEqual(entry.type, 'parser') self.assertEqual(entry.name, 'json') self.assertEqual(entry.version, '1.0') self.assertEqual(entry.operator, '==')
def test_blacklist_by_type_name(self): """A type and specific name is blacklisted, so blacklist is passed to child for type""" blacklist = [objects.BlacklistEntry('parser', 'json')] self.assertEqual(self.gdict._filter(blacklist), self.expected) self.mock_type_parser._filter.assert_called_with(blacklist, newest_only=False) self.mock_type_engine._filter.assert_called_with([], newest_only=False) self.mock_type_empty._filter.assert_called_with([], newest_only=False)
def test_blacklist_by_version(self): """Only a version is blacklisted, so blacklist is passed to all children""" blacklist = [objects.BlacklistEntry(None, None, '1.0')] self.assertEqual(self.tdict._filter(blacklist), self.expected) self.mock_plugin_json._filter.assert_called_with(blacklist, newest_only=False) self.mock_plugin_xml._filter.assert_called_with(blacklist, newest_only=False)
def test_empty(self): """Unless the entire type is blacklisted, an empty return value is still included""" blacklist = [objects.BlacklistEntry('empty', 'json')] self.assertEqual(self.gdict._filter(blacklist), self.expected) self.mock_type_parser._filter.assert_called_with([], newest_only=False) self.mock_type_engine._filter.assert_called_with([], newest_only=False) self.mock_type_empty._filter.assert_called_with(blacklist, newest_only=False)
def test_blacklist_by_type(self): """Entire type is blacklisted, so it should be empty""" blacklist = [objects.BlacklistEntry('parser')] self.expected['parser'] = {} self.assertEqual(self.gdict._filter(blacklist), self.expected) self.mock_type_parser._filter.assert_not_called() self.mock_type_engine._filter.assert_called_with([], newest_only=False) self.mock_type_empty._filter.assert_called_with([], newest_only=False)
def test_version_blacklist(self): """Return specific version if not blacklisted""" blacklist = [objects.BlacklistEntry(None, None, '1.0')] self.assertEqual( self.udict._filter(blacklist, newest_only=True, version='1.0'), None) self.assertEqual( self.udict._filter(blacklist, newest_only=True, version='2.0'), 'dos')
def test_blacklist_by_name(self): """A name is blacklisted in all types, so all children are passed the blacklist""" blacklist = [objects.BlacklistEntry(None, 'json')] self.assertEqual(self.gdict._filter(blacklist), self.expected) self.mock_type_parser._filter.assert_called_with(blacklist, newest_only=False) self.mock_type_engine._filter.assert_called_with(blacklist, newest_only=False) self.mock_type_empty._filter.assert_called_with(blacklist, newest_only=False)
def test_no_result(self): """Blacklist filters all""" blacklist = [objects.BlacklistEntry(None, None, '4.0', '<')] self.assertIsNone(self.udict._filter(blacklist)) self.assertEqual(self.udict._cache['blacklist'][('4.0', '<')], set(['1.0', '2.0', '3.0'])) self.assertIsNone(self.udict._filter(blacklist, newest_only=True)) self.assertEqual(self.udict._cache['blacklist'][('4.0', '<')], set(['1.0', '2.0', '3.0']))
def test_empty(self): """Empty values are not included""" mock_plugin_empty = mock.Mock() mock_plugin_empty._filter.return_value = None self.tdict['empty'] = mock_plugin_empty self.assertEqual(self.tdict._filter(), self.expected) self.tdict._filter() self.mock_plugin_json._filter.assert_called_with(None, newest_only=False) self.mock_plugin_xml._filter.assert_called_with(None, newest_only=False) mock_plugin_empty._filter.assert_called_with(None, newest_only=False) blacklist = [objects.BlacklistEntry(None, None, '1.0')] self.assertEqual(self.tdict._filter(blacklist), self.expected) self.mock_plugin_json._filter.assert_called_with(blacklist, newest_only=False) self.mock_plugin_xml._filter.assert_called_with(blacklist, newest_only=False) mock_plugin_empty._filter.assert_called_with(blacklist, newest_only=False)
def test_repr(self): """Ensure repr displays properly""" entry = objects.BlacklistEntry('parser', 'json', '1.0', '>=') self.assertEqual(repr(entry), "BlacklistEntry('parser', 'json', '>=', '1.0')")
def test_ver_bad(self): """Version is not a string""" with self.assertRaises(TypeError): objects.BlacklistEntry('parser', 'json', 1.0)
def test_empty(self): """Some value must be supplied or all plugins would be blacklisted""" with self.assertRaises(AttributeError): objects.BlacklistEntry() with self.assertRaises(AttributeError): objects.BlacklistEntry(operator='>')
def test_op_bad(self): """Operators must be in '=', '==', '!=', '<', '<=', '>', '>='""" with self.assertRaises(AttributeError): objects.BlacklistEntry('parser', 'json', '1.0', '%')
def test_blacklist_all(self): """Type is blacklisted, so all are blacklisted""" blacklist = [objects.BlacklistEntry('parser')] self.assertEqual(self.tdict._filter(blacklist), {}) self.mock_plugin_json._filter.assert_not_called() self.mock_plugin_xml._filter.assert_not_called()