Skip to content

Commit

Permalink
Commit changes made by code formatters
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Dec 1, 2023
1 parent 8ab4b59 commit 4d3522f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
7 changes: 5 additions & 2 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'],
Expand Down
33 changes: 22 additions & 11 deletions test/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -32,18 +37,21 @@ 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
nonexistent_file_path = 'nonexistent.csv'
with self.assertRaises(FileNotFoundError):
convert_csv_to_json(nonexistent_file_path)


class TestAddGeocodingToJson(unittest.TestCase):

@patch('src.main.geocode', side_effect=[
Expand All @@ -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)
Expand All @@ -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()

0 comments on commit 4d3522f

Please sign in to comment.