Я импортирую все функции из тестируемого скрипта. Здесь используются переменные среды prod, но только для проверки загрузки и чтения данных из S3 (не выгрузки). В REPL тот же код работает нормально — я могу подключиться к своему экземпляру S3, прочитать и загрузить данные, находящиеся на нем.
Теперь в своем наборе тестов я сохраняю получение ошибки IncompleteReadError при запуске pytest --record-mode=once. Это происходит независимо от того, удаляю ли я существующие кассеты или нет.
Вот оригинальные функции, которые я тестирую:
Код: Выделить всё
def s3_connect_get_files(
validated_target_params: dict,
) -> Tuple[s3fs.S3FileSystem, List[str]]:
"""
connects to our s3 instance, returning
our bucket s3fs object and a list of the files
in the target infile directory specified in
our data model for the target.
returns:
- Tuple(the_bucket (s3fs obj), files (list) )
raises:
- FileNotFoundError if we can't find the dir
on our s3
"""
try:
the_bucket = s3fs.S3FileSystem(
key=AWS_ACCESS_KEY_ID,
secret=AWS_SECRET_ACCESS_KEY,
client_kwargs={"endpoint_url": validated_target_params["endpoint_url"]},
)
files = the_bucket.glob(
f"{validated_target_params['in_path']}/"
f"{validated_target_params['glob_pattern']}"
)
return the_bucket, files
except FileNotFoundError as e:
logger.error("could not connect to s3. check credentials!")
logger.error(f"original error type: {type(e).__name__}")
logger.error(f"original error message: {e}")
raise
def read_files(
the_bucket: s3fs.S3FileSystem, files: List[str], validated_target_params: dict
) -> dict:
"""reads all files into memory
raises:
- NotImplementedError; if we encounter a reader
type we haven't defined yet.
"""
records = {}
logger.info("Now reading data to be validated and de-duped.")
for file in files:
if (
validated_target_params["reader"].value == "pandas"
): # we need to call value as we're using an Enum
try:
df = pd.read_csv(the_bucket.open(file))
file_last_modified = the_bucket.info(file).get("LastModified")
df["file_last_modified"] = file_last_modified
records[file] = {
"data": df,
"last_modified_at": file_last_modified,
}
logger.info(f"Loaded {file} with {len(df)} rows using pandas")
except Exception as e:
logger.error(f"original error type: {type(e).__name__}")
logger.error(f"original error message: {e}")
raise
else:
raise NotImplementedError(
f"{validated_target_params['reader']} is not yet implemented as a reader."
)
return records
Код: Выделить всё
@pytest.mark.vcr()
def test_s3_connect_get_files(validated_target_params) -> None:
"""test s3 connection and file retrieval"""
the_bucket, files = s3_connect_get_files(validated_target_params)
assert isinstance(the_bucket, S3FileSystem)
assert isinstance(files, list)
@pytest.mark.vcr()
def test_read_files_pandas(validated_target_params) -> None:
"""test reading files using pandas"""
the_bucket, files = s3_connect_get_files(validated_target_params)
records = read_files(the_bucket, files, validated_target_params)
assert isinstance(records, dict)
assert len(records) == len(files)
assert all(isinstance(df["data"], pd.DataFrame) for df in records.values())
Код: Выделить всё
E botocore.exceptions.IncompleteReadError: 0 read, but total bytes expected is 6163243.
.venv/lib/python3.11/site-packages/aiobotocore/response.py:125: IncompleteReadError
Подробнее здесь: https://stackoverflow.com/questions/792 ... -sometimes
Мобильная версия