Skip to content

Commit

Permalink
Add unit test for the presenter and the model
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamedAlmaki committed Dec 9, 2024
1 parent 941c3c8 commit 746f8a1
Show file tree
Hide file tree
Showing 13 changed files with 484 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ BayesFitting::BayesFitting(QWidget *parent)
std::make_unique<StretchModel>(), std::move(stretchRunner)));
}

std::unique_ptr<MantidQt::API::AlgorithmRunner> BayesFitting::createAlgorithmRunner() {
std::unique_ptr<MantidQt::API::AlgorithmRunner> BayesFitting::createAlgorithmRunner() const {
auto jobRunner = std::make_unique<MantidQt::API::QtJobRunner>(true);
return std::make_unique<MantidQt::API::AlgorithmRunner>(std::move(jobRunner));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class MANTIDQT_INELASTIC_DLL BayesFitting : public InelasticInterface {
void initLayout() override;

private:
std::unique_ptr<MantidQt::API::AlgorithmRunner> createAlgorithmRunner();
std::unique_ptr<MantidQt::API::AlgorithmRunner> createAlgorithmRunner() const;

std::string documentationPage() const override;

Expand Down
16 changes: 4 additions & 12 deletions qt/scientific_interfaces/Inelastic/BayesFitting/StretchModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@
#include "MantidAPI/AlgorithmManager.h"
#include "MantidAPI/AlgorithmProperties.h"
#include "MantidAPI/AlgorithmRuntimeProps.h"
#include "MantidQtWidgets/Spectroscopy/SettingsWidget/SettingsHelper.h"

using namespace Mantid::API;

namespace MantidQt::CustomInterfaces {

MantidQt::API::IConfiguredAlgorithm_sptr StretchModel::stretchAlgorithm(const StretchRunData &algParams,
const std::string &fitWorkspaceName,
const std::string &contourWorkspaceName) const {
auto const useQuickBayes = SettingsHelper::hasDevelopmentFlag("quickbayes");

const std::string &contourWorkspaceName,
const bool useQuickBayes) const {
auto properties = std::make_unique<AlgorithmRuntimeProps>();

properties->setProperty("SampleWorkspace", algParams.sampleName);
Expand All @@ -40,7 +38,7 @@ MantidQt::API::IConfiguredAlgorithm_sptr StretchModel::stretchAlgorithm(const St
auto stretch = AlgorithmManager::Instance().create(algorithmName);
stretch->initialize();

return std::make_shared<API::ConfiguredAlgorithm>(std::move(stretch), std::move(properties));
return std::make_shared<API::ConfiguredAlgorithm>(stretch, std::move(properties));
}

API::IConfiguredAlgorithm_sptr StretchModel::setupSaveAlgorithm(const std::string &wsName) const {
Expand All @@ -49,13 +47,7 @@ API::IConfiguredAlgorithm_sptr StretchModel::setupSaveAlgorithm(const std::strin

auto saveProps = std::make_unique<Mantid::API::AlgorithmRuntimeProps>();

const auto saveDir = Mantid::Kernel::ConfigService::Instance().getString("defaultsave.directory");
auto filename = saveDir + wsName + ".nxs";
if (filename.empty())
saveProps->setProperty("Filename", wsName + ".nxs");
else
saveProps->setProperty("Filename", filename);

saveProps->setPropertyValue("Filename", wsName + ".nxs");
saveProps->setPropertyValue("InputWorkspace", wsName);

return std::make_shared<API::ConfiguredAlgorithm>(saveAlgo, std::move(saveProps));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class MANTIDQT_INELASTIC_DLL IStretchModel {

virtual MantidQt::API::IConfiguredAlgorithm_sptr stretchAlgorithm(const StretchRunData &algParams,
const std::string &fitWorkspaceName,
const std::string &contourWorkspaceName) const = 0;
const std::string &contourWorkspaceName,
const bool useQuickBayes) const = 0;

virtual API::IConfiguredAlgorithm_sptr setupSaveAlgorithm(const std::string &wsName) const = 0;
};
Expand All @@ -31,7 +32,8 @@ class MANTIDQT_INELASTIC_DLL StretchModel : public IStretchModel {

MantidQt::API::IConfiguredAlgorithm_sptr stretchAlgorithm(const StretchRunData &algParams,
const std::string &fitWorkspaceName,
const std::string &contourWorkspaceName) const override;
const std::string &contourWorkspaceNames,
const bool useQuickBayes = false) const override;

API::IConfiguredAlgorithm_sptr setupSaveAlgorithm(const std::string &wsName) const override;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@ struct PlotType {
inline static const std::string ALL = "All";
inline static const std::string SIGMA = "Sigma";
inline static const std::string BETA = "Beta";
inline static const std::string FWHM = "FWHM";
};
} // namespace

namespace MantidQt::CustomInterfaces {
StretchPresenter::StretchPresenter(QWidget *parent, StretchView *view, std::unique_ptr<StretchModel> model,
StretchPresenter::StretchPresenter(QWidget *parent, IStretchView *view, std::unique_ptr<IStretchModel> model,
std::unique_ptr<API::IAlgorithmRunner> algorithmRunner)
: BayesFittingTab(parent, std::move(algorithmRunner)), m_previewSpec(0), m_save(false), m_view(view),
m_model(std::move(model)) {
: BayesFittingTab(parent, std::move(algorithmRunner)), m_previewSpec(0), m_view(view), m_model(std::move(model)) {
m_view->subscribePresenter(this);

setRunWidgetPresenter(std::make_unique<RunPresenter>(this, m_view->getRunWidget()));
Expand All @@ -55,7 +53,8 @@ void StretchPresenter::handleRun() {
m_fitWorkspaceName = baseName + "_Stretch_Fit";
m_contourWorkspaceName = baseName + "_Stretch_Contour";

auto stretch = m_model->stretchAlgorithm(algParams, m_fitWorkspaceName, m_contourWorkspaceName);
auto const useQuickBayes = SettingsHelper::hasDevelopmentFlag("quickbayes");
auto stretch = m_model->stretchAlgorithm(algParams, m_fitWorkspaceName, m_contourWorkspaceName, useQuickBayes);
m_algorithmRunner->execute(stretch);
}

Expand Down Expand Up @@ -120,15 +119,15 @@ void StretchPresenter::notifyPlotClicked() {

std::string const plotType = m_view->getPlotType();
auto const plotErrors = SettingsHelper::externalPlotErrorBars();
auto const plotSigma = (plotType == "All" || plotType == "Sigma");
auto const plotBeta = (plotType == "All" || plotType == "Beta");
auto const plotSigma = (plotType == PlotType::ALL || plotType == PlotType::SIGMA);
auto const plotBeta = (plotType == PlotType::ALL || plotType == PlotType::BETA);

auto const fitWorkspace = getADSWorkspace<WorkspaceGroup>(m_fitWorkspaceName);
for (auto it = fitWorkspace->begin(); it < fitWorkspace->end(); ++it) {
auto const name = (*it)->getName();
if (plotSigma && name.substr(name.length() - 5) == "Sigma") {
if (plotSigma && name.substr(name.length() - 5) == PlotType::SIGMA) {
m_plotter->plotSpectra(name, "0", plotErrors);
} else if (plotBeta && name.substr(name.length() - 4) == "Beta") {
} else if (plotBeta && name.substr(name.length() - 4) == PlotType::BETA) {
m_plotter->plotSpectra(name, "0", plotErrors);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class MANTIDQT_INELASTIC_DLL IStretchPresenter : public IRunSubscriber,
class MANTIDQT_INELASTIC_DLL StretchPresenter : public BayesFittingTab, public IStretchPresenter {

public:
StretchPresenter(QWidget *parent, StretchView *view, std::unique_ptr<StretchModel> model,
StretchPresenter(QWidget *parent, IStretchView *view, std::unique_ptr<IStretchModel> model,
std::unique_ptr<API::IAlgorithmRunner> algorithmRunner);
~StretchPresenter() override = default;

Expand Down Expand Up @@ -63,9 +63,8 @@ class MANTIDQT_INELASTIC_DLL StretchPresenter : public BayesFittingTab, public I
std::string m_fitWorkspaceName;
std::string m_contourWorkspaceName;

bool m_save;
StretchView *m_view;
std::unique_ptr<StretchModel> m_model;
IStretchView *m_view;
std::unique_ptr<IStretchModel> m_model;
};
} // namespace CustomInterfaces
} // namespace MantidQt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
using namespace MantidQt::MantidWidgets::WorkspaceUtils;
using namespace MantidQt::CustomInterfaces::InterfaceUtils;

// TODO(): range setters are copied from inelastic tab refactor them
namespace {

static const unsigned int NUM_DECIMALS = 6;
Expand All @@ -39,9 +38,12 @@ struct PlotType {

namespace MantidQt::CustomInterfaces {
StretchView::StretchView(QWidget *parent)
: m_dblManager(new QtDoublePropertyManager()), m_properties(), m_propTree(new QtTreePropertyBrowser()) {
: m_dblManager(new QtDoublePropertyManager()), m_properties(), m_propTree(new QtTreePropertyBrowser()),
m_dblEdFac(new DoubleEditorFactory()) {
m_uiForm.setupUi(parent);

m_propTree->setFactoryForManager(m_dblManager, m_dblEdFac);

auto eRangeSelector = m_uiForm.ppPlot->addRangeSelector("StretchERange");
connect(eRangeSelector, &MantidWidgets::RangeSelector::minValueChanged, this, &StretchView::minValueChanged);
connect(eRangeSelector, &MantidWidgets::RangeSelector::maxValueChanged, this, &StretchView::maxValueChanged);
Expand Down Expand Up @@ -307,7 +309,7 @@ void StretchView::setPlotContourEnabled(bool enabled) {

void StretchView::setSaveResultEnabled(bool enabled) { m_uiForm.pbSave->setEnabled(enabled); }

int StretchView::displaySaveDirectoryMessage() {
bool StretchView::displaySaveDirectoryMessage() {
char const *textMessage = "BayesStretch requires a default save directory and "
"one is not currently set."
" If run, the algorithm will default to saving files "
Expand Down
10 changes: 6 additions & 4 deletions qt/scientific_interfaces/Inelastic/BayesFitting/StretchView.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include "DllConfig.h"
#include "MantidQtWidgets/Common/QtPropertyBrowser/DoubleEditorFactory.h"
#include "MantidQtWidgets/Common/QtPropertyBrowser/QtTreePropertyBrowser.h"
#include "MantidQtWidgets/Common/QtPropertyBrowser/QtTreePropertyBrowser"
#include "MantidQtWidgets/Common/UserInputValidator.h"
#include "MantidQtWidgets/Spectroscopy/RunWidget/IRunSubscriber.h"
#include "StretchData.h"
Expand All @@ -19,6 +19,8 @@ namespace CustomInterfaces {

class MANTIDQT_INELASTIC_DLL IStretchViewSubscriber {
public:
virtual ~IStretchViewSubscriber() = default;

virtual void notifySaveClicked() = 0;
virtual void notifyPlotClicked() = 0;
virtual void notifyPlotContourClicked() = 0;
Expand Down Expand Up @@ -49,7 +51,7 @@ class MANTIDQT_INELASTIC_DLL IStretchView {
virtual void setLoadHistory(bool doLoadHistory) = 0;

virtual void resetPlotContourOptions(const std::vector<std::string> &contourNames) = 0;
virtual int displaySaveDirectoryMessage() = 0;
virtual bool displaySaveDirectoryMessage() = 0;

virtual void setPlotADSEnabled(bool enabled) = 0;
virtual void setPlotResultEnabled(bool enabled) = 0;
Expand Down Expand Up @@ -85,7 +87,7 @@ class MANTIDQT_INELASTIC_DLL StretchView : public QWidget, public IStretchView {
void setLoadHistory(bool doLoadHistory) override;

void resetPlotContourOptions(const std::vector<std::string> &contourNames) override;
int displaySaveDirectoryMessage() override;
bool displaySaveDirectoryMessage() override;

void setPlotADSEnabled(bool enabled) override;
void setPlotResultEnabled(bool enabled) override;
Expand All @@ -110,12 +112,12 @@ private slots:
private:
void formatTreeWidget(QtTreePropertyBrowser *treeWidget, QMap<QString, QtProperty *> const &properties) const;

private:
Ui::Stretch m_uiForm;
QtDoublePropertyManager *m_dblManager;
QMap<QString, QtProperty *> m_properties;
IStretchViewSubscriber *m_presenter;
QtTreePropertyBrowser *m_propTree;
DoubleEditorFactory *m_dblEdFac;
};
} // namespace CustomInterfaces
} // namespace MantidQt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
get_filename_component(SUB_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}" NAME)

set(TEST_FILES StretchModelTest.h StretchPresenterTest.h)

set(TEST_HELPERS MockObjects.h)

list(TRANSFORM TEST_FILES PREPEND ${SUB_DIRECTORY}/)
list(TRANSFORM TEST_HELPERS PREPEND ${SUB_DIRECTORY}/)

set(ALL_TEST_FILES
${ALL_TEST_FILES} ${TEST_FILES}
PARENT_SCOPE
)

set(ALL_TEST_HELPERS
${ALL_TEST_HELPERS} ${TEST_HELPERS}
PARENT_SCOPE
)
71 changes: 71 additions & 0 deletions qt/scientific_interfaces/Inelastic/test/BayesFitting/MockObjects.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright &copy; 2024 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source
// & Institut Laue - Langevin
// SPDX - License - Identifier: GPL - 3.0 +
#pragma once

#include <cxxtest/TestSuite.h>
#include <gmock/gmock.h>

#include "BayesFitting/StretchModel.h"
#include "BayesFitting/StretchView.h"

#include "MantidKernel/WarningSuppressions.h"
#include "MantidQtWidgets/Common/IConfiguredAlgorithm.h"

#include <string>

using namespace MantidQt;
using namespace MantidQt::CustomInterfaces;

GNU_DIAG_OFF_SUGGEST_OVERRIDE

class MockStretchView : public IStretchView {
public:
virtual ~MockStretchView() = default;

MOCK_METHOD(void, subscribePresenter, (IStretchViewSubscriber * presenter), (override));
MOCK_METHOD(void, loadSettings, (const QSettings &settings), (override));
MOCK_METHOD(void, applySettings, ((std::map<std::string, QVariant> const &settings)), (override));
MOCK_METHOD(void, validateUserInput, (IUserInputValidator * validator), (const, override));

MOCK_METHOD(StretchRunData, getRunData, (), (const, override));
MOCK_METHOD(CurrentPreviewData, getCurrentPreviewData, (), (const, override));
MOCK_METHOD(std::string, getPlotType, (), (const, override));
MOCK_METHOD(std::string, getPlotContour, (), (const, override));
MOCK_METHOD(IRunView *, getRunWidget, (), (const, override));

MOCK_METHOD(void, setupFitOptions, (), (override));
MOCK_METHOD(void, setupPropertyBrowser, (), (override));
MOCK_METHOD(void, setupPlotOptions, (), (override));

MOCK_METHOD(void, setFileExtensionsByName, (bool filter), (override));
MOCK_METHOD(void, setLoadHistory, (bool doLoadHistory), (override));

MOCK_METHOD(void, resetPlotContourOptions, ((const std::vector<std::string> &contourNames)), (override));
MOCK_METHOD(bool, displaySaveDirectoryMessage, (), (override));

MOCK_METHOD(void, setPlotADSEnabled, (bool enabled), (override));
MOCK_METHOD(void, setPlotResultEnabled, (bool enabled), (override));
MOCK_METHOD(void, setPlotContourEnabled, (bool enabled), (override));
MOCK_METHOD(void, setSaveResultEnabled, (bool enabled), (override));
MOCK_METHOD(void, setButtonsEnabled, (bool enabled), (override));
MOCK_METHOD(void, setPlotResultIsPlotting, (bool plotting), (override));
MOCK_METHOD(void, setPlotContourIsPlotting, (bool plotting), (override));
};

class MockStretchModel : public IStretchModel {
public:
virtual ~MockStretchModel() = default;

MOCK_METHOD(MantidQt::API::IConfiguredAlgorithm_sptr, stretchAlgorithm,
((const StretchRunData &algParams), const std::string &fitWorkspaceName,
const std::string &contourWorkspaceName, const bool useQuickBayes),
(const, override));

MOCK_METHOD(API::IConfiguredAlgorithm_sptr, setupSaveAlgorithm, (const std::string &wsName), (const, override));
};

GNU_DIAG_ON_SUGGEST_OVERRIDE
Loading

0 comments on commit 746f8a1

Please sign in to comment.