def test_success_withkey(self): """ unit test to validate get_rows_info() success scenario (with unique_col value passed) in which header and values were retrieved successfully formatted it with format_data. get_rows_info() should return formatted output as dictionary """ out = "dummy output\n0000 output" header_pattern = r'(\w+)\s(\w+)' value_pattern = r'(\d+)\s(\w+)' def format_data(dict_in): return {"key": "value", "dummy": "output"} unique_col = "key" devices = get_rows_info(out, header_pattern, value_pattern, unique_col=unique_col, format_data=format_data, hdr_index=0, val_start_index=1) self.assertEqual(devices, {"value": { "key": "value", "dummy": "output" }})
def test_nokey_novalue_match(self): """ unit test to validate get_rows_info() by passing key as none and header matches but values are not matched get_rows_info() should return empty list """ out = "dummy output\n0000 output" header_pattern = r'(\w+)\s(\w+)' value_pattern = r'\d+\s\d(\w+)' # regex doesn't match values in out return_value = get_rows_info(out, header_pattern, value_pattern) self.assertEqual(return_value, [])
def test_success_without_format_data(self): """ unit test to validate get_rows_info() method success scenario without passing format_data. get_rows_info() should return the header and values mapped """ out = "dummy output\n0000 output" header_pattern = r'(\w+)\s(\w+)' value_pattern = r'(\d+)\s(\w+)' unique_col = 'dummy' devices = get_rows_info(out, header_pattern, value_pattern, unique_col=unique_col, hdr_index=0, val_start_index=1) self.assertEqual(devices, {'0000': {"dummy": "0000", "output": "output"}})
def test_success_without_key(self): """ unit test to validate get_rows_info() success scenario (without passing unique_col value) in which header and values were retrieved successfully formatted it with format_data. get_rows_info() should return formatted output as list """ out = "dummy output\n0000 output" header_pattern = r'(\w+)\s(\w+)' value_pattern = r'(\d+)\s(\w+)' def format_data(dict_in): return {"key": "value", "dummy": "output"} devices = get_rows_info(out, header_pattern, value_pattern, format_data=format_data, hdr_index=0, val_start_index=1) self.assertEqual(devices, [{"key": "value", "dummy": "output"}])
def test__header_value_lenmissmatch(self): """ unit test to validate get_rows_info() with miss match in length of header items parsed out using header regex and that of value items parsed out using value regex. get_rows_info() should return {} with key """ out = "dummy output\n \n 0000 output" header_pattern = r'(\w+)\s(\w+)' value_pattern = r'\d+\s(\w+)' def format_data(data): return {"key": "value", "dummy": "output"} unique_col = "key" devices = get_rows_info(out, header_pattern, value_pattern, unique_col=unique_col, format_data=format_data) self.assertEqual(devices, {})