|
| 1 | +# SPDX-FileCopyrightText: 2023-present deepset GmbH <info@deepset.ai> |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +import uuid |
| 6 | +from unittest.mock import MagicMock |
| 7 | + |
| 8 | +import pytest |
| 9 | +from haystack.dataclasses import Document |
| 10 | +from haystack.utils import Secret |
| 11 | + |
| 12 | +from haystack_integrations.document_stores.oracle import OracleConnectionConfig, OracleDocumentStore |
| 13 | + |
| 14 | +_USER = "haystack" |
| 15 | +_PASSWORD = "haystack" |
| 16 | +_DSN = "localhost:1521/freepdb1" |
| 17 | + |
| 18 | + |
| 19 | +def _make_store(table: str, embedding_dim: int) -> OracleDocumentStore: |
| 20 | + return OracleDocumentStore( |
| 21 | + connection_config=OracleConnectionConfig( |
| 22 | + user=Secret.from_token(_USER), |
| 23 | + password=Secret.from_token(_PASSWORD), |
| 24 | + dsn=Secret.from_token(_DSN), |
| 25 | + ), |
| 26 | + table_name=table, |
| 27 | + embedding_dim=embedding_dim, |
| 28 | + distance_metric="COSINE", |
| 29 | + create_table_if_not_exists=True, |
| 30 | + ) |
| 31 | + |
| 32 | + |
| 33 | +@pytest.fixture |
| 34 | +def document_store(): |
| 35 | + """768-dim store required by the mixin's filterable_docs fixture.""" |
| 36 | + table = f"hs_sync_{uuid.uuid4().hex[:8]}" |
| 37 | + s = _make_store(table, embedding_dim=768) |
| 38 | + yield s |
| 39 | + with s._get_connection() as conn, conn.cursor() as cur: |
| 40 | + cur.execute(f"DROP TABLE {table} PURGE") |
| 41 | + conn.commit() |
| 42 | + |
| 43 | + |
| 44 | +@pytest.fixture |
| 45 | +def embedding_store(): |
| 46 | + """4-dim store for embedding-retrieval, HNSW, and async tests.""" |
| 47 | + table = f"hs_emb_{uuid.uuid4().hex[:8]}" |
| 48 | + s = _make_store(table, embedding_dim=4) |
| 49 | + yield s |
| 50 | + with s._get_connection() as conn, conn.cursor() as cur: |
| 51 | + cur.execute(f"DROP TABLE {table} PURGE") |
| 52 | + conn.commit() |
| 53 | + |
| 54 | + |
| 55 | +@pytest.fixture |
| 56 | +def mock_pool(monkeypatch): |
| 57 | + """Patch oracledb.create_pool to return a mock pool with a mock connection/cursor.""" |
| 58 | + cursor = MagicMock() |
| 59 | + cursor.fetchall.return_value = [] |
| 60 | + cursor.fetchone.return_value = (0,) |
| 61 | + cursor.rowcount = 1 |
| 62 | + |
| 63 | + conn = MagicMock() |
| 64 | + conn.cursor.return_value.__enter__ = lambda _: cursor |
| 65 | + conn.cursor.return_value.__exit__ = MagicMock(return_value=False) |
| 66 | + |
| 67 | + pool = MagicMock() |
| 68 | + pool.acquire.return_value.__enter__ = lambda _: conn |
| 69 | + pool.acquire.return_value.__exit__ = MagicMock(return_value=False) |
| 70 | + |
| 71 | + monkeypatch.setattr( |
| 72 | + "haystack_integrations.document_stores.oracle.document_store.oracledb.create_pool", |
| 73 | + lambda **_: pool, |
| 74 | + ) |
| 75 | + return pool, conn, cursor |
| 76 | + |
| 77 | + |
| 78 | +@pytest.fixture |
| 79 | +def patched_store(monkeypatch): |
| 80 | + """Real OracleDocumentStore instance with a patched (mock) connection pool.""" |
| 81 | + monkeypatch.setenv("ORACLE_USER", "u") |
| 82 | + monkeypatch.setenv("ORACLE_PASSWORD", "p") |
| 83 | + monkeypatch.setenv("ORACLE_DSN", "localhost/xe") |
| 84 | + return OracleDocumentStore( |
| 85 | + connection_config=OracleConnectionConfig( |
| 86 | + user=Secret.from_env_var("ORACLE_USER"), |
| 87 | + password=Secret.from_env_var("ORACLE_PASSWORD"), |
| 88 | + dsn=Secret.from_env_var("ORACLE_DSN"), |
| 89 | + ), |
| 90 | + table_name="test_docs", |
| 91 | + embedding_dim=4, |
| 92 | + create_table_if_not_exists=False, |
| 93 | + ) |
| 94 | + |
| 95 | + |
| 96 | +@pytest.fixture |
| 97 | +def mock_store(): |
| 98 | + """MagicMock of OracleDocumentStore for retriever unit tests.""" |
| 99 | + store = MagicMock(spec=OracleDocumentStore) |
| 100 | + store.distance_metric = "COSINE" |
| 101 | + store._embedding_retrieval.return_value = [Document(id="A" * 32, content="hi")] |
| 102 | + store._embedding_retrieval_async.return_value = [Document(id="A" * 32, content="hi")] |
| 103 | + store.to_dict.return_value = { |
| 104 | + "type": "haystack_integrations.document_stores.oracle.document_store.OracleDocumentStore", |
| 105 | + "init_parameters": { |
| 106 | + "connection_config": { |
| 107 | + "user": {"type": "env_var", "env_vars": ["ORACLE_USER"], "strict": False}, |
| 108 | + "password": {"type": "env_var", "env_vars": ["ORACLE_PASSWORD"], "strict": False}, |
| 109 | + "dsn": {"type": "env_var", "env_vars": ["ORACLE_DSN"], "strict": False}, |
| 110 | + "wallet_location": None, |
| 111 | + "wallet_password": None, |
| 112 | + "min_connections": 1, |
| 113 | + "max_connections": 5, |
| 114 | + }, |
| 115 | + "table_name": "test_docs", |
| 116 | + "embedding_dim": 4, |
| 117 | + "distance_metric": "COSINE", |
| 118 | + "create_table_if_not_exists": False, |
| 119 | + "create_index": False, |
| 120 | + "hnsw_neighbors": 32, |
| 121 | + "hnsw_ef_construction": 200, |
| 122 | + "hnsw_accuracy": 95, |
| 123 | + "hnsw_parallel": 4, |
| 124 | + }, |
| 125 | + } |
| 126 | + return store |
0 commit comments