Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .genignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# https://www.speakeasyapi.dev/docs/customize-sdks/monkey-patching

# ignore human-written test files
tests/test_utils_retries.py

# ignore Makefile
Makefile
19 changes: 19 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
PACKAGE_NAME := unstructured-python-client
CURRENT_DIR := $(shell pwd)
ARCH := $(shell uname -m)

###########
# Install #
###########

test-install:
pip install requests_mock

#################
# Test and Lint #
#################

.PHONY: test
test:
PYTHONPATH=. pytest \
tests
47 changes: 47 additions & 0 deletions tests/test_utils_retries.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import os
import pytest

import requests_mock

from unstructured_client import UnstructuredClient
from unstructured_client.models import shared
from unstructured_client.utils.retries import BackoffStrategy, RetryConfig


def get_api_key():
api_key = os.getenv("UNS_API_KEY")
if api_key is None:
raise ValueError("""UNS_API_KEY environment variable not set.
Set it in your current shell session with `export UNS_API_KEY=<api_key>`""")
return api_key

# this test requires UNS_API_KEY be set in your shell session. Ex: `export UNS_API_KEY=<api_key>`
def test_backoff_strategy():
filename = "README.md"
backoff_strategy = BackoffStrategy(
initial_interval=100, max_interval=1000, exponent=1.5, max_elapsed_time=3000
)
retries = RetryConfig(
strategy="backoff", backoff=backoff_strategy, retry_connection_errors=True
)

with requests_mock.Mocker() as mock:
# mock a 500 status code for POST requests to the api
mock.post("https://api.unstructured.io/general/v0/general", status_code=500)
session = UnstructuredClient(api_key_auth=get_api_key())

with open(filename, "rb") as f:
files=shared.Files(
content=f.read(),
file_name=filename,
)

req = shared.PartitionParameters(files=files)

with pytest.raises(Exception) as excinfo:
resp = session.general.partition(req, retries=retries)
assert resp.status_code == 500
assert "API error occurred" in str(excinfo.value)

# the number of retries varies
assert len(mock.request_history) > 1