Skip to content

Commit de486af

Browse files
authored
Merge pull request #15 from Unstructured-IO/jj/create-test
chore: create test
2 parents 13d88bb + 009d5bb commit de486af

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

.genignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# https://www.speakeasyapi.dev/docs/customize-sdks/monkey-patching
2+
3+
# ignore human-written test files
4+
tests/test_utils_retries.py
5+
6+
# ignore Makefile
7+
Makefile

Makefile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
PACKAGE_NAME := unstructured-python-client
2+
CURRENT_DIR := $(shell pwd)
3+
ARCH := $(shell uname -m)
4+
5+
###########
6+
# Install #
7+
###########
8+
9+
test-install:
10+
pip install requests_mock
11+
12+
#################
13+
# Test and Lint #
14+
#################
15+
16+
.PHONY: test
17+
test:
18+
PYTHONPATH=. pytest \
19+
tests

tests/test_utils_retries.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import os
2+
import pytest
3+
4+
import requests_mock
5+
6+
from unstructured_client import UnstructuredClient
7+
from unstructured_client.models import shared
8+
from unstructured_client.utils.retries import BackoffStrategy, RetryConfig
9+
10+
11+
def get_api_key():
12+
api_key = os.getenv("UNS_API_KEY")
13+
if api_key is None:
14+
raise ValueError("""UNS_API_KEY environment variable not set.
15+
Set it in your current shell session with `export UNS_API_KEY=<api_key>`""")
16+
return api_key
17+
18+
# this test requires UNS_API_KEY be set in your shell session. Ex: `export UNS_API_KEY=<api_key>`
19+
def test_backoff_strategy():
20+
filename = "README.md"
21+
backoff_strategy = BackoffStrategy(
22+
initial_interval=100, max_interval=1000, exponent=1.5, max_elapsed_time=3000
23+
)
24+
retries = RetryConfig(
25+
strategy="backoff", backoff=backoff_strategy, retry_connection_errors=True
26+
)
27+
28+
with requests_mock.Mocker() as mock:
29+
# mock a 500 status code for POST requests to the api
30+
mock.post("https://api.unstructured.io/general/v0/general", status_code=500)
31+
session = UnstructuredClient(api_key_auth=get_api_key())
32+
33+
with open(filename, "rb") as f:
34+
files=shared.Files(
35+
content=f.read(),
36+
file_name=filename,
37+
)
38+
39+
req = shared.PartitionParameters(files=files)
40+
41+
with pytest.raises(Exception) as excinfo:
42+
resp = session.general.partition(req, retries=retries)
43+
assert resp.status_code == 500
44+
assert "API error occurred" in str(excinfo.value)
45+
46+
# the number of retries varies
47+
assert len(mock.request_history) > 1

0 commit comments

Comments
 (0)