def test_cohort_membership_basic_detail(self): def get_for_display_mock(session, user_id, by_id): assert_equal(by_id, unicode(self.cohort.id)) return self.cohort cohort_service_mock = CohortService() cohort_service_mock.get_for_display = get_for_display_mock with cohort_service_set(app, cohort_service_mock): response = self.app.get('/cohorts/{0}/membership'.format( self.cohort.id)) assert_equal(response.status_code, 200) assert_true(response.data.find(self.cohort.name) >= 0)
def setup_cohort_service(): if request.endpoint is not None: if request.path.startswith('/cohorts'): cohort_service = getattr(g, 'cohort_service', None) tag_service = getattr(g, 'tag_service', None) centralauth_service = getattr(g, 'centralauth_service', None) if cohort_service is None: g.cohort_service = CohortService() if tag_service is None: g.tag_service = TagService() if centralauth_service is None: g.centralauth_service = CentralAuthService()
def setup_filemanager(): if request.endpoint is not None: if request.path.startswith('/reports'): file_manager = getattr(g, 'file_manager', None) if file_manager is None: g.file_manager = PublicReportFileManager( app.logger, app.absolute_path_to_app_root) cohort_service = getattr(g, 'cohort_service', None) centralauth_service = getattr(g, 'centralauth_service', None) if cohort_service is None: g.cohort_service = CohortService() if centralauth_service is None: g.centralauth_service = CentralAuthService()
def __init__(self, cohort, metric, *args, **kwargs): """ Parameters: metric : an instance of a Metric class cohort : a logical cohort object args : should include any parameters needed by ReportNode kwargs : should include any parameters needed by ReportNode """ super(MultiProjectMetricReport, self).__init__(*args, **kwargs) cohort_service = CohortService() self.children = [] for project, user_ids in cohort_service.get_users_by_project(cohort): # note that user_ids is actually just an iterator self.children.append( MetricReport(metric, cohort.id, user_ids, project, *args, **kwargs))
def __init__(self, cohort, metric, *args, **kwargs): """ Parameters: metric : an instance of a Metric class cohort : a logical cohort object args : should include any parameters needed by ReportNode kwargs : should include any parameters needed by ReportNode """ super(SumAggregateByUserReport, self).__init__(*args, **kwargs) # Get mediawiki's username map to be able to aggregate. service = CohortService() session = db.get_session() self.usernames = service.get_wikiusernames_for_cohort(cohort.id, session) self.children = [ MultiProjectMetricReport(cohort, metric, *args, **kwargs) ]
def test_delete_cohort_wikiuser(self): username_to_delete = 'To Delete' def delete_cohort_wikiuser_mock(raw_id_or_name, cohort_id, current_user_id, session, invalid_only): assert_equal(raw_id_or_name, username_to_delete) assert_equal(cohort_id, unicode(self.cohort.id)) assert_equal(invalid_only, False) cohort_service_mock = CohortService() cohort_service_mock.delete_cohort_wikiuser = delete_cohort_wikiuser_mock with cohort_service_set(app, cohort_service_mock): response = self.app.post( '/cohorts/{0}/membership/delete'.format(self.cohort.id), data={'raw_id_or_name': username_to_delete}) assert_equal(response.status_code, 200) assert_equal(json.loads(response.data)['message'], 'success')
def test_cohort_membership_full_detail(self): def get_for_display_mock(session, user_id, by_id): assert_equal(by_id, unicode(self.cohort.id)) return self.cohort def get_membership_mock(cohort, session): assert_equal(cohort, self.cohort) return 'mock' cohort_service_mock = CohortService() cohort_service_mock.get_for_display = get_for_display_mock cohort_service_mock.get_membership = get_membership_mock with cohort_service_set(app, cohort_service_mock): response = self.app.get( '/cohorts/{0}/membership?full_detail=true'.format( self.cohort.id)) assert_equal(response.status_code, 200) assert_equal(json.loads(response.data), {'membership': 'mock'})
def setUp(self): """ NOTE: self.cohort is a CohortStore object. When testing CohortService, one should use logical cohort objects (ie. FixedCohort, WikiCohort, etc.) """ DatabaseTest.setUp(self) self.common_cohort_1() self.editor_user_ids = [e.user_id for e in self.editors] self.cohort_service = CohortService() empty_cohort = CohortStore(enabled=True, class_name='Cohort') self.session.add(empty_cohort) self.session.commit() empty_cohort_user = CohortUserStore( user_id=self.owner_user_id, cohort_id=empty_cohort.id, role=CohortUserRole.OWNER, ) self.session.add(empty_cohort_user) self.session.commit() self.invalid_empty_cohort = CohortStore(enabled=True, class_name='FixedCohort', validated=True) self.session.add(self.invalid_empty_cohort) self.session.commit() invalid_empty_cohort_user = CohortUserStore( user_id=self.owner_user_id, cohort_id=self.invalid_empty_cohort.id, role=CohortUserRole.OWNER, ) self.session.add(invalid_empty_cohort_user) self.session.commit() self.empty_cohort = self.cohort_service.get(self.session, self.owner_user_id, by_id=empty_cohort.id) self.fixed_cohort = self.cohort_service.get(self.session, self.owner_user_id, by_id=self.cohort.id)
def __init__(self, parameters, user_id=0, recurrent_parent_id=None, created=None, persistent_id=None): """ Parameters: parameters : dictionary containing the following required keys: name : the name of the report cohort : dictionary defining the cohort, has keys: id : the id of the cohort metric : dictionary defining the metric, has keys: name : the name of the python class to instantiate recurrent : whether to rerun this daily public : whether to expose results publicly user_id : the user wishing to run this report recurrent_parent_id : the parent ReportStore.id for a recurrent run created : if set, represents the date of a recurrent run. This need not be the timestamp when the recurrent run has been created -- for example when backfilling. Hence, this does not fully match the semantics of the word 'created'. But since neither start nor end date have a separate column in the report table in the database, the covered period for backfilled recurring reports would be hard to identify when only looking at the raw database tables. To overcome this issue, we abuse the created date here. Raises: KeyError if required parameters are missing """ # get cohort cohort_service = CohortService() cohort_dict = parameters['cohort'] session = db.get_session() cohort = cohort_service.get(session, user_id, by_id=cohort_dict['id']) parameters['cohort']['size'] = cohort.size # construct metric metric_dict = parameters['metric'] metric = metric_classes[metric_dict['name']](**metric_dict) # if this is a recurrent run, don't show it in the UI if recurrent_parent_id is not None: self.show_in_ui = False public = parameters.get('public', False) recurrent = parameters.get('recurrent', False) super(RunReport, self).__init__(name=parameters['name'], user_id=user_id, parameters=parameters, public=public, recurrent=recurrent, recurrent_parent_id=recurrent_parent_id, created=created, store=True, persistent_id=persistent_id) self.recurrent_parent_id = recurrent_parent_id self.public = public validate_report = ValidateReport(metric, cohort, recurrent_parent_id is None, user_id=user_id) if validate_report.valid(): if recurrent and recurrent_parent_id is None: # Valid parent recurrent report # We do not add children that compute data as parent recurrent # reports just help the scheduler orchestrate child runs. # However, we employ a NullReport to allow coalescing even # when there was no recurrent run yet. self.children = [NullReport()] else: # Valid, but not a parent recurring report, so we add the child # node that does the real computation self.children = [ AggregateReport(metric, cohort, metric_dict, parameters=parameters, user_id=user_id) ] else: self.children = [validate_report]
from nose.tools import assert_equals, raises, assert_true, assert_is_not_none from tests.fixtures import QueueDatabaseTest, d from wikimetrics.models import RunProgramMetricsReport, ReportStore, WikiUserStore from wikimetrics.utils import parse_pretty_date, format_pretty_date from wikimetrics.enums import Aggregation from wikimetrics.api import CohortService cohort_service = CohortService() def make_pending(report): report.status = 'PENDING' class RunProgramMetricsReportWithInvalidCohort(QueueDatabaseTest): def setUp(self): QueueDatabaseTest.setUp(self) self.common_cohort_1() @raises(Exception) def test_raises_unvalidated_cohort(self): self.cohort.validated = False jr = RunProgramMetricsReport(self.cohort.id, parse_pretty_date('2015-01-01 00:00:00'), parse_pretty_date('2015-01-31 00:00:00'), self.owner_user_id) jr.task.delay(jr).get() def test_invalid_cohort_returns_failure(self): self.cohort.validated = True
def setUp(self): WebTest.setUp(self) self.cohort_service = CohortService()
def setUp(self): DatabaseTest.setUp(self) self.common_cohort_1() self.cohort_service = CohortService()
def get_validation_info(self): cohort_service = CohortService() return cohort_service.get_validation_info(self.cohort, self.session, True)