<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import calendar
import datetime
import os
import site
import sys
import time
import unittest

MINUTE = 60
HOUR = 60 * MINUTE
DAY = 24 * HOUR

# Python 3 compatibility
if isinstance(map, type):
    xrange = range

# Add the parent directory relative to the test case because we assume the
# test will be in a subdirectory relative to the main code base.
sys.path.insert(0, os.path.join(
    os.path.dirname(os.path.realpath(__file__)), os.pardir))

import cronex


class test_testedmodule(unittest.TestCase):
    def test_substitution(self):
        """
        Verify that the special substitutions are replaced by the correct,
        expanded cron expressions.
        """
        testcases = [("@yearly", "0 0 1 1 *"),
            ("@annually", "0 0 1 1 *"),
            ("@monthly", "0 0 1 * *"),
            ("@weekly", "0 0 * * 0"),
            ("@daily", "0 0 * * *"),
            ("@midnight", "0 0 * * *"),
            ("@hourly", "0 * * * *")]
        for a, b in testcases:
            obj = cronex.CronExpression(a)
            self.assertTrue(b in repr(obj))

    def test_compute_numtab(self):
        """
        Verify that calling compute_numtab after modifying the string-version
        of an expression results in the static trigger-value cache being
        updated.
        """
        testex1 = cronex.CronExpression("*/7 5-10 5 * *")
        testex2 = cronex.CronExpression("*/5 23-2 5 8 *")
        self.assertNotEqual(testex1.string_tab, testex2.string_tab)
        self.assertNotEqual(testex1.numerical_tab, testex2.numerical_tab)
        testex1.string_tab = testex2.string_tab
        testex1.compute_numtab()
        self.assertEqual(testex1.string_tab, testex2.string_tab)
        self.assertEqual(testex1.numerical_tab, testex2.numerical_tab)

    def test_periodics_dom(self):
        """
        Verify that arbitrary-period repeaters in the day-of-the-month field
        work as expected.
        """
        now = int(time.time())
        then = time.gmtime(now - 491 * DAY)
        now_tuple = time.gmtime(now)
        testex = cronex.CronExpression("* * %491 * *")
        self.assertFalse(testex.check_trigger(now_tuple[:5]))
        testex.epoch = tuple(list(then[:5]) + [0])
        self.assertTrue(testex.check_trigger(now_tuple[:5]))
        self.assertTrue(testex.check_trigger(then[:5]))

    def test_periodics_hours(self):
        """
        Verify that arbitrary-period repeaters in the hours field work as
        expected.
        """
        now = int(time.time())
        then = time.gmtime(now - 9001 * HOUR)
        now_tuple = time.gmtime(now)
        testex = cronex.CronExpression("* %9001 * * *")
        self.assertFalse(testex.check_trigger(now_tuple[:5]))
        testex.epoch = tuple(list(then[:5]) + [0])
        self.assertTrue(testex.check_trigger(now_tuple[:5]))
        self.assertTrue(testex.check_trigger(then[:5]))

    def test_periodics_minutes(self):
        """
        Verify that arbitrary-period repeaters in the minutes field work as
        expected.
        """
        now = int(time.time())
        then = time.gmtime(now - 814075 * MINUTE)
        now_tuple = time.gmtime(now)
        testex = cronex.CronExpression("%814075 * * * *")
        self.assertFalse(testex.check_trigger(now_tuple[:5]))
        testex.epoch = tuple(list(then[:5]) + [0])
        self.assertTrue(testex.check_trigger(now_tuple[:5]))
        self.assertTrue(testex.check_trigger(then[:5]))

    def test_periodics_month(self):
        """
        Verify that arbitrary-period repeaters in the month field work as
        expected.
        """
        now = int(time.time())
        then = time.gmtime(now - 1337 * DAY)
        now_tuple = time.gmtime(now)
        curmon = now_tuple[1]
        thnmon = then[1]
        if curmon < thnmon:
            curmon += 12
        per = 36 + curmon - thnmon
        testex = cronex.CronExpression("* * * %%%i *" % per)
        self.assertFalse(testex.check_trigger(now_tuple[:5]))
        testex.epoch = tuple(list(then[:5]) + [0])
        self.assertTrue(testex.check_trigger(now_tuple[:5]))
        self.assertTrue(testex.check_trigger(then[:5]))

    def test_parse_atom(self):
        """
        Verify that parsing atoms returns sets containing all of the expected
        values.
        """
        input_expect = [
            (('5-10', (1, 20)), set([5, 6, 7, 8, 9, 10])),
            (('10-5', (1, 12)), set([10, 11, 12, 1, 2, 3, 4, 5])),
            (('5-10/2', (1, 20)), set([5, 7, 9])),
            (('10-5/2', (1, 12)), set([10, 12, 2, 4])),
            (('11-5/2', (1, 12)), set([11, 1, 3, 5])),
            (('10-5/3', (1, 11)), set([10, 2, 5])),
            (('11-5/3', (1, 14)), set([11, 14, 3])),
            (('*', (1, 100)), set(xrange(1, 101))),
            (('*/5', (1, 100)), set(xrange(1, 101, 5))),
            (('666', (1, 1000)), set([666])),
            (('21-1', (0, 23)), set([21, 22, 23, 0, 1]))]

        for give_the_function, want_from_function in input_expect:<commit_msg>Fixed unit test for repeaters in the months field<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import calendar
import datetime
import os
import site
import sys
import time
import unittest

MINUTE = 60
HOUR = 60 * MINUTE
DAY = 24 * HOUR

# Python 3 compatibility
if isinstance(map, type):
    xrange = range

