def test_success(self): model_mock = mock.MagicMock(field=mock.sentinel.field) res = api.exists(self.sess, model_mock, field=mock.sentinel.field) self.sess.query.assert_called_once_with(mock.sentinel.field) self.query.filter_by.assert_called_once_with(field=mock.sentinel.field) self.query.limit.assert_called_once_with(1) self.assertTrue(res)
def test_not_found(self): model_mock = mock.MagicMock(field=mock.sentinel.field) self.query.one.side_effect = sao_exc.NoResultFound res = api.exists(self.sess, model_mock, field=mock.sentinel.field) self.sess.query.assert_called_once_with(mock.sentinel.field) self.query.filter_by.assert_called_once_with(field=mock.sentinel.field) self.query.limit.assert_called_once_with(1) self.assertFalse(res)
def exists(self, obj_type, key): """ Returns True if an object of the supplied type and key exists in backend storage. :param obj_type: A `procession.objects.Object` class. :param key: string key for the object. """ sess = self._get_session() model = _get_db_model_from_obj_class(obj_type) return api.exists(sess, model, key)