Skip to content

Commit

Permalink
Implement unit tests to catch issue #343.
Browse files Browse the repository at this point in the history
  • Loading branch information
csadorf committed Aug 20, 2020
1 parent 1a7a933 commit 1b13dc2
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down

0 comments on commit 1b13dc2

Please sign in to comment.