|
| 1 | +import logging |
| 2 | +import pytest |
| 3 | +import time |
| 4 | +import psycopg2 |
| 5 | + |
| 6 | +from minds.client import Client |
| 7 | +from minds.exceptions import ObjectNotFound |
| 8 | +from . import config |
| 9 | + |
| 10 | +logging.basicConfig( |
| 11 | + level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s" |
| 12 | +) |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture(scope="session") |
| 16 | +def db_ground_truth(): |
| 17 | + """ |
| 18 | + Connects directly to the PG database to fetch the true max values for assertions, |
| 19 | + making tests resilient to data changes. |
| 20 | + """ |
| 21 | + conn = None |
| 22 | + try: |
| 23 | + # Use a dictionary of connection parameters that psycopg2 expects |
| 24 | + conn_params = { |
| 25 | + "dbname": config.POSTGRES_CONFIG["database"], |
| 26 | + "user": config.POSTGRES_CONFIG["user"], |
| 27 | + "password": config.POSTGRES_CONFIG["password"], |
| 28 | + "host": config.POSTGRES_CONFIG["host"], |
| 29 | + "port": config.POSTGRES_CONFIG["port"], |
| 30 | + } |
| 31 | + conn = psycopg2.connect(**conn_params) |
| 32 | + cur = conn.cursor() |
| 33 | + |
| 34 | + # Fully qualify the table name with its schema ('demo_data') to resolve the "relation does not exist" error. |
| 35 | + cur.execute("SELECT MAX(rental_price) FROM demo_data.home_rentals;") |
| 36 | + max_rental_price = cur.fetchone()[0] |
| 37 | + |
| 38 | + cur.close() |
| 39 | + logging.info( |
| 40 | + f"Fetched ground truth from DB: max_rental_price={max_rental_price}" |
| 41 | + ) |
| 42 | + return {"max_rental_price": max_rental_price} |
| 43 | + except Exception as e: |
| 44 | + pytest.skip(f"Could not connect to PG database to get ground truth: {e}") |
| 45 | + finally: |
| 46 | + if conn: |
| 47 | + conn.close() |
| 48 | + |
| 49 | + |
| 50 | +@pytest.fixture(scope="session") |
| 51 | +def sdk_client() -> Client: |
| 52 | + """Initialize a Minds Client using centralized config.""" |
| 53 | + logging.info(f"Connecting to Minds API at {config.MINDS_API_BASE_URL}") |
| 54 | + client = Client(api_key=config.AUTH_TOKEN, base_url=config.MINDS_API_BASE_URL) |
| 55 | + return client |
| 56 | + |
| 57 | + |
| 58 | +@pytest.fixture(scope="function") |
| 59 | +def sdk_datasource(sdk_client: Client): |
| 60 | + """Creates a temporary datasource for test use and cleans it up.""" |
| 61 | + timestamp = str(int(time.time()))[-6:] |
| 62 | + ds_base = config.DATASOURCE_CONFIGS[0] |
| 63 | + ds_name = f"{ds_base['name_prefix']}_{timestamp}" |
| 64 | + |
| 65 | + ds_config = { |
| 66 | + "name": ds_name, |
| 67 | + "engine": ds_base["engine"], |
| 68 | + "connection_data": ds_base["connection_data"], |
| 69 | + "description": "Temporary test datasource", |
| 70 | + } |
| 71 | + |
| 72 | + logging.info(f"Creating test datasource {ds_name}") |
| 73 | + ds = sdk_client.datasources.create(**ds_config) |
| 74 | + |
| 75 | + yield ds # make datasource available to tests |
| 76 | + |
| 77 | + # Cleanup |
| 78 | + logging.info(f"Dropping test datasource {ds_name}") |
| 79 | + try: |
| 80 | + sdk_client.datasources.drop(ds_name) |
| 81 | + except ObjectNotFound: |
| 82 | + logging.warning(f"Datasource {ds_name} already removed.") |
| 83 | + |
| 84 | + |
| 85 | +@pytest.fixture(scope="function") |
| 86 | +def sdk_mind(sdk_client: Client, sdk_datasource): |
| 87 | + """Creates a temporary mind for test use and cleans it up.""" |
| 88 | + timestamp = int(time.time()) |
| 89 | + mind_name = f"test_mind_{timestamp}" |
| 90 | + |
| 91 | + logging.info( |
| 92 | + f"Creating test mind {mind_name} with datasource {sdk_datasource.name}" |
| 93 | + ) |
| 94 | + # Create the mind with 'home_rentals' from the start. |
| 95 | + sdk_client.minds.create( |
| 96 | + mind_name, |
| 97 | + datasources=[{"name": sdk_datasource.name, "tables": ["home_rentals"]}], |
| 98 | + provider="openai", |
| 99 | + ) |
| 100 | + |
| 101 | + yield mind_name |
| 102 | + |
| 103 | + # Cleanup |
| 104 | + mind_name_upd = f"{mind_name}_upd" |
| 105 | + logging.info(f"Dropping test mind {mind_name_upd}") |
| 106 | + try: |
| 107 | + sdk_client.minds.drop(mind_name_upd) |
| 108 | + except ObjectNotFound: |
| 109 | + # Also try dropping the original name in case the test failed before the update |
| 110 | + try: |
| 111 | + sdk_client.minds.drop(mind_name) |
| 112 | + except ObjectNotFound: |
| 113 | + logging.warning(f"Mind {mind_name} or {mind_name_upd} already removed.") |
0 commit comments