# Add the parent directory relative to the test case because we assume the
# test will be in a subdirectory relative to the main code base.
sys.path.insert(0, os.path.join(
    os.path.dirname(os.path.realpath(__file__)), os.pardir))

import cronex


class test_testedmodule(unittest.TestCase):
    def test_substitution(self):
        """
        Verify that the special substitutions are replaced by the correct,
        expanded cron expressions.
        """
        testcases = [("@yearly", "0 0 1 1 *"),
            ("@annually", "0 0 1 1 *"),
            ("@monthly", "0 0 1 * *"),
            ("@weekly", "0 0 * * 0"),
            ("@daily", "0 0 * * *"),
            ("@midnight", "0 0 * * *"),
            ("@hourly", "0 * * * *")]
        for a, b in testcases:
            obj = cronex.CronExpression(a)
            self.assertTrue(b in repr(obj))

    def test_compute_numtab(self):
        """
        Verify that calling compute_numtab after modifying the string-version
        of an expression results in the static trigger-value cache being
        updated.
        """
        testex1 = cronex.CronExpression("*/7 5-10 5 * *")
        testex2 = cronex.CronExpression("*/5 23-2 5 8 *")
        self.assertNotEqual(testex1.string_tab, testex2.string_tab)
        self.assertNotEqual(testex1.numerical_tab, testex2.numerical_tab)
        testex1.string_tab = testex2.string_tab
        testex1.compute_numtab()
        self.assertEqual(testex1.string_tab, testex2.string_tab)
        self.assertEqual(testex1.numerical_tab, testex2.numerical_tab)

    def test_periodics_dom(self):
        """
        Verify that arbitrary-period repeaters in the day-of-the-month field
        work as expected.
        """
        now = int(time.time())
        then = time.gmtime(now - 491 * DAY)
        now_tuple = time.gmtime(now)
        testex = cronex.CronExpression("* * %491 * *")
        self.assertFalse(testex.check_trigger(now_tuple[:5]))
        testex.epoch = tuple(list(then[:5]) + [0])
        self.assertTrue(testex.check_trigger(now_tuple[:5]))
        self.assertTrue(testex.check_trigger(then[:5]))

    def test_periodics_hours(self):
        """
        Verify that arbitrary-period repeaters in the hours field work as
        expected.
        """
        now = int(time.time())
        then = time.gmtime(now - 9001 * HOUR)
        now_tuple = time.gmtime(now)
        testex = cronex.CronExpression("* %9001 * * *")
        self.assertFalse(testex.check_trigger(now_tuple[:5]))
        testex.epoch = tuple(list(then[:5]) + [0])
        self.assertTrue(testex.check_trigger(now_tuple[:5]))
        self.assertTrue(testex.check_trigger(then[:5]))

    def test_periodics_minutes(self):
        """
        Verify that arbitrary-period repeaters in the minutes field work as
        expected.
        """
        now = int(time.time())
        then = time.gmtime(now - 814075 * MINUTE)
        now_tuple = time.gmtime(now)
        testex = cronex.CronExpression("%814075 * * * *")
        self.assertFalse(testex.check_trigger(now_tuple[:5]))
        testex.epoch = tuple(list(then[:5]) + [0])
        self.assertTrue(testex.check_trigger(now_tuple[:5]))
        self.assertTrue(testex.check_trigger(then[:5]))

    def test_periodics_month(self):
        """
        Verify that arbitrary-period repeaters in the month field work as
        expected. More specifically, it verifies that the repeaters are
        triggered every "N" calendar months regardless of the day for triggers
        set to go offer every 1 month, ever 2 months, etc... through 18 months
        from 1970 through 1980.
        """
        for period in range(2, 18):
            calendar_months = 0
            description = "* * * %%%i *" % period
            cron_expression = cronex.CronExpression(description)

            for year in range(1970, 1980):
                for month in range(1, 13):
                    days_in_month = calendar.monthrange(year, month)[-1]
                    should_trigger = not (calendar_months % period)

                    for day in range(1, days_in_month + 1):
                        time_tuple = (year, month, day, 0, 0)
                        triggered = cron_expression.check_trigger(time_tuple)
                        self.assertEqual(should_trigger, triggered,
                            "%s: expected trigger to be %r for %r" %
                            (description, should_trigger, time_tuple)
                        )

                    calendar_months += 1

    def test_parse_atom(self):
        """
        Verify that parsing atoms returns sets containing all of the expected
        values.
        """
        input_expect = [
            (('5-10', (1, 20)), set([5, 6, 7, 8, 9, 10])),
            (('10-5', (1, 12)), set([10, 11, 12, 1, 2, 3, 4, 5])),
            (('5-10/2', (1, 20)), set([5, 7, 9])),
            (('10-5/2', (1, 12)), set([10, 12, 2, 4])),
            (('11-5/2', (1, 12)), set([11, 1, 3, 5])),
            (('10-5/3', (1, 11)), set([10, 2, 5])),
            (('11-5/3', (1, 14)), set([11, 14, 3])),
            (('*', (1, 100)), set(xrange(1, 101))),
            (('*/5', (1, 100)), set(xrange(1, 101, 5))),
            (('666', (1, 1000)), set([666])),
            (('21-1', (0, 23)), set([21, 22, 23, 0, 1]))]

        for give_the_function, want_from_function in input_expect:
{'fraction_non_alphanumeric': 0.14576365663322186, 'fraction_numerical': 0.046079524340393904, 'mean_word_length': 2.234675480769231, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7411976', 'n_tokens_mistral': 3748, 'n_tokens_neox': 3420, 'n_words': 845}