-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
126 lines (94 loc) · 3.48 KB
/
Copy pathconftest.py
File metadata and controls
126 lines (94 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
"""Shared fixtures for integration tests.
These tests spin up an in-memory Phoenix server per session and create a
real ``phoenix.client.Client`` connected to it. They are gated behind
the ``integration`` marker so they can be skipped in fast CI runs::
pytest -m integration # run only integration tests
pytest -m 'not integration' # skip integration tests (default)
"""
from __future__ import annotations
import os
import socket
import textwrap
import uuid
from pathlib import Path
import pytest
def _find_free_port() -> int:
"""Return an available TCP port on localhost."""
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind(("", 0))
return s.getsockname()[1]
def _unique_tag() -> str:
"""Return a short unique suffix for dataset names."""
return uuid.uuid4().hex[:8]
@pytest.fixture(scope="session")
def phoenix_server():
"""Launch an in-memory Phoenix instance for the entire test session.
Yields the session URL (e.g. ``http://localhost:54321``).
Cleans up the server and all data after the session ends.
"""
import phoenix as px
port = _find_free_port()
px.launch_app(run_in_thread=True, use_temp_dir=True, port=port)
url = f"http://localhost:{port}"
yield url
px.close_app(delete_data=True)
@pytest.fixture()
def phoenix_client(phoenix_server: str):
"""Return a ``phoenix.client.Client`` connected to the test server.
Sets ``PHOENIX_COLLECTOR_ENDPOINT`` so that code which creates its own
``Client()`` (like ``evalwire.cli._make_client``) also points at the
test instance.
"""
from phoenix.client import Client
old = os.environ.get("PHOENIX_COLLECTOR_ENDPOINT")
os.environ["PHOENIX_COLLECTOR_ENDPOINT"] = phoenix_server
client = Client()
yield client
# Restore original env
if old is None:
os.environ.pop("PHOENIX_COLLECTOR_ENDPOINT", None)
else:
os.environ["PHOENIX_COLLECTOR_ENDPOINT"] = old
@pytest.fixture()
def integration_csv(tmp_path: Path) -> tuple[Path, str, str]:
"""Write a small CSV testset with unique tag names.
Returns ``(csv_path, search_tag, router_tag)`` so tests can reference
the unique dataset names.
"""
tag = _unique_tag()
search_tag = f"search_{tag}"
router_tag = f"router_{tag}"
content = textwrap.dedent(f"""\
user_query,expected_output,tags
"find cycling paths","url-a | url-b","{search_tag} | {router_tag}"
"find parks","url-c","{search_tag}"
"route me home","home","{router_tag}"
""")
csv_file = tmp_path / "testset.csv"
csv_file.write_text(content)
return csv_file, search_tag, router_tag
@pytest.fixture()
def integration_experiments(tmp_path: Path) -> tuple[Path, str]:
"""Create a minimal experiments/ directory for integration testing.
Returns ``(experiments_dir, experiment_name)`` where *experiment_name*
is the unique dataset-matching directory name.
"""
tag = _unique_tag()
exp_name = f"exp_{tag}"
base = tmp_path / "experiments"
base.mkdir()
exp_dir = base / exp_name
exp_dir.mkdir()
(exp_dir / "task.py").write_text(
textwrap.dedent("""\
async def task(example):
return example.input.get("user_query", "") + " result"
""")
)
(exp_dir / "check_output.py").write_text(
textwrap.dedent("""\
def check_output(output, expected):
return 1.0 if output else 0.0
""")
)
return base, exp_name