Example #1
0
    def test_notify_raises_if_command_unknown(self):
        '''
        Test that an exception is thrown if notify is called with a command it does not recognise.
        '''
        fl_presenter = FileLoaderPresenter(self.mock_view)

        # Create a fake command/enum
        fake_enum = Enum(value='invalid', names=[('bad_command', -200000)])

        with self.assertRaises(ValueError):
            fl_presenter.notify(fake_enum.bad_command)
    def __init__(self, parent=None):

        QAction.__init__(self, parent, text="Open...")

        self.parent = parent

        # Placeholder for the filename
        self.fname = None

        # Action for opening a file
        self.triggered.connect(self.open_file)

        self._presenter = FileLoaderPresenter(self)
Example #3
0
    def test_closing_filedialog_does_nothing(self):
        '''
        Test that closing the FileDialog without choosing a file causes the presenter to return without attempting
        to open/interpret a file.
        '''

        mock_view = mock.create_autospec(FileLoaderViewInterface)

        '''
        Tell the mock FileLoaderView to behave as if the FileDialog was closed without a file being chosen by returning
        an empty "filename"
        '''
        mock_view.get_selected_file_path = mock.MagicMock(side_effect = lambda: ('',''))

        fl_presenter = FileLoaderPresenter(mock_view)
        fl_presenter.register_master(self.mock_main_presenter)

        # Mock the `_load_data` method to check its calls
        fl_presenter._load_data = mock.MagicMock()

        fl_presenter.notify(Command.FILEOPENREQUEST)

        # Check that the `_load_data` and `set_dict` methods were not called due to a file not being selected
        fl_presenter._load_data.assert_not_called()
        self.mock_main_presenter.set_dict.assert_not_called()
class FileLoaderWidget(QAction, FileLoaderViewInterface):
    def __init__(self, parent=None):

        QAction.__init__(self, parent, text="Open...")

        self.parent = parent

        # Placeholder for the filename
        self.fname = None

        # Action for opening a file
        self.triggered.connect(self.open_file)

        self._presenter = FileLoaderPresenter(self)

    def get_selected_file_path(self):
        return self.fname

    def show_reject_file_message(self, error_msg):
        '''
        Error message displayed when the chosen file couldn't be read into an xarray. Simply a copy of the exception
        thrown during file reading.
        '''

        error_dialog = QErrorMessage()
        error_dialog.showMessage(error_msg)
        error_dialog.exec_()

    def open_file(self):

        # Create and show a file dialog with a NetCDF filter
        filedialog = QFileDialog()

        # Store the location of the file that was selected
        self.fname = filedialog.getOpenFileName(self.parent, "Open file",
                                                "/home", "NetCDF (*.nc)")

        # Inform the presenter that the user attempted to open a file
        self._presenter.notify(Command.FILEOPENREQUEST)

    def get_presenter(self):
        return self._presenter
Example #5
0
    def test_bad_file_shows_message_on_view(self):
        '''
        Test that the request to open a bad file causes the view to display a message.
        '''

        fl_presenter = FileLoaderPresenter(self.mock_view)
        fl_presenter.register_master(self.mock_main_presenter)

        # Mock the `open_dataset` function in the FileLoaderTool so that it returns a bad dataset
        with mock.patch("datasetviewer.fileloader.FileLoaderTool.open_dataset",
                        side_effect=lambda path: self.empty_data):

            fl_presenter.notify(Command.FILEOPENREQUEST)

            '''
            Check that loading a bad dataset caused a message to be displayed on the view indicating that the file
            was rejected.
            '''
            self.mock_view.show_reject_file_message.assert_called_once()

            '''
            Check that the `set_dict` method in the MainViewPresenter was not called as a result of the file being
            rejected.
            '''
            self.mock_main_presenter.set_dict.assert_not_called()
Example #6
0
    def test_file_selection_calls_get_path_on_view(self):
        '''
        Test that a FILEOPENREQUEST in the FileLoaderPresenter's `notify` function causes the `get_selected_file_path`
        method on the FileLoaderView to be called.
        '''
        fl_presenter = FileLoaderPresenter(self.mock_view)
        fl_presenter.register_master(self.mock_main_presenter)

        # Mock the `_load_data` method so that no actual file-reading occurs
        fl_presenter._load_data = mock.MagicMock()

        # Notify the FileLoaderPresenter of a FILE
        fl_presenter.notify(Command.FILEOPENREQUEST)
        self.mock_view.get_selected_file_path.assert_called_once()
Example #7
0
    def test_load_file(self):
        '''
        Test that a FILEOPENREQUEST in `notify` causes the `_load_data` method to be called with the expected argument.
        '''
        fl_presenter = FileLoaderPresenter(self.mock_view)
        fl_presenter.register_master(self.mock_main_presenter)

        # Mock the `_load_data` method
        fl_presenter._load_data = mock.MagicMock()

        fl_presenter.notify(Command.FILEOPENREQUEST)

        '''
        Check that the `_load_data` method was called with the first element from the filename-extension tuple that gets
        returned by the FileLoaderView.
        '''
        fl_presenter._load_data.assert_called_once_with(self.fake_file_path[0])
Example #8
0
    def test_file_open_success_informs_main_presenter(self):
        '''
        Test that successfully opening a file in the FileLoaderPresenter causes the resulting DataSet to be sent to the
        MainViewPresenter.
        '''

        fl_presenter = FileLoaderPresenter(self.mock_view)
        fl_presenter.register_master(self.mock_main_presenter)

        # Mock the `file_to_dict` function from the FileLoaderTool and make it return a fake dictionary
        with mock.patch("datasetviewer.fileloader.FileLoaderTool.file_to_dict",
                        side_effect = lambda path: self.empty_dict):

            fl_presenter.notify(Command.FILEOPENREQUEST)

            # Check that the `set_dict` function in the MainViewPresenter was called with the same dictionary
            self.mock_main_presenter.set_dict.assert_called_once_with(self.empty_dict)
Example #9
0
 def test_presenter_throws_if_view_none(self):
     '''
     Test that the FileLoaderPresenter throws an Exception when the FileLoaderView is None.
     '''
     with self.assertRaises(ValueError):
         FileLoaderPresenter(None)