Skip to content

Commit

Permalink
Refactor tests structure
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielEidlin authored and JonatanMartens committed Mar 13, 2022
1 parent 1af614c commit e7ec552
Showing 1 changed file with 60 additions and 48 deletions.
108 changes: 60 additions & 48 deletions tests/unit/job/job_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,75 +8,87 @@


@pytest.mark.asyncio
async def test_success(job_with_adapter):
complete_job_mock = AsyncMock()
job_with_adapter.zeebe_adapter.complete_job = complete_job_mock
class TestSetSuccessStatus:
async def test_updates_job_in_zeebe(self, job_with_adapter):
complete_job_mock = AsyncMock()
job_with_adapter.zeebe_adapter.complete_job = complete_job_mock

await job_with_adapter.set_success_status()
await job_with_adapter.set_success_status()

complete_job_mock.assert_called_with(job_key=job_with_adapter.key, variables=job_with_adapter.variables)
assert job_with_adapter.status == JobStatus.Completed
complete_job_mock.assert_called_with(job_key=job_with_adapter.key, variables=job_with_adapter.variables)

async def test_status_is_set(self, job_with_adapter):
complete_job_mock = AsyncMock()
job_with_adapter.zeebe_adapter.complete_job = complete_job_mock

@pytest.mark.asyncio
async def test_success_no_zeebe_adapter(job_without_adapter):
with pytest.raises(NoZeebeAdapterError):
await job_without_adapter.set_success_status()
await job_with_adapter.set_success_status()

assert job_with_adapter.status == JobStatus.Completed

@pytest.mark.asyncio
async def test_error(job_with_adapter):
throw_error_mock = AsyncMock()
job_with_adapter.zeebe_adapter.throw_error = throw_error_mock
message = str(uuid4())
async def test_raises_without_zeebe_adapter(self, job_without_adapter):
with pytest.raises(NoZeebeAdapterError):
await job_without_adapter.set_success_status()

await job_with_adapter.set_error_status(message)

throw_error_mock.assert_called_with(job_key=job_with_adapter.key, message=message, error_code="")
assert job_with_adapter.status == JobStatus.ErrorThrown
@pytest.mark.asyncio
class TestSetErrorStatus:
async def test_updates_job_in_zeebe(self, job_with_adapter):
throw_error_mock = AsyncMock()
job_with_adapter.zeebe_adapter.throw_error = throw_error_mock
message = str(uuid4())

await job_with_adapter.set_error_status(message)

@pytest.mark.asyncio
async def test_error_with_code(job_with_adapter):
throw_error_mock = AsyncMock()
job_with_adapter.zeebe_adapter.throw_error = throw_error_mock
message = str(uuid4())
error_code = "custom-error-code"
throw_error_mock.assert_called_with(job_key=job_with_adapter.key, message=message, error_code="")

await job_with_adapter.set_error_status(message, error_code)
async def test_updates_job_in_zeebe_with_code(self, job_with_adapter):
throw_error_mock = AsyncMock()
job_with_adapter.zeebe_adapter.throw_error = throw_error_mock
message = str(uuid4())
error_code = "custom-error-code"

throw_error_mock.assert_called_with(job_key=job_with_adapter.key, message=message, error_code=error_code)
assert job_with_adapter.status == JobStatus.ErrorThrown
await job_with_adapter.set_error_status(message, error_code)

throw_error_mock.assert_called_with(job_key=job_with_adapter.key, message=message, error_code=error_code)

@pytest.mark.asyncio
async def test_error_no_zeebe_adapter(job_without_adapter):
with pytest.raises(NoZeebeAdapterError):
async def test_status_is_set(self, job_with_adapter):
throw_error_mock = AsyncMock()
job_with_adapter.zeebe_adapter.throw_error = throw_error_mock
message = str(uuid4())
await job_without_adapter.set_error_status(message)

await job_with_adapter.set_error_status(message)

@pytest.mark.asyncio
async def test_failure(job_with_adapter):
fail_job_mock = AsyncMock()
job_with_adapter.zeebe_adapter.fail_job = fail_job_mock
message = str(uuid4())

await job_with_adapter.set_failure_status(message)
assert job_with_adapter.status == JobStatus.ErrorThrown

fail_job_mock.assert_called_with(
job_key=job_with_adapter.key, retries=job_with_adapter.retries - 1, message=message
)
assert job_with_adapter.status == JobStatus.Failed
async def test_raises_without_zeebe_adapter(self, job_without_adapter):
with pytest.raises(NoZeebeAdapterError):
message = str(uuid4())
await job_without_adapter.set_error_status(message)


@pytest.mark.asyncio
async def test_failure_no_zeebe_adapter(job_without_adapter):
with pytest.raises(NoZeebeAdapterError):
class TestSetFailureStatus:
async def test_updates_job_in_zeebe(self, job_with_adapter):
fail_job_mock = AsyncMock()
job_with_adapter.zeebe_adapter.fail_job = fail_job_mock
message = str(uuid4())
await job_without_adapter.set_failure_status(message)

await job_with_adapter.set_failure_status(message)

fail_job_mock.assert_called_with(
job_key=job_with_adapter.key, retries=job_with_adapter.retries - 1, message=message
)

async def test_status_is_set(self, job_with_adapter):
fail_job_mock = AsyncMock()
job_with_adapter.zeebe_adapter.fail_job = fail_job_mock
message = str(uuid4())

await job_with_adapter.set_failure_status(message)

assert job_with_adapter.status == JobStatus.Failed

def test_equality_raises_not_implemented_on_other_type(job_without_adapter: Job):
with pytest.raises(NotImplementedError):
job_without_adapter == "test"
async def test_raises_without_zeebe_adapter(self, job_without_adapter):
with pytest.raises(NoZeebeAdapterError):
message = str(uuid4())
await job_without_adapter.set_failure_status(message)

0 comments on commit e7ec552

Please sign in to comment.