-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_flights.py
273 lines (221 loc) · 10.5 KB
/
test_flights.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import json
import csv
import logging
import re
import pytest
from typing import List, Dict, Any
def setup_custom_logger(name, log_file, level=logging.ERROR):
formatter = logging.Formatter(fmt='%(asctime)s - %(levelname)s - %(message)s')
handler = logging.FileHandler(log_file, mode='a')
handler.setFormatter(formatter)
logger = logging.getLogger(name)
logger.setLevel(level)
logger.addHandler(handler)
return logger
# load_json_data function
def load_json_data(file_path: str) -> List[Dict[str, Any]]:
logger = setup_custom_logger('json_logger', 'app.log', level=logging.ERROR)
try:
with open(file_path, 'r') as file:
data = json.load(file)
return data['flights']
except FileNotFoundError as e:
logger.error(f"File '{file_path}' not found.")
except json.JSONDecodeError as e:
logger.error(f"Error decoding JSON in file '{file_path}': {e}")
except Exception as e:
logger.error(f"An unexpected error occurred while loading data: {e}")
return []
file_path = '/Users/sheila/Desktop/Prueba_Tecnica/data.json'
flight_records1 = load_json_data(file_path)
# Test loading JSON data function
def test_load_json_data_valid_file():
file_path = '/Users/sheila/Desktop/Prueba_Tecnica/data.json'
flight_records = load_json_data(file_path)
assert isinstance(flight_records, list)
assert len(flight_records) > 0
def test_load_json_data_invalid_file():
file_path = '/Users/sheila/Desktop/Prueba_Tecnica/invalid_jsonfile.json'
flight_records = load_json_data(file_path)
assert isinstance(flight_records, list)
assert len(flight_records) == 0
def test_load_json_data_invalid_json():
file_path = '/Users/sheila/Desktop/Prueba_Tecnica/invalid_jsondata.json'
flight_records = load_json_data(file_path)
assert isinstance(flight_records, list)
assert len(flight_records) == 0
# load_csv_data function
def load_csv_data(file_path: str) -> List[Dict[str, Any]]:
logger = setup_custom_logger('csv_logger', 'app.log', level=logging.ERROR)
flight_records2 = []
try:
with open(file_path, 'r', newline='') as csvfile:
csv_reader = csv.DictReader(csvfile)
for row in csv_reader:
flight_record = {
'Flight_ID': row['Flight_ID'],
'Passengers': int(row['Passengers']),
'Revenue': int(row['Revenue'])
}
flight_records2.append(flight_record)
return flight_records2
except FileNotFoundError as e:
logger.error(f"File '{file_path}' not found.")
except csv.Error as e:
logger.error(f"Error reading CSV file '{file_path}': {e}")
except Exception as e:
logger.error(f"An unexpected error occurred while loading data: {e}")
return []
file_path = '/Users/sheila/Desktop/Prueba_Tecnica/data.csv'
flight_records2 = load_csv_data(file_path)
# Test loading CSV data function
def test_load_csv_data_valid_file():
file_path = '/Users/sheila/Desktop/Prueba_Tecnica/data.csv'
flight_records = load_csv_data(file_path)
assert isinstance(flight_records, list)
assert len(flight_records) > 0
def test_load_csv_data_invalid_file():
file_path = '/Users/sheila/Desktop/Prueba_Tecnica/invalid_csvfile.csv'
flight_records = load_csv_data(file_path)
assert isinstance(flight_records, list)
assert len(flight_records) == 0
def test_load_csv_data_invalid_csv():
file_path = '/Users/sheila/Desktop/Prueba_Tecnica/invalid_csvdata.csv'
flight_records = load_csv_data(file_path)
assert isinstance(flight_records, list)
assert len(flight_records) == 0
# parse_log_data function
def parse_log_data(file_path: str) -> List[Dict[str, Any]]:
logger = setup_custom_logger('log_parser', 'app.log', level=logging.ERROR)
flight_records3 = []
try:
with open(file_path, 'r') as log_file:
for line in log_file:
match = re.match(r'(\d{4}-\d{2}-\d{2}) (\w+) from (\w+) to (\w+) departed.', line)
if match:
flight_record = {
'Date': match.group(1),
'Flight_ID': match.group(2),
'Departure_Airport': match.group(3),
'Arrival_Airport': match.group(4)
}
flight_records3.append(flight_record)
else:
logger.error(f"Error parsing line: {line.strip()}")
return flight_records3
except FileNotFoundError as e:
logger.error(f"File '{file_path}' not found.")
except Exception as e:
logger.error(f"An unexpected error occurred while parsing data: {e}")
return []
file_path = '/Users/sheila/Desktop/Prueba_Tecnica/data.log'
flight_records3 = parse_log_data(file_path)
# Test loading LOG data function
def test_load_log_data_valid_file():
file_path = '/Users/sheila/Desktop/Prueba_Tecnica/data.log'
flight_records = parse_log_data(file_path)
assert isinstance(flight_records, list)
assert len(flight_records) > 0
def test_load_log_data_invalid_file():
file_path = '/Users/sheila/Desktop/Prueba_Tecnica/invalid_logfile.log'
flight_records = parse_log_data(file_path)
assert isinstance(flight_records, list)
assert len(flight_records) == 0
def test_load_csv_data_invalid_log():
file_path = '/Users/sheila/Desktop/Prueba_Tecnica/invalid_logdata.log'
flight_records = parse_log_data(file_path)
assert isinstance(flight_records, list)
assert len(flight_records) == 0
# combine_data function
def combine_data(json_data: List[Dict[str, Any]], csv_data: List[Dict[str, Any]], log_data: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
logger = setup_custom_logger('data_combiner', 'app.log', level=logging.ERROR)
combined_data = []
try:
for json_flight in json_data:
flight_id = json_flight['Flight_ID']
matching_csv_flights = [flight for flight in csv_data if flight['Flight_ID'] == flight_id]
matching_log_flights = [flight for flight in log_data if flight['Flight_ID'] == flight_id]
if not matching_csv_flights:
logger.error(f"CSV data not found for Flight_ID '{flight_id}'. Skipping data combination.")
continue
if not matching_log_flights:
logger.error(f"Log data not found for Flight_ID '{flight_id}'. Skipping data combination.")
continue
combined_flight = {
'Flight_ID': flight_id,
'Date': json_flight.get('Date'),
'Departure_Airport': json_flight.get('Departure_Airport'),
'Arrival_Airport': json_flight.get('Arrival_Airport'),
'Duration_Minutes': json_flight.get('Duration_Minutes'),
'Passengers': matching_csv_flights[0].get('Passengers'),
'Revenue': matching_csv_flights[0].get('Revenue')
}
combined_data.append(combined_flight)
except Exception as e:
logger.error(f"An unexpected error occurred while combining data: {e}")
return combined_data
json_data = flight_records1
csv_data = flight_records2
log_data = flight_records3
combined_data = combine_data(json_data, csv_data, log_data)
# Testing combine_data function
def test_lenght_combine_data():
combined_data = combine_data(flight_records1, flight_records2, flight_records3)
assert len(combined_data) == len(flight_records1) == len(flight_records2) == len(flight_records3)
def test_keys_combine_data():
combined_data = combine_data(flight_records1, flight_records2, flight_records3)
expected_keys = {'Flight_ID', 'Date', 'Departure_Airport', 'Arrival_Airport', 'Duration_Minutes', 'Passengers', 'Revenue'}
assert all(set(record.keys()) == expected_keys for record in combined_data)
def get_flight_ids(records):
return [record['Flight_ID'] for record in records]
def test_combine_data_with_valid_data():
combined_data = combine_data(flight_records1, flight_records2, flight_records3)
flight_ids_1 = get_flight_ids(flight_records1)
flight_ids_2 = get_flight_ids(flight_records2)
flight_ids_3 = get_flight_ids(flight_records3)
combined_flight_ids = get_flight_ids(combined_data)
assert set(flight_ids_1) == set(flight_ids_2) == set(flight_ids_3)
assert set(combined_flight_ids) == set(flight_ids_1)
def test_no_duplicates_in_combined_data():
combined_data = combine_data(flight_records1, flight_records2, flight_records3)
ids = [record['Flight_ID'] for record in combined_data]
assert len(ids) == len(set(ids))
# calculate_revenue_per_passenger function
def calculate_revenue_per_passenger(flights_data: List[Dict[str, Any]]) -> None:
logger = setup_custom_logger('revenue_calculator', 'app.log', level=logging.ERROR)
try:
for flight in flights_data:
passengers = flight.get('Passengers')
revenue = flight.get('Revenue')
if passengers is not None and revenue is not None and passengers > 0:
revenue_per_passenger = revenue / passengers
flight['Revenue_Per_Passenger'] = revenue_per_passenger
else:
logger.error(f"Invalid data for Flight_ID '{flight.get('Flight_ID')}'. Unable to calculate 'Revenue_Per_Passenger'.")
except Exception as e:
logger.error(f"An unexpected error occurred while calculating 'Revenue_Per_Passenger': {e}")
flights_data = combined_data
calculate_revenue_per_passenger(flights_data)
# Testing revenue_per_passenger
def test_calculate_revenue_per_passenger_empty_data():
flights_data = []
calculate_revenue_per_passenger(flights_data)
assert len(flights_data) == 0
def test_calculate_revenue_per_passenger_valid_data():
flights_data = [
{'Flight_ID': 1, 'Passengers': 100, 'Revenue': 2000},
{'Flight_ID': 2, 'Passengers': 50, 'Revenue': 1000},
]
calculate_revenue_per_passenger(flights_data)
assert 'Revenue_Per_Passenger' in flights_data[0]
assert 'Revenue_Per_Passenger' in flights_data[1]
assert flights_data[0]['Revenue_Per_Passenger'] == 20.0
assert flights_data[1]['Revenue_Per_Passenger'] == 20.0
def test_calculate_revenue_per_passenger_missing_data():
flights_data = [
{'Flight_ID': 1, 'Revenue': 2000},
{'Flight_ID': 2, 'Passengers': 100, 'Revenue': None},
]
calculate_revenue_per_passenger(flights_data)
assert 'Revenue_Per_Passenger' not in flights_data[0]
assert 'Revenue_Per_Passenger' not in flights_data[1]