Source

trac-dvbcronrecording-plugin / src / tests / computer_test.py

import unittest
import time

import logging
_logg = logging.getLogger("TESTS")
if not _logg.handlers:
    _logg.addHandler(logging.StreamHandler())
    _logg.setLevel(logging.INFO)

from dvbcronrecording.computer import DvbCronRecordingPlanComputer
from trac.core import ComponentManager

# fix this...
def get(self, instance, owner):
     if instance is None:
         return self
     config = getattr(instance, 'config', None)
     if config and isinstance(config, Configuration):
         section = config[self.section]
         value = self.accessor(section, self.name, self.default)
         return value
     return self.default
 
from trac.config import Option
Option.__get__ = get

import collections
Channel = collections.namedtuple("Channel",["channelname", "adapter"])
Entry = collections.namedtuple("Entry",["channelname","title",
    "newtime","endtime", "extratime", "weekday", "onlydate", "status", "priority"])
Locale = collections.namedtuple("Locale",["language"])
Request = collections.namedtuple("Request",["locale"])

class DvbComputerTests(unittest.TestCase):
    env = None
    logg = _logg
    def setUp(self):
        if self.env is None:
            self.env = ComponentManager() 
        self.computer = DvbCronRecordingPlanComputer(self.env)
        self.computer.logg = _logg
        self.computer._localtime = time.localtime(1334887400)
    def test_01_options(self):
        self.assertEqual("plan4", self.computer.algorithm, )
        self.assertTrue("3sat" in self.computer.rankup_channels)
        self.assertEqual(0.3, self.computer.rankup_value)
        self.assertEqual(0.5, self.computer.onlydate_rankup)
    def test_02_channels(self):
        channellist = [Channel("3sat",1),Channel("arte",0), Channel("arte",1)]
        self.computer.set_adapters_from_channels_list(channellist)
        self.assertTrue(u"1" in self.computer.adapterlist())
        self.assertEqual([u"1"], self.computer.adapters("3sat"))
        self.assertEqual([u"0", u"1"], self.computer.adapters("arte"))
    def test_03_entries(self):
        a1 = Entry("arte","A1","22:30","23:30","0", "6", "", "ok",1)
        req = Request(Locale("de"))
        res = self.computer.plan4(req, [a1])
        self.logg.info("RESULT:%s", res)
        E = u"""
{[{  [22:30-23:30]+0 arte@0 (* 1,2,3,4,5,6,7,8,9,10,11,12 6) 'A1' }]}"""
        self.assertEqual(E, unicode(res))
    def test_04_entries(self):
        a1 = Entry("arte","A1","22:30","23:30","0", "", "22.04.", "ok",1)
        req = Request(Locale("de"))
        res = self.computer.plan4(req, [a1])
        self.logg.info("RESULT:%s", res)
        E = u"""
{[{  [22:30-23:30]+0 arte@0 (22 4 6) 'A1' }]}"""
        self.assertEqual(E, unicode(res))
    def test_10_2entries_samegroupandcontinue(self):
        a1 = Entry("arte","A1","22:30","23:30","0", "", "22.04.", "ok",1)
        a2 = Entry("arte","A2","23:30","00:30","0", "", "22.04.", "ok",1)
        req = Request(Locale("de"))
        res = self.computer.plan4(req, [a1, a2])
        self.logg.info("RESULT:%s", res)
        E = u"""
{[{  [22:30-23:30]+0 arte@0 (22 4 6) 'A1' },
  {  [23:30-00:30]+0 arte@0 (22 4 6) 'A2' }]}"""
        self.assertEqual(E, unicode(res))
    def test_11_2entries_differentgroups(self):
        a1 = Entry("arte","A1","22:30","23:30","0", "", "22.04.", "ok",1)
        a2 = Entry("arte","A2","23:33","00:33","0", "", "22.04.", "ok",1)
        req = Request(Locale("de"))
        res = self.computer.plan4(req, [a1, a2])
        self.logg.info("RESULT:%s", res)
        E = u"""
{[{  [22:30-23:30]+0 arte@0 (22 4 6) 'A1' }],
 [{  [23:33-00:33]+0 arte@0 (22 4 6) 'A2' }]}"""
        self.assertEqual(E, unicode(res))
    def test_20_2entries_differentchannels0(self):
        channellist = [Channel("3sat",0),Channel("arte",0), Channel("arte",1)]
        self.computer.set_adapters_from_channels_list(channellist)
        self.assertTrue(u"1" in self.computer.adapterlist())
        a1 = Entry("arte","A1","22:30","23:30","0", "", "22.04.", "ok",1)
        a2 = Entry("3sat","A2","22:33","23:33","0", "", "22.04.", "ok",1)
        req = Request(Locale("de"))
        res = self.computer.plan4(req, [a1, a2])
        self.logg.info("RESULT:%s", res)
        E = u"""
{[{  [22:30-23:30]+0 arte@1 (22 4 6) 'A1' }],
 [{  [22:33-23:33]+0 3sat@0 (22 4 6) 'A2' }]}"""
        self.assertEqual(E, unicode(res))
    def test_21_2entries_differentchannels1(self):
        channellist = [Channel("3sat",1),Channel("arte",0), Channel("arte",1)]
        self.computer.set_adapters_from_channels_list(channellist)
        self.assertTrue(u"1" in self.computer.adapterlist())
        a1 = Entry("arte","A1","22:30","23:30","0", "", "22.04.", "ok",1)
        a2 = Entry("3sat","A2","22:33","23:33","0", "", "22.04.", "ok",1)
        req = Request(Locale("de"))
        res = self.computer.plan4(req, [a1, a2])
        self.logg.info("RESULT:%s", res)
        E = u"""
{[{  [22:30-23:30]+0 arte@0 (22 4 6) 'A1' }],
 [{  [22:33-23:33]+0 3sat@1 (22 4 6) 'A2' }]}"""
        self.assertEqual(E, unicode(res))
    def test_23_3entries_differentchannels0(self):
        channellist = [Channel("3sat",0),Channel("arte",0), Channel("arte",1)]
        self.computer.set_adapters_from_channels_list(channellist)
        self.assertTrue(u"1" in self.computer.adapterlist())
        a1 = Entry("arte","A1","22:30","23:30","0", "", "22.04.", "ok",1)
        a2 = Entry("3sat","A2","22:33","23:33","0", "", "22.04.", "ok",1)
        a3 = Entry("3sat","A3","23:33","00:33","0", "", "22.04.", "ok",1)
        req = Request(Locale("de"))
        res = self.computer.plan4(req, [a1, a2, a3])
        self.logg.info("RESULT:%s", res)
        E = u"""
{[{  [22:30-23:30]+0 arte@1 (22 4 6) 'A1' }],
 [{  [22:33-23:33]+0 3sat@0 (22 4 6) 'A2' },
  {  [23:33-00:33]+0 3sat@0 (22 4 6) 'A3' }]}"""
        self.assertEqual(E, unicode(res))
    def test_24_3entries_differentchannels1(self):
        channellist = [Channel("3sat",1),Channel("arte",0), Channel("arte",1)]
        self.computer.set_adapters_from_channels_list(channellist)
        self.assertTrue(u"1" in self.computer.adapterlist())
        a1 = Entry("arte","A1","22:30","23:30","0", "", "22.04.", "ok",1)
        a2 = Entry("3sat","A2","22:33","23:33","0", "", "22.04.", "ok",1)
        a3 = Entry("3sat","A3","23:33","00:33","0", "", "22.04.", "ok",1)
        req = Request(Locale("de"))
        res = self.computer.plan4(req, [a1, a2, a3])
        self.logg.info("RESULT:%s", res)
        E = u"""
{[{  [22:30-23:30]+0 arte@0 (22 4 6) 'A1' }],
 [{  [22:33-23:33]+0 3sat@1 (22 4 6) 'A2' },
  {  [23:33-00:33]+0 3sat@1 (22 4 6) 'A3' }]}"""
        self.assertEqual(E, unicode(res))
    def test_25_3entries_differentchannels_inside1(self):
        channellist = [Channel("3sat",1),Channel("arte",0), Channel("arte",1)]
        self.computer.set_adapters_from_channels_list(channellist)
        self.assertTrue(u"1" in self.computer.adapterlist())
        a1 = Entry("3sat","A1","22:33","23:33","0", "", "22.04.", "ok",1)
        a2 = Entry("arte","A2","23:30","00:30","0", "", "22.04.", "ok",1)
        a3 = Entry("3sat","A3","23:33","00:33","0", "", "22.04.", "ok",1)
        req = Request(Locale("de"))
        res = self.computer.plan4(req, [a1, a2, a3])
        self.logg.info("RESULT:%s", res)
        E = u"""
{[{  [22:33-23:33]+0 3sat@1 (22 4 6) 'A1' },
  {  [23:33-00:33]+0 3sat@1 (22 4 6) 'A3' }],
 [{  [23:30-00:30]+0 arte@0 (22 4 6) 'A2' }]}"""
        self.assertEqual(E, unicode(res))
    def test_26_3entries_differentchannels_inside2(self):
        channellist = [Channel("3sat",1),Channel("arte",0), Channel("arte",1)]
        self.computer.set_adapters_from_channels_list(channellist)
        self.assertTrue(u"1" in self.computer.adapterlist())
        a1 = Entry("arte","A1","22:33","23:33","0", "", "22.04.", "ok",1)
        a2 = Entry("3sat","A2","23:30","00:30","0", "", "22.04.", "ok",1)
        a3 = Entry("arte","A3","23:33","00:33","0", "", "22.04.", "ok",1)
        req = Request(Locale("de"))
        res = self.computer.plan4(req, [a1, a2, a3])
        self.logg.info("RESULT:%s", res)
        E = u"""
{[{  [22:33-23:33]+0 arte@0 (22 4 6) 'A1' },
  {  [23:33-00:33]+0 arte@0 (22 4 6) 'A3' }],
 [{  [23:30-00:30]+0 3sat@1 (22 4 6) 'A2' }]}"""
        self.assertEqual(E, unicode(res))
    def test_27_3entries_differentchannels_atend(self):
        channellist = [Channel("3sat",1),Channel("arte",0), Channel("arte",1)]
        self.computer.set_adapters_from_channels_list(channellist)
        self.assertTrue(u"1" in self.computer.adapterlist())
        a1 = Entry("arte","A1","22:33","23:33","0", "", "22.04.", "ok",1)
        a2 = Entry("arte","A2","23:33","00:33","0", "", "22.04.", "ok",1)
        a3 = Entry("3sat","A3","23:50","00:50","0", "", "22.04.", "ok",1)
        req = Request(Locale("de"))
        res = self.computer.plan4(req, [a1, a2, a3])
        self.logg.info("RESULT:%s", res)
        E = u"""
{[{  [22:33-23:33]+0 arte@0 (22 4 6) 'A1' },
  {  [23:33-00:33]+0 arte@0 (22 4 6) 'A2' }],
 [{  [23:50-00:50]+0 3sat@1 (22 4 6) 'A3' }]}"""
        self.assertEqual(E, unicode(res))
    def test_28_3entries_differentchannels_nextday(self):
        channellist = [Channel("3sat",1),Channel("arte",0), Channel("arte",1)]
        self.computer.set_adapters_from_channels_list(channellist)
        self.assertTrue(u"1" in self.computer.adapterlist())
        a1 = Entry("arte","A1","22:33","23:33","0", "", "22.04.", "ok",1)
        a2 = Entry("arte","A2","23:33","00:33","0", "", "22.04.", "ok",1)
        a3 = Entry("3sat","A3","00:10","01:10","0", "", "23.04.", "ok",1)
        req = Request(Locale("de"))
        res = self.computer.plan4(req, [a1, a2, a3])
        self.logg.info("RESULT:%s", res)
        E = u"""
{[{  [22:33-23:33]+0 arte@0 (22 4 6) 'A1' },
  {  [23:33-00:33]+0 arte@0 (22 4 6) 'A2' }],
 [{  [00:10-01:10]+0 3sat@1 (23 4 0) 'A3' }]}"""
        self.assertEqual(E, unicode(res))
    def test_40_4entries_samegroupandcontinue(self):
        channellist = [Channel("pro7",0),Channel("arte",0)]
        self.computer.set_adapters_from_channels_list(channellist)
        self.assertTrue(u"1" not in self.computer.adapterlist())
        a1 = Entry("arte","A1","22:30","23:30","0", "", "22.04.", "ok",1)
        a2 = Entry("arte","A2","23:30","00:30","3", "", "22.04.", "ok",1)
        a3 = Entry("arte","A3","00:30","01:10","3", "", "23.04.", "ok",1)
        a4 = Entry("arte","A4","01:10","01:50","5", "", "23.04.", "ok",1)
        req = Request(Locale("de"))
        res = self.computer.plan4(req, [a1, a2, a3, a4])
        self.logg.info("RESULT:%s", res)
        E = u"""
{[{  [22:30-23:30]+0 arte@0 (22 4 6) 'A1' },
  {  [23:30-00:30]+3 arte@0 (22 4 6) 'A2' },
  {  [00:33-01:10]+3 arte@0 (23 4 0) 'A3' },
  {  [01:13-01:50]+5 arte@0 (23 4 0) 'A4' }]}"""
        self.assertEqual(E, unicode(res))
    def test_41_4entries_overlap1(self):
        channellist = [Channel("pro7",0),Channel("arte",0)]
        self.computer.set_adapters_from_channels_list(channellist)
        self.assertTrue(u"1" not in self.computer.adapterlist())
        a1 = Entry("pro7","A1","22:30","23:30","0", "", "22.04.", "ok",1)
        a2 = Entry("arte","A2","23:20","00:30","3", "", "22.04.", "ok",1)
        a3 = Entry("arte","A3","00:30","01:10","3", "", "23.04.", "ok",1)
        a4 = Entry("arte","A4","01:10","01:50","5", "", "23.04.", "ok",1)
        req = Request(Locale("de"))
        res = self.computer.plan4(req, [a1, a2, a3, a4])
        self.logg.info("RESULT:%s", res)
        E = u"""
{[{  [22:30-23:20]+0 pro7@0 (22 4 6) 'A1' },
  {  [23:20-00:30]+3 arte@0 (22 4 6) 'A2' },
  {  [00:33-01:10]+3 arte@0 (23 4 0) 'A3' },
  {  [01:13-01:50]+5 arte@0 (23 4 0) 'A4' }]}"""
        self.assertEqual(E, unicode(res))
    def test_42_4entries_overlap2(self):
        channellist = [Channel("pro7",0),Channel("arte",0)]
        self.computer.set_adapters_from_channels_list(channellist)
        self.assertTrue(u"1" not in self.computer.adapterlist())
        a1 = Entry("pro7","A1","22:30","00:40","0", "", "22.04.", "ok",1)
        a2 = Entry("arte","A2","23:20","00:30","3", "", "22.04.", "ok",1)
        a3 = Entry("arte","A3","00:30","01:10","3", "", "23.04.", "ok",1)
        a4 = Entry("arte","A4","01:10","01:50","5", "", "23.04.", "ok",1)
        req = Request(Locale("de"))
        res = self.computer.plan4(req, [a1, a2, a3, a4])
        self.logg.info("RESULT:%s", res)
        E = u"""
{[{  [22:30-23:20]+0 pro7@0 (22 4 6) 'A1' },
  {  [23:20-00:30]+3 arte@0 (22 4 6) 'A2' },
  {  [00:33-01:10]+3 arte@0 (23 4 0) 'A3' },
  {  [01:13-01:50]+5 arte@0 (23 4 0) 'A4' }]}"""
        self.assertEqual(E, unicode(res))
    def test_43_4entries_delete_start1(self): 
        channellist = [Channel("pro7",0),Channel("arte",0)]
        self.computer.set_adapters_from_channels_list(channellist)
        self.assertTrue(u"1" not in self.computer.adapterlist())
        a1 = Entry("pro7","A1","23:20","23:40","0", "", "22.04.", "ok",1)
        a2 = Entry("arte","A2","23:20","00:30","3", "", "22.04.", "ok",1)
        a3 = Entry("arte","A3","00:30","01:10","3", "", "23.04.", "ok",1)
        a4 = Entry("arte","A4","01:10","01:50","5", "", "23.04.", "ok",1)
        req = Request(Locale("de"))
        res = self.computer.plan4(req, [a1, a2, a3, a4])
        self.logg.info("RESULT:%s", res)
        E = u"""
{[{  [--DELETED--]   pro7@0 (22 4 6) 'A1' },
  {  [23:20-00:30]+3 arte@0 (22 4 6) 'A2' },
  {  [00:33-01:10]+3 arte@0 (23 4 0) 'A3' },
  {  [01:13-01:50]+5 arte@0 (23 4 0) 'A4' }]}"""
        self.assertEqual(E, unicode(res))
    def test_44_4entries_delete_start2(self): 
        channellist = [Channel("pro7",0),Channel("arte",0)]
        self.computer.set_adapters_from_channels_list(channellist)
        self.assertTrue(u"1" not in self.computer.adapterlist())
        a1 = Entry("pro7","A1","23:20","00:40","0", "", "22.04.", "ok",1)
        a2 = Entry("arte","A2","23:20","00:30","3", "", "22.04.", "ok",1)
        a3 = Entry("arte","A3","00:30","01:10","3", "", "23.04.", "ok",1)
        a4 = Entry("arte","A4","01:10","01:50","5", "", "23.04.", "ok",1)
        req = Request(Locale("de"))
        res = self.computer.plan4(req, [a1, a2, a3, a4])
        self.logg.info("RESULT:%s", res)
        E = u"""
{[{  [--DELETED--]   pro7@0 (22 4 6) 'A1' },
  {  [23:20-00:30]+3 arte@0 (22 4 6) 'A2' },
  {  [00:33-01:10]+3 arte@0 (23 4 0) 'A3' },
  {  [01:13-01:50]+5 arte@0 (23 4 0) 'A4' }]}"""
        self.assertEqual(E, unicode(res))
    def test_45_4entries_delete_inside1(self):
        channellist = [Channel("pro7",0),Channel("arte",0)]
        self.computer.set_adapters_from_channels_list(channellist)
        self.assertTrue(u"1" not in self.computer.adapterlist())
        a1 = Entry("arte","A1","23:20","00:30","3", "", "22.04.", "ok",1)
        a2 = Entry("pro7","A2","23:40","00:20","0", "", "22.04.", "ok",1)
        a3 = Entry("arte","A3","00:30","01:10","3", "", "23.04.", "ok",1)
        a4 = Entry("arte","A4","01:10","01:50","5", "", "23.04.", "ok",1)
        req = Request(Locale("de"))
        res = self.computer.plan4(req, [a1, a2, a3, a4])
        self.logg.info("RESULT:%s", res)
        E = u"""
{[{  [23:20-00:30]+3 arte@0 (22 4 6) 'A1' },
  {  [--DELETED--]   pro7@0 (23 4 0) 'A2' },
  {  [00:33-01:10]+3 arte@0 (23 4 0) 'A3' },
  {  [01:13-01:50]+5 arte@0 (23 4 0) 'A4' }]}"""
        self.assertEqual(E, unicode(res))
    def test_46_4entries_delete2_inside1(self): 
        channellist = [Channel("pro7",0),Channel("arte",0),Channel("3sat",0)]
        self.computer.set_adapters_from_channels_list(channellist)
        self.assertTrue(u"1" not in self.computer.adapterlist())
        a1 = Entry("arte","A1","23:20","00:30","3", "", "22.04.", "ok",1)
        a2 = Entry("pro7","A2","23:40","00:20","0", "", "22.04.", "ok",1)
        a3 = Entry("3sat","A3","00:10","01:10","3", "", "23.04.", "ok",1)
        a4 = Entry("arte","A4","01:10","01:50","5", "", "23.04.", "ok",1)
        req = Request(Locale("de"))
        res = self.computer.plan4(req, [a1, a2, a3, a4])
        self.logg.info("RESULT:%s", res)
        E = u"""
{[{  [23:20-00:30]+3 arte@0 (22 4 6) 'A1' },
  {  [--DELETED--]   pro7@0 (23 4 0) 'A2' },
  {  [00:33-01:10]+3 3sat@0 (23 4 0) 'A3' },
  {  [01:13-01:50]+5 arte@0 (23 4 0) 'A4' }]}"""
        self.assertEqual(E, unicode(res))
    @unittest.expectedFailure
    def test_47_4entries_delete3_inside1_FAIL(self): 
        _logg.error(" FIXME all wrong ")
        channellist = [Channel("pro7",0),Channel("arte",0),Channel("3sat",0)]
        self.computer.set_adapters_from_channels_list(channellist)
        self.assertTrue(u"1" not in self.computer.adapterlist())
        a1 = Entry("arte","A1","23:20","00:30","3", "", "22.04.", "ok",1)
        a2 = Entry("pro7","A2","23:40","00:20","0", "", "22.04.", "ok",1)
        a3 = Entry("3sat","A3","23:40","01:10","3", "", "23.04.", "ok",1)
        a4 = Entry("arte","A4","01:10","01:50","5", "", "23.04.", "ok",1)
        req = Request(Locale("de"))
        res = self.computer.plan4(req, [a1, a2, a3, a4])
        self.logg.info("RESULT:%s", res)
        E = u"""
{[{  [23:20-00:30]+3 arte@0 (22 4 6) 'A1' },
  {  [--DELETED--]   pro7@0 (23 4 0) 'A2' },
  {  [00:33-01:10]+3 3sat@0 (23 4 0) 'A3' },
  {  [01:13-01:50]+5 arte@0 (23 4 0) 'A4' }]}"""
        self.assertEqual(E, unicode(res))

if __name__ == "__main__":
    import sys
    for arg in sys.argv:
        if arg.startswith("-vv"):
            print "log level DEBUG"
            _logg.setLevel(logging.DEBUG)
    unittest.main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.