Skip to content

Commit

Permalink
Refactor Upload task code a bit
Browse files Browse the repository at this point in the history
This does a couple of things:
- Moves some logging out to the main task function
- Removes some task checks, as there is already a check for `argument_list` in the main task fn
- Also removes checks for `checkpoints`, as those should always exist, and adds assertions for that
  • Loading branch information
Swatinem committed Aug 26, 2024
1 parent 93c03f9 commit 45d51b3
Show file tree
Hide file tree
Showing 2 changed files with 210 additions and 307 deletions.
53 changes: 24 additions & 29 deletions tasks/tests/unit/test_upload_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,7 @@ def test_upload_task_no_bot(
assert commit.message == ""
assert commit.parent_commit_id is None
mocked_1.assert_called_with(
mocker.ANY,
commit,
UserYaml({"codecov": {"max_report_age": "764y ago"}}),
[
Expand All @@ -733,7 +734,6 @@ def test_upload_task_no_bot(
commit.report,
mocker.ANY,
mocker.ANY,
mocker.ANY,
)
assert not mocked_fetch_yaml.called

Expand Down Expand Up @@ -780,6 +780,7 @@ def test_upload_task_bot_no_permissions(
assert commit.message == ""
assert commit.parent_commit_id is None
mocked_1.assert_called_with(
mocker.ANY,
commit,
UserYaml({"codecov": {"max_report_age": "764y ago"}}),
[
Expand All @@ -789,7 +790,6 @@ def test_upload_task_bot_no_permissions(
commit.report,
mocker.ANY,
mocker.ANY,
mocker.ANY,
)
assert not mocked_fetch_yaml.called

Expand Down Expand Up @@ -835,9 +835,9 @@ def test_upload_task_bot_unauthorized(
commitid=commit.commitid,
redis_connection=mock_redis,
)
mock_checkpoints = MagicMock(name="checkpoints")
result = UploadTask().run_impl_within_lock(
dbsession,
upload_args,
dbsession, upload_args, checkpoints=mock_checkpoints, kwargs={}
)
assert {"was_setup": False, "was_updated": False} == result
assert commit.message == ""
Expand All @@ -857,6 +857,7 @@ def test_upload_task_bot_unauthorized(
.first()
)
mocked_schedule_task.assert_called_with(
mocker.ANY,
commit,
UserYaml({"codecov": {"max_report_age": "764y ago"}}),
[
Expand All @@ -866,7 +867,6 @@ def test_upload_task_bot_unauthorized(
commit.report,
mocker.ANY,
mocker.ANY,
mocker.ANY,
)

def test_upload_task_upload_already_created(
Expand Down Expand Up @@ -938,16 +938,17 @@ def fail_if_try_to_create_upload(*args, **kwargs):
commitid=commit.commitid,
redis_connection=mock_redis,
)
mock_checkpoints = MagicMock(name="checkpoints")
result = UploadTask().run_impl_within_lock(
dbsession,
upload_args,
dbsession, upload_args, checkpoints=mock_checkpoints, kwargs={}
)
assert {"was_setup": True, "was_updated": True} == result
assert commit.message == ""
assert commit.parent_commit_id is None
assert commit.report is not None
assert commit.report.details is not None
mocked_schedule_task.assert_called_with(
mocker.ANY,
commit,
UserYaml({"codecov": {"max_report_age": "764y ago"}}),
[
Expand All @@ -961,7 +962,6 @@ def fail_if_try_to_create_upload(*args, **kwargs):
report,
mocker.ANY,
mocker.ANY,
mocker.ANY,
)


Expand Down Expand Up @@ -1067,37 +1067,29 @@ def test_normalize_upload_arguments(
assert b"Some weird value" == content

@pytest.mark.django_db(databases={"default"})
def test_schedule_task_with_no_tasks(self, dbsession, mocker):
def test_schedule_task_with_one_task(self, dbsession, mocker):
mocked_chain = mocker.patch("tasks.upload.chain")
commit = CommitFactory.create()
commit_yaml = UserYaml({})
argument_list = []
commit_yaml = UserYaml({"codecov": {"max_report_age": "100y ago"}})
argument_dict = {"argument_dict": 1}
argument_list = [argument_dict]
dbsession.add(commit)
dbsession.flush()
upload_args = UploadContext(
repoid=commit.repoid,
commitid=commit.commitid,
redis_connection=mock_redis,
)
mock_checkpoints = MagicMock(name="checkpoints")
result = UploadTask().schedule_task(
dbsession,
commit,
commit_yaml,
argument_list,
ReportFactory.create(),
None,
dbsession,
upload_args,
checkpoints=mock_checkpoints,
)
assert result is None
mock_checkpoints.log.assert_called_with(UploadFlow.NO_REPORTS_FOUND)

@pytest.mark.django_db(databases={"default"})
def test_schedule_task_with_one_task(self, dbsession, mocker):
mocked_chain = mocker.patch("tasks.upload.chain")
commit = CommitFactory.create()
commit_yaml = UserYaml({"codecov": {"max_report_age": "100y ago"}})
argument_dict = {"argument_dict": 1}
argument_list = [argument_dict]
dbsession.add(commit)
dbsession.flush()
result = UploadTask().schedule_task(
commit, commit_yaml, argument_list, ReportFactory.create(), None, dbsession
)
assert result == mocked_chain.return_value.apply_async.return_value
t1 = upload_processor_task.s(
{},
Expand Down Expand Up @@ -1492,5 +1484,8 @@ def test_upload_not_ready_to_build_report(
commitid=commit.commitid,
redis_connection=mock_redis,
)
mock_checkpoints = MagicMock(name="checkpoints")
with pytest.raises(Retry):
task.run_impl_within_lock(dbsession, upload_args)
task.run_impl_within_lock(
dbsession, upload_args, checkpoints=mock_checkpoints, kwargs={}
)
Loading

0 comments on commit 45d51b3

Please sign in to comment.