"""Integration tests for Odoo connection.
These tests require a real Odoo instance and credentials.
Set the following environment variables:
- ODOO_URL
- ODOO_DB
- ODOO_USERNAME
- ODOO_API_KEY
Run with: pytest tests/integration -v -m integration
"""
from __future__ import annotations
import os
import pytest
# Skip all tests in this module if credentials are not set
pytestmark = pytest.mark.integration
def has_odoo_credentials() -> bool:
"""Check if Odoo credentials are available."""
required_vars = ["ODOO_URL", "ODOO_DB", "ODOO_USERNAME", "ODOO_API_KEY"]
return all(os.environ.get(var) for var in required_vars)
@pytest.mark.skipif(
not has_odoo_credentials(),
reason="Odoo credentials not configured",
)
class TestOdooConnection:
"""Integration tests for Odoo connection."""
def test_connection(self):
"""Test basic connection to Odoo."""
from mcp_odoo.client import OdooClient
client = OdooClient()
info = client.test_connection()
assert info["status"] == "connected"
assert info["user_id"] > 0
assert info["database"]
def test_search_read_projects(self):
"""Test searching projects."""
from mcp_odoo.client import OdooClient
from mcp_odoo.constants import OdooModel
client = OdooClient()
projects = client.search_read(
OdooModel.PROJECT,
[],
["name"],
limit=5,
)
# Should return a list (may be empty)
assert isinstance(projects, list)
def test_search_read_employees(self):
"""Test searching employees."""
from mcp_odoo.client import OdooClient
from mcp_odoo.constants import OdooModel
client = OdooClient()
employees = client.search_read(
OdooModel.EMPLOYEE,
[],
["name"],
limit=5,
)
assert isinstance(employees, list)
def test_user_info(self):
"""Test getting current user info."""
from mcp_odoo.client import OdooClient
client = OdooClient()
user = client.get_user_info()
assert "name" in user
assert "login" in user
assert "company_id" in user
def test_invalid_model(self):
"""Test error handling for invalid model."""
from mcp_odoo.client import OdooClient
from mcp_odoo.exceptions import OdooError
client = OdooClient()
with pytest.raises(OdooError):
client.search_read(
"invalid.model.name",
[],
["name"],
)
@pytest.mark.skipif(
not has_odoo_credentials(),
reason="Odoo credentials not configured",
)
class TestClientFactory:
"""Integration tests for OdooClientFactory."""
def test_singleton_pattern(self):
"""Test that factory returns same instance."""
from mcp_odoo.client import OdooClientFactory
# Reset to ensure clean state
OdooClientFactory.reset()
client1 = OdooClientFactory.get_client()
client2 = OdooClientFactory.get_client()
assert client1 is client2
def test_reset(self):
"""Test factory reset creates new instance."""
from mcp_odoo.client import OdooClientFactory
client1 = OdooClientFactory.get_client()
OdooClientFactory.reset()
client2 = OdooClientFactory.get_client()
assert client1 is not client2