From 8eb470eaf7549980018a6e927ad32ae630186ea6 Mon Sep 17 00:00:00 2001 From: ialarmedalien Date: Mon, 13 Nov 2023 08:56:39 -0800 Subject: [PATCH] Fix remaining self.assert tests; move strange test integrated into util file into its own file --- .../narrative/tests/test_appmanager.py | 6 +-- src/biokbase/narrative/tests/test_kvp.py | 13 ++++++ .../narrative/tests/test_log_proxy.py | 44 ++++++++++++------- src/biokbase/narrative/tests/util.py | 14 ------ 4 files changed, 43 insertions(+), 34 deletions(-) create mode 100644 src/biokbase/narrative/tests/test_kvp.py diff --git a/src/biokbase/narrative/tests/test_appmanager.py b/src/biokbase/narrative/tests/test_appmanager.py index e7ad9624de..23c1f6b84e 100644 --- a/src/biokbase/narrative/tests/test_appmanager.py +++ b/src/biokbase/narrative/tests/test_appmanager.py @@ -290,7 +290,7 @@ def test_run_app__good_inputs(self, auth, c): assert self.jm.get_job(self.test_job_id) == new_job self._verify_comm_success(c.return_value.send_comm_message, False) - assert False is self.jm._running_jobs[new_job.job_id]["refresh"] + assert self.jm._running_jobs[new_job.job_id]["refresh"] is False @mock.patch(CLIENTS_AM, get_mock_client) @mock.patch(JOB_COMM_MOCK) @@ -484,7 +484,7 @@ def test_run_legacy_batch_app__good_inputs(self, auth, c): assert self.jm.get_job(self.test_job_id) == new_job self._verify_comm_success(c.return_value.send_comm_message, False) - assert False is self.jm._running_jobs[new_job.job_id]["refresh"] + assert self.jm._running_jobs[new_job.job_id]["refresh"] is False @mock.patch(CLIENTS_AM, get_mock_client) @mock.patch(JOB_COMM_MOCK) @@ -797,7 +797,7 @@ def test_run_app_batch__good_inputs(self, auth, c): self._verify_comm_success(c.return_value.send_comm_message, True, num_jobs=4) for job in [parent_job, *child_jobs]: - assert False is self.jm._running_jobs[job.job_id]["refresh"] + assert self.jm._running_jobs[job.job_id]["refresh"] is False @mock.patch(CLIENTS_AM, get_mock_client) @mock.patch(JOB_COMM_MOCK) diff --git a/src/biokbase/narrative/tests/test_kvp.py b/src/biokbase/narrative/tests/test_kvp.py new file mode 100644 index 0000000000..da4687e80a --- /dev/null +++ b/src/biokbase/narrative/tests/test_kvp.py @@ -0,0 +1,13 @@ +from biokbase.narrative.common.kvp import parse_kvp + + +def test_parse_kvp() -> None: + for user_input, text, kvp in ( + ("foo", "foo", {}), + ("name=val", "", {"name": "val"}), + ("a name=val boy", "a boy", {"name": "val"}), + ): + rkvp = {} + rtext = parse_kvp(user_input, rkvp) + assert text == rtext + assert kvp == rkvp diff --git a/src/biokbase/narrative/tests/test_log_proxy.py b/src/biokbase/narrative/tests/test_log_proxy.py index 09e91dee19..f266f98f2a 100644 --- a/src/biokbase/narrative/tests/test_log_proxy.py +++ b/src/biokbase/narrative/tests/test_log_proxy.py @@ -7,7 +7,8 @@ import time import unittest -from biokbase.narrative.common import log_proxy as proxy +import pytest +from biokbase.narrative.common import log_proxy __author__ = "Dan Gunter " @@ -21,8 +22,8 @@ class MainTestCase(unittest.TestCase): def setUp(self): self._config(["db: test", "collection: kblog"]) - if proxy.g_log is None: - proxy.g_log = logging.getLogger(proxy.LOGGER_NAME) + if log_proxy.g_log is None: + log_proxy.g_log = logging.getLogger(log_proxy.LOGGER_NAME) def _config(self, lines): text = "\n".join(lines) @@ -33,7 +34,7 @@ def test_run_proxy(self): pid = os.fork() if pid == 0: print("Run child") - proxy.run(self) + log_proxy.run(self) else: time.sleep(1) print("Wait for child to start") @@ -51,16 +52,21 @@ def test_run_proxy(self): def test_configuration(self): # empty self._config([]) - self.assertRaises(ValueError, proxy.DBConfiguration, self.conf) + with pytest.raises(ValueError): + log_proxy.DBConfiguration(self.conf) + # , proxy.DBConfiguration, self.conf) # missing collection self._config(["db: test"]) - self.assertRaises(KeyError, proxy.DBConfiguration, self.conf) + with pytest.raises(KeyError): + log_proxy.DBConfiguration(self.conf) # bad db name self._config(["db: 1test", "collection: kblog"]) - self.assertRaises(ValueError, proxy.DBConfiguration, self.conf) + with pytest.raises(ValueError): + log_proxy.DBConfiguration(self.conf) # bad db name self._config(["db: test.er", "collection: kblog"]) - self.assertRaises(ValueError, proxy.DBConfiguration, self.conf) + with pytest.raises(ValueError): + log_proxy.DBConfiguration(self.conf) # too long self._config( [ @@ -69,24 +75,27 @@ def test_configuration(self): "ddddddddddddddddddddddddddddddd", ] ) - self.assertRaises(ValueError, proxy.DBConfiguration, self.conf) + with pytest.raises(ValueError): + log_proxy.DBConfiguration(self.conf) # bad collection self._config(["db: test", "collection: kb$log"]) - self.assertRaises(ValueError, proxy.DBConfiguration, self.conf) + with pytest.raises(ValueError): + log_proxy.DBConfiguration(self.conf) # user, no pass self._config(["db: test", "collection: kblog", "user: joe"]) - self.assertRaises(KeyError, proxy.DBConfiguration, self.conf) + with pytest.raises(KeyError): + log_proxy.DBConfiguration(self.conf) class LogRecordTest(unittest.TestCase): def setUp(self): - if proxy.g_log is None: - proxy.g_log = logging.getLogger(proxy.LOGGER_NAME) + if log_proxy.g_log is None: + log_proxy.g_log = logging.getLogger(log_proxy.LOGGER_NAME) def test_basic(self): for input in {}, {"message": "hello"}: - kbrec = proxy.DBRecord(input) - kbrec = proxy.DBRecord({"message": "greeting;Hello=World"}) + kbrec = log_proxy.DBRecord(input) + kbrec = log_proxy.DBRecord({"message": "greeting;Hello=World"}) assert kbrec.record["event"] == "greeting" assert kbrec.record["Hello"] == "World" @@ -96,8 +105,9 @@ def test_strict(self): {12: "xanthium"}, {"message": "Hello=World;greeting"}, ): - proxy.DBRecord(inp) - self.assertRaises(ValueError, proxy.DBRecord, inp, strict=True) + log_proxy.DBRecord(inp) + with pytest.raises(ValueError): + log_proxy.DBRecord(inp, strict=True) if __name__ == "__main__": diff --git a/src/biokbase/narrative/tests/util.py b/src/biokbase/narrative/tests/util.py index f2bef94a0f..e4a728c2d4 100644 --- a/src/biokbase/narrative/tests/util.py +++ b/src/biokbase/narrative/tests/util.py @@ -202,20 +202,6 @@ def read_json_file(path): return data -class MyTestCase(unittest.TestCase): - def test_kvparse(self): - for user_input, text, _kvp in ( - ("foo", "foo", {}), - ("name=val", "", {"name": "val"}), - ("a name=val boy", "a boy", {"name": "val"}), - ): - rkvp = {} - rtext = util.parse_kvp(user_input, rkvp) - assert text == rtext - - assert text == rtext - - class SocketServerBuf(socketserver.TCPServer): allow_reuse_address = True