-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add service to load bundle analysis report (#310)
- Loading branch information
1 parent
aabaeae
commit 8d0aaae
Showing
4 changed files
with
76 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from typing import Optional | ||
|
||
from shared.bundle_analysis import BundleAnalysisReport, BundleAnalysisReportLoader | ||
from shared.storage import get_appropriate_storage_service | ||
|
||
from core.models import Commit | ||
from reports.models import CommitReport | ||
from services.archive import ArchiveService | ||
|
||
|
||
def load_report( | ||
commit: Commit, report_code: Optional[str] = None | ||
) -> Optional[BundleAnalysisReport]: | ||
storage = get_appropriate_storage_service() | ||
|
||
commit_report = commit.reports.filter( | ||
report_type=CommitReport.ReportType.BUNDLE_ANALYSIS, | ||
code=report_code, | ||
).first() | ||
if commit_report is None: | ||
return None | ||
|
||
loader = BundleAnalysisReportLoader( | ||
storage_service=storage, | ||
repo_key=ArchiveService.get_archive_hash(commit.repository), | ||
) | ||
return loader.load(commit_report.external_id) |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
from shared.bundle_analysis import BundleAnalysisReport, StoragePaths | ||
from shared.bundle_analysis.storage import get_bucket_name | ||
from shared.storage.memory import MemoryStorageService | ||
|
||
from core.tests.factories import CommitFactory, RepositoryFactory | ||
from reports.models import CommitReport | ||
from reports.tests.factories import CommitReportFactory | ||
from services.archive import ArchiveService | ||
from services.bundle_analysis import load_report | ||
|
||
|
||
@pytest.mark.django_db | ||
@patch("services.bundle_analysis.get_appropriate_storage_service") | ||
def test_load_report(get_storage_service): | ||
storage = MemoryStorageService({}) | ||
get_storage_service.return_value = storage | ||
|
||
repo = RepositoryFactory() | ||
commit = CommitFactory(repository=repo) | ||
|
||
# no commit report record | ||
assert load_report(commit) is None | ||
|
||
commit_report = CommitReportFactory( | ||
commit=commit, report_type=CommitReport.ReportType.BUNDLE_ANALYSIS | ||
) | ||
|
||
storage_path = StoragePaths.bundle_report.path( | ||
repo_key=ArchiveService.get_archive_hash(repo), | ||
report_key=commit_report.external_id, | ||
) | ||
|
||
# nothing in storage | ||
assert load_report(commit) is None | ||
|
||
with open("./services/tests/samples/bundle_report.sqlite", "rb") as f: | ||
storage.write_file(get_bucket_name(), storage_path, f) | ||
|
||
report = load_report(commit) | ||
assert report is not None | ||
assert isinstance(report, BundleAnalysisReport) |