def get_one(self, obj_type, search_spec): """ Returns a single Python dict that matches the supplied search spec. :param obj_type: A `procession.objects.Object` class. :param search_spec: A `procession.search.SearchSpec` :raises `procession.exc.NotFound` if no such object found in backend storage. """ sess = self._get_session() model = _get_db_model_from_obj_class(obj_type) filters = search_spec.filters db_model = api.get_one(sess, model, **filters) return db_model.to_dict()
def test_not_found(self): self.query.one.side_effect = sao_exc.NoResultFound with testtools.ExpectedException(exc.NotFound): api.get_one(self.sess, mock.sentinel.model, field=mock.sentinel.field) self.sess.query.assert_called_once_with(mock.sentinel.model) self.query.filter_by.assert_called_once_with(field=mock.sentinel.field)
def test_success(self): res = api.get_one(self.sess, mock.sentinel.model, field=mock.sentinel.field) self.sess.query.assert_called_once_with(mock.sentinel.model) self.query.filter_by.assert_called_once_with(field=mock.sentinel.field) self.assertEqual(self.query.one.return_value, res)