def test_validation(self): self.mock_find_image.side_effect = [ "id1", exceptions.NotFound(), exceptions.NoUniqueMatch() ] self.assertTrue(self.constraint.validate("foo", self.ctx)) self.assertFalse(self.constraint.validate("bar", self.ctx)) self.assertFalse(self.constraint.validate("baz", self.ctx))
def _find_with_attr(self, entity, **kwargs): """Find a item for entity with attributes matching ``**kwargs``.""" matches = list(self._findall_with_attr(entity, **kwargs)) num_matches = len(matches) if num_matches == 0: msg = ("No %(name)s matching %(args)s.") % { 'name': entity, 'args': kwargs } raise exceptions.NotFound(msg) elif num_matches > 1: raise exceptions.NoUniqueMatch() else: return matches[0]
def find(self, **kwargs): """Find a single item with attributes matching ``**kwargs``. This isn't very efficient: it loads the entire list then filters on the Python side. """ matches = self.findall(**kwargs) num_matches = len(matches) if num_matches == 0: msg = "No %s matching %s." % (self.resource_class.__name__, kwargs) raise exceptions.NotFound(msg) elif num_matches > 1: raise exceptions.NoUniqueMatch() else: return matches[0]