cubicweb/test/unittest_toolsutils.py
changeset 11476 a9f26de5ea6c
parent 11452 628dd5832495
equal deleted inserted replaced
11475:d2fcd81b7ca9 11476:a9f26de5ea6c
    15 #
    15 #
    16 # You should have received a copy of the GNU Lesser General Public License along
    16 # You should have received a copy of the GNU Lesser General Public License along
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    18 
    18 
    19 
    19 
       
    20 import os
    20 import tempfile
    21 import tempfile
    21 import unittest
    22 import unittest
    22 
    23 
    23 from cubicweb.toolsutils import RQLExecuteMatcher, read_config
    24 from cubicweb.toolsutils import (RQLExecuteMatcher, option_value_from_env,
       
    25                                  read_config)
    24 
    26 
    25 
    27 
    26 class RQLExecuteMatcherTests(unittest.TestCase):
    28 class RQLExecuteMatcherTests(unittest.TestCase):
    27     def matched_query(self, text):
    29     def matched_query(self, text):
    28         match = RQLExecuteMatcher.match(text)
    30         match = RQLExecuteMatcher.match(text)
    76 """
    78 """
    77 
    79 
    78 
    80 
    79 class ToolsUtilsTC(unittest.TestCase):
    81 class ToolsUtilsTC(unittest.TestCase):
    80 
    82 
       
    83     def test_option_value_from_env(self):
       
    84         os.environ['CW_DB_HOST'] = 'here'
       
    85         try:
       
    86             self.assertEqual(option_value_from_env('db-host'), 'here')
       
    87             self.assertEqual(option_value_from_env('db-host', 'nothere'), 'here')
       
    88             self.assertEqual(option_value_from_env('db-hots', 'nothere'), 'nothere')
       
    89         finally:
       
    90             del os.environ['CW_DB_HOST']
       
    91 
    81     def test_read_config(self):
    92     def test_read_config(self):
    82         with tempfile.NamedTemporaryFile() as f:
    93         with tempfile.NamedTemporaryFile() as f:
    83             f.write(SOURCES_CONTENT)
    94             f.write(SOURCES_CONTENT)
    84             f.seek(0)
    95             f.seek(0)
    85             config = read_config(f.name)
    96             config = read_config(f.name)
    94                 'db-host': None,
   105                 'db-host': None,
    95             },
   106             },
    96         }
   107         }
    97         self.assertEqual(config, expected)
   108         self.assertEqual(config, expected)
    98 
   109 
       
   110     def test_read_config_env(self):
       
   111         os.environ['CW_DB_HOST'] = 'here'
       
   112         try:
       
   113             with tempfile.NamedTemporaryFile() as f:
       
   114                 f.write(SOURCES_CONTENT)
       
   115                 f.seek(0)
       
   116                 config = read_config(f.name)
       
   117         finally:
       
   118             del os.environ['CW_DB_HOST']
       
   119         self.assertEqual(config['system']['db-host'], 'here')
       
   120 
    99 
   121 
   100 if __name__ == '__main__':
   122 if __name__ == '__main__':
   101     unittest.main()
   123     unittest.main()