Skip to content

Commit

Permalink
fix: fix test result env
Browse files Browse the repository at this point in the history
Signed-off-by: joseph-sentry <joseph.sawaya@sentry.io>
  • Loading branch information
joseph-sentry committed Jan 15, 2024
1 parent 481bed4 commit 9284878
Show file tree
Hide file tree
Showing 2 changed files with 247 additions and 3 deletions.
8 changes: 5 additions & 3 deletions tasks/test_results_finisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def check_if_no_success(self, previous_result):
)

def get_or_create_test(self, db_session, test_dict, testsuite, name, repoid, env):
test_hash = hash((testsuite, name))
test_hash = hash((testsuite, name, env))
if test_hash not in test_dict:
test = Test(
repoid=repoid,
Expand All @@ -185,7 +185,7 @@ def get_or_create_test(self, db_session, test_dict, testsuite, name, repoid, env
db_session.add(test)
test_dict.update({test_hash: test})
else:
test = test_dict.get(test_dict)
test = test_dict.get(test_hash)

return test

Expand Down Expand Up @@ -229,7 +229,9 @@ def get_existing_test_instance_by_test(self, db_session, commit):

def get_test_dict(self, db_session, repoid):
existing_tests = db_session.query(Test).filter(Test.repoid == repoid)
return {hash((test.testsuite, test.name)) for test in existing_tests}
return {
hash((test.testsuite, test.name, test.env)): test for test in existing_tests
}


RegisteredTestResultsFinisherTask = celery_app.register_task(TestResultsFinisherTask())
Expand Down
242 changes: 242 additions & 0 deletions tasks/tests/unit/test_test_results_finisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,248 @@ async def test_upload_finisher_task_call(
)
assert expected_result == result

@pytest.mark.asyncio
@pytest.mark.integration
async def test_upload_finisher_task_call_existing_tests_diff_env(
self,
mocker,
mock_configuration,
dbsession,
codecov_vcr,
mock_storage,
mock_redis,
celery_app,
):
upload = UploadFactory.create()
dbsession.add(upload)
dbsession.flush()

mocker.patch.object(TestResultsFinisherTask, "app", celery_app)

commit = CommitFactory.create(
message="hello world",
commitid="cd76b0821854a780b60012aed85af0a8263004ad",
repository__owner__unencrypted_oauth_token="test7lk5ndmtqzxlx06rip65nac9c7epqopclnoy",
repository__owner__username="joseph-sentry",
repository__owner__service="github",
repository__name="codecov-demo",
)

pull = PullFactory.create(repository=commit.repository, head=commit.commitid)

_ = mocker.patch(
"services.test_results.fetch_and_update_pull_request_information_from_commit",
return_value=EnrichedPull(
database_pull=pull,
provider_pull={},
),
)
m = mocker.MagicMock(
edit_comment=AsyncMock(return_value=True),
post_comment=AsyncMock(return_value={"id": 1}),
)
mocked_repo_provider = mocker.patch(
"services.test_results.get_repo_provider_service",
return_value=m,
)

dbsession.add(commit)
dbsession.flush()
current_report_row = CommitReport(
commit_id=commit.id_, report_type=ReportType.TEST_RESULTS.value
)
dbsession.add(current_report_row)
dbsession.flush()
upload.report = current_report_row
dbsession.flush()

repoid = upload.report.commit.repoid

test1 = Test(
repoid=repoid,
name="api.temp.calculator.test_calculator::test_add",
testsuite="pytest",
env="a",
)
dbsession.add(test1)
dbsession.flush()

result = await TestResultsFinisherTask().run_async(
dbsession,
[
[
{
"successful": True,
"upload_id": upload.id,
"env": "b",
"run_number": None,
"testrun_list": [
{
"duration_seconds": 0.001,
"name": "api.temp.calculator.test_calculator::test_add",
"outcome": int(Outcome.Pass),
"testsuite": "pytest",
"failure_message": None,
},
{
"duration_seconds": 0.001,
"name": "api.temp.calculator.test_calculator::test_subtract",
"outcome": int(Outcome.Pass),
"testsuite": "pytest",
"failure_message": None,
},
{
"duration_seconds": 0.0,
"name": "api.temp.calculator.test_calculator::test_multiply",
"outcome": int(Outcome.Pass),
"testsuite": "pytest",
"failure_message": None,
},
{
"duration_seconds": 0.001,
"name": "hello world",
"outcome": int(Outcome.Failure),
"testsuite": "hello world testsuite",
"failure_message": "bad failure",
},
],
}
],
],
repoid=repoid,
commitid=commit.commitid,
commit_yaml={"codecov": {"max_report_age": False}},
)
expected_result = {"notify_attempted": True, "notify_succeeded": True}
m.post_comment.assert_called_with(
pull.pullid,
"## [Codecov](url) Report\n\n**Test Failures Detected**: Due to failing tests, we cannot provide coverage reports at this time.\n\n### :x: Failed Test Results: \nCompleted 4 tests with **`1 failed`**, 3 passed and 0 skipped.\n<details><summary>View the full list of failed tests</summary>\n\n| **File path** | **Failure message** |\n| :-- | :-- |\n| hello world testsuite::hello world | <pre>bad failure</pre> |",
)
assert expected_result == result
tests = dbsession.query(Test).filter_by(repoid=repoid).all()
assert len(tests) == 5

@pytest.mark.asyncio
@pytest.mark.integration
async def test_upload_finisher_task_call_existing_tests(
self,
mocker,
mock_configuration,
dbsession,
codecov_vcr,
mock_storage,
mock_redis,
celery_app,
):
upload = UploadFactory.create()
dbsession.add(upload)
dbsession.flush()

mocker.patch.object(TestResultsFinisherTask, "app", celery_app)

commit = CommitFactory.create(
message="hello world",
commitid="cd76b0821854a780b60012aed85af0a8263004ad",
repository__owner__unencrypted_oauth_token="test7lk5ndmtqzxlx06rip65nac9c7epqopclnoy",
repository__owner__username="joseph-sentry",
repository__owner__service="github",
repository__name="codecov-demo",
)

pull = PullFactory.create(repository=commit.repository, head=commit.commitid)

_ = mocker.patch(
"services.test_results.fetch_and_update_pull_request_information_from_commit",
return_value=EnrichedPull(
database_pull=pull,
provider_pull={},
),
)
m = mocker.MagicMock(
edit_comment=AsyncMock(return_value=True),
post_comment=AsyncMock(return_value={"id": 1}),
)
mocked_repo_provider = mocker.patch(
"services.test_results.get_repo_provider_service",
return_value=m,
)

dbsession.add(commit)
dbsession.flush()
current_report_row = CommitReport(
commit_id=commit.id_, report_type=ReportType.TEST_RESULTS.value
)
dbsession.add(current_report_row)
dbsession.flush()
upload.report = current_report_row
dbsession.flush()

test1 = Test(
repoid=upload.report.commit.repoid,
name="api.temp.calculator.test_calculator::test_add",
testsuite="pytest",
env="",
)
dbsession.add(test1)
dbsession.flush()

result = await TestResultsFinisherTask().run_async(
dbsession,
[
[
{
"successful": True,
"upload_id": upload.id,
"env": "",
"run_number": None,
"testrun_list": [
{
"duration_seconds": 0.001,
"name": "api.temp.calculator.test_calculator::test_add",
"outcome": int(Outcome.Pass),
"testsuite": "pytest",
"failure_message": None,
},
{
"duration_seconds": 0.001,
"name": "api.temp.calculator.test_calculator::test_subtract",
"outcome": int(Outcome.Pass),
"testsuite": "pytest",
"failure_message": None,
},
{
"duration_seconds": 0.0,
"name": "api.temp.calculator.test_calculator::test_multiply",
"outcome": int(Outcome.Pass),
"testsuite": "pytest",
"failure_message": None,
},
{
"duration_seconds": 0.001,
"name": "hello world",
"outcome": int(Outcome.Failure),
"testsuite": "hello world testsuite",
"failure_message": "bad failure",
},
],
}
],
],
repoid=upload.report.commit.repoid,
commitid=commit.commitid,
commit_yaml={"codecov": {"max_report_age": False}},
)
expected_result = {"notify_attempted": True, "notify_succeeded": True}
m.post_comment.assert_called_with(
pull.pullid,
"## [Codecov](url) Report\n\n**Test Failures Detected**: Due to failing tests, we cannot provide coverage reports at this time.\n\n### :x: Failed Test Results: \nCompleted 4 tests with **`1 failed`**, 3 passed and 0 skipped.\n<details><summary>View the full list of failed tests</summary>\n\n| **File path** | **Failure message** |\n| :-- | :-- |\n| hello world testsuite::hello world | <pre>bad failure</pre> |",
)
assert expected_result == result
tests = (
dbsession.query(Test).filter_by(repoid=upload.report.commit.repoid).all()
)
assert len(tests) == 4

@pytest.mark.asyncio
@pytest.mark.integration
async def test_upload_finisher_task_call_no_failures(
Expand Down

0 comments on commit 9284878

Please sign in to comment.