tests/config.py
author Sandro Knauß <knauss@netzguerilla.net>
Thu, 23 Feb 2012 16:57:57 +0100
branchdevel
changeset 183 07ee5543751b
parent 179 af65fcbd59d5
child 184 6b0ff82dff81
permissions -rw-r--r--
offer.provider now handles the options dict and loadConfig is only in Provider class

from mock import patch, Mock
from twisted.trial import unittest
import signal
import io
import ConfigParser
from  iro import config 

class TestConfig(unittest.TestCase):
    '''test config class'''
    
    def setUp(self):
        self._reloadList=config.config.reloadList
        config.config.reloadlist=[]

    def tearDown(self):
        config.config.reloadlist = self._reloadList

    @patch('iro.config.config')
    def testReadConfig(self,pConfig):
        config.readConfig()
        self.assertEqual([i[0] for i in pConfig.method_calls],["read","reload"])
        pConfig.read.assert_called_once_with(config.confFiles)
        pConfig.reload.assert_called_once_with()

    @patch('signal.signal')
    @patch('iro.config.readConfig')
    def testRegisterSignal(self, pReadConfig, pSignal):
        config.registerSignal()
        self.assertEqual(pSignal.call_args[0][0],signal.SIGUSR2)
        self.assertEqual(pReadConfig.called,0)
        pSignal.call_args[0][1](None, None)
        pReadConfig.assert_called_once_with()

    def testRegisterReload(self):
        def x():
            pass
        config.config.registerReload(x)
        self.assertEqual(config.config.reloadList,[x])

    def testReload(self):
        x = Mock()
        config.config.reloadList = [x] 
        config.config.reload_()
        x.assert_called_once_with()


class TestRead(unittest.TestCase):

    def tearDown(self):
        for s in config.config.sections():
            config.config.remove_section(s)

    @patch('iro.config.ConfigParser.read')
    def testMain(self,pRead):
        sample_config = """[main]
hostname = localhost
port = 8000
"""
        config.config.readfp(io.BytesIO(sample_config))
        config.config.read([])
        pRead.assert_called_once_with(config.config,[])

    @patch('iro.config.ConfigParser.read')
    def testMainBadPort(self,pRead):
        sample_config = """[main]
hostname = localhost
port = -8000
"""
        config.config.readfp(io.BytesIO(sample_config))
        self.assertRaises(config.ValidateException, config.config.read, [])

    @patch('iro.config.ConfigParser.read')
    def testMainNoMust(self,pRead):
        sample_config = """[main]
port = 8000
"""
        config.config.readfp(io.BytesIO(sample_config))
        self.assertRaises(ConfigParser.NoOptionError, config.config.read, [])

    def testMust(self):
        pass
    testMust.todo= "To implement"

    def testProviders(self):
        pass
    testProviders.todo = "to implement"