Refactor favourite implementation and add some tests.
[recordtv.git] / src / rtv_favourite.py
blob10922d2f91262ccb1a99a49ac9fdc447a1579b8e
1 #!/usr/bin/python
3 import re
4 import rtv_propertiesfile, rtv_programmeinfo
6 class Favourite(rtv_propertiesfile.PropertiesFile):
8 def __init__( self ):
9 self.title_re = None
10 self.channel = None
11 self.not_channel = None
12 self.deleteAfterDays = None
13 self.priority = None
14 self.record = "yes"
15 self.destination = None
16 self.real_title = None
17 self.unique_subtitles = True
18 self.filetype = None
20 self._title_re = None
23 def matches( self, pi ):
25 if self.title_re is None:
26 return False # This favourite is empty
28 if self._title_re is None:
29 self._title_re = re.compile( self.title_re + "$" )
31 if not self._title_re.match( pi.title ):
32 return False
34 if self.channel is not None and self.channel != pi.channel:
35 return False
37 return True
38 # TODO: other things to match on e.g. time, categories
40 class FakeProg( object ):
41 def __init__( self, title, channel ):
42 self.title = title
43 self.channel = channel
45 def test_Empty_favourite_never_matches():
46 fav = Favourite()
47 assert( not fav.matches( FakeProg( "t", "c" ) ) )
49 def test_Wrong_title_doesnt_match():
50 fav = Favourite()
51 fav.title_re = "other"
52 assert( not fav.matches( FakeProg( "this", "c" ) ) )
54 def test_Right_title_matches():
55 fav = Favourite()
56 fav.title_re = "this"
57 assert( fav.matches( FakeProg( "this", "c" ) ) )
59 def test_Wrong_channel_doesnt_match():
60 fav = Favourite()
61 fav.title_re = "this"
62 fav.channel = "oc"
63 assert( not fav.matches( FakeProg( "this", "c" ) ) )
65 def test_Right_channel_does_match():
66 fav = Favourite()
67 fav.title_re = "this"
68 fav.channel = "c"
69 assert( fav.matches( FakeProg( "this", "c" ) ) )
71 def test( config ):
72 test_Empty_favourite_never_matches()
73 test_Wrong_title_doesnt_match()
74 test_Right_title_matches()
75 test_Wrong_channel_doesnt_match()
76 test_Right_channel_does_match()