Skip to content

Commit

Permalink
Pass the unittest without any error (#147)
Browse files Browse the repository at this point in the history
I added a patcher to mock the `_constants` of each app.

This is related to #145.
  • Loading branch information
BECATRUE authored Sep 16, 2023
2 parents 7a28957 + 68d0766 commit 9acfaaf
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 19 deletions.
23 changes: 20 additions & 3 deletions tests/test_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import copy
import json
import unittest
from collections import namedtuple
from typing import Any, Callable, Dict, Optional
from unittest import mock

Expand All @@ -13,6 +14,10 @@

from iquip.apps import builder

_CONSTANTS_DICT = {"proxy_ip": "127.0.0.1", "proxy_port": 8000}

CONSTANTS = namedtuple("ConstantNamespace", _CONSTANTS_DICT.keys())(**_CONSTANTS_DICT)

EMPTY_EXPERIMENT_INFO = {
"name": "name",
"arginfo": {}
Expand Down Expand Up @@ -297,6 +302,8 @@ def get_thread(
experimentPath=EXPERIMENT_PATH,
experimentArgs=experimentArgs,
schedOpts=SCHED_OPTS,
ip=CONSTANTS.proxy_ip,
port=CONSTANTS.proxy_port,
callback=callback,
parent=parent
)
Expand All @@ -311,6 +318,7 @@ def setUp(self):
f"{type_}Value": mock.MagicMock(return_value=QWidget())
for type_ in ("Boolean", "String", "Enumeration", "Number", "DateTime")
}
constants_patcher = mock.patch("iquip.apps.builder.BuilderApp._constants", CONSTANTS)
entries_patcher = mock.patch.multiple(
"iquip.apps.builder",
_BooleanEntry=self.mocked_entries["BooleanValue"],
Expand All @@ -319,7 +327,9 @@ def setUp(self):
_NumberEntry=self.mocked_entries["NumberValue"],
_DateTimeEntry=self.mocked_entries["DateTimeValue"]
)
constants_patcher.start()
entries_patcher.start()
self.addCleanup(constants_patcher.stop)
self.addCleanup(entries_patcher.stop)

def tearDown(self):
Expand Down Expand Up @@ -390,6 +400,8 @@ def test_reload_args(self, mocked_experiment_info_thread_cls):
app.reloadArgs()
mocked_experiment_info_thread_cls.assert_called_once_with(
"experimentPath",
CONSTANTS.proxy_ip,
CONSTANTS.proxy_port,
mocked_on_reloaded,
app
)
Expand Down Expand Up @@ -432,6 +444,8 @@ def test_submit(self, mocked_experiment_submit_thread_cls):
"experimentPath",
experimentArgs,
schedOpts,
CONSTANTS.proxy_ip,
CONSTANTS.proxy_port,
mocked_on_submitted,
app
)
Expand Down Expand Up @@ -466,10 +480,13 @@ class SubmitFunctionalTest(unittest.TestCase):

def setUp(self):
self.qapp = QApplication([])
patcher = mock.patch("requests.get")
self.mocked_get = patcher.start()
constants_patcher = mock.patch("iquip.apps.builder.BuilderApp._constants", CONSTANTS)
requests_get_patcher = mock.patch("requests.get")
constants_patcher.start()
self.mocked_get = requests_get_patcher.start()
self.mocked_response = self.mocked_get.return_value
self.addCleanup(patcher.stop)
self.addCleanup(constants_patcher.stop)
self.addCleanup(requests_get_patcher.stop)

