<commit_before>from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from datetime import datetime, timedelta
from django.test import SimpleTestCase, TestCase
from corehq.apps.domain.shortcuts import create_domain
from corehq.apps.saved_reports.models import ReportConfig, ReportNotification
from corehq.apps.saved_reports.scheduled import guess_reporting_minute, get_scheduled_report_ids
from corehq.apps.reports.views import get_scheduled_report_response
from corehq.apps.users.models import WebUser


class GuessReportingMinuteTest(SimpleTestCase):

    def testOnTheHour(self):
        self.assertEqual(0, guess_reporting_minute(datetime(2014, 10, 31, 12, 0)))

    def testAfterTheHour(self):
        self.assertEqual(0, guess_reporting_minute(datetime(2014, 10, 31, 12, 5)))

    def testOnTheHalfHour(self):
        self.assertEqual(30, guess_reporting_minute(datetime(2014, 10, 31, 12, 30)))

    def testAfterTheHalfHour(self):
        self.assertEqual(30, guess_reporting_minute(datetime(2014, 10, 31, 12, 35)))

    def testOnQuarterAfter(self):
        self.assertEqual(15, guess_reporting_minute(datetime(2014, 10, 31, 12, 15)))

    def testAfterQuarterAfter(self):
        self.assertEqual(15, guess_reporting_minute(datetime(2014, 10, 31, 12, 20)))

    def testOnQuarterOf(self):
        self.assertEqual(45, guess_reporting_minute(datetime(2014, 10, 31, 12, 45)))

    def testAfterQuarterOf(self):
        self.assertEqual(45, guess_reporting_minute(datetime(2014, 10, 31, 12, 50)))

    def testOutOfBounds(self):
        for minute in (6, 14, 21, 29, 36, 44, 51, 59):
            self.assertRaises(ValueError, guess_reporting_minute, datetime(2014, 10, 31, 12, minute))


class ScheduledReportTest(TestCase):

    def setUp(self):
        for report in ReportNotification.view(
            'reportconfig/all_notifications',
            include_docs=True,
            reduce=False,
        ).all():
            report.delete()

    def _check(self, period, as_of, count):
        # get_scheduled_report_ids relies on end_datetime
        # being strictly greater than start_datetime
        # for tests targeting an exact minute mark,
        # we need to add a small amount to make it after.
        # This is a reasonable thing to do because in production,
        # it'll always run a short time after the periodic task is fired
        as_of += timedelta(microseconds=1)
        self.assertEqual(count, len(list(get_scheduled_report_ids(period, end_datetime=as_of))))

    def testDefaultValue(self):
        now = datetime.utcnow()
        # This line makes sure that the date of the ReportNotification is an increment of 15 minutes
        ReportNotification(hour=now.hour, minute=(now.minute // 15) * 15, interval='daily').save()
        if now.minute % 15 <= 5:
            self._check('daily', None, 1)
        else:
            self.assertRaises(
                ValueError,
                lambda: list(get_scheduled_report_ids('daily', end_datetime=None))
            )

    def testDailyReportEmptyMinute(self):
        ReportNotification(hour=12, minute=None, interval='daily').save()
        self._check('daily', datetime(2014, 10, 31, 12, 0), 1)
        self._check('daily', datetime(2014, 10, 31, 12, 30), 0)  # half hour shouldn't count

    def testDailyReportWithMinute(self):
        ReportNotification(hour=12, minute=0, interval='daily').save()
        self._check('daily', datetime(2014, 10, 31, 12, 0), 1)<commit_msg>Remove nondeterministic scheduled reports test testDefaultValue<commit_after>from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from datetime import datetime, timedelta
from django.test import SimpleTestCase, TestCase
from corehq.apps.domain.shortcuts import create_domain
from corehq.apps.saved_reports.models import ReportConfig, ReportNotification
from corehq.apps.saved_reports.scheduled import guess_reporting_minute, get_scheduled_report_ids
from corehq.apps.reports.views import get_scheduled_report_response
from corehq.apps.users.models import WebUser


class GuessReportingMinuteTest(SimpleTestCase):

    def testOnTheHour(self):
        self.assertEqual(0, guess_reporting_minute(datetime(2014, 10, 31, 12, 0)))

    def testAfterTheHour(self):
        self.assertEqual(0, guess_reporting_minute(datetime(2014, 10, 31, 12, 5)))

    def testOnTheHalfHour(self):
        self.assertEqual(30, guess_reporting_minute(datetime(2014, 10, 31, 12, 30)))

    def testAfterTheHalfHour(self):
        self.assertEqual(30, guess_reporting_minute(datetime(2014, 10, 31, 12, 35)))

    def testOnQuarterAfter(self):
        self.assertEqual(15, guess_reporting_minute(datetime(2014, 10, 31, 12, 15)))

    def testAfterQuarterAfter(self):
        self.assertEqual(15, guess_reporting_minute(datetime(2014, 10, 31, 12, 20)))

    def testOnQuarterOf(self):
        self.assertEqual(45, guess_reporting_minute(datetime(2014, 10, 31, 12, 45)))

    def testAfterQuarterOf(self):
        self.assertEqual(45, guess_reporting_minute(datetime(2014, 10, 31, 12, 50)))

    def testOutOfBounds(self):
        for minute in (6, 14, 21, 29, 36, 44, 51, 59):
            self.assertRaises(ValueError, guess_reporting_minute, datetime(2014, 10, 31, 12, minute))


class ScheduledReportTest(TestCase):

    def setUp(self):
        for report in ReportNotification.view(
            'reportconfig/all_notifications',
            include_docs=True,
            reduce=False,
        ).all():
            report.delete()

    def _check(self, period, as_of, count):
        # get_scheduled_report_ids relies on end_datetime
        # being strictly greater than start_datetime
        # for tests targeting an exact minute mark,
        # we need to add a small amount to make it after.
        # This is a reasonable thing to do because in production,
        # it'll always run a short time after the periodic task is fired
        as_of += timedelta(microseconds=1)
        self.assertEqual(count, len(list(get_scheduled_report_ids(period, end_datetime=as_of))))

    def testDailyReportEmptyMinute(self):
        ReportNotification(hour=12, minute=None, interval='daily').save()
        self._check('daily', datetime(2014, 10, 31, 12, 0), 1)
        self._check('daily', datetime(2014, 10, 31, 12, 30), 0)  # half hour shouldn't count

    def testDailyReportWithMinute(self):
        ReportNotification(hour=12, minute=0, interval='daily').save()
        self._check('daily', datetime(2014, 10, 31, 12, 0), 1)
{'fraction_non_alphanumeric': 0.09866220735785954, 'fraction_numerical': 0.05503192459714199, 'mean_word_length': 3.7777777777777777, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3519185', 'n_tokens_mistral': 2285, 'n_tokens_neox': 1872, 'n_words': 516}