1+ import datetime
2+
13from django .core .management import call_command
24from django .test import TestCase
35from elasticsearch_metrics .tests .util import djelme_test_backends
46
57from framework .celery_tasks import app as celery_app
8+ from osf .metrics import reports as es6_reports
9+ from osf .metrics .es8_metrics import (
10+ MonthlyInstitutionSummaryReportEs8 ,
11+ MonthlyInstitutionalUserReportEs8 ,
12+ MonthlyPrivateSpamMetricsReportEs8 ,
13+ MonthlyPublicItemUsageReportEs8 ,
14+ MonthlySpamSummaryReportEs8 ,
15+ )
16+ from osf .metrics .counted_usage import CountedAuthUsage
17+ from osf .metrics .utils import YearMonth
618from osf_tests import factories
19+ from website import settings as website_settings
720
821
922class TestMonthlyReportersGo (TestCase ):
@@ -13,12 +26,55 @@ def setUp(self):
1326 'task_always_eager' : True ,
1427 'task_eager_propagates' : True ,
1528 })
16- # set up data, so each reporter outputs something
29+ self ._report_yearmonth = YearMonth .from_date (datetime .date .today ())
30+ # set up for institutional-user report
1731 _inst = factories .InstitutionFactory ()
1832 _user = factories .UserFactory ()
1933 _user .add_or_update_affiliated_institution (_inst )
20- factories .PreprintFactory ()
34+ # set up for public item usage report
35+ _reg = factories .RegistrationFactory (is_public = True )
36+ CountedAuthUsage .record (
37+ platform_iri = website_settings .DOMAIN ,
38+ item_guid = _reg ._id ,
39+ session_id = 'blarg' ,
40+ user_is_authenticated = True ,
41+ action_labels = ['view' , 'web' ],
42+ )
43+ CountedAuthUsage ._get_connection ().indices .refresh (CountedAuthUsage ._template_pattern )
44+ # TODO when switching to use es8 data
45+ # OsfCountedUsageEvent.record(
46+ # item_osfid=_preprint._id,
47+ # action_labels=['view', 'web'],
48+ # user_id=_user._id,
49+ # )
50+ # OsfCountedUsageEvent.refresh()
2151
2252 def test_for_smoke (self ):
23- call_command ('monthly_reporters_go' )
24- # TODO: assert more specifically
53+ self ._assert_count (MonthlyInstitutionSummaryReportEs8 , 0 )
54+ self ._assert_count (MonthlyInstitutionalUserReportEs8 , 0 )
55+ self ._assert_count (MonthlyPrivateSpamMetricsReportEs8 , 0 )
56+ self ._assert_count (MonthlyPublicItemUsageReportEs8 , 0 )
57+ self ._assert_count (MonthlySpamSummaryReportEs8 , 0 )
58+ self ._assert_count (es6_reports .SpamSummaryReport , 0 )
59+ self ._assert_count (es6_reports .InstitutionalUserReport , 0 )
60+ self ._assert_count (es6_reports .InstitutionMonthlySummaryReport , 0 )
61+ self ._assert_count (es6_reports .PublicItemUsageReport , 0 )
62+ self ._assert_count (es6_reports .PrivateSpamMetricsReport , 0 )
63+ call_command ('monthly_reporters_go' , yearmonth = str (self ._report_yearmonth ))
64+ self ._assert_count (MonthlyInstitutionSummaryReportEs8 , 1 )
65+ self ._assert_count (MonthlyInstitutionalUserReportEs8 , 1 )
66+ self ._assert_count (MonthlyPrivateSpamMetricsReportEs8 , 1 )
67+ self ._assert_count (MonthlyPublicItemUsageReportEs8 , 1 )
68+ self ._assert_count (MonthlySpamSummaryReportEs8 , 1 )
69+ self ._assert_count (es6_reports .SpamSummaryReport , 1 )
70+ self ._assert_count (es6_reports .InstitutionalUserReport , 1 )
71+ self ._assert_count (es6_reports .InstitutionMonthlySummaryReport , 1 )
72+ self ._assert_count (es6_reports .PublicItemUsageReport , 1 )
73+ self ._assert_count (es6_reports .PrivateSpamMetricsReport , 1 )
74+
75+ def _assert_count (self , recordtype , expected_count ):
76+ if hasattr (recordtype , 'refresh' ):
77+ recordtype .refresh ()
78+ else : # elasticsearch_metrics.imps.elastic6
79+ recordtype ._get_connection ().indices .refresh (recordtype ._template_pattern )
80+ self .assertEqual (recordtype .search ().count (), expected_count )
0 commit comments