prediqai / tests /api /test_api_organizations.py
ganesh-vilje's picture
Deploy to Hugging Face Main
f8f02c0
import pytest
from unittest.mock import AsyncMock, patch, MagicMock
from bson import ObjectId
from fastapi import FastAPI
from fastapi.testclient import TestClient
from app.api.v1.organizations import organization_router
# Create an isolated FastAPI app just for this router
app = FastAPI()
app.include_router(organization_router, prefix="/v1/organizations")
mock_org_id = str(ObjectId())
mock_db_org = {
"_id": ObjectId(mock_org_id),
"name": "Test Org",
"description": "Test Description",
"email_domains": ["test.com"]
}
@pytest.fixture
def mock_org_collection():
collection_mock = AsyncMock()
collection_mock.insert_one = AsyncMock()
collection_mock.update_one = AsyncMock()
collection_mock.delete_one = AsyncMock()
collection_mock.find_one = AsyncMock()
cursor_mock = AsyncMock()
async def async_generator():
yield mock_db_org
cursor_mock.__aiter__ = lambda self: async_generator()
collection_mock.find = MagicMock(return_value=cursor_mock)
with patch("app.services.organizations.get_async_collection", return_value=collection_mock):
yield collection_mock
@pytest.fixture
def org_client():
from app.database.mongodb import get_async_database
app.dependency_overrides[get_async_database] = lambda: MagicMock()
with TestClient(app) as client:
yield client
app.dependency_overrides.clear()
def test_create_organization(org_client, mock_org_collection):
mock_insert_result = MagicMock()
mock_insert_result.inserted_id = ObjectId(mock_org_id)
mock_org_collection.insert_one.return_value = mock_insert_result
payload = {
"name": "Test Org",
"description": "Test Description",
"email_domains": ["test.com"]
}
response = org_client.post("/v1/organizations/", json=payload)
assert response.status_code == 200
data = response.json()
assert data["status"] is True
assert data["message"] == "Organization created successfully."
mock_org_collection.insert_one.assert_called_once()
def test_get_organization(org_client, mock_org_collection):
mock_org_collection.find_one.return_value = mock_db_org
response = org_client.get(f"/v1/organizations/{mock_org_id}")
assert response.status_code == 200
data = response.json()
assert data["status"] is True
mock_org_collection.find_one.assert_called_once()
def test_list_organizations(org_client, mock_org_collection):
response = org_client.get("/v1/organizations/")
assert response.status_code == 200
data = response.json()
assert data["status"] is True
assert len(data["data"]) == 1
mock_org_collection.find.assert_called_once()
def test_update_organization(org_client, mock_org_collection):
mock_update_result = MagicMock()
mock_update_result.modified_count = 1
mock_org_collection.update_one.return_value = mock_update_result
payload = {
"name": "Updated Org"
}
response = org_client.put(f"/v1/organizations/{mock_org_id}", json=payload)
assert response.status_code == 200
data = response.json()
assert data["status"] is True
mock_org_collection.update_one.assert_called_once()
def test_delete_organization(org_client, mock_org_collection):
mock_delete_result = MagicMock()
mock_delete_result.deleted_count = 1
mock_org_collection.delete_one.return_value = mock_delete_result
response = org_client.delete(f"/v1/organizations/{mock_org_id}")
assert response.status_code == 200
data = response.json()
assert data["status"] is True
mock_org_collection.delete_one.assert_called_once()