From 1b13dc2aefaa31b9b0a0c210bf597d08a15e7ced Mon Sep 17 00:00:00 2001 From: Carl Simon Adorf Date: Thu, 20 Aug 2020 18:15:28 +0200 Subject: [PATCH] Implement unit tests to catch issue #343. --- tests/test_project.py | 59 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/tests/test_project.py b/tests/test_project.py index 314822297..97ce072bf 100644 --- a/tests/test_project.py +++ b/tests/test_project.py @@ -47,10 +47,14 @@ def suspend_logging(): class MockScheduler(Scheduler): _jobs = {} # needs to be singleton _scripts = {} + _invalid_chars = [] @classmethod def jobs(cls): for job in cls._jobs.values(): + for char in cls._invalid_chars: + if char in job._id(): + raise RuntimeError(f"Invalid character in job id: {char}") yield job @classmethod @@ -1391,6 +1395,36 @@ def test_submit_groups(self): project.submit_operations(_id=cluster_job_id, operations=operations) assert len(list(MockScheduler.jobs())) == 1 + def test_submit_groups_invalid_char_with_error(self, monkeypatch): + monkeypatch.setattr(MockScheduler, '_invalid_chars', ['/']) + MockScheduler.reset() + project = self.mock_project() + operations = [project.groups['group1']._create_submission_job_operation( + project._entrypoint, dict(), job) for job in project] + assert len(list(MockScheduler.jobs())) == 0 + cluster_job_id = project._store_bundled(operations) + with redirect_stderr(StringIO()): + project.submit_operations(_id=cluster_job_id, operations=operations) + with pytest.raises(RuntimeError): + assert len(list(MockScheduler.jobs())) == 1 + MockScheduler.reset() + + def test_submit_groups_invalid_char_avoid_error(self, monkeypatch): + monkeypatch.setattr(MockScheduler, '_invalid_chars', ['/']) + monkeypatch.setattr(MockEnvironment, 'JOB_ID_SEPARATOR', '-', raising=False) + + MockScheduler.reset() + project = self.mock_project() + operations = [project.groups['group1']._create_submission_job_operation( + project._entrypoint, dict(), job) for job in project] + assert len(list(MockScheduler.jobs())) == 0 + cluster_job_id = project._store_bundled(operations) + with redirect_stderr(StringIO()): + project.submit_operations(_id=cluster_job_id, operations=operations) + with pytest.raises(RuntimeError): + assert len(list(MockScheduler.jobs())) == 1 + MockScheduler.reset() + def test_submit(self): MockScheduler.reset() project = self.mock_project() @@ -1401,6 +1435,31 @@ def test_submit(self): assert len(list(MockScheduler.jobs())) == num_jobs_submitted MockScheduler.reset() + def test_submit_invalid_char_with_error(self, monkeypatch): + monkeypatch.setattr(MockScheduler, '_invalid_chars', ['/']) + MockScheduler.reset() + project = self.mock_project() + assert len(list(MockScheduler.jobs())) == 0 + with redirect_stderr(StringIO()): + project.submit(names=['group1', 'group2']) + num_jobs_submitted = 2 * len(project) + with pytest.raises(RuntimeError): + assert len(list(MockScheduler.jobs())) == num_jobs_submitted + MockScheduler.reset() + + def test_submit_invalid_char_avoid_error(self, monkeypatch): + monkeypatch.setattr(MockScheduler, '_invalid_chars', ['/']) + monkeypatch.setattr(MockEnvironment, 'JOB_ID_SEPARATOR', '-', raising=False) + + MockScheduler.reset() + project = self.mock_project() + assert len(list(MockScheduler.jobs())) == 0 + with redirect_stderr(StringIO()): + project.submit(names=['group1', 'group2']) + num_jobs_submitted = 2 * len(project) + assert len(list(MockScheduler.jobs())) == num_jobs_submitted + MockScheduler.reset() + def test_group_resubmit(self): MockScheduler.reset() project = self.mock_project()