def tearDown(self):
del self.qapp
Expand Down
66 changes: 50 additions & 16 deletions tests/test_explorer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Unit tests for explorer module."""

import unittest
from collections import namedtuple
from unittest import mock

import requests
Expand All @@ -12,16 +13,23 @@
from iquip import protocols
from iquip.apps import explorer

_CONSTANTS_DICT = {"proxy_ip": "127.0.0.1", "proxy_port": 8000}

CONSTANTS = namedtuple("ConstantNamespace", _CONSTANTS_DICT.keys())(**_CONSTANTS_DICT)

class FileFinderThreadTest(unittest.TestCase):
"""Unit tests for _FileFinderThread class."""

# pylint: disable=duplicate-code
def setUp(self):
self.qapp = QApplication([])
patcher = mock.patch("requests.get")
self.mocked_get = patcher.start()
constants_patcher = mock.patch("iquip.apps.explorer.ExplorerApp._constants", CONSTANTS)
requests_get_patcher = mock.patch("requests.get")
constants_patcher.start()
self.mocked_get = requests_get_patcher.start()
self.mocked_response = self.mocked_get.return_value
self.addCleanup(patcher.stop)
self.addCleanup(constants_patcher.stop)
self.addCleanup(requests_get_patcher.stop)

def tearDown(self):
del self.qapp
Expand All @@ -31,8 +39,14 @@ def test_init_thread(self):
callback = mock.MagicMock()
parent = QObject()
with mock.patch("iquip.apps.explorer._FileFinderThread.fetched") as mocked_fetched:
thread = explorer._FileFinderThread(path="path", widget=widget,
callback=callback, parent=parent)
thread = explorer._FileFinderThread(
path="path",
widget=widget,
ip=CONSTANTS.proxy_ip,
port=CONSTANTS.proxy_port,
callback=callback,
parent=parent
)
self.assertEqual(thread.path, "path")
self.assertEqual(thread.widget, widget)
mocked_fetched.connect.assert_called_once_with(callback, type=Qt.QueuedConnection)
Expand All @@ -42,8 +56,14 @@ def test_run(self):
widget = QTreeWidgetItem()
parent = QObject()
with mock.patch("iquip.apps.explorer._FileFinderThread.fetched") as mocked_fetched:
thread = explorer._FileFinderThread(path="path", widget=widget,
callback=mock.MagicMock(), parent=parent)
thread = explorer._FileFinderThread(
path="path",
widget=widget,
ip=CONSTANTS.proxy_ip,
port=CONSTANTS.proxy_port,
callback=mock.MagicMock(),
parent=parent
)
thread.run()
thread.wait()
self.mocked_get.assert_called_once_with("http://127.0.0.1:8000/ls/",
Expand All @@ -57,8 +77,14 @@ def test_run_exception(self):
widget = QTreeWidgetItem()
parent = QObject()
with mock.patch("iquip.apps.explorer._FileFinderThread.fetched") as mocked_fetched:
thread = explorer._FileFinderThread(path="path", widget=widget,
callback=mock.MagicMock(), parent=parent)
thread = explorer._FileFinderThread(
path="path",
widget=widget,
ip=CONSTANTS.proxy_ip,
port=CONSTANTS.proxy_port,
callback=mock.MagicMock(),
parent=parent
)
thread.run()
thread.wait()
self.mocked_get.assert_called_once_with("http://127.0.0.1:8000/ls/",
Expand All @@ -72,9 +98,12 @@ class ExplorerAppTest(unittest.TestCase):

def setUp(self):
self.qapp = QApplication([])
patcher = mock.patch("iquip.apps.explorer._FileFinderThread")
self.mocked_file_finder_thread_cls = patcher.start()
self.addCleanup(patcher.stop)
constants_patcher = mock.patch("iquip.apps.explorer.ExplorerApp._constants", CONSTANTS)
file_finder_thread_patcher = mock.patch("iquip.apps.explorer._FileFinderThread")
constants_patcher.start()
self.mocked_file_finder_thread_cls = file_finder_thread_patcher.start()
self.addCleanup(constants_patcher.stop)
self.addCleanup(file_finder_thread_patcher.stop)

def tearDown(self):
del self.qapp
Expand Down Expand Up @@ -140,6 +169,8 @@ def test_open_experiment(self, mocked_experiment_info_thread_cls):
mocked["fullPath"].assert_called_with(item)
mocked_experiment_info_thread_cls.assert_called_with(
mocked["fullPath"].return_value,
CONSTANTS.proxy_ip,
CONSTANTS.proxy_port,
mocked["openBuilder"],
app
)
Expand All @@ -165,7 +196,7 @@ def test_open_builder(self):
module="iquip.apps.builder",
cls="BuilderApp",
show=True,
pos="right",
pos="center",
args={
"experimentPath": "experimentPath",
"experimentClsName": "experimentClsName",
Expand Down Expand Up @@ -193,9 +224,12 @@ class ExplorerFunctionalTest(unittest.TestCase):

def setUp(self):
self.qapp = QApplication([])
patcher = mock.patch("iquip.apps.explorer._FileFinderThread")
self.mocked_file_finder_thread_cls = patcher.start()
self.addCleanup(patcher.stop)
constants_patcher = mock.patch("iquip.apps.explorer.ExplorerApp._constants", CONSTANTS)
file_finder_thread_patcher = mock.patch("iquip.apps.explorer._FileFinderThread")
constants_patcher.start()
self.mocked_file_finder_thread_cls = file_finder_thread_patcher.start()
self.addCleanup(constants_patcher.stop)
self.addCleanup(file_finder_thread_patcher.stop)

def tearDown(self):
del self.qapp
Expand Down
11 changes: 11 additions & 0 deletions tests/test_scheduler.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Unit tests for scheduler module."""

import unittest
from collections import namedtuple
from unittest import mock

from PyQt5.QtWidgets import QApplication
Expand All @@ -9,6 +10,10 @@
from iquip.apps import scheduler
from iquip.protocols import SubmittedExperimentInfo

_CONSTANTS_DICT = {"proxy_ip": "127.0.0.1", "proxy_port": 8000}

CONSTANTS = namedtuple("ConstantNamespace", _CONSTANTS_DICT.keys())(**_CONSTANTS_DICT)

class ExperimentModelTest(unittest.TestCase):
"""Unit tests for ExperimentModel class."""

Expand Down Expand Up @@ -64,10 +69,13 @@ class SchedulerAppTest(unittest.TestCase):

def setUp(self):
self.qapp = QApplication([])
constants_patcher = mock.patch("iquip.apps.scheduler.SchedulerApp._constants", CONSTANTS)
thread_patcher = mock.patch("iquip.apps.scheduler._ExperimentQueueFetcherThread")
worker_patcher = mock.patch("iquip.apps.scheduler.SchedulerPostWorker")
constants_patcher.start()
thread_patcher.start()
worker_patcher.start()
self.addCleanup(constants_patcher.stop)
self.addCleanup(thread_patcher.stop)
self.addCleanup(worker_patcher.stop)

Expand Down Expand Up @@ -96,10 +104,13 @@ class SchedulerFunctionalTest(unittest.TestCase):

def setUp(self):
self.qapp = QApplication([])
constants_patcher = mock.patch("iquip.apps.scheduler.SchedulerApp._constants", CONSTANTS)
thread_patcher = mock.patch("iquip.apps.scheduler._ExperimentQueueFetcherThread")
worker_patcher = mock.patch("iquip.apps.scheduler.SchedulerPostWorker")
constants_patcher.start()
thread_patcher.start()
worker_patcher.start()
self.addCleanup(constants_patcher.stop)
self.addCleanup(thread_patcher.stop)
self.addCleanup(worker_patcher.stop)

Expand Down

0 comments on commit 9acfaaf

Please sign in to comment.