diff --git a/src/main.py b/src/main.py index be0337d..072797f 100644 --- a/src/main.py +++ b/src/main.py @@ -19,6 +19,7 @@ def convert_csv_to_json(file_path): raise ValueError('Failed to convert CSV file to JSON. Exiting script.') return csv_rows + def add_geocoding_to_json(data): for d in data: # Variables @@ -37,14 +38,16 @@ def add_geocoding_to_json(data): d['time_zone'] = time_zone return data + if __name__ == '__main__': - csv_file_path=os.getcwd() + '/../test_data/sites_with_clients.csv' + csv_file_path = os.getcwd() + '/../test_data/sites_with_clients.csv' # Convert CSV to valid JSON json_data_without_geocoding = convert_csv_to_json(csv_file_path) - json_data_with_geocoding = add_geocoding_to_json(json_data_without_geocoding) + json_data_with_geocoding = add_geocoding_to_json( + json_data_without_geocoding) juniper_script( mist_api_token=os.environ['MIST_API_TOKEN'], diff --git a/test/test_main.py b/test/test_main.py index 2e797da..8da86ba 100644 --- a/test/test_main.py +++ b/test/test_main.py @@ -4,16 +4,21 @@ from unittest.mock import patch from src.main import convert_csv_to_json, add_geocoding_to_json + class TestCsvToJson(unittest.TestCase): def setUp(self): # Create a temporary CSV file for testing self.csv_data = [ - {'Site Name': 'Test location 1', 'Site Address': '40 Mayflower Dr, Plymouth PL2 3DG', 'Enable GovWifi': ' "TRUE"', 'Enable MoJWifi': ' "FALSE"', 'GovWifi Radius Key': '00000DD0000BC0EEE000', 'Wired NACS Radius Key': '00000DD0000BC0EEE000'}, - {'Site Name': 'Test location 2', 'Site Address': '102 Petty France, London SW1H 9AJ', 'Enable GovWifi': ' "TRUE"', 'Enable MoJWifi': ' "FALSE"', 'GovWifi Radius Key': '0D0E0DDE000BC0EEE000', 'Wired NACS Radius Key': '00000DD0000BC0EEE000'}, - {'Site Name': 'Test location 3', 'Site Address': 'Met Office, FitzRoy Road, Exeter, Devon, EX1 3PB', 'Enable GovWifi': ' "TRUE"', 'Enable MoJWifi': ' "FALSE"', 'GovWifi Radius Key': '0D0E0DDE080BC0EEE000', 'Wired NACS Radius Key': '00000DD0000BC0EEE000'} + {'Site Name': 'Test location 1', 'Site Address': '40 Mayflower Dr, Plymouth PL2 3DG', 'Enable GovWifi': ' "TRUE"', + 'Enable MoJWifi': ' "FALSE"', 'GovWifi Radius Key': '00000DD0000BC0EEE000', 'Wired NACS Radius Key': '00000DD0000BC0EEE000'}, + {'Site Name': 'Test location 2', 'Site Address': '102 Petty France, London SW1H 9AJ', 'Enable GovWifi': ' "TRUE"', + 'Enable MoJWifi': ' "FALSE"', 'GovWifi Radius Key': '0D0E0DDE000BC0EEE000', 'Wired NACS Radius Key': '00000DD0000BC0EEE000'}, + {'Site Name': 'Test location 3', 'Site Address': 'Met Office, FitzRoy Road, Exeter, Devon, EX1 3PB', 'Enable GovWifi': ' "TRUE"', + 'Enable MoJWifi': ' "FALSE"', 'GovWifi Radius Key': '0D0E0DDE080BC0EEE000', 'Wired NACS Radius Key': '00000DD0000BC0EEE000'} ] - self.csv_file = tempfile.NamedTemporaryFile(mode='w', delete=False, newline='', suffix='.csv') + self.csv_file = tempfile.NamedTemporaryFile( + mode='w', delete=False, newline='', suffix='.csv') self.csv_writer = csv.DictWriter(self.csv_file, fieldnames=[ 'Site Name', 'Site Address', @@ -32,11 +37,13 @@ def test_given_csv_file_when_csv_valid_then_convert_to_json(self): self.assertEqual(actual_json, expected_json) def test_given_csv_file_when_csv_file_empty_then_raise_value_error(self): - empty_csv_file = tempfile.NamedTemporaryFile(mode='w', delete=False, newline='', suffix='.csv') + empty_csv_file = tempfile.NamedTemporaryFile( + mode='w', delete=False, newline='', suffix='.csv') empty_csv_file.close() with self.assertRaises(ValueError) as error: convert_csv_to_json(empty_csv_file.name) - self.assertEqual(str(error.exception), 'Failed to convert CSV file to JSON. Exiting script.') + self.assertEqual(str(error.exception), + 'Failed to convert CSV file to JSON. Exiting script.') def test_given_file_path_when_csv_file_not_found_then_raise_FileNotFoundError(self): # Test if the function handles a nonexistent CSV file correctly @@ -44,6 +51,7 @@ def test_given_file_path_when_csv_file_not_found_then_raise_FileNotFoundError(se with self.assertRaises(FileNotFoundError): convert_csv_to_json(nonexistent_file_path) + class TestAddGeocodingToJson(unittest.TestCase): @patch('src.main.geocode', side_effect=[ @@ -63,13 +71,17 @@ def test_given_site_name_and_site_address_in_json_format_when_function_called_th data = [ {'Site Name': 'Site1', 'Site Address': '40 Mayflower Dr, Plymouth PL2 3DG'}, {'Site Name': 'Site2', 'Site Address': '102 Petty France, London SW1H 9AJ'}, - {'Site Name': 'Site3', 'Site Address': 'Met Office, FitzRoy Road, Exeter, Devon, EX1 3PB'} + {'Site Name': 'Site3', + 'Site Address': 'Met Office, FitzRoy Road, Exeter, Devon, EX1 3PB'} ] expected_data = [ - {'Site Name': 'Site1', 'Site Address': '40 Mayflower Dr, Plymouth PL2 3DG', 'gps': {'latitude': 50.3868633, 'longitude': -4.1539256}, 'country_code': 'GB', 'time_zone': 'Europe/London'}, - {'Site Name': 'Site2', 'Site Address': '102 Petty France, London SW1H 9AJ', 'gps': {'latitude': 51.499929300000005, 'longitude': -0.13477761285315926}, 'country_code': 'GB', 'time_zone': 'Europe/London'}, - {'Site Name': 'Site3', 'Site Address': 'Met Office, FitzRoy Road, Exeter, Devon, EX1 3PB', 'gps': {'latitude': 50.727350349999995, 'longitude': -3.4744726127760086}, 'country_code': 'GB', 'time_zone': 'Europe/London'} + {'Site Name': 'Site1', 'Site Address': '40 Mayflower Dr, Plymouth PL2 3DG', 'gps': { + 'latitude': 50.3868633, 'longitude': -4.1539256}, 'country_code': 'GB', 'time_zone': 'Europe/London'}, + {'Site Name': 'Site2', 'Site Address': '102 Petty France, London SW1H 9AJ', 'gps': { + 'latitude': 51.499929300000005, 'longitude': -0.13477761285315926}, 'country_code': 'GB', 'time_zone': 'Europe/London'}, + {'Site Name': 'Site3', 'Site Address': 'Met Office, FitzRoy Road, Exeter, Devon, EX1 3PB', 'gps': { + 'latitude': 50.727350349999995, 'longitude': -3.4744726127760086}, 'country_code': 'GB', 'time_zone': 'Europe/London'} ] actual_data = add_geocoding_to_json(data) @@ -78,4 +90,3 @@ def test_given_site_name_and_site_address_in_json_format_when_function_called_th find_timezone.assert_called() mock_find_country_code.assert_called() mock_geocode.assert_called() -