buildframework/helium/sf/python/pythoncore/lib/pythoncoretests/test_session_provider.py
changeset 628 7c4a911dc066
parent 588 c7c26511138f
equal deleted inserted replaced
588:c7c26511138f 628:7c4a911dc066
    17 #Description:
    17 #Description:
    18 #===============================================================================
    18 #===============================================================================
    19 
    19 
    20 """ Test cases for ccm python toolkit."""
    20 """ Test cases for ccm python toolkit."""
    21 
    21 
    22 # pylint: disable-msg=R0201
    22 # pylint: disable=R0201
    23 
    23 
    24 import unittest
    24 import unittest
    25 import ccm
    25 import ccm
    26 import ccm.extra
    26 import ccm.extra
    27 import os
    27 import os
    29 import tempfile
    29 import tempfile
    30 
    30 
    31 _logger = logging.getLogger('test.test_session_provider')
    31 _logger = logging.getLogger('test.test_session_provider')
    32 logging.basicConfig(level=logging.INFO)
    32 logging.basicConfig(level=logging.INFO)
    33 
    33 
       
    34 
    34 class MockResultSession(ccm.AbstractSession):
    35 class MockResultSession(ccm.AbstractSession):
    35     """ Fake session used to test Result"""
    36     """ Fake session used to test Result"""
    36     def __init__(self, behave = {}, database="fakedb"):
    37     def __init__(self, behave=None, database="fakedb"):
    37         ccm.AbstractSession.__init__(self, None, None, None, None)
    38         ccm.AbstractSession.__init__(self, None, None, None, None)
       
    39         if behave == None:
       
    40             behave = {}
    38         self._behave = behave
    41         self._behave = behave
    39         self._database = database
    42         self._database = database
    40         self.dbpath = "/path/to/" + database
    43         self.dbpath = "/path/to/" + database
    41         self._session_addr = "LOCALHOST:127.0.0.1:1234"
    44         self._session_addr = "LOCALHOST:127.0.0.1:1234"
    42     
    45     
    54             result.output = self._behave[cmdline]
    57             result.output = self._behave[cmdline]
    55         else:
    58         else:
    56             result.status = -1  
    59             result.status = -1  
    57         return result
    60         return result
    58 
    61 
       
    62 
    59 class MockOpener(object):
    63 class MockOpener(object):
    60     """ An Opener which provides a mock session """
    64     """ An Opener which provides a mock session """
    61     def __init__(self):
    65     def __init__(self):
    62         self.failOnNewOpen = False
    66         self.failOnNewOpen = False
    63     
    67     
    64 # pylint: disable-msg=W0613
    68 # pylint: disable=W0613
    65 #need disable msg to prevent pylint warning as this is emulating the real method.
    69 #need disable msg to prevent pylint warning as this is emulating the real method.
    66     def __call__(self, username=None, password=None, engine=None, dbpath=None, database=None, reuse=True):
    70     def __call__(self, username=None, password=None, engine=None, dbpath=None, database=None, reuse=True):
    67         assert self.failOnNewOpen == False, "This method should not be called again."
    71         assert self.failOnNewOpen == False, "This method should not be called again."
    68         if database != "fakedb":
    72         if database != "fakedb":
    69             raise ccm.CCMException("Invalid database")
    73             raise ccm.CCMException("Invalid database")
    70         return MockResultSession()
    74         return MockResultSession()
    71 # pylint: enable-msg=W0613
    75 # pylint: enable-msg=W0613
       
    76 
    72 
    77 
    73 class SessionProviderTest(unittest.TestCase):
    78 class SessionProviderTest(unittest.TestCase):
    74     """ Testing Results parsers. """
    79     """ Testing Results parsers. """
    75     def test_get_valid_db(self):
    80     def test_get_valid_db(self):
    76         """ Test the opening of a valid database. """
    81         """ Test the opening of a valid database. """
    82         """ Test the opening of an invalid database. """
    87         """ Test the opening of an invalid database. """
    83         prov = ccm.extra.SessionProvider(opener=MockOpener())
    88         prov = ccm.extra.SessionProvider(opener=MockOpener())
    84         try:
    89         try:
    85             _ = prov.get(database="invaliddb")
    90             _ = prov.get(database="invaliddb")
    86             assert False, "Should raise Exception when giving unexisting dbase.'"
    91             assert False, "Should raise Exception when giving unexisting dbase.'"
    87         except Exception, exc:
    92         except ccm.CCMException, exc:
    88             _logger.info(exc)
    93             _logger.info(exc)
    89 
       
    90 
    94 
    91 
    95 
    92 class CachedSessionProviderTest(unittest.TestCase):
    96 class CachedSessionProviderTest(unittest.TestCase):
    93     """ Testing Results parsers. """
    97     """ Testing Results parsers. """
    94     session_cache = os.path.join(tempfile.mkdtemp(), 'session_cache.xml')
    98     session_cache = os.path.join(tempfile.mkdtemp(), 'session_cache.xml')
   115         """ Test the opening of an invalid database (cached). """
   119         """ Test the opening of an invalid database (cached). """
   116         prov = ccm.extra.CachedSessionProvider(opener=MockOpener())
   120         prov = ccm.extra.CachedSessionProvider(opener=MockOpener())
   117         try:
   121         try:
   118             _ = prov.get(database="invaliddb")
   122             _ = prov.get(database="invaliddb")
   119             assert False, "Should raise Exception when giving unexisting dbase.'"
   123             assert False, "Should raise Exception when giving unexisting dbase.'"
   120         except Exception, exc:
   124         except ccm.CCMException, exc:
   121             _logger.info(exc)
   125             _logger.info(exc)
   122         
   126         
   123     def test_open_session_twice(self):
   127     def test_open_session_twice(self):
   124         """ Open session then free it then open it again... """
   128         """ Open session then free it then open it again... """
   125         opener = MockOpener()
   129         opener = MockOpener()