Files
comet-ml--opik/sdks/python/tests/unit/cli/test_run.py
T
wehub-resource-sync 5a558eb09e
Backend Tests / discover-tests (push) Waiting to run
Backend Tests / ${{ matrix.name }} (push) Blocked by required conditions
Build and Publish SDK / build-and-publish (push) Waiting to run
Build Opik Docker Images / set-version (push) Waiting to run
Build Opik Docker Images / build-backend (push) Blocked by required conditions
Build Opik Docker Images / build-sandbox-executor-python (push) Blocked by required conditions
Build Opik Docker Images / build-python-backend (push) Blocked by required conditions
Build Opik Docker Images / build-frontend (push) Blocked by required conditions
Build Opik Docker Images / create-git-tag (push) Blocked by required conditions
ClickHouse Migration Cluster Check / validate-clickhouse-migrations (push) Waiting to run
Docs - Publish / run (push) Waiting to run
E2E Tests - Post Merge (v2) / 🧪 E2E v2 Tests (${{ github.event.inputs.tier || 't1' }}) (push) Waiting to run
E2E Tests - Post Merge (v2) / 📢 Slack Notification (push) Blocked by required conditions
Frontend Unit Tests / Test on Node 20 (push) Waiting to run
Guardrails E2E Tests / Select Python version matrix (push) Waiting to run
Guardrails E2E Tests / Guardrails E2E Tests ${{matrix.python_version}} (push) Blocked by required conditions
Guardrails E2E Tests / 📢 Slack Notification (push) Blocked by required conditions
Guardrails Backend Unit Tests / Guardrails Backend Unit Tests (push) Waiting to run
Guardrails Backend Unit Tests / 📢 Slack Notification (push) Blocked by required conditions
Lint Opik Helm Chart / lint-helm-chart (Helm v3.21.0) (push) Waiting to run
Lint Opik Helm Chart / lint-helm-chart (Helm v4.2.0) (push) Waiting to run
Lint Opik Helm Chart / unittest-helm-chart (push) Waiting to run
Lint Opik Helm Chart / render-equality (push) Waiting to run
Opik Optimizer - Unit Tests / Opik Optimizer Unit Tests Python ${{matrix.python_version}} (push) Waiting to run
Python BE E2E Tests / Python BE E2E (push) Waiting to run
Python Backend Tests / run-python-backend-tests (push) Waiting to run
Python SDK Unit Tests / Python SDK Unit Tests ${{matrix.python_version}} (push) Waiting to run
Release Drafter / update_release_draft (push) Waiting to run
SDK E2E Libraries Integration Tests / Check Secrets (push) Waiting to run
SDK E2E Libraries Integration Tests / Missed OpenAI API Key Warning (push) Blocked by required conditions
SDK E2E Libraries Integration Tests / build-opik (push) Blocked by required conditions
SDK E2E Libraries Integration Tests / E2E Lib Integration Python ${{matrix.python_version}} (push) Blocked by required conditions
TypeScript SDK Integration Build & Publish / build-and-publish (opik-gemini) (push) Waiting to run
TypeScript SDK Integration Build & Publish / build-and-publish (opik-langchain) (push) Waiting to run
TypeScript SDK Integration Build & Publish / build-and-publish (opik-openai) (push) Waiting to run
TypeScript SDK Integration Build & Publish / build-and-publish (opik-otel) (push) Waiting to run
TypeScript SDK Integration Build & Publish / build-and-publish (opik-vercel) (push) Waiting to run
TypeScript SDK Build & Publish / build-and-publish (push) Waiting to run
TypeScript SDK Unit Tests / Test on Node ${{ matrix.node-version }} (push) Waiting to run
TypeScript SDK Compatibility V1.x E2E Tests / Select Node version matrix (push) Has been cancelled
TypeScript SDK Compatibility V1.x E2E Tests / TypeScript SDK Compatibility V1.x E2E Tests Node ${{matrix.node_version}} (push) Has been cancelled
TypeScript SDK E2E Tests / TypeScript SDK E2E Tests Node ${{matrix.node_version}} (push) Has been cancelled
Opik Optimizer - E2E Tests / build-opik (push) Has been cancelled
TypeScript SDK Compatibility V1.x E2E Tests / build-opik (push) Has been cancelled
Python SDK E2E Tests / Select Python version matrix (push) Has been cancelled
Python SDK E2E Tests / Python SDK E2E Tests ${{matrix.python_version}} (push) Has been cancelled
Python SDK E2E Tests / build-opik (push) Has been cancelled
Python SDK Compatibility V1.x E2E Tests / Select Python version matrix (push) Has been cancelled
Python SDK Compatibility V1.x E2E Tests / Python SDK Compatibility V1.x E2E Tests ${{matrix.python_version}} (push) Has been cancelled
Python SDK Compatibility V1.x E2E Tests / build-opik (push) Has been cancelled
TypeScript SDK E2E Tests / Select Node version matrix (push) Has been cancelled
TypeScript SDK E2E Tests / build-opik (push) Has been cancelled
Opik Optimizer - E2E Tests / Opik Optimizer E2E Tests Python ${{matrix.python_version}} (push) Has been cancelled
Opik Optimizer - E2E Tests / Opik Optimizer Integration Smoke Tests (push) Has been cancelled
🐙 Code Quality / detect (push) Has been cancelled
🐙 Code Quality / lint (${{ matrix.leg.name }}) (push) Has been cancelled
🐙 Code Quality / summary (push) Has been cancelled
TypeScript SDK Library Integration Tests / Check Secrets (push) Has been cancelled
TypeScript SDK Library Integration Tests / opik-vercel (Vercel AI SDK / eve) (push) Has been cancelled
SDK Library Integration Tests Runner / Check Secrets (push) Has been cancelled
SDK Library Integration Tests Runner / Missed OpenAI API Key Warning (push) Has been cancelled
SDK Library Integration Tests Runner / Build (push) Has been cancelled
SDK Library Integration Tests Runner / openai_tests (push) Has been cancelled
SDK Library Integration Tests Runner / langchain_tests (push) Has been cancelled
SDK Library Integration Tests Runner / langchain_legacy_tests (push) Has been cancelled
SDK Library Integration Tests Runner / llama_index_tests (push) Has been cancelled
SDK Library Integration Tests Runner / anthropic_tests (push) Has been cancelled
SDK Library Integration Tests Runner / mistral_tests (push) Has been cancelled
SDK Library Integration Tests Runner / groq_tests (push) Has been cancelled
SDK Library Integration Tests Runner / aisuite_tests (push) Has been cancelled
SDK Library Integration Tests Runner / haystack_tests (push) Has been cancelled
SDK Library Integration Tests Runner / dspy_tests (push) Has been cancelled
SDK Library Integration Tests Runner / crewai_v0_tests (push) Has been cancelled
SDK Library Integration Tests Runner / crewai_v1_tests (push) Has been cancelled
SDK Library Integration Tests Runner / genai_tests (push) Has been cancelled
SDK Library Integration Tests Runner / adk_tests (push) Has been cancelled
SDK Library Integration Tests Runner / adk_legacy_1_3_0_tests (push) Has been cancelled
SDK Library Integration Tests Runner / evaluation_metrics_tests (push) Has been cancelled
SDK Library Integration Tests Runner / bedrock_tests (push) Has been cancelled
SDK Library Integration Tests Runner / litellm_tests (push) Has been cancelled
SDK Library Integration Tests Runner / harbor_tests (push) Has been cancelled
SDK Library Integration Tests Runner / Slack Notification (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:25:44 +08:00

176 lines
7.7 KiB
Python

from unittest.mock import MagicMock, patch
import pytest
from opik.cli.local_runner.preflight import (
maybe_auto_configure,
should_create_project,
)
from opik.rest_api.core.api_error import ApiError
class TestShouldCreateProject:
def test__headless__returns_create_without_lookup(self):
api = MagicMock()
result = should_create_project(api, "any", workspace=None, headless=True)
# Headless skips preflight, so we want to create but don't yet know
# whether the project is actually missing — the resolver must look up.
assert result == (True, False)
api.projects.retrieve_project.assert_not_called()
def test__project_exists__returns_no_create(self):
api = MagicMock()
api.projects.retrieve_project.return_value = MagicMock(id="proj-1")
result = should_create_project(api, "exists", workspace="ws", headless=False)
assert result == (False, False)
def test__non_404_error__returns_no_create_to_let_downstream_format(self):
api = MagicMock()
api.projects.retrieve_project.side_effect = ApiError(
status_code=401, body={"message": "unauthorized"}
)
result = should_create_project(api, "x", workspace="ws", headless=False)
assert result == (False, False)
@patch("opik.cli.local_runner.preflight.sys.stdin")
def test__missing_and_not_tty__returns_no_create(self, mock_stdin):
mock_stdin.isatty.return_value = False
api = MagicMock()
api.projects.retrieve_project.side_effect = ApiError(
status_code=404, body={"errors": ["not found"]}
)
result = should_create_project(api, "missing", workspace="ws", headless=False)
assert result == (False, False)
@patch("opik.cli.local_runner.preflight.click.confirm", return_value=True)
@patch("opik.cli.local_runner.preflight.sys.stdin")
def test__missing_and_tty_user_confirms__returns_create_and_known_missing(
self, mock_stdin, mock_confirm
):
mock_stdin.isatty.return_value = True
api = MagicMock()
api.projects.retrieve_project.side_effect = ApiError(
status_code=404, body={"errors": ["not found"]}
)
result = should_create_project(api, "missing", workspace="ws", headless=False)
# Interactive preflight saw 404 and user confirmed — downstream can
# skip the redundant lookup.
assert result == (True, True)
prompt_text = mock_confirm.call_args[0][0]
assert "missing" in prompt_text
assert "ws" in prompt_text
@patch("opik.cli.local_runner.preflight.click.confirm", return_value=False)
@patch("opik.cli.local_runner.preflight.sys.stdin")
def test__missing_and_tty_user_declines__returns_no_create(
self, mock_stdin, mock_confirm
):
mock_stdin.isatty.return_value = True
api = MagicMock()
api.projects.retrieve_project.side_effect = ApiError(
status_code=404, body={"errors": ["not found"]}
)
result = should_create_project(api, "missing", workspace="ws", headless=False)
assert result == (False, False)
@patch("opik.cli.local_runner.preflight.click.confirm", return_value=True)
@patch("opik.cli.local_runner.preflight.sys.stdin")
def test__no_workspace__prompt_omits_workspace_label(
self, mock_stdin, mock_confirm
):
mock_stdin.isatty.return_value = True
api = MagicMock()
api.projects.retrieve_project.side_effect = ApiError(status_code=404, body={})
should_create_project(api, "missing", workspace=None, headless=False)
prompt_text = mock_confirm.call_args[0][0]
assert "in workspace" not in prompt_text
class TestMaybeAutoConfigure:
def _patch_env(self, monkeypatch, opik_api_key=None):
if opik_api_key is None:
monkeypatch.delenv("OPIK_API_KEY", raising=False)
else:
monkeypatch.setenv("OPIK_API_KEY", opik_api_key)
def _config_probe(self, *, config_file_exists=False, api_key=None):
probe = MagicMock()
probe.config_file_exists = config_file_exists
probe.api_key = api_key
return probe
@patch("opik.cli.configure.run_interactive_configure")
def test__non_interactive_flag__skips(self, mock_configure, monkeypatch):
self._patch_env(monkeypatch)
maybe_auto_configure(api_key_arg=None, non_interactive=True, headless=False)
mock_configure.assert_not_called()
@patch("opik.cli.configure.run_interactive_configure")
def test__headless__skips(self, mock_configure, monkeypatch):
self._patch_env(monkeypatch)
maybe_auto_configure(api_key_arg=None, non_interactive=False, headless=True)
mock_configure.assert_not_called()
@patch("opik.cli.configure.run_interactive_configure")
def test__api_key_arg__skips(self, mock_configure, monkeypatch):
self._patch_env(monkeypatch)
maybe_auto_configure(api_key_arg="abc", non_interactive=False, headless=False)
mock_configure.assert_not_called()
@patch("opik.cli.configure.run_interactive_configure")
def test__env_api_key__skips(self, mock_configure, monkeypatch):
self._patch_env(monkeypatch, opik_api_key="from-env")
maybe_auto_configure(api_key_arg=None, non_interactive=False, headless=False)
mock_configure.assert_not_called()
@patch("opik.cli.configure.run_interactive_configure")
@patch("opik.cli.local_runner.preflight.sys.stdin")
def test__stdin_not_tty__skips(self, mock_stdin, mock_configure, monkeypatch):
self._patch_env(monkeypatch)
mock_stdin.isatty.return_value = False
maybe_auto_configure(api_key_arg=None, non_interactive=False, headless=False)
mock_configure.assert_not_called()
@patch("opik.cli.configure.run_interactive_configure")
@patch("opik.cli.local_runner.preflight.OpikConfig")
@patch("opik.cli.local_runner.preflight.sys.stdin")
def test__config_file_already_exists__skips(
self, mock_stdin, mock_config_cls, mock_configure, monkeypatch
):
self._patch_env(monkeypatch)
mock_stdin.isatty.return_value = True
mock_config_cls.return_value = self._config_probe(config_file_exists=True)
maybe_auto_configure(api_key_arg=None, non_interactive=False, headless=False)
mock_configure.assert_not_called()
@patch("opik.cli.configure.run_interactive_configure")
@patch("opik.cli.local_runner.preflight.OpikConfig")
@patch("opik.cli.local_runner.preflight.sys.stdin")
def test__probe_resolves_api_key_from_env_chain__skips(
self, mock_stdin, mock_config_cls, mock_configure, monkeypatch
):
# The OpikConfig probe layer can pick up an API key from elsewhere
# (e.g. .env file) even when OPIK_API_KEY isn't in os.environ. We
# treat that as "already configured enough" and skip the prompt.
self._patch_env(monkeypatch)
mock_stdin.isatty.return_value = True
mock_config_cls.return_value = self._config_probe(api_key="from-dotenv")
maybe_auto_configure(api_key_arg=None, non_interactive=False, headless=False)
mock_configure.assert_not_called()
@patch("opik.cli.configure.run_interactive_configure")
@patch("opik.cli.local_runner.preflight.OpikConfig")
@patch("opik.cli.local_runner.preflight.sys.stdin")
def test__no_config_and_interactive__runs_configure(
self, mock_stdin, mock_config_cls, mock_configure, monkeypatch
):
self._patch_env(monkeypatch)
mock_stdin.isatty.return_value = True
mock_config_cls.return_value = self._config_probe()
maybe_auto_configure(api_key_arg=None, non_interactive=False, headless=False)
mock_configure.assert_called_once()
if __name__ == "__main__":
pytest.main([__file__, "-v"])