def test_write_dict_to_csv_file(self):
     """Test write_dict_to_csv_file."""
     data_dict = {'col_1': [1, 2, 3, 4, 5], 'col_2': [6, 7, 8, 9, 10]}
     # Initialise path strings for temporary file and directory
     temp_dir, file_path = str(), str()
     try:
         # Create temporary directory
         temp_dir = tempfile.mkdtemp()
         # Create path for temporary CSV file
         pyu4v_path = os.path.join(temp_dir, 'temp_spread.csv')
         # Write to CSV file, assert file exists
         file_handler.write_dict_to_csv_file(
             pyu4v_path, data_dict, delimiter='#', quotechar='"')
         self.assertTrue(os.path.isfile(pyu4v_path))
         # Read the CSV file and assert contents are equal to data written
         # in previous step
         read_data = file_handler.read_csv_values(
             pyu4v_path, convert=True, delimiter='#', quotechar='"')
         self.assertEqual(data_dict, read_data)
         # Cleanup
         self.addCleanup(self.cleanup_files, pyu4v_orig=None,
                         temp_file=pyu4v_path, temp_dir=temp_dir)
     # If there are any exceptions raised ensure cleanup is carried out on
     # temporary files
     except Exception as e:
         self.addCleanup(self.cleanup_files, pyu4v_orig=None,
                         temp_file=file_path, temp_dir=temp_dir)
         raise Exception('Test failed with exception: {msg}'.format(msg=e))
Beispiel #2
0
    def read_csv_values(file_name):
        """Read any csv file with headers.

        DEPRECATION NOTICE: CommonFunctions.read_csv_values() will be
        refactored in PyU4V version 9.3 in favour of
        utils.file_handler.read_csv_values(). For further information please
        consult PyU4V 9.1 release notes.

        You can extract the multiple lists from the headers in the CSV file.
        In your own script, call this function and assign to data variable,
        then extract the lists to the variables.

        :param file_name: path to the file -- str
        :returns: file contents -- dict
        """
        return file_handler.read_csv_values(file_name)
 def test_set_thresholds_from_csv(self):
     """Test set_thresholds_from_csv."""
     # Generate CSV settings file
     csv_file_name = 'test.csv'
     temp_dir = self.create_temp_directory()
     csv_file_path = os.path.join(temp_dir, csv_file_name)
     self.perf.generate_threshold_settings_csv(csv_file_path)
     self.assertTrue(os.path.isfile(csv_file_path))
     # Read CSV file
     csv_data = file_handler.read_csv_values(csv_file_path)
     # Make change to metric
     num_metrics = len(csv_data.get('metric'))
     orig_values = (0, 0)
     updated_values = (0, 0)
     metric_set = 'ReadResponseTime'
     for i in range(0, num_metrics):
         category = csv_data.get(pc.CATEGORY)[i]
         metric = csv_data.get(pc.METRIC)[i]
         if category == pc.ARRAY and metric == metric_set:
             orig_values = (csv_data.get(pc.FIRST_THRESH)[i],
                            csv_data.get(pc.SEC_THRESH)[i])
             updated_values = (int(orig_values[0]) + 5,
                               int(orig_values[1]) + 5)
             csv_data[pc.FIRST_THRESH][i] = updated_values[0]
             csv_data[pc.SEC_THRESH][i] = updated_values[1]
             csv_data[pc.KPI][i] = True
     # Write updated metrics list to CSV
     csv_file_name_updated = 'test_updated.csv'
     csv_file_path_updated = os.path.join(temp_dir, csv_file_name_updated)
     file_handler.write_dict_to_csv_file(csv_file_path_updated, csv_data)
     # Apply update to metrics via CSV
     self.perf.set_thresholds_from_csv(csv_file_path_updated)
     # Get updated threshold settings from Unisphere
     t_settings = self.perf.get_threshold_category_settings(pc.ARRAY)
     for t in t_settings.get(pc.PERF_THRESH):
         if t.get(pc.METRIC) == metric_set:
             self.assertEqual(t.get(pc.FIRST_THRESH), updated_values[0])
             self.assertEqual(t.get(pc.SEC_THRESH), updated_values[1])
     # Reapply old metric settings
     self.perf.set_thresholds_from_csv(csv_file_path)
     # Check old settings were successfully re-applied
     t_settings = self.perf.get_threshold_category_settings(pc.ARRAY)
     for t in t_settings.get(pc.PERF_THRESH):
         if t.get(pc.METRIC) == metric_set:
             self.assertEqual(t.get(pc.FIRST_THRESH), int(orig_values[0]))
             self.assertEqual(t.get(pc.SEC_THRESH), int(orig_values[1]))
