def test_user_id_assigned_properly(self):
        parameters = {
            'name': 'Bytes - test',
            'cohort': {
                'id': self.cohort.id,
                'name': self.cohort.name,
            },
            'metric': {
                'name': 'BytesAdded',
                'namespaces': '0,1,2',
                'start_date': '2013-01-01 00:00:00',
                'end_date': '2013-01-02 00:00:00',
                'individualResults': True,
                'aggregateResults': True,
                'aggregateSum': True,
                'aggregateAverage': False,
                'aggregateStandardDeviation': False,
            },
        }

        jr = RunReport(parameters, user_id=self.owner_user_id)
        jr.task.delay(jr).get()
        self.session.commit()
        # executing directly the code that will be run by the scheduler
        recurring_reports()

        # make sure all report nodes have a user_id
        no_user_id = self.session.query(func.count(ReportStore)) \
            .filter(ReportStore.user_id == None) \
            .one()[0]
        assert_equals(no_user_id, 0)
    def inject_and_fetch_recurrent_run(self):
        parameters = {
            'name': 'Edits - test',
            'cohort': {
                'id': self.cohort.id,
                'name': self.cohort.name,
            },
            'metric': {
                'name': 'NamespaceEdits',
                'namespaces': [0, 1, 2],
                'start_date': '2013-01-01 00:00:00',
                'end_date': '2013-01-03 00:00:00',
                'individualResults': True,
                'aggregateResults': False,
                'aggregateSum': False,
                'aggregateAverage': False,
                'aggregateStandardDeviation': False,
            },
            'recurrent': True,
        }

        jr = RunReport(parameters, user_id=self.owner_user_id)
        jr.task.delay(jr).get()
        self.session.commit()

        # executing directly the code that will be run by the scheduler
        recurring_reports()
        recurrent_runs = self.session.query(ReportStore) \
            .filter(ReportStore.recurrent_parent_id == jr.persistent_id) \
            .all()

        return recurrent_runs
    def test_user_id_assigned_properly(self):
        parameters = {
            'name': 'Bytes - test',
            'cohort': {
                'id': self.cohort.id,
                'name': self.cohort.name,
            },
            'metric': {
                'name': 'BytesAdded',
                'namespaces': '0,1,2',
                'start_date': '2013-01-01 00:00:00',
                'end_date': '2013-01-02 00:00:00',
                'individualResults': True,
                'aggregateResults': True,
                'aggregateSum': True,
                'aggregateAverage': False,
                'aggregateStandardDeviation': False,
            },
        }

        jr = RunReport(parameters, user_id=self.owner_user_id)
        jr.task.delay(jr).get()
        self.session.commit()
        # executing directly the code that will be run by the scheduler
        recurring_reports()

        # make sure all report nodes have a user_id
        no_user_id = self.session.query(func.count(ReportStore)) \
            .filter(ReportStore.user_id == None) \
            .one()[0]
        assert_equals(no_user_id, 0)
    def test_scheduler(self):
        parameters = {
            'name': 'Edits - test',
            'cohort': {
                'id': self.cohort.id,
                'name': self.cohort.name,
            },
            'metric': {
                'name': 'NamespaceEdits',
                'namespaces': [0, 1, 2],
                'start_date': '2013-01-01 00:00:00',
                'end_date': '2013-01-03 00:00:00',
                'individualResults': True,
                'aggregateResults': False,
                'aggregateSum': False,
                'aggregateAverage': False,
                'aggregateStandardDeviation': False,
            },
            'recurrent': True,
        }

        jr = RunReport(parameters, user_id=self.owner_user_id)
        jr.task.delay(jr).get()
        self.session.commit()

        # executing directly the code that will be run by the scheduler
        recurring_reports()
        recurrent_runs = self.session.query(ReportStore) \
            .filter(ReportStore.recurrent_parent_id == jr.persistent_id) \
            .all()

        # make sure we have one and no more than one recurrent run
        assert_equals(len(recurrent_runs), 1)
    def test_many_parallel_runs(self):
        parameters = {
            'name': 'Edits - test',
            'cohort': {
                'id': self.cohort.id,
                'name': self.cohort.name,
            },
            'metric': {
                'name': 'NamespaceEdits',
                'namespaces': [0, 1, 2],
                'start_date': '2013-01-01 00:00:00',
                'end_date': '2013-01-03 00:00:00',
                'individualResults': True,
                'aggregateResults': False,
                'aggregateSum': False,
                'aggregateAverage': False,
                'aggregateStandardDeviation': False,
            },
            'recurrent': True,
        }

        jr = RunReport(parameters,
                       user_id=self.owner_user_id,
                       created=datetime.today() -
                       timedelta(days=self.total_runs))
        jr.task.delay(jr).get()
        self.session.commit()

        # executing directly the code that will be run by the scheduler
        recurring_reports()

        recurrent_runs = self.session.query(ReportStore) \
            .filter(ReportStore.recurrent_parent_id == jr.persistent_id) \
            .all()

        successful_runs = filter(lambda x: x.status == 'SUCCESS',
                                 recurrent_runs)

        # make sure we have one and no more than one recurrent run
        assert_equal(len(successful_runs), self.total_runs)