Skip to content

Commit

Permalink
Cleanup more tests that use unnecessary fixtures/mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Yun-Kim committed Jan 8, 2025
1 parent 7ada912 commit f39ac5c
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 39 deletions.
27 changes: 2 additions & 25 deletions tests/llmobs/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,6 @@ def mock_llmobs_span_writer():
patcher.stop()


@pytest.fixture
def mock_llmobs_span_agentless_writer():
patcher = mock.patch("ddtrace.llmobs._llmobs.LLMObsSpanWriter")
LLMObsSpanWriterMock = patcher.start()
m = mock.MagicMock()
LLMObsSpanWriterMock.return_value = m
yield m
patcher.stop()


@pytest.fixture
def mock_llmobs_eval_metric_writer():
patcher = mock.patch("ddtrace.llmobs._llmobs.LLMObsEvalMetricWriter")
Expand Down Expand Up @@ -84,11 +74,7 @@ def mock_llmobs_submit_evaluation():
@pytest.fixture
def mock_http_writer_send_payload_response():
with mock.patch(
"ddtrace.internal.writer.HTTPWriter._send_payload",
return_value=Response(
status=200,
body="{}",
),
"ddtrace.internal.writer.HTTPWriter._send_payload", return_value=Response(status=200, body="{}"),
):
yield

Expand Down Expand Up @@ -123,12 +109,6 @@ def mock_evaluator_sampler_logs():
yield m


@pytest.fixture
def mock_http_writer_logs():
with mock.patch("ddtrace.internal.writer.writer.log") as m:
yield m


