From 2cbe6c38712fed87a61e959e31f0723ba99ee968 Mon Sep 17 00:00:00 2001 From: sebastianMindee <130448732+sebastianMindee@users.noreply.github.com> Date: Fri, 24 May 2024 15:06:18 +0200 Subject: [PATCH] :recycle: fix improper test & improper enqueueing for localresponse (#237) --- mindee/input/local_response.py | 24 +++++++++++++++++++----- tests/Input/test_local_response.py | 3 ++- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/mindee/input/local_response.py b/mindee/input/local_response.py index b9017541..37c2192a 100644 --- a/mindee/input/local_response.py +++ b/mindee/input/local_response.py @@ -2,6 +2,7 @@ import hmac import io import json +import os from pathlib import Path from typing import Any, BinaryIO, Dict, Union @@ -15,18 +16,31 @@ class LocalResponse: """File object of the local response.""" def __init__(self, input_file: Union[BinaryIO, str, Path, bytes]): - if isinstance(input_file, BinaryIO): - self._file = input_file + if isinstance(input_file, (BinaryIO, io.BufferedReader)): + str_stripped = ( + input_file.read().decode("utf-8").replace("\r", "").replace("\n", "") + ) + self._file = io.BytesIO(str_stripped.encode("utf-8")) self._file.seek(0) - elif isinstance(input_file, (str, Path)): + elif isinstance(input_file, Path) or ( + isinstance(input_file, str) and os.path.exists(input_file) + ): with open(input_file, "r", encoding="utf-8") as file: self._file = io.BytesIO( file.read().replace("\r", "").replace("\n", "").encode() ) + elif isinstance(input_file, str): + self._file = io.BytesIO( + input_file.replace("\r", "").replace("\n", "").encode("utf-8") + ) elif isinstance(input_file, bytes): - self._file = io.BytesIO(input_file) + str_stripped = ( + input_file.decode("utf-8").replace("\r", "").replace("\n", "") + ) + self._file = io.BytesIO(str_stripped.encode("utf-8")) + self._file.seek(0) else: - raise MindeeError("Incompatible type for input.") + raise MindeeError(f"Incompatible type for input '{type(input_file)}'.") @property def as_dict(self) -> Dict[str, Any]: diff --git a/tests/Input/test_local_response.py b/tests/Input/test_local_response.py index 293fd1ec..5858e2d6 100644 --- a/tests/Input/test_local_response.py +++ b/tests/Input/test_local_response.py @@ -22,7 +22,8 @@ def file_path(): def test_valid_file_local_response(dummy_secret_key, signature, file_path): - local_response = LocalResponse(file_path) + with open(file_path, "rb") as file: + local_response = LocalResponse(file) assert local_response._file is not None assert not local_response.is_valid_hmac_signature( dummy_secret_key, "invalid signature"