def setUp(self):
     self.fitprop_browser = None
     self.mock_view = mock.create_autospec(EngineeringDiffractionGui)
     self.presenter = EngineeringDiffractionPresenter()
     self.create_test_calibration_presenter()
     self.create_test_focus_presenter()
     self.create_test_fitting_presenter()
     self.create_test_settings_presenter()
     self.mock_view.presenter = self.presenter
     self.mock_tabs = MagicMock()
     self.mock_view.tabs = self.mock_tabs
     self.mock_tabs.currentIndex.return_value = 0
     self.encoder = EngineeringDiffractionEncoder()
     self.decoder = EngineeringDiffractionDecoder()
     self.fit_ws = None
class EngineeringDiffractionDecoderTest(unittest.TestCase):
    def setUp(self):
        self.encoder = EngineeringDiffractionEncoder()
        self.decoder = EngineeringDiffractionDecoder()
        _load_test_file()
        _create_fit_workspace()

    def tearDown(self):
        self.gui.close()

    def test_blank_gui_decodes(self):
        blank_dict = {
            'encoder_version': IO_VERSION,
            'current_tab': 0,
            'settings_dict': SETTINGS_DICT
        }
        self.gui = self.decoder.decode(blank_dict)
        self.assertEqual(blank_dict, self.encoder.encode(self.gui))

    def test_decode_produces_gui_returning_same_dict(self):
        self.gui = self.decoder.decode(ENCODED_DICT)
        post_decode_dict = self.encoder.encode(self.gui)
        self.assertEqual(ENCODED_DICT, post_decode_dict)
 def setUp(self):
     self.encoder = EngineeringDiffractionEncoder()
     self.decoder = EngineeringDiffractionDecoder()
     _load_test_file()
     _create_fit_workspace()
class EngineeringDiffractionEncoderTest(unittest.TestCase):
    def setUp(self):
        self.fitprop_browser = None
        self.mock_view = mock.create_autospec(EngineeringDiffractionGui)
        self.presenter = EngineeringDiffractionPresenter()
        self.create_test_calibration_presenter()
        self.create_test_focus_presenter()
        self.create_test_fitting_presenter()
        self.create_test_settings_presenter()
        self.mock_view.presenter = self.presenter
        self.mock_tabs = MagicMock()
        self.mock_view.tabs = self.mock_tabs
        self.mock_tabs.currentIndex.return_value = 0
        self.encoder = EngineeringDiffractionEncoder()
        self.decoder = EngineeringDiffractionDecoder()
        self.fit_ws = None

    def create_test_focus_presenter(self):
        focus_model = FocusModel()
        focus_view = mock.create_autospec(FocusView)
        self.presenter.focus_presenter = FocusPresenter(
            focus_model, focus_view)

    def create_test_calibration_presenter(self):
        cal_model = CalibrationModel()
        cal_view = mock.create_autospec(CalibrationView)
        self.presenter.calibration_presenter = CalibrationPresenter(
            cal_model, cal_view)

    def create_test_fitting_presenter(self):
        fitting_view = mock.create_autospec(FittingView)
        self.presenter.fitting_presenter = FittingPresenter(fitting_view)
        self.presenter.focus_presenter.add_focus_subscriber(
            self.presenter.fitting_presenter.data_widget.presenter.
            focus_run_observer)
        fitting_plot_view = mock.create_autospec(FittingPlotView)
        self.fitprop_browser = mock.create_autospec(EngDiffFitPropertyBrowser)
        fitting_plot_view.fit_browser = self.fitprop_browser
        self.presenter.fitting_presenter.plot_widget.view = fitting_plot_view

    def create_test_settings_presenter(self):
        settings_model = SettingsModel()
        settings_view = mock.create_autospec(SettingsView)
        settings_presenter = SettingsPresenter(settings_model, settings_view)
        self.presenter.settings_presenter = settings_presenter
        settings_presenter.settings = SETTINGS_DICT
        settings_presenter._save_settings_to_file()

    def test_blank_gui_encodes(self):
        self.mock_tabs.currentIndex.return_value = 0
        test_dic = self.encoder.encode(self.mock_view)
        self.assertEqual(
            {
                'encoder_version': IO_VERSION,
                'current_tab': 0,
                'settings_dict': SETTINGS_DICT
            }, test_dic)

    def test_loaded_workspaces_encode(self):
        self.presenter.fitting_presenter.data_widget.presenter.model.load_files(
            TEST_FILE)
        self.fitprop_browser.read_current_fitprop.return_value = None
        test_dic = self.encoder.encode(self.mock_view)
        self.assertEqual(
            {
                'encoder_version': IO_VERSION,
                'current_tab': 0,
                'data_loaded_workspaces': [TEST_WS],
                'plotted_workspaces': [],
                'fit_properties': None,
                'fit_results': {},
                'settings_dict': SETTINGS_DICT,
                'background_params': {
                    'ENGINX_277208_focused_bank_2': []
                }
            }, test_dic)

    def test_background_params_encode(self):
        self.presenter.fitting_presenter.data_widget.presenter.model.load_files(
            TEST_FILE)
        self.fitprop_browser.read_current_fitprop.return_value = None
        self.presenter.fitting_presenter.data_widget.model._bg_params = {
            TEST_WS: [True, 70, 4000, True]
        }
        test_dic = self.encoder.encode(self.mock_view)
        self.assertEqual(
            {
                'encoder_version': IO_VERSION,
                'current_tab': 0,
                'data_loaded_workspaces': [TEST_WS],
                'plotted_workspaces': [],
                'fit_properties': None,
                'fit_results': {},
                'settings_dict': SETTINGS_DICT,
                'background_params': {
                    TEST_WS: [True, 70, 4000, True]
                }
            }, test_dic)

    def test_fits_encode(self):
        self.presenter.fitting_presenter.data_widget.presenter.model.load_files(
            TEST_FILE)
        self.presenter.fitting_presenter.data_widget.presenter.model._fit_results = FIT_RESULTS
        self.fitprop_browser.read_current_fitprop.return_value = FIT_DICT
        self.fitprop_browser.plotDiff.return_value = True
        self.presenter.fitting_presenter.data_widget.presenter.plotted = {
            FIT_WS
        }
        test_dic = self.encoder.encode(self.mock_view)
        self.assertEqual(
            {
                'encoder_version': IO_VERSION,
                'current_tab': 0,
                'data_loaded_workspaces': [TEST_WS],
                'plotted_workspaces': [FIT_WS],
                'fit_properties': FIT_DICT,
                'fit_results': FIT_RESULTS,
                'plot_diff': 'True',
                'settings_dict': SETTINGS_DICT,
                'background_params': {
                    'ENGINX_277208_focused_bank_2': []
                }
            }, test_dic)