Skip to content

Commit 0d3164b

Browse files
committed
tests improve
1 parent 3481f14 commit 0d3164b

6 files changed

Lines changed: 26 additions & 21 deletions

File tree

osf/metrics/reporters/institution_summary_monthly.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ def iter_report_kwargs(self, continue_after: dict | None = None):
2222
def report(self, **report_kwargs):
2323
_institution = Institution.objects.get(pk=report_kwargs['institution_pk'])
2424
reports = self.generate_report(_institution)
25-
_report = next(r for r in reports if isinstance(r, InstitutionMonthlySummaryReport))
26-
return _report
25+
return reports
2726

2827
def generate_report(self, institution):
2928
node_queryset = institution.nodes.filter(

osf/metrics/reporters/private_spam_metrics.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from ._base import MonthlyReporter
55
from osf.metrics.es8_metrics import PrivateSpamMetricsReportEs8
66

7-
87
class PrivateSpamMetricsReporter(MonthlyReporter):
98
report_name = 'Private Spam Metrics'
109

osf/metrics/reporters/public_item_usage.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,16 @@ def report(self, **report_kwargs):
6565
raise _SkipItem
6666
_obj = _guid.referent
6767
_reports = self._init_report(_obj)
68-
_report = next(r for r in _reports if isinstance(r, PublicItemUsageReport))
69-
self._fill_report_counts(_report, _obj)
70-
if not any((
71-
_report.view_count,
72-
_report.view_session_count,
73-
_report.download_count,
74-
_report.download_session_count,
75-
)):
76-
raise _SkipItem
77-
return _report
68+
for _report in _reports:
69+
self._fill_report_counts(_report, _obj)
70+
if not any((
71+
_report.view_count,
72+
_report.view_session_count,
73+
_report.download_count,
74+
_report.download_session_count,
75+
)):
76+
raise _SkipItem
77+
return _reports
7878
except _SkipItem:
7979
return None
8080

osf_tests/metrics/reporters/test_institutional_summary_reporter.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import logging
44
from django.test import TestCase
55
from osf.metrics.reporters import InstitutionalSummaryMonthlyReporter
6+
from osf.metrics.reports import InstitutionMonthlySummaryReport
67
from osf.metrics.utils import YearMonth
78
from osf_tests.factories import (
89
InstitutionFactory,
@@ -79,10 +80,10 @@ def _create_active_user(cls, institution, date_confirmed):
7980

8081
def test_report_generation(self):
8182
reporter = InstitutionalSummaryMonthlyReporter(self._yearmonth)
82-
reports = list_monthly_reports(reporter)
83-
self.assertEqual(len(reports), 1)
83+
reports_raw = list_monthly_reports(reporter)
84+
self.assertEqual(len(reports_raw[0]), 2)
8485

85-
report = reports[0]
86+
report = next(r for r in reports_raw[0] if isinstance(r, InstitutionMonthlySummaryReport))
8687
self.assertEqual(report.institution_id, self._institution._id)
8788
self.assertEqual(report.user_count, 2) # _logged_in_user and _active_user
8889
self.assertEqual(report.public_project_count, 1)
@@ -115,7 +116,8 @@ def test_report_generation_multiple_institutions(self):
115116

116117
# Run the reporter for the current month (February 2018)
117118
reporter = InstitutionalSummaryMonthlyReporter(self._yearmonth)
118-
reports = list_monthly_reports(reporter)
119+
reports_raw= list_monthly_reports(reporter)
120+
reports = [item for sublist in reports_raw for item in sublist if isinstance(item, InstitutionMonthlySummaryReport)]
119121
self.assertEqual(len(reports), 3) # Reports for self._institution, institution2, institution3
120122

121123
# Extract reports by institution
@@ -264,7 +266,8 @@ def test_high_counts_multiple_institutions(self):
264266
if enable_benchmarking:
265267
reporter_start_time = time.time()
266268
reporter = InstitutionalSummaryMonthlyReporter(self._yearmonth)
267-
reports = list_monthly_reports(reporter)
269+
reports_raw = list_monthly_reports(reporter)
270+
reports = [item for sublist in reports_raw for item in sublist if isinstance(item, InstitutionMonthlySummaryReport)]
268271
assert len(reports) == additional_institution_count + 1
269272

270273
if enable_benchmarking:

osf_tests/metrics/reporters/test_public_item_usage_reporter.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,10 @@ def test_no_data(self, ym_empty):
174174

175175
def test_reporter(self, ym_empty, ym_sparse, ym_busy, sparse_month_usage, busy_month_item0, busy_month_item1, busy_month_item2, item0):
176176
_empty = list_monthly_reports(PublicItemUsageReporter(ym_empty))
177-
_sparse = list_monthly_reports(PublicItemUsageReporter(ym_sparse))
178-
_busy = list_monthly_reports(PublicItemUsageReporter(ym_busy))
177+
_sparse_raw = list_monthly_reports(PublicItemUsageReporter(ym_sparse))
178+
_sparse = [item for sublist in _sparse_raw for item in sublist if isinstance(item, PublicItemUsageReport)]
179+
_busy_raw = list_monthly_reports(PublicItemUsageReporter(ym_busy))
180+
_busy = [item for sublist in _busy_raw for item in sublist if isinstance(item, PublicItemUsageReport)]
179181

180182
# empty month:
181183
assert _empty == []

osf_tests/metrics/test_spam_count_reporter.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22
from datetime import datetime
33
from osf.metrics.reporters.private_spam_metrics import PrivateSpamMetricsReporter
4+
from osf.metrics.reports import PrivateSpamMetricsReport
45
from osf.metrics.utils import YearMonth
56
from osf_tests.factories import NodeLogFactory, NodeFactory
67
from unittest.mock import patch
@@ -30,7 +31,8 @@ def test_private_spam_metrics_reporter():
3031
mock_akismet_get_hammed_count.return_value = 10
3132

3233
reporter = PrivateSpamMetricsReporter(report_yearmonth)
33-
report = reporter.report()
34+
reports_raw = reporter.report()
35+
report = next(r for r in reports_raw if isinstance(r, PrivateSpamMetricsReport))
3436

3537
assert report.node_oopspam_flagged == 10, f"Expected 10, got {report.node_oopspam_flagged}"
3638
assert report.node_oopspam_hammed == 5, f"Expected 5, got {report.node_oopspam_hammed}"

0 commit comments

Comments
 (0)