def johnson_post(): try: current_app.logger.debug("Johnson post received") if len(request.files) > 0: file_key = request.files.keys()[0] # read the uploaded file to a string, then split the string by linebreaks to form a list # of records. done this way to allow multiple import functions to use the import file # (the uploaded file can only be read once) uploaded_file = request.files[file_key] uploaded_file_string = uploaded_file.read().replace("\r\n", "\n").replace("\r", "\n") records = uploaded_file_string.split("\n") post_handler = PostHandler() post_handler.handle_johnson_post(records) else: current_app.logger.warn("Johnson post received without attached file") abort(500) # throws an exception current_app.logger.info("Johnson post successful") return jsonify({"success": True}) except: current_app.logger.exception("An error occurred attempting to post johnson data") abort(500)
def test_handle_johnson_post(self, open_mock, path_join): file_mock = MagicMock() post_handler = PostHandler() post_handler.write_johnson_post_data = Mock() post_handler.johnson_raw_folder = Mock() post_handler.handle_johnson_post(file_mock) path_join.assert_called_with(post_handler.johnson_raw_folder, post_handler.date_str) open_mock.assert_called_with(path_join.return_value, "a") post_handler.write_johnson_post_data.assert_called_with(file_mock, open_mock.return_value.__enter__.return_value)