Skip to content
This repository was archived by the owner on May 5, 2025. It is now read-only.

Commit 1a01551

Browse files
authored
Handle no base report (#143)
* Handle no base report * Added tests * added assertion
1 parent 7a21d8e commit 1a01551

File tree

4 files changed

+45
-5
lines changed

4 files changed

+45
-5
lines changed

internal_api/compare/serializers.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,15 @@ class ComparisonFullSrcSerializer(serializers.Serializer):
3737
untracked_files = serializers.SerializerMethodField()
3838

3939
def __init__(self, obj, *args, **kwargs):
40-
self.tracked_file_names = set(
41-
[f.name for f in obj.base_report.file_reports()] +
42-
[f.name for f in obj.head_report.file_reports()]
43-
)
40+
if (obj.base_report):
41+
self.tracked_file_names = set(
42+
[f.name for f in obj.base_report.file_reports()] +
43+
[f.name for f in obj.head_report.file_reports()]
44+
)
45+
else:
46+
self.tracked_file_names = set(
47+
[f.name for f in obj.head_report.file_reports()]
48+
)
4449
super().__init__(obj, *args, **kwargs)
4550

4651
def get_tracked_files(self, obj):

internal_api/tests/unit/views/test_compare_view.py

+29-1
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ def setUp(self):
436436
self.repo = RepositoryFactory()
437437
self.commit_base = CommitFactory(repository=self.repo)
438438
self.commit_head = CommitFactory(repository=self.repo)
439-
439+
self.commit_base_no_report = CommitFactory(repository=self.repo, report=None)
440440
self.client.force_login(user=self.repo.author)
441441

442442
def test_returns_calculated_diff_data_with_commit_refs(self, mocked_comparison, *_):
@@ -566,3 +566,31 @@ def test_correctly_sorts_tracked_and_untracked_files(self, mocked_head_file_repo
566566
if file_name != made_up_file:
567567
continue
568568
assert file_name in response.data["untracked_files"]
569+
570+
def test_missing_base_report(self, mocked_comparison, *_):
571+
self._configure_comparison_mock_with_commit_factory_report(mocked_comparison)
572+
expected_data = {
573+
'tracked_files': {
574+
'awesome/__init__.py': {
575+
'segments': True},
576+
'tests/__init__.py': {
577+
'segments': True
578+
},
579+
'tests/test_sample.py': {
580+
'segments': True
581+
}
582+
},
583+
'untracked_files': []
584+
}
585+
response = self._get_compare_src(
586+
kwargs={
587+
"orgName": self.repo.author.username,
588+
"repoName": self.repo.name
589+
},
590+
query_params={
591+
"head": self.commit_head.commitid,
592+
"base": self.commit_base_no_report.commitid
593+
}
594+
)
595+
assert response.status_code == status.HTTP_200_OK
596+
assert response.data == expected_data

services/archive.py

+2
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ def build_report_from_commit(self, commit):
248248
Returns:
249249
SerializableReport: A report with all information from such commit
250250
"""
251+
if not commit.report:
252+
return None
251253
commitid = commit.commitid
252254
chunks = ArchiveService(commit.repository).read_chunks(commitid)
253255
files = commit.report['files']

services/tests/test_archive.py

+5
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,8 @@ def test_build_report_from_commit_with_flags(self, db, mocker, codecov_vcr):
117117
assert tuple(file_3.totals) == (0, 7, 2, 5, 0, '28.57143', 0, 0, 0, 0, 0, 0, 0)
118118
mocked.assert_called_with('abf6d4d')
119119
assert list(res.totals) == [3, 20, 3, 17, 0, '15.00000', 0, 0, 0, 2, 0, 0, 0]
120+
121+
def test_build_report_from_commit_no_report(self, db, mocker, codecov_vcr):
122+
commit = CommitFactory(report=None)
123+
report = ReportService().build_report_from_commit(commit)
124+
assert report is None

0 commit comments

Comments
 (0)