Beispiel #4
0
 def test_write_to_csv_file(self):
     """Test write_to_csv_file."""
     # Initialise path strings for temporary file and directory
     temp_dir, file_path = str(), str()
     try:
         # Create temporary directory
         temp_dir = tempfile.mkdtemp()
         # Create path for temporary CSV file
         pyu4v_path = os.path.join(temp_dir, 'temp_spread.csv')
         # Initialise empty string
         data = list()
         # Create file contents
         for i in range(0, 9, 3):
             line = list()
             for x in range(i, i + 3):
                 val = 'key' if i <= 2 else 'value'
                 line.append('{prefix}-{num}'.format(prefix=val, num=x))
             data.append(line)
         # Write file contents to the temporary file path
         file_handler.write_to_csv_file(pyu4v_path, data)
         # Assert the file was created at the path specified
         assert os.path.isfile(pyu4v_path) is True
         # Read the contents of the file just written to
         read_contents = file_handler.read_csv_values(pyu4v_path)
         # Assert file contents are what we expect them to be
         ref_contents = {
             'key-0': ['value-3', 'value-6'],
             'key-1': ['value-4', 'value-7'],
             'key-2': ['value-5', 'value-8']
         }
         self.assertEqual(ref_contents, read_contents)
         # Cleanup
         self.addCleanup(self.cleanup_files,
                         pyu4v_orig=None,
                         temp_file=pyu4v_path,
                         temp_dir=temp_dir)
     # If there are any exceptions raised ensure cleanup is carried out on
     # temporary files
     except Exception as e:
         self.addCleanup(self.cleanup_files,
                         pyu4v_orig=None,
                         temp_file=file_path,
                         temp_dir=temp_dir)
         raise Exception('Test failed with exception: {msg}'.format(msg=e))
Beispiel #5
0
 def test_read_csv_values(self):
     """Test read_csv_values."""
     # Initialise path strings for temporary file and directory
     temp_dir, file_path = str(), str()
     try:
         # Create temporary directory
         temp_dir = tempfile.mkdtemp()
         # Create path for temporary CSV file
         pyu4v_path = os.path.join(temp_dir, 'temp_spread.csv')
         # Initialise empty string
         data = str()
         # Create file contents
         for i in range(0, 9):
             val = 'key' if i <= 2 else 'value'
             data += '{prefix}-{num}'.format(prefix=val, num=i)
             data += '\n' if (i + 1) % 3 == 0 else ','
         # Write file contents to the temporary file path
         with open(pyu4v_path, 'w') as file:
             file.writelines(data)
         # Using the file just written to, read contents and return dict
         read_contents = file_handler.read_csv_values(pyu4v_path)
         # Assert return is a dict
         self.assertIsInstance(read_contents, dict)
         ref_contents = {
             'key-0': ['value-3', 'value-6'],
             'key-1': ['value-4', 'value-7'],
             'key-2': ['value-5', 'value-8']
         }
         # Assert CSV reader returned expected populated dict
         self.assertEqual(ref_contents, read_contents)
         # Cleanup temporary files
         self.addCleanup(self.cleanup_files,
                         pyu4v_orig=None,
                         temp_file=pyu4v_path,
                         temp_dir=temp_dir)
     # If there are any exceptions raised ensure cleanup is carried out on
     # temporary files
     except Exception as e:
         self.addCleanup(self.cleanup_files,
                         pyu4v_orig=None,
                         temp_file=file_path,
                         temp_dir=temp_dir)
         raise Exception('Test failed with exception: {msg}'.format(msg=e))