def test_FileDict_copyandrename(self): """FileDict can digest the a ComponentFile with the copy method, and rename it""" TestList = FileDict({ "my_testfile": ComponentFile(src="testfile_original", dest="testfile_copy") }) TestList.digest() assert os.path.isfile("./testfile_copy") self.cleanup_list.append("testfile_copy")
def test_FileDict_new_good_arguments(self): """FileDict can be initialized with correct arguments""" TestList = FileDict({"first_key": ComponentFile("/foo/bar")}) self.assertIsInstance(TestList, FileDict) TestList = FileDict({ "first_key": ComponentFile("/foo/bar"), "second_key": ComponentFile("/lar/mar") }) self.assertIsInstance(TestList, FileDict)
def test_FileDict_digest_empty(self): """FileDict are empty after digestion with flag=work""" TestList = FileDict({ "my link": ComponentFile(src="testfile_original", dest="testfile_link_newlink", copy_method="link") }) TestList.digest(flag="work") self.assertEqual(len(TestList), 0) self.cleanup_list.append("testfile_link_newlink")
def test_FileDict_linkandrename(self): """FileDict can digest the a ComponentFile with the link method, and rename it""" TestList = FileDict({ "my link": ComponentFile(src="testfile_original", dest="testfile_link", copy_method="link") }) TestList.digest() assert os.path.islink("./testfile_link") self.cleanup_list.append("testfile_link")
def test_env_override_filetables(self): """ ComponentCompute can override ComponentFile object `src` from enviornmental variables """ self.test_component_compute.files["input"] = FileDict( {"foo": ComponentFile("/foo/bar")}) with set_env(foo="/fiz/buzz"): self.test_component_compute._prepare_override_filetables_from_env() self.assertEqual( self.test_component_compute.files["input"]["foo"].src, "/fiz/buzz", "The actual input is %s" % self.test_component_compute.files["input"]["foo"].src)
def test_read_filetables(self): """ ComponentCompute can read a default JSON filetable """ self.test_component_compute._prepare_read_filetables() self.assertEqual( self.test_component_compute.files["input"], FileDict({ "input_file_1": ComponentFile(src="/this/is/the/first/testfile/file1_source", dest="/this/is/the/first/testfile/file1_dest"), "input_file_2": ComponentFile(src="/this/is/the/second/testfile/file2_source", dest="/this/is/the/second/testfile/file2_dest") }))
def test_modify_filetables(self): """ ComponentCompute can modify src files based upon a modify JSON filetable """ self.test_component_compute._prepare_read_filetables() input_before = self.test_component_compute.files["input"].items() self.test_component_compute._prepare_modify_filetables() input_after = self.test_component_compute.files["input"].items() self.assertEqual( self.test_component_compute.files["input"], FileDict({ "input_file_1": ComponentFile( src="/this/is/the/first/testfile/file1_source_different", dest="/this/is/the/first/testfile/file1_dest"), "input_file_2": ComponentFile(src="/this/is/the/second/testfile/file2_source", dest="/this/is/the/second/testfile/file2_dest") }), msg="Input before was %s, Input after was %s" % (input_before, input_after))
def __init__(self, **EchamComputeArgs): # FIXME: This all belongs in a generalized class, not here... super(Echam6Couple_Ice, self).__init__(**EchamComputeArgs) try: assert isinstance(self.calendar, CouplingEsmCalendar) except AssertionError: raise TypeError( "You must supply a calendar with coupling functionality: CouplingEsmCalendar, and not %s" % type(self.calendar)) self.files["couple"] = FileDict() self._register_directory("couple", use_Name="generic") self.__cleanup_list = [] self._cdo_stderr = open(self.couple_dir + "/EchamCouple_Ice_cdo_log", "w") self.CDO = cdo.Cdo(logging=True, logFile=self._cdo_stderr) # Get relevant environmental variables self.ECHAM_TO_ISM_multiyear_mean = load_environmental_variable_1_0( "ECHAM_TO_ISM_multiyear_mean") self.ECHAM_TO_ISM_time_mean = load_environmental_variable_1_0( "ECHAM_TO_ISM_time_mean")
def setUp(self): self.test_list = FileDict() with open("testfile_original", "w") as f: f.write("This is a small testfile") self.cleanup_list = ["testfile_original"]
class TestFileDict(unittest.TestCase): """Various tests for FileDict""" def setUp(self): self.test_list = FileDict() with open("testfile_original", "w") as f: f.write("This is a small testfile") self.cleanup_list = ["testfile_original"] def test_FileDict_new_empty(self): """An empty FileDict can be initialized""" self.assertIsInstance(self.test_list, FileDict) def test_FileDict_new_good_arguments(self): """FileDict can be initialized with correct arguments""" TestList = FileDict({"first_key": ComponentFile("/foo/bar")}) self.assertIsInstance(TestList, FileDict) TestList = FileDict({ "first_key": ComponentFile("/foo/bar"), "second_key": ComponentFile("/lar/mar") }) self.assertIsInstance(TestList, FileDict) def test_FileDict_new_bad_arguments(self): """FileDict cannot be initialized with bad arguments""" self.assertRaises(TypeError, FileDict, "a", 1, 1.0) def test_FileDict_update(self): """FileDict can be updated to properly""" self.test_list.update({"foo": ComponentFile("/foo/bar")}) self.assertEqual(self.test_list["foo"], ComponentFile("/foo/bar")) def test_FileDict_append_bad(self): """FileDict fails if you try to do something stupid, like appending a non-ComponentFile""" self.assertRaises(TypeError, self.test_list.update, {"my favorite key": "lala"}) def test_FileDict_copyandrename(self): """FileDict can digest the a ComponentFile with the copy method, and rename it""" TestList = FileDict({ "my_testfile": ComponentFile(src="testfile_original", dest="testfile_copy") }) TestList.digest() assert os.path.isfile("./testfile_copy") self.cleanup_list.append("testfile_copy") def test_FileDict_linkandrename(self): """FileDict can digest the a ComponentFile with the link method, and rename it""" TestList = FileDict({ "my link": ComponentFile(src="testfile_original", dest="testfile_link", copy_method="link") }) TestList.digest() assert os.path.islink("./testfile_link") self.cleanup_list.append("testfile_link") def test_FileDict_digest_empty(self): """FileDict are empty after digestion with flag=work""" TestList = FileDict({ "my link": ComponentFile(src="testfile_original", dest="testfile_link_newlink", copy_method="link") }) TestList.digest(flag="work") self.assertEqual(len(TestList), 0) self.cleanup_list.append("testfile_link_newlink") def tearDown(self): """Removes testfiles generated by FileDict tests""" for testfile in self.cleanup_list: os.remove(testfile)