def test_write_images(self): """Test the write_images function.""" test_table = Table("Some Table") # Get test PDF some_pdf = "%s/minimal.pdf" % os.path.dirname(__file__) # This should work fine test_table.add_image(some_pdf) testdir = tmp_directory_name() self.addCleanup(shutil.rmtree, testdir) try: test_table.write_images(testdir) except TypeError: self.fail("Table.write_images raised an unexpected TypeError.") # Check that output file exists expected_file = os.path.join(testdir, "minimal.png") self.assertTrue(os.path.exists(expected_file)) # Try wrong type of input argument bad_arguments = [None, 5, {}, []] for argument in bad_arguments: with self.assertRaises(TypeError): test_table.write_images(argument) self.doCleanups()
def test_create_files_with_removal(self): """Test the removal of old files in create_files()""" testdir = tmp_directory_name() # Step 1: Create test directory containing random file os.makedirs(testdir) self.addCleanup(shutil.rmtree, testdir) testfile = os.path.join(testdir, "test.txt") with open(testfile, "w") as f: f.write("test") self.assertTrue(os.path.isfile(testfile)) # Step 2: Create submission and write output to test directory # Without overwriting of files test_submission = Submission() tab = Table("test") test_submission.add_table(tab) test_submission.create_files(testdir, remove_old=False) # Test file should still exist self.assertTrue(os.path.isfile(testfile)) # Step 3: Recreate submission files with removal test_submission.create_files(testdir, remove_old=True) # Test file should no longer exist self.assertFalse(os.path.isfile(testfile))
def test_copy_files(self): """Test the copy_files function.""" test_table = Table("Some Table") some_pdf = "%s/minimal.pdf" % os.path.dirname(__file__) testdir = tmp_directory_name() self.addCleanup(shutil.rmtree, testdir) os.makedirs(testdir) test_table.add_additional_resource("a plot",some_pdf, copy_file=True) test_table.copy_files(testdir)
def test_create_files(self): """Test create_files() for Submission.""" testdir = tmp_directory_name() test_submission = Submission() tab = Table("test") test_submission.add_table(tab) test_submission.create_files(testdir) self.doCleanups()
def test_write_yaml(self): """Test write_yaml() for Table.""" test_table = Table("Some Table") test_variable = Variable("Some Variable") test_table.add_variable(test_variable) testdir = tmp_directory_name() self.addCleanup(shutil.rmtree, testdir) try: test_table.write_yaml(testdir) except TypeError: self.fail("Table.write_yaml raised an unexpected TypeError.") with self.assertRaises(TypeError): test_table.write_yaml(None) self.doCleanups()
def test_yaml_output(self): """Test yaml dump""" tmp_dir = tmp_directory_name() # Create test dictionary testlist = [("x", 1.2), ("x", 2.2), ("y", 0.12), ("y", 0.22)] testdict = defaultdict(list) for key, value in testlist: testdict[key].append(value) # Create test submission test_submission = Submission() test_table = Table("TestTable") x_variable = Variable("X", is_independent=True, is_binned=False) x_variable.values = testdict['x'] y_variable = Variable("Y", is_independent=False, is_binned=False) y_variable.values = testdict['y'] test_table.add_variable(x_variable) test_table.add_variable(y_variable) test_submission.add_table(test_table) test_submission.create_files(tmp_dir) # Test read yaml file table_file = os.path.join(tmp_dir, "testtable.yaml") try: with open(table_file, 'r') as testfile: testyaml = yaml.safe_load(testfile) except yaml.YAMLError as exc: print(exc) # Test compare yaml file to string testtxt = ( "dependent_variables:\n- header:\n name: Y\n values:\n" + " - value: 0.12\n - value: 0.22\nindependent_variables:\n" + "- header:\n name: X\n values:\n - value: 1.2\n - value: 2.2\n" ) with open(table_file, 'r') as testfile: testyaml = testfile.read() self.assertEqual(str(testyaml), testtxt) self.addCleanup(os.remove, "submission.tar.gz") self.addCleanup(shutil.rmtree, tmp_dir) self.doCleanups()