@pytest.fixture
def mock_llmobs_logs():
with mock.patch("ddtrace.llmobs._llmobs.log") as m:
Expand Down Expand Up @@ -161,10 +141,7 @@ def LLMObs(

@pytest.fixture
def AgentlessLLMObs(
mock_llmobs_span_agentless_writer,
mock_llmobs_eval_metric_writer,
mock_llmobs_evaluator_runner,
ddtrace_global_config,
mock_llmobs_span_writer, mock_llmobs_eval_metric_writer, mock_llmobs_evaluator_runner, ddtrace_global_config,
):
global_config = default_global_config()
global_config.update(ddtrace_global_config)
Expand Down
8 changes: 4 additions & 4 deletions tests/llmobs/test_llmobs_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -1189,10 +1189,10 @@ def test_submit_evaluation_with_numerical_metric_enqueues_writer_with_score_metr


def test_flush_calls_periodic_agentless(
AgentlessLLMObs, mock_llmobs_span_agentless_writer, mock_llmobs_eval_metric_writer, mock_llmobs_evaluator_runner
AgentlessLLMObs, mock_llmobs_span_writer, mock_llmobs_eval_metric_writer, mock_llmobs_evaluator_runner
):
AgentlessLLMObs.flush()
mock_llmobs_span_agentless_writer.periodic.assert_called_once()
mock_llmobs_span_writer.periodic.assert_called_once()
mock_llmobs_eval_metric_writer.periodic.assert_called_once()
mock_llmobs_evaluator_runner.periodic.assert_called_once()

Expand All @@ -1216,14 +1216,14 @@ def test_flush_does_not_call_periodic_when_llmobs_is_disabled(

def test_flush_does_not_call_periodic_when_llmobs_is_disabled_agentless(
AgentlessLLMObs,
mock_llmobs_span_agentless_writer,
mock_llmobs_span_writer,
mock_llmobs_eval_metric_writer,
mock_llmobs_evaluator_runner,
mock_llmobs_logs,
disabled_llmobs,
):
AgentlessLLMObs.flush()
mock_llmobs_span_agentless_writer.periodic.assert_not_called()
mock_llmobs_span_writer.periodic.assert_not_called()
mock_llmobs_eval_metric_writer.periodic.assert_not_called()
mock_llmobs_evaluator_runner.periodic.assert_not_called()
mock_llmobs_logs.warning.assert_has_calls(
Expand Down
2 changes: 1 addition & 1 deletion tests/llmobs/test_llmobs_span_agent_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def test_flush_queue_when_event_cause_queue_to_exceed_payload_limit(
[
mock.call("flushing queue because queuing next event will exceed EVP payload limit"),
mock.call("encode %d LLMObs span events to be sent", 5),
]
], any_order=True
)


Expand Down
15 changes: 6 additions & 9 deletions tests/llmobs/test_llmobs_span_agentless_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,26 +75,25 @@ def test_truncating_oversized_events(mock_writer_logs, mock_http_writer_send_pay
)


def test_send_completion_event(mock_writer_logs, mock_http_writer_logs, mock_http_writer_send_payload_response):
def test_send_completion_event(mock_writer_logs, mock_http_writer_send_payload_response):
with override_global_config(dict(_dd_site=DATADOG_SITE, _dd_api_key="foobar.baz")):
llmobs_span_writer = LLMObsSpanWriter(is_agentless=True, interval=1, timeout=1)
llmobs_span_writer.start()
llmobs_span_writer.enqueue(_completion_event())
llmobs_span_writer.periodic()
mock_writer_logs.debug.assert_has_calls([mock.call("encode %d LLMObs span events to be sent", 1)])
mock_http_writer_logs.error.assert_not_called()


def test_send_chat_completion_event(mock_writer_logs, mock_http_writer_logs, mock_http_writer_send_payload_response):
def test_send_chat_completion_event(mock_writer_logs, mock_http_writer_send_payload_response):
with override_global_config(dict(_dd_site=DATADOG_SITE, _dd_api_key="foobar.baz")):
llmobs_span_writer = LLMObsSpanWriter(is_agentless=True, interval=1, timeout=1)
llmobs_span_writer.start()
llmobs_span_writer.enqueue(_chat_completion_event())
llmobs_span_writer.periodic()
mock_writer_logs.debug.assert_has_calls([mock.call("encode %d LLMObs span events to be sent", 1)])
mock_http_writer_logs.error.assert_not_called()


@mock.patch("ddtrace.internal.writer.writer.log")
def test_send_completion_bad_api_key(mock_http_writer_logs, mock_http_writer_put_response_forbidden):
with override_global_config(dict(_dd_site=DATADOG_SITE, _dd_api_key="<bad-api-key>")):
llmobs_span_writer = LLMObsSpanWriter(is_agentless=True, interval=1, timeout=1)
Expand All @@ -109,7 +108,7 @@ def test_send_completion_bad_api_key(mock_http_writer_logs, mock_http_writer_put
)


def test_send_timed_events(mock_writer_logs, mock_http_writer_logs, mock_http_writer_send_payload_response):
def test_send_timed_events(mock_writer_logs, mock_http_writer_send_payload_response):
with override_global_config(dict(_dd_site=DATADOG_SITE, _dd_api_key="foobar.baz")):
llmobs_span_writer = LLMObsSpanWriter(is_agentless=True, interval=0.01, timeout=1)
llmobs_span_writer.start()
Expand All @@ -122,10 +121,9 @@ def test_send_timed_events(mock_writer_logs, mock_http_writer_logs, mock_http_wr
llmobs_span_writer.enqueue(_chat_completion_event())
time.sleep(0.1)
mock_writer_logs.debug.assert_has_calls([mock.call("encode %d LLMObs span events to be sent", 1)])
mock_http_writer_logs.error.assert_not_called()


def test_send_multiple_events(mock_writer_logs, mock_http_writer_logs, mock_http_writer_send_payload_response):
def test_send_multiple_events(mock_writer_logs, mock_http_writer_send_payload_response):
with override_global_config(dict(_dd_site=DATADOG_SITE, _dd_api_key="foobar.baz")):
llmobs_span_writer = LLMObsSpanWriter(is_agentless=True, interval=0.01, timeout=1)
llmobs_span_writer.start()
Expand All @@ -135,10 +133,9 @@ def test_send_multiple_events(mock_writer_logs, mock_http_writer_logs, mock_http
llmobs_span_writer.enqueue(_chat_completion_event())
time.sleep(0.1)
mock_writer_logs.debug.assert_has_calls([mock.call("encode %d LLMObs span events to be sent", 2)])
mock_http_writer_logs.error.assert_not_called()


def test_send_on_exit(mock_writer_logs, run_python_code_in_subprocess):
def test_send_on_exit(run_python_code_in_subprocess):
env = os.environ.copy()
pypath = [os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))]
if "PYTHONPATH" in env:
Expand Down

0 comments on commit f39ac5c

Please sign in to comment.