Skip to content

Commit

Permalink
Make sure to close the csv file by using 'with open'
Browse files Browse the repository at this point in the history
  • Loading branch information
the-other-james committed Dec 12, 2023
1 parent d291439 commit 1da9f83
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
26 changes: 14 additions & 12 deletions onair/data_handling/csv_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,22 @@ def process_data_file(self, data_file):
##### INITIAL PROCESSING ####
def parse_csv_data(self, data_file):
#Read in the data set
csv_file = open(data_file, 'r', newline='')
dataset = csv.reader(csv_file, delimiter=',')

#Initialize the entire data dictionary
all_data = []
index = 0
for row in dataset:
if index == 0:
# Skip first row (headers)
pass
else:
rowVals = floatify_input(list(row))
all_data.append(rowVals)
index = index + 1

with open(data_file, 'r', newline='') as csv_file:
dataset = csv.reader(csv_file, delimiter=',')

#Initialize the entire data dictionary
index = 0
for row in dataset:
if index == 0:
# Skip first row (headers)
pass
else:
rowVals = floatify_input(list(row))
all_data.append(rowVals)
index = index + 1

return all_data

Expand Down
12 changes: 9 additions & 3 deletions test/onair/data_handling/test_csv_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ def test_CSV_parse_csv_data_returns_empty_list_when_parsed_dataset_is_empty(mock
# Arrange
arg_dataFile = MagicMock()

fake_file_iterator = MagicMock()
fake_csv_file = MagicMock()
fake_csv_file.configure_mock(**{'__enter__.return_value': fake_file_iterator})
fake_dataset = []
forced_return_contains = MagicMock()
fake_second_data_set = MagicMock()
Expand All @@ -61,7 +63,7 @@ def test_CSV_parse_csv_data_returns_empty_list_when_parsed_dataset_is_empty(mock
assert csv_parser.open.call_args_list[0].args == (arg_dataFile, 'r')
assert csv_parser.open.call_args_list[0].kwargs == ({'newline':''})
assert csv_parser.csv.reader.call_count == 1
assert csv_parser.csv.reader.call_args_list[0].args == (fake_csv_file, )
assert csv_parser.csv.reader.call_args_list[0].args == (fake_file_iterator, )
assert csv_parser.csv.reader.call_args_list[0].kwargs == ({'delimiter':','})
assert csv_parser.floatify_input.call_count == 0

Expand All @@ -71,7 +73,9 @@ def test_CSV_parse_csv_data_returns_empty_list_when_parsed_dataset_is_just_heade
# Arrange
arg_dataFile = MagicMock()

fake_file_iterator = MagicMock()
fake_csv_file = MagicMock()
fake_csv_file.configure_mock(**{'__enter__.return_value': fake_file_iterator})
fake_dataset = [['fake column header', 'another fake column header']]
forced_return_contains = MagicMock()
fake_second_data_set = MagicMock()
Expand All @@ -91,7 +95,7 @@ def test_CSV_parse_csv_data_returns_empty_list_when_parsed_dataset_is_just_heade
assert csv_parser.open.call_count == 1
assert csv_parser.open.call_args_list[0].args == (arg_dataFile, 'r')
assert csv_parser.csv.reader.call_count == 1
assert csv_parser.csv.reader.call_args_list[0].args == (fake_csv_file, )
assert csv_parser.csv.reader.call_args_list[0].args == (fake_file_iterator, )
assert csv_parser.csv.reader.call_args_list[0].kwargs == ({'delimiter':','})
assert csv_parser.floatify_input.call_count == 0

Expand All @@ -102,7 +106,9 @@ def test_CSV_parse_csv_data_returns_list_of_row_values_when_parsed_dataset(mocke
# Arrange
arg_dataFile = MagicMock()

fake_file_iterator = MagicMock()
fake_csv_file = MagicMock()
fake_csv_file.configure_mock(**{'__enter__.return_value': fake_file_iterator})
fake_dataset = [['fake column header', 'another fake column header']]
expected_result_list = []
num_fake_rows = pytest.gen.randint(1, 10) # arbitrary, from 1 to 10
Expand All @@ -124,7 +130,7 @@ def test_CSV_parse_csv_data_returns_list_of_row_values_when_parsed_dataset(mocke
assert csv_parser.open.call_count == 1
assert csv_parser.open.call_args_list[0].args == (arg_dataFile, 'r')
assert csv_parser.csv.reader.call_count == 1
assert csv_parser.csv.reader.call_args_list[0].args == (fake_csv_file, )
assert csv_parser.csv.reader.call_args_list[0].args == (fake_file_iterator, )
assert csv_parser.csv.reader.call_args_list[0].kwargs == ({'delimiter':','})
assert csv_parser.floatify_input.call_count == num_fake_rows

Expand Down

0 comments on commit 1da9f83

Please sign in to comment.