Package elisa :: Package core :: Package tests :: Module test_component
[hide private]
[frames] | no frames]

Source Code for Module elisa.core.tests.test_component

  1  # Elisa - Home multimedia server 
  2  # Copyright (C) 2006-2008 Fluendo Embedded S.L. (www.fluendo.com). 
  3  # All rights reserved. 
  4  # 
  5  # This file is available under one of two license agreements. 
  6  # 
  7  # This file is licensed under the GPL version 3. 
  8  # See "LICENSE.GPL" in the root of this distribution including a special 
  9  # exception to use Elisa with Fluendo's plugins. 
 10  # 
 11  # The GPL part of Elisa is also available under a commercial licensing 
 12  # agreement from Fluendo. 
 13  # See "LICENSE.Elisa" in the root directory of this distribution package 
 14  # for details on that license. 
 15   
 16  from twisted.trial import unittest 
 17  from elisa.core.tests.elisa_test_case import ElisaTestCase 
 18   
 19  import new, os, sys 
 20  import platform 
 21  from elisa.core import component 
 22  from elisa.core import config 
 23  from elisa.core import common 
 24   
25 -class FooComponent(component.Component):
26 27 default_config = {'str_option': 'default str', 28 'list_option': ['default','list'], 29 'another_option': 'bar' 30 31 } 32 33 config_doc = {'str_option': 'a string option quoi', 34 'list_option': 'gimme a list' 35 } 36 37 # this modules depends on a non-existant foo module 38 dependencies = ['site','foo']
39
40 -class BarComponent(component.Component):
41 """ 42 For test purpose this component depends explicitely on stdlib's modules 43 """ 44 45 dependencies = ['site','sys', 'os.path']
46
47 -class WinComponent(component.Component):
48 49 platforms = ['Windows',]
50 51
52 -class LinComponent(component.Component):
53 54 platforms = ['Linux',]
55
56 -class FakeApplication:
57 58 config = None
59
60 -class TestComponent(ElisaTestCase):
61
62 - def test_init(self):
63 """ Check the component correctly loads its config 64 """ 65 66 comp = FooComponent() 67 conf = config.Config('test.conf') 68 69 section = {'str_option':'some string', 70 'list_option':['some','list'], 71 } 72 conf.set_section('foo', section) 73 74 comp.name = 'foo' 75 comp.id = 0 76 comp.load_config(conf) 77 78 keys = comp.config.keys() 79 self.failUnless('str_option' in keys) 80 self.failUnless('list_option' in keys) 81 self.failUnless('another_option' in keys) 82 83 self.assertEqual(comp.config['str_option'], 'some string') 84 self.assertEqual(comp.config['list_option'], ['some', 'list']) 85 self.assertEqual(comp.config['another_option'], 'bar')
86
87 - def test_no_config(self):
88 """ Check the default config of the component is used when 89 it's not found in the application's config. 90 """ 91 comp = FooComponent() 92 conf = config.Config('test.conf') 93 94 comp.name = 'foo' 95 comp.id = 0 96 comp.load_config(conf) 97 98 self.assertEqual(comp.config, comp.default_config)
99
100 - def test_multiple_instances(self):
101 """ Check we can use multiple instances of the same component 102 103 But with different configurations 104 """ 105 conf = config.Config('test.conf') 106 107 section0 = {'str_option':'some string', 108 'list_option':['some','list'], 109 } 110 conf.set_section('foo:0', section0) 111 112 # hey, we should have a config for that instance in the app's config 113 comp0 = FooComponent() 114 comp0.name = 'foo' 115 comp0.id = 0 116 comp0.load_config(conf) 117 118 # ho shit, no config for that one should be found 119 comp1 = FooComponent() 120 comp1.name = 'foo' 121 comp1.id = 1 122 comp1.load_config(conf) 123 124 keys = comp0.config.keys() 125 self.failUnless('str_option' in keys) 126 self.failUnless('list_option' in keys) 127 self.failUnless('another_option' in keys) 128 129 self.assertEqual(comp0.config['str_option'], 'some string') 130 self.assertEqual(comp0.config['list_option'], ['some', 'list']) 131 self.assertEqual(comp0.config['another_option'], 'bar') 132 133 self.assertEqual(comp1.config, comp1.default_config)
134
135 - def test_clean(self):
136 """ Check the config is saved on component.clean() 137 """ 138 empty = 'empty.conf' 139 try: 140 os.unlink(empty) 141 except: 142 pass 143 144 conf = config.Config(empty) 145 146 fake_app = FakeApplication() 147 fake_app.config = conf 148 common.set_application(fake_app) 149 150 comp = FooComponent() 151 comp.name = 'foo' 152 comp.id = 0 153 comp.load_config(conf) 154 155 self.assertEqual(comp.config, comp.default_config) 156 157 comp.clean() 158 self.assertEqual(conf.get_section('foo'), comp.config)
159
161 #print self.__module__ 162 #import pdb; pdb.set_trace() 163 self.assertRaises(component.UnMetDependency, 164 component.check_python_dependencies, 165 'test', ['ponyheaven']) 166 167 test_mod = new.module('test_mod') 168 test_mod.__version__ = '0.1' 169 sys.modules['test_mod'] = test_mod 170 self.failUnless(component.check_python_dependencies('test', 171 ['test_mod >= 0.1']))
172 - def test_parse_dependency(self):
173 dep = component.parse_dependency("pgm") 174 self.assertEqual(dep, ("pgm", "", "")) 175 176 for sign in ("=", "==", ">=", ">", "<=", "<"): 177 dep = component.parse_dependency("pgm %s 0.3" % sign) 178 self.assertEqual(dep, ("pgm", sign, "0.3"))
179
180 - def test_check_version(self):
181 182 mod = new.module('test_module') 183 mod.__version__ = '0.1' 184 185 for sign in ("=", "==", ">=", "<="): 186 self.failUnless(component._check_version('test_component', 187 'test_module', mod, 188 sign, '0.1')) 189 self.assertRaises(component.UnMetDependency, 190 component._check_version,'test_component', 191 'test_module', mod, '>', 192 '0.2') 193 194 del mod.__version__ 195 self.failIf(component._check_version('test_component', 'test_module', 196 mod, '==', '0.1')) 197 198 def version(): 199 return (0, 1)
200 201 mod.version = version 202 self.failUnless(component._check_version('test_component', 'test_module', 203 mod, '==', '0.1'))
204
205 - def test_check_platforms(self):
206 207 if os.name == 'posix': 208 platforms = ('win32',) 209 else: 210 platforms = ('posix',) 211 212 self.assertRaises(component.UnSupportedPlatform, 213 component.check_platforms, 'test', platforms) 214 self.failUnless(component.check_platforms('test', (os.name,)))
215