|
11 | 11 | from app.services.collections import helpers |
12 | 12 | from app.tests.utils.utils import get_project |
13 | 13 | from app.tests.utils.collection import get_vector_store_collection |
14 | | -from app.services.collections.helpers import ensure_unique_name |
| 14 | +from app.services.collections.helpers import ( |
| 15 | + ensure_unique_name, |
| 16 | + get_service_name, |
| 17 | + to_collection_public, |
| 18 | +) |
| 19 | +from app.models import Collection, ProviderType |
| 20 | +from app.core.util import now |
15 | 21 |
|
16 | 22 |
|
17 | 23 | def test_extract_error_message_parses_json_and_strips_prefix() -> None: |
@@ -167,3 +173,109 @@ def test_ensure_unique_name_conflict_with_vector_store_collection(db: Session) - |
167 | 173 |
|
168 | 174 | assert exc.value.status_code == 409 |
169 | 175 | assert "already exists" in exc.value.detail |
| 176 | + |
| 177 | + |
| 178 | +# get_service_name |
| 179 | + |
| 180 | + |
| 181 | +def test_get_service_name_openai() -> None: |
| 182 | + """Test that OpenAI provider returns correct service name.""" |
| 183 | + result = get_service_name("openai") |
| 184 | + assert result == "openai vector store" |
| 185 | + |
| 186 | + |
| 187 | +def test_get_service_name_case_insensitive() -> None: |
| 188 | + """Test that provider name is case-insensitive.""" |
| 189 | + assert get_service_name("OpenAI") == "openai vector store" |
| 190 | + assert get_service_name("OPENAI") == "openai vector store" |
| 191 | + assert get_service_name("OpEnAi") == "openai vector store" |
| 192 | + |
| 193 | + |
| 194 | +def test_get_service_name_unknown_provider() -> None: |
| 195 | + """Test that unknown providers return empty string.""" |
| 196 | + assert get_service_name("unknown") == "" |
| 197 | + assert get_service_name("bedrock") == "" # Commented out in the mapping |
| 198 | + assert get_service_name("gemini") == "" # Commented out in the mapping |
| 199 | + assert get_service_name("") == "" |
| 200 | + |
| 201 | + |
| 202 | +# to_collection_public |
| 203 | + |
| 204 | + |
| 205 | +def test_to_collection_public_vector_store() -> None: |
| 206 | + """Test conversion of vector store collection to public model.""" |
| 207 | + collection = Collection( |
| 208 | + id=uuid4(), |
| 209 | + project_id=1, |
| 210 | + provider=ProviderType.openai, |
| 211 | + llm_service_id="vs_123", |
| 212 | + llm_service_name="openai vector store", # Matches get_service_name("openai") |
| 213 | + name="Test Collection", |
| 214 | + description="Test description", |
| 215 | + inserted_at=now(), |
| 216 | + updated_at=now(), |
| 217 | + deleted_at=None, |
| 218 | + ) |
| 219 | + |
| 220 | + result = to_collection_public(collection) |
| 221 | + |
| 222 | + # For vector store, should map to knowledge_base fields |
| 223 | + assert result.id == collection.id |
| 224 | + assert result.knowledge_base_id == "vs_123" |
| 225 | + assert result.knowledge_base_provider == "openai vector store" |
| 226 | + assert result.llm_service_id is None |
| 227 | + assert result.llm_service_name is None |
| 228 | + assert result.project_id == 1 |
| 229 | + assert result.inserted_at == collection.inserted_at |
| 230 | + assert result.updated_at == collection.updated_at |
| 231 | + assert result.deleted_at is None |
| 232 | + |
| 233 | + |
| 234 | +def test_to_collection_public_assistant() -> None: |
| 235 | + """Test conversion of assistant collection to public model.""" |
| 236 | + collection = Collection( |
| 237 | + id=uuid4(), |
| 238 | + project_id=2, |
| 239 | + provider=ProviderType.openai, |
| 240 | + llm_service_id="asst_456", |
| 241 | + llm_service_name="gpt-4", # Does NOT match vector store name |
| 242 | + name="Assistant Collection", |
| 243 | + description="Assistant description", |
| 244 | + inserted_at=now(), |
| 245 | + updated_at=now(), |
| 246 | + deleted_at=None, |
| 247 | + ) |
| 248 | + |
| 249 | + result = to_collection_public(collection) |
| 250 | + |
| 251 | + # For assistant, should map to llm_service fields |
| 252 | + assert result.id == collection.id |
| 253 | + assert result.llm_service_id == "asst_456" |
| 254 | + assert result.llm_service_name == "gpt-4" |
| 255 | + assert result.knowledge_base_id is None |
| 256 | + assert result.knowledge_base_provider is None |
| 257 | + assert result.project_id == 2 |
| 258 | + assert result.inserted_at == collection.inserted_at |
| 259 | + assert result.updated_at == collection.updated_at |
| 260 | + assert result.deleted_at is None |
| 261 | + |
| 262 | + |
| 263 | +def test_to_collection_public_with_deleted_at() -> None: |
| 264 | + """Test that deleted_at field is properly included when set.""" |
| 265 | + deleted_time = now() |
| 266 | + collection = Collection( |
| 267 | + id=uuid4(), |
| 268 | + project_id=3, |
| 269 | + provider=ProviderType.openai, |
| 270 | + llm_service_id="vs_789", |
| 271 | + llm_service_name="openai vector store", |
| 272 | + name="Deleted Collection", |
| 273 | + description="Deleted", |
| 274 | + inserted_at=now(), |
| 275 | + updated_at=now(), |
| 276 | + deleted_at=deleted_time, |
| 277 | + ) |
| 278 | + |
| 279 | + result = to_collection_public(collection) |
| 280 | + |
| 281 | + assert result.deleted_at == deleted_time |
0 commit comments