97e91a83f3
Ruff / Ruff (push) Has been cancelled
Test / Core Tests (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.10) (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.11) (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.12) (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.13) (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.9) (push) Has been cancelled
Test / Full Coverage (Python 3.11) (push) Has been cancelled
Test / Core Provider Tests (OpenAI) (push) Has been cancelled
Test / Core Provider Tests (Anthropic) (push) Has been cancelled
Test / Core Provider Tests (Google) (push) Has been cancelled
Test / Core Provider Tests (Other) (push) Has been cancelled
Test / Anthropic Tests (push) Has been cancelled
Test / Gemini Tests (push) Has been cancelled
Test / Google GenAI Tests (push) Has been cancelled
Test / Vertex AI Tests (push) Has been cancelled
Test / OpenAI Tests (push) Has been cancelled
Test / Writer Tests (push) Has been cancelled
Test / Auto Client Tests (push) Has been cancelled
ty / type-check (push) Has been cancelled
179 lines
5.2 KiB
Python
179 lines
5.2 KiB
Python
import pytest
|
|
import instructor
|
|
from openai import OpenAI
|
|
import pprint
|
|
|
|
|
|
@pytest.fixture
|
|
def client():
|
|
return instructor.from_openai(OpenAI())
|
|
|
|
|
|
def log_kwargs(*args, **kwargs):
|
|
pprint.pprint({"args": args, "kwargs": kwargs})
|
|
|
|
|
|
def log_kwargs_1(*args, **kwargs):
|
|
pprint.pprint({"args": args, "kwargs": kwargs})
|
|
|
|
|
|
def log_kwargs_2(*args, **kwargs):
|
|
pprint.pprint({"args": args, "kwargs": kwargs})
|
|
|
|
|
|
hook_names = [item.value for item in instructor.hooks.HookName]
|
|
hook_enums = [instructor.hooks.HookName(hook_name) for hook_name in hook_names]
|
|
hook_functions = [log_kwargs, log_kwargs_1, log_kwargs_2]
|
|
hook_object = instructor.hooks.Hooks()
|
|
|
|
|
|
@pytest.mark.parametrize("hook_name", hook_names)
|
|
@pytest.mark.parametrize("num_functions", [1, 2, 3])
|
|
def test_on_method_str(
|
|
client: instructor.Instructor, hook_name: str, num_functions: int
|
|
):
|
|
functions_to_add = hook_functions[:num_functions]
|
|
hook_enum = hook_object.get_hook_name(hook_name)
|
|
|
|
assert hook_enum not in client.hooks._handlers
|
|
|
|
for func in functions_to_add:
|
|
client.on(hook_name, func)
|
|
|
|
assert hook_enum in client.hooks._handlers
|
|
assert len(client.hooks._handlers[hook_enum]) == num_functions
|
|
|
|
for func in functions_to_add:
|
|
assert func in client.hooks._handlers[hook_enum]
|
|
|
|
|
|
@pytest.mark.parametrize("hook_enum", hook_enums)
|
|
@pytest.mark.parametrize("num_functions", [1, 2, 3])
|
|
def test_on_method_enum(
|
|
client: instructor.Instructor,
|
|
hook_enum: instructor.hooks.HookName,
|
|
num_functions: int,
|
|
):
|
|
functions_to_add = hook_functions[:num_functions]
|
|
assert hook_enum not in client.hooks._handlers
|
|
|
|
for func in functions_to_add:
|
|
client.on(hook_enum, func)
|
|
|
|
assert hook_enum in client.hooks._handlers
|
|
assert len(client.hooks._handlers[hook_enum]) == num_functions
|
|
|
|
for func in functions_to_add:
|
|
assert func in client.hooks._handlers[hook_enum]
|
|
|
|
|
|
@pytest.mark.parametrize("hook_name", hook_names)
|
|
@pytest.mark.parametrize("num_functions", [1, 2, 3])
|
|
def test_off_method_str(
|
|
client: instructor.Instructor,
|
|
hook_name: str,
|
|
num_functions: int,
|
|
):
|
|
functions_to_add = hook_functions[:num_functions]
|
|
hook_enum = hook_object.get_hook_name(hook_name)
|
|
assert hook_enum not in client.hooks._handlers
|
|
|
|
for func in functions_to_add:
|
|
client.on(hook_name, func)
|
|
|
|
assert hook_enum in client.hooks._handlers
|
|
assert len(client.hooks._handlers[hook_enum]) == num_functions
|
|
|
|
for func in functions_to_add:
|
|
client.off(hook_name, func)
|
|
if client.hooks._handlers.get(hook_enum):
|
|
assert func not in client.hooks._handlers[hook_enum]
|
|
else:
|
|
assert hook_enum not in client.hooks._handlers
|
|
|
|
assert hook_enum not in client.hooks._handlers
|
|
|
|
|
|
@pytest.mark.parametrize("hook_enum", hook_enums)
|
|
@pytest.mark.parametrize("num_functions", [1, 2, 3])
|
|
def test_off_method_enum(
|
|
client: instructor.Instructor,
|
|
hook_enum: instructor.hooks.HookName,
|
|
num_functions: int,
|
|
):
|
|
functions_to_add = hook_functions[:num_functions]
|
|
assert hook_enum not in client.hooks._handlers
|
|
for func in functions_to_add:
|
|
client.on(hook_enum, func)
|
|
|
|
assert hook_enum in client.hooks._handlers
|
|
assert len(client.hooks._handlers[hook_enum]) == num_functions
|
|
|
|
for func in functions_to_add:
|
|
client.off(hook_enum, func)
|
|
if client.hooks._handlers.get(hook_enum):
|
|
assert func not in client.hooks._handlers[hook_enum]
|
|
else:
|
|
assert hook_enum not in client.hooks._handlers
|
|
|
|
assert hook_enum not in client.hooks._handlers
|
|
|
|
|
|
@pytest.mark.parametrize("hook_name", hook_names)
|
|
@pytest.mark.parametrize("num_functions", [1, 2, 3])
|
|
def test_clear_method_str(
|
|
client: instructor.Instructor,
|
|
hook_name: str,
|
|
num_functions: int,
|
|
):
|
|
functions_to_add = hook_functions[:num_functions]
|
|
|
|
for func in functions_to_add:
|
|
client.on(hook_name, func)
|
|
|
|
hook_enum = hook_object.get_hook_name(hook_name)
|
|
|
|
assert hook_enum in client.hooks._handlers
|
|
assert len(client.hooks._handlers[hook_enum]) == num_functions
|
|
|
|
client.clear(hook_name)
|
|
assert hook_enum not in client.hooks._handlers
|
|
|
|
|
|
@pytest.mark.parametrize("hook_enum", hook_enums)
|
|
@pytest.mark.parametrize("num_functions", [1, 2, 3])
|
|
def test_clear_method(
|
|
client: instructor.Instructor,
|
|
hook_enum: instructor.hooks.HookName,
|
|
num_functions: int,
|
|
):
|
|
functions_to_add = hook_functions[:num_functions]
|
|
|
|
for func in functions_to_add:
|
|
client.on(hook_enum, func)
|
|
|
|
assert hook_enum in client.hooks._handlers
|
|
assert len(client.hooks._handlers[hook_enum]) == num_functions
|
|
|
|
client.clear(hook_enum)
|
|
assert hook_enum not in client.hooks._handlers
|
|
|
|
|
|
@pytest.mark.parametrize("hook_enum", hook_enums)
|
|
@pytest.mark.parametrize("num_functions", [1, 2, 3])
|
|
def test_clear_no_args(
|
|
client: instructor.Instructor,
|
|
hook_enum: instructor.hooks.HookName,
|
|
num_functions: int,
|
|
):
|
|
functions_to_add = hook_functions[:num_functions]
|
|
|
|
for func in functions_to_add:
|
|
client.on(hook_enum, func)
|
|
|
|
assert hook_enum in client.hooks._handlers
|
|
assert len(client.hooks._handlers[hook_enum]) == num_functions
|
|
|
|
client.clear()
|
|
assert hook_enum not in client.hooks._handlers
|