def test_opens_correct_file(self): mocked_handle = mock.mock_open() expected_file_path = "/foo/bar.csv" parser = BatchCsvParser() patchable = "builtins.open" with mock.patch(patchable, mocked_handle): parser.save_batch_file(rows=[], file_path=expected_file_path) mocked_handle.assert_called_with(expected_file_path, "w", newline="")
def test_parses_row_to_csv_correctly(self): test_row = RowEntries() test_row.sample_scatter = "SANS2D00022025" test_row.sample_transmission = "SANS2D00022052" test_row.sample_direct = "SANS2D00022022" test_row.output_name = "another_file" test_row.user_file = "a_user_file.txt" test_row.sample_thickness = "1.0" test_row.sample_height = 5.0 test_row.sample_width = 5.4 expected = "sample_sans,SANS2D00022025," \ "sample_trans,SANS2D00022052,"\ "sample_direct_beam,SANS2D00022022," \ "can_sans,," \ "can_trans,," \ "can_direct_beam,,"\ "output_as,another_file," \ "user_file,a_user_file.txt," \ "sample_thickness,1.0,"\ "sample_height,5.0," \ "sample_width,5.4" mocked_handle = mock.mock_open() parser = BatchCsvParser() patchable = "builtins.open" with mock.patch(patchable, mocked_handle): parser.save_batch_file(rows=[test_row], file_path='') result = mocked_handle() args, kwargs = result.write.call_args # Strip new lines etc self.assertTrue(isinstance(args[0], str)) written = args[0].replace('\n', '').replace('\r', '') self.assertEqual(expected, written)