From 17e90c47617683454680f4630ddf848d76926a9b Mon Sep 17 00:00:00 2001 From: Manuel Cortez Date: Sat, 5 Jan 2019 18:54:57 -0600 Subject: [PATCH] Added unittest for audioRecorder --- src/run_tests.py | 2 +- src/test/test_audiorecorder.py | 69 ++++++++++++++++++++++---------- src/test/test_base_interactor.py | 2 +- 3 files changed, 49 insertions(+), 24 deletions(-) diff --git a/src/run_tests.py b/src/run_tests.py index 7d21581..d819934 100644 --- a/src/run_tests.py +++ b/src/run_tests.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- import unittest -testmodules = ["test.test_setup_py2exe", "test.test_base_interactor", "test.test_renderers"] +testmodules = ["test.test_setup_py2exe", "test.test_base_interactor", "test.test_audiorecorder", "test.test_renderers"] suite = unittest.TestSuite() diff --git a/src/test/test_audiorecorder.py b/src/test/test_audiorecorder.py index 3cbf2f6..5ff4474 100644 --- a/src/test/test_audiorecorder.py +++ b/src/test/test_audiorecorder.py @@ -9,34 +9,59 @@ class audioRecorderTestCase(unittest.TestCase): """ Test both the presenter and interactor of the audio recorder feature. View stuff will be mocked.""" -# @mock.patch("presenters.audioRecorder.sound_lib", esp_set=True) -# @mock.patch("presenters.audioRecorder.pub", esp_set=True) -# @mock.patch("presenters.audioRecorder.tempfile", esp_set=True) -# @mock.patch("presenters.audioRecorder.sound", esp_set=True) -# @mock.patch("presenters.audioRecorder.output", esp_set=True) -# @mock.patch("presenters.audioRecorder.os", esp_set=True) -# def test_audiorecorder_interactor(self, soundlib_mock, pub_mock, tempfile_mock, sound_mock, output_mock, os_mock, widgetUtils_mock, interactor_pub_mock): -# """ Test methods for audio recorder. """ -# tempfile_mock.mktemp.return_value = "somefile.wav" -# sound_mock.get_recording.return_value = "some_recording" -# view=mock.MagicMock(name="view") -# interactor_ = interactor.audioRecorderInteractor() -# presenter_ = presenter.audioRecorderPresenter(view=view, interactor=interactor_) -# interactor_.install.assert_called_with(view=view, presenter=presenter_) -# interactor_.start.assert_called_with() -# # Start sending events to the presenter and see its reactions. -# presenter_.start_recording() -# print(presenter_.recording) + @mock.patch("presenters.audioRecorder.sound_lib", esp_set=True) + @mock.patch("presenters.audioRecorder.pub", esp_set=True) + @mock.patch("presenters.audioRecorder.tempfile", esp_set=True) + @mock.patch("presenters.audioRecorder.sound", esp_set=True) + @mock.patch("presenters.audioRecorder.output", esp_set=True) + @mock.patch("presenters.audioRecorder.os", esp_set=True) + def test_audiorecorder_presenter(self, os_mock, output_mock, sound_mock, tempfile_mock, pub_mock, soundlib_mock): + """ Test methods for audio recorder presenter. """ + tempfile_mock.mktemp.return_value = "somefile.wav" + sound_mock.get_recording.return_value = mock.MagicMock() + soundlib_mock.stream.fileStream.return_value = mock.MagicMock() + view=mock.MagicMock(name="view") + interactor_ = mock.MagicMock(name="interactor", esp_set=interactor.audioRecorderInteractor) + presenter_ = presenter.audioRecorderPresenter(view=view, interactor=interactor_) + # Start sending events to the presenter and see its reactions. + presenter_.start_recording() + tempfile_mock.mktemp.assert_any_call(suffix=".wav") + sound_mock.get_recording.assert_any_call(presenter_.file) + presenter_.recording.play.assert_any_call() + pub_mock.sendMessage.assert_any_call("audiorecorder_set_label", control="record", label=_("&Stop")) + pub_mock.sendMessage.assert_any_call("audiorecorder_disable_control", control="ok") + presenter_.stop_recording() + presenter_.recording.stop.assert_any_call() + presenter_.recording.free.assert_any_call() + pub_mock.sendMessage.assert_any_call("audiorecorder_set_label", control="record", label=_("&Record")) + pub_mock.sendMessage.assert_any_call("audiorecorder_disable_control", control="record") + pub_mock.sendMessage.assert_any_call("audiorecorder_enable_control", control="play") + pub_mock.sendMessage.assert_any_call("audiorecorder_enable_control", control="discard") + pub_mock.sendMessage.assert_any_call("audiorecorder_enable_control", control="ok") + pub_mock.sendMessage.assert_any_call("audiorecorder_focus_control", control="play") + presenter_.discard_recording() + self.assertTrue(presenter_.playing==None) - @mock.patch("interactors.base.pub", esp_set=True) @mock.patch("interactors.audioRecorder.widgetUtils", esp_set=True) - def test_audiorecorder_interactor(self, widgetUtils_mock, pub_mock): - pub_mock.subscribe.return_value = True + def test_audiorecorder_interactor(self, widgetUtils_mock): view=mock.MagicMock(name="view") interactor_ = interactor.audioRecorderInteractor() presenter_ = mock.MagicMock(name="Presenter", esp_set=presenter.audioRecorderPresenter) interactor_.install(view=view, presenter=presenter_) - print(pub_mock.subscribe.called) + # Test if events have been connected to WX + widgetUtils_mock.connect_event.assert_any_call(view.play, widgetUtils_mock.BUTTON_PRESSED, interactor_.on_play) + widgetUtils_mock.connect_event.assert_any_call(view.record, widgetUtils_mock.BUTTON_PRESSED, interactor_.on_record) + widgetUtils_mock.connect_event.assert_any_call(view.discard, widgetUtils_mock.BUTTON_PRESSED, interactor_.on_discard) + # Let's call some methods simulating user interaction. + interactor_.on_record() + presenter_.toggle_recording.assert_called_with() + interactor_.on_play() + presenter_.play.assert_called_with() + # Let's simulate user response here + view.get_response.return_value = widgetUtils_mock.OK + interactor_.start() + # this should call on_postprocess after receiving the OK signal. + presenter_.postprocess.assert_called_with() def setUp(self): languageHandler.setLanguage("en") diff --git a/src/test/test_base_interactor.py b/src/test/test_base_interactor.py index 7ddbe7d..a0a9a83 100644 --- a/src/test/test_base_interactor.py +++ b/src/test/test_base_interactor.py @@ -26,7 +26,7 @@ class baseInteractorTestCase(unittest.TestCase): interactor_.set_label(control="some_control", label="label") view.set.assert_called_with("some_control", "label") interactor_.focus_control(control="some_control") - view.some_control.SetFocus() + view.some_control.SetFocus.assert_called_with() interactor_.uninstall() pub_mock.unsubscribe.assert_any_call(interactor_.disable_control, "base_disable_control"), pub_mock.unsubscribe.assert_any_call(interactor_.enable_control, "base_enable_control"),