1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
#!/usr/bin/env python
#
# Copyright (C) 2011 Martin Owens
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 3.0 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library.
#
"""
Test crontab enumerations.
"""
import os
import sys
sys.path.insert(0, '../')
import unittest
from crontab import CronTab
try:
from test import test_support
except ImportError:
from test import support as test_support
INITAL_TAB = """
* * * JAN SAT enums
* * * MAR-APR * ranges
* * * * MON,FRI,WED multiples
* * * * * com1 # Comment One
* * * * * com2 # Comment One
"""
COMMANDS = [
'enums',
'ranges',
'multiples',
'com1', 'com2',
]
RESULT_TAB = """
* * * JAN SAT enums
* * * MAR-APR * ranges
* * * * MON,WED,FRI multiples
* * * * * com1 # Comment One
* * * * * com2 # Comment One
"""
class EnumTestCase(unittest.TestCase):
"""Test basic functionality of crontab."""
def setUp(self):
self.crontab = CronTab(tab=INITAL_TAB)
def test_01_presevation(self):
"""All Entries Re-Rendered Correctly"""
self.crontab.write()
results = RESULT_TAB.split('\n')
line_no = 0
for line in self.crontab.intab.split('\n'):
self.assertEqual(str(line), results[line_no])
line_no += 1
def test_02_simple_enum(self):
"""Simple Enumerations"""
e = list(self.crontab.find_command('enums'))[0]
self.assertEqual(e.month, 'JAN')
self.assertEqual(e.month.render(True), '1')
self.assertEqual(e.dow, 'SAT')
self.assertEqual(e.dow.render(True), '6')
def test_03_enum_range(self):
"""Enumeration Ranges"""
e = list(self.crontab.find_command('ranges'))[0]
self.assertEqual(e.month, 'MAR-APR')
self.assertEqual(e.month.render(True), '3-4' )
def test_04_sets(self):
"""Enumeration Sets"""
e = list(self.crontab.find_command('multiples'))[0]
self.assertEqual(e.dow, 'MON,WED,FRI')
self.assertEqual(e.dow.render(True), '1,3,5' )
def test_05_create(self):
"""Create by Enumeration"""
job = self.crontab.new(command='new')
job.month.on('JAN')
job.dow.on('SUN')
self.assertEqual(str(job), '* * * JAN SUN new')
def test_06_create_range(self):
"""Created Enum Range"""
job = self.crontab.new(command='new2')
job.month.during('APR', 'NOV').every(2)
self.assertEqual(str(job), '* * * APR-NOV/2 * new2')
def test_07_create_set(self):
"""Created Enum Set"""
job = self.crontab.new(command='new3')
job.month.on('APR')
job.month.also.on('NOV','JAN')
self.assertEqual(str(job), '* * * JAN,APR,NOV * new3')
def test_08_find_comment(self):
"""Comment Set"""
jobs = list(self.crontab.find_comment('Comment One'))
self.assertEqual(len(jobs), 2)
for job in jobs:
self.assertEqual(job.comment, 'Comment One')
if __name__ == '__main__':
test_support.run_unittest(
EnumTestCase,
)
|