Source code for tests.custom_callables

#!/local/bin/python
'''
-------------------
custom_callables.py
-------------------

A module containing tests for the standard argparser custom callables I'm 
using :ref:`custom_callables.py <custom_callables_autodocs>`. These tests use 
the standard `unittest <http://docs.python.org/2/library/unittest.html>`_ 
package and extend the :py:class:`unittest.TestCase` class.

.. moduleauthor:: Nick Schurch <nschurch@dundee.ac.uk>

:created_on: 2013-04-08

'''

__version__ = "1.0"

import unittest, os, tempfile, shutil, argparse, warnings
import script_options.custom_callables as cc
        
[docs]class TestParsingRoutines(unittest.TestCase): ''' give the path-based functions a non-existent path and check that they return it or error appropriately'''
[docs] def setUp(self): # setup a new temp directory with a file in it self.existing_path = tempfile.mkdtemp() self.existing_file = "%s/thisfileexists.txt" % self.existing_path temp_file = open(self.existing_file, "w") temp_file.write("this exists") temp_file.close() # make up a mythical path and file based on the existing dir self.nonexisting_path = self.existing_path+"/testfornonexistingdir" self.nonexisting_file = self.nonexisting_path+"/thisdoesntexists.txt"
[docs] def test_input_path(self): result = cc.input_path(self.existing_path) msg = "Not finding existing path" self.assertEqual(result, self.existing_path, msg) # make sure this raises an argparse error self.assertRaises(argparse.ArgumentTypeError, cc.input_path, self.nonexisting_path) # make sure this raises an Type error if not given a string self.assertRaises(TypeError, cc.input_path, 5) self.assertRaises(TypeError, cc.input_path, [5]) self.assertRaises(TypeError, cc.input_path, {"5":5})
[docs] def test_input_file(self): result = cc.input_file(self.existing_file) msg = "Not finding existing file" self.assertEqual(result, self.existing_file, msg) # make sure this raises an argparse error self.assertRaises(argparse.ArgumentTypeError, cc.input_file, self.nonexisting_file) # make sure this raises an Type error if not given a string self.assertRaises(TypeError, cc.input_file, 5) self.assertRaises(TypeError, cc.input_file, [5]) self.assertRaises(TypeError, cc.input_file, {"5":5})
[docs] def test_output_path(self): result = cc.output_path(self.existing_path) msg = "Not finding existing path" self.assertEqual(result, self.existing_path, msg) # ignore expected warning messgaes with warnings.catch_warnings(): warnings.simplefilter("ignore") result = cc.output_path(self.nonexisting_path) msg = "Not creating the new path" self.assertEqual(result, self.nonexisting_path, msg) msg = "Not returning the correct path string" self.assertTrue(os.path.exists(self.nonexisting_path), msg) # make sure this raises an Type error if not given a string self.assertRaises(TypeError, cc.output_path, 5) self.assertRaises(TypeError, cc.output_path, [5]) self.assertRaises(TypeError, cc.output_path, {"5":5})
[docs] def test_output_file(self): result = cc.output_file(self.existing_file) msg = "Not finding existing file" self.assertEqual(result, self.existing_file, msg) # ignore expected warning messgaes with warnings.catch_warnings(): warnings.simplefilter("ignore") result = cc.output_path(self.nonexisting_file) msg = "Not returning the correct file string" self.assertEqual(result, self.nonexisting_file, msg) msg = "Not creating the new path" self.assertTrue(os.path.exists(self.nonexisting_path), msg) # make sure this raises an Type error if not given a string self.assertRaises(TypeError, cc.output_path, 5) self.assertRaises(TypeError, cc.output_path, [5]) self.assertRaises(TypeError, cc.output_path, {"5":5})
[docs] def tearDown(self): # remove teh tmpdir tree shutil.rmtree(self.existing_path)
if __name__ == '__main__': unittest.main()