Graph-Native Infrastructure for Context and Accountable AI Systems
Python agent-memoryaiai-governanceai-infrastructureartificial-intelligencecontext-engineeringcontext-graphsdata-engineeringdecision-intelligencedeveloper-toolsexplainable-aigenerative-aigraph-ragknowledge-graphllmontologyprovenancepythonreasoningsemantic-search
feat: add Amazon Redshift ingestor (#1587)
Sameer Kadam05aec1975def
diff --git a/docs/docs.json b/docs/docs.jsonindex 7eb7b1b0..4d8df8a1 100644--- a/docs/docs.json+++ b/docs/docs.json@@ -107,7 +107,8 @@"integrations/docling","integrations/snowflake","integrations/databricks",- "integrations/salesforce"+ "integrations/salesforce",+ "integrations/redshift"]},{diff --git a/docs/integrations/redshift.md b/docs/integrations/redshift.mdnew file mode 100644index 00000000..1112970c--- /dev/null+++ b/docs/integrations/redshift.md@@ -0,0 +1,342 @@+---+title: "Amazon Redshift Integration"+description: "Ingest structured data from Amazon Redshift tables and queries into Semantica's KG pipeline."+icon: "database"+---++> Extract data from Amazon Redshift into Semantica with password/native or IAM-role authentication, using the PostgreSQL-compatible wire protocol.+++## Installation++```bash+# Install with Redshift support+pip install "semantica[db-redshift]"++# Or install the connector separately+pip install redshift-connector>=2.0.0+```++`redshift-connector` is an optional dependency. A plain `pip install semantica` never pulls it in, and `import semantica.ingest` never loads it eagerly — the SDK is imported only when you first use `RedshiftConnector` or `RedshiftIngestor`.++<Note>+This connector uses the Redshift database wire protocol for read ingestion. COPY, UNLOAD, S3 integration, Spectrum external tables, and the Redshift Data API are outside the current scope of this integration.+</Note>+++## Basic Usage++```python+from semantica.ingest import RedshiftIngestor+import os++ingestor = RedshiftIngestor(+ host=os.getenv("REDSHIFT_HOST"),+ database=os.getenv("REDSHIFT_DATABASE"),+ user=os.getenv("REDSHIFT_USER"),+ password=os.getenv("REDSHIFT_PASSWORD"),+)++data = ingestor.ingest_table("customers")+print(f"Retrieved {data.row_count} rows — columns: {data.columns}")+```++<Tip>+Use environment variables (or a `.env` file with `python-dotenv`) to keep credentials out of source code. `RedshiftIngestor()` with no arguments reads from `REDSHIFT_*` environment variables automatically.+</Tip>+++## Authentication++<Tabs>+ <Tab title="Password / Native">+ The standard Redshift database username and password:++ ```python+ import os+ from semantica.ingest import RedshiftIngestor++ ingestor = RedshiftIngestor(+ host=os.getenv("REDSHIFT_HOST"), # e.g. cluster.abc.us-east-1.redshift.amazonaws.com+ database=os.getenv("REDSHIFT_DATABASE"),+ user=os.getenv("REDSHIFT_USER"),+ password=os.getenv("REDSHIFT_PASSWORD"),+ port=5439, # default; omit to use the default+ ssl=True, # default+ )+ ```++ Required environment variables:++ ```bash+ export REDSHIFT_HOST="cluster.abc.us-east-1.redshift.amazonaws.com"+ export REDSHIFT_DATABASE="dev"+ export REDSHIFT_USER="awsuser"+ export REDSHIFT_PASSWORD="your-password"+ ```+ </Tab>+ <Tab title="IAM Role (Recommended for AWS)">+ Use `iam=True` to obtain temporary credentials via+ `GetClusterCredentials`. The connector delegates credential resolution+ entirely to `redshift-connector` / boto3 — Semantica never calls AWS+ APIs directly.++ **Profile-based** (reads `~/.aws/credentials`):++ ```python+ import os+ from semantica.ingest import RedshiftIngestor++ ingestor = RedshiftIngestor(+ host=os.getenv("REDSHIFT_HOST"),+ database=os.getenv("REDSHIFT_DATABASE"),+ iam=True,+ db_user=os.getenv("REDSHIFT_DB_USER"),+ cluster_identifier=os.getenv("REDSHIFT_CLUSTER_IDENTIFIER"),+ profile="default", # AWS credentials-file profile+ )+ ```++ **Explicit AWS credentials** (e.g. for CI/CD or IAM roles with short-lived keys):++ ```python+ ingestor = RedshiftIngestor(+ host=os.getenv("REDSHIFT_HOST"),+ database=os.getenv("REDSHIFT_DATABASE"),+ iam=True,+ db_user=os.getenv("REDSHIFT_DB_USER"),+ cluster_identifier=os.getenv("REDSHIFT_CLUSTER_IDENTIFIER"),+ region=os.getenv("REDSHIFT_REGION"),+ access_key_id=os.getenv("REDSHIFT_ACCESS_KEY_ID"),+ secret_access_key=os.getenv("REDSHIFT_SECRET_ACCESS_KEY"),+ session_token=os.getenv("REDSHIFT_SESSION_TOKEN"), # only for temporary creds+ )+ ```++ When neither `profile` nor explicit keys are supplied, `redshift-connector`+ falls back to the standard AWS credential chain: `AWS_*` environment+ variables, instance-profile metadata, etc.++ Required for IAM mode: `host`, `database`, `db_user`, `cluster_identifier`.+ The `region` parameter is optional when it can be inferred from the+ credential chain or the cluster endpoint.+ </Tab>+</Tabs>+++## Environment Variables++All constructor parameters have `REDSHIFT_*` environment-variable fallbacks.+Explicit constructor values always take precedence.++| Variable | Parameter | Default |+|---|---|---|+| `REDSHIFT_HOST` | `host` | — |+| `REDSHIFT_DATABASE` | `database` | — |+| `REDSHIFT_USER` | `user` | — |+| `REDSHIFT_PASSWORD` | `password` | — |+| `REDSHIFT_PORT` | `port` | `5439` |+| `REDSHIFT_SCHEMA` | `schema` | `"public"` |+| `REDSHIFT_DB_USER` | `db_user` | — |+| `REDSHIFT_CLUSTER_IDENTIFIER` | `cluster_identifier` | — |+| `REDSHIFT_REGION` | `region` | — |+| `REDSHIFT_PROFILE` | `profile` | — |+| `REDSHIFT_ACCESS_KEY_ID` | `access_key_id` | — |+| `REDSHIFT_SECRET_ACCESS_KEY` | `secret_access_key` | — |+| `REDSHIFT_SESSION_TOKEN` | `session_token` | — |+++## Querying++### Ingest a table++```python+data = ingestor.ingest_table("orders")+print(f"{data.row_count} rows, columns: {data.columns}")+```++### Schema, filters, and pagination++```python+data = ingestor.ingest_table(+ "orders",+ schema="sales", # defaults to the ingestor's schema attribute ("public")+ database="analytics", # defaults to the connector's database+ where="status = 'shipped' AND total > 100",+ order_by="created_at DESC",+ limit=5000,+ offset=0,+)+```++<Warning>+`where` and `order_by` accept raw SQL fragments and must be trusted, operator-controlled input. Do not pass raw end-user strings here. They are validated against a blocklist that rejects statement separators, `UNION`, DML/DDL keywords, and time-based injection patterns, but this is not a full parser.+</Warning>++### Custom SQL++```python+data = ingestor.ingest_query("""+ SELECT customer_id, SUM(total) AS lifetime_value+ FROM sales.orders+ WHERE status = 'completed'+ GROUP BY customer_id+ ORDER BY lifetime_value DESC+ LIMIT 1000+""")+print(f"{data.row_count} rows")+```++### Parameterized queries++Use `%s` placeholders (DB-API 2.0 `format` paramstyle, which is the default for `redshift-connector`):++```python+data = ingestor.ingest_query(+ "SELECT id, name FROM users WHERE region = %s AND active = %s",+ params=("us-east-1", True),+)+```++### Batch fetching for large result sets++Use `batch_size` to control the driver fetch size — rows are fetched from+the server in chunks of that size rather than all at once, and each chunk is+converted immediately before the next is requested:++```python+data = ingestor.ingest_query(+ "SELECT * FROM large_events_table",+ batch_size=10000,+)+```++`batch_size` controls how many rows the driver reads from Redshift per+round-trip. The returned `RedshiftData.data` list still contains all matching+rows; use `LIMIT`/`OFFSET` in the query itself if you need to cap the total+result size.+++## Schema Discovery++### List base tables in a schema++```python+tables = ingestor.list_tables(schema="public")+print(tables) # ["customers", "orders", "products", ...]+```++Views are excluded; only base tables are returned.++### Inspect column metadata++```python+schema = ingestor.get_table_schema("customers", schema="public")++for col in schema["columns"]:+ print(f"{col['name']}: {col['type']} (nullable={col['nullable']})")++print("Primary keys:", schema["primary_keys"])+```++Each column dict contains:++| Key | Type | Description |+|---|---|---|+| `name` | `str` | Column name |+| `type` | `str` | Redshift data type (e.g. `"integer"`, `"character varying"`) |+| `nullable` | `bool` | Whether the column accepts `NULL` |++`primary_keys` is a list of column-name strings (empty list when no primary key is defined).+++## Export as Semantica Documents++Convert ingested rows to the document format that `GraphBuilder` consumes:++```python+documents = ingestor.export_as_documents(+ data,+ id_field="customer_id", # column used as document ID; defaults to "id"+ text_fields=["name", "notes"], # columns joined as document text; omit to auto-select+)++print(f"Created {len(documents)} documents")+# Each document:+# {+# "id": "12345",+# "text": "Alice Acme customer notes here",+# "metadata": {+# "source": "redshift",+# "table": "customers",+# "database": "analytics",+# "schema": "public",+# "row_data": { ... full cleaned row ... }+# }+# }+```++**ID resolution**: `str(row.get(id_field, row_index))` — the integer row index is used as a deterministic fallback when the `id_field` column is absent.++**Text when `text_fields` is provided**: each non-`None` field value is converted to a string and joined with a single space.++**Text when `text_fields=None`**: only columns whose values are already `str` type are joined. Integer, float, boolean, and `None` values are excluded, matching the Snowflake and Databricks connector behavior.++Pass the documents directly to `GraphBuilder`:++```python+from semantica.kg import GraphBuilder++builder = GraphBuilder()+graph = builder.build(documents)+print(f"Entities: {graph['metadata']['num_entities']}")+```+++## Context Manager++Prefer the context manager for jobs that run multiple queries — it opens one connection on entry and closes it on exit, so every call inside the `with` block reuses the same authenticated session:++```python+with RedshiftIngestor(+ host=os.getenv("REDSHIFT_HOST"),+ database=os.getenv("REDSHIFT_DATABASE"),+ user=os.getenv("REDSHIFT_USER"),+ password=os.getenv("REDSHIFT_PASSWORD"),+) as ingestor:+ customers = ingestor.ingest_table("customers", limit=50000)+ orders = ingestor.ingest_table("orders", limit=50000)+ schema = ingestor.get_table_schema("customers")+ tables = ingestor.list_tables()+# Connection closed automatically on exit, even if an exception is raised.+```++Standalone calls (without `with`) open and close a transient connection per call.+++## Connection Test++```python+from semantica.ingest import RedshiftConnector++connector = RedshiftConnector(+ host="cluster.abc.us-east-1.redshift.amazonaws.com",+ database="dev",+ user="awsuser",+ password="your-password",+)+if connector.test_connection():+ print("Connection OK")+else:+ print("Connection failed — check host, credentials, and network access")+```+++## See Also++- [Ingest Module](../reference/ingest) — Full `RedshiftIngestor` reference and all other ingestors.+- [Snowflake Integration](/integrations/snowflake) — SQL data warehouse connector with similar table/query ingestion.+- [Databricks Integration](/integrations/databricks) — Delta Lake / Unity Catalog connector.+- [Pipeline](../reference/pipeline) — Use Redshift ingestion as a pipeline step.+- [Installation](../installation) — All optional dependency extras.+- [Knowledge Graph](../reference/kg) — Build a knowledge graph from ingested Redshift data.diff --git a/pyproject.toml b/pyproject.tomlindex 95abee0e..b369bf3f 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -158,6 +158,7 @@ db-arrow = ["pyarrow>=14.0.0,<24.0.0; python_version < '3.10'"]db-salesforce = ["simple-salesforce>=1.12.0"]+db-redshift = ["redshift-connector>=2.0.0"]ingest-parquet = ["pyarrow>=24.0.0; python_version >= '3.10'","pyarrow>=14.0.0,<24.0.0; python_version < '3.10'"@@ -170,7 +171,7 @@ ingest-sap = ["requests>=2.28.0"]ingest-git = ["GitPython>=3.1.58"]db-all = [- "semantica[db-snowflake,db-databricks,db-salesforce,db-arrow]"+ "semantica[db-snowflake,db-databricks,db-salesforce,db-redshift,db-arrow]"]# ---- Embedding / Models ----diff --git a/semantica/ingest/__init__.py b/semantica/ingest/__init__.pyindex bba55644..f9d752cb 100644--- a/semantica/ingest/__init__.py+++ b/semantica/ingest/__init__.py@@ -247,6 +247,10 @@ _LAZY_EXPORTS: Dict[str, Tuple[str, str]] = {"SalesforceIngestor": (".salesforce_ingestor", "SalesforceIngestor"),"SalesforceData": (".salesforce_ingestor", "SalesforceData"),"SalesforceConnector": (".salesforce_ingestor", "SalesforceConnector"),+ # Redshift ingestion+ "RedshiftIngestor": (".redshift_ingestor", "RedshiftIngestor"),+ "RedshiftData": (".redshift_ingestor", "RedshiftData"),+ "RedshiftConnector": (".redshift_ingestor", "RedshiftConnector"),}_OPTIONAL_DEPENDENCY_MESSAGES = {@@ -289,6 +293,10 @@ _OPTIONAL_DEPENDENCY_MESSAGES = {"Salesforce ingestion requires optional dependency 'simple-salesforce'. ""Install it with: pip install 'semantica[db-salesforce]'"),+ ".redshift_ingestor": (+ "Redshift ingestion requires optional dependency 'redshift-connector'. "+ "Install it with: pip install 'semantica[db-redshift]'"+ ),}@@ -307,7 +315,14 @@ def __getattr__(name: str) -> Any:missing_name is Noneor any(pkg in missing_name- for pkg in ("git", "bs4", "pyarrow", "simple_salesforce", "lxml")+ for pkg in (+ "git",+ "bs4",+ "pyarrow",+ "simple_salesforce",+ "lxml",+ "redshift_connector",+ ))):raise ImportError(message) from exc@@ -349,6 +364,15 @@ def __getattr__(name: str) -> Any:if message:raise ImportError(message)+ if module_name == ".redshift_ingestor" and name in {+ "RedshiftIngestor",+ "RedshiftConnector",+ }:+ if not getattr(module, "REDSHIFT_AVAILABLE", True):+ message = _OPTIONAL_DEPENDENCY_MESSAGES.get(module_name)+ if message:+ raise ImportError(message)+value = getattr(module, attr_name)globals()[name] = valuereturn value@@ -439,6 +463,10 @@ __all__ = ["SalesforceIngestor","SalesforceData","SalesforceConnector",+ # Redshift ingestion+ "RedshiftIngestor",+ "RedshiftData",+ "RedshiftConnector",# Registry and Methods"MethodRegistry","method_registry",diff --git a/semantica/ingest/redshift_ingestor.py b/semantica/ingest/redshift_ingestor.pynew file mode 100644index 00000000..2a9c3181--- /dev/null+++ b/semantica/ingest/redshift_ingestor.py@@ -0,0 +1,1561 @@+"""Amazon Redshift ingestion module.++Pulls data from an Amazon Redshift cluster and flattens it into document+dicts that the pipeline can feed to ``GraphBuilder``.++Design notes+------------+Three classes, matching the Snowflake/Databricks/Salesforce ingestors:++- ``RedshiftData``: holds the rows returned by one table fetch or query+ (``data``, ``row_count``, ``columns``, ``table_name``, ``query``,+ ``database``, ``schema``, ``metadata``, ``ingested_at``). No SDK+ dependency — always importable.++- ``RedshiftConnector``: manages the ``redshift_connector`` client+ lifecycle. Raises ``ImportError`` at instantiation time when the+ optional ``redshift-connector`` package is absent. Supports both+ password/native authentication and IAM-role authentication.++- ``RedshiftIngestor``: orchestrates ingestion. Composes+ ``RedshiftConnector`` and exposes ``ingest_table``, ``ingest_query``,+ and ``_convert_rows``. Schema-discovery (``get_table_schema``,+ ``list_tables``) and document export (``export_as_documents``) are+ implemented in later stages.++Authentication+--------------+**Password / native**::++ conn = RedshiftConnector(+ host="cluster.abc.us-east-1.redshift.amazonaws.com",+ database="dev",+ user="awsuser",+ password="secret",+ )++**IAM role (profile-based)**::++ conn = RedshiftConnector(+ host="cluster.abc.us-east-1.redshift.amazonaws.com",+ database="dev",+ iam=True,+ db_user="awsuser",+ cluster_identifier="my-cluster",+ profile="default", # reads ~/.aws/credentials+ )++**IAM role (explicit credentials)**::++ conn = RedshiftConnector(+ host="cluster.abc.us-east-1.redshift.amazonaws.com",+ database="dev",+ iam=True,+ db_user="awsuser",+ cluster_identifier="my-cluster",+ region="us-east-1",+ access_key_id="AKIA...",+ secret_access_key="...",+ session_token="...", # only for temporary credentials+ )++SDK parameter note+------------------+The secret-access-key parameter accepted by ``redshift_connector.connect()``+is named ``secret_access_key`` (confirmed from the official SDK README+example). The AWS docs configuration table lists it as+``secret_access_key_id``, which appears to be a documentation error;+the README connect() example — the authoritative usage reference — uses+``secret_access_key``.++SQL safety+----------+Table/schema/database names are validated with+:func:`~semantica.ingest.db_ingestor._validate_sql_identifier` (must+match ``^[A-Za-z_][A-Za-z0-9_]*$``) and then escaped with double-quotes+before being interpolated into query strings. ``WHERE`` and+``ORDER BY`` fragments are checked with+:func:`~semantica.ingest.db_ingestor._validate_sql_fragment` which+blocks statement separators, ``UNION``, DML/DDL keywords, comment+sequences, and time-based blind-injection oracles.++Optional dependency+-------------------+Install the SDK before using ``RedshiftConnector`` or+``RedshiftIngestor``::++ pip install "semantica[db-redshift]"++or directly::++ pip install redshift-connector>=2.0.0++The top-level ``import semantica.ingest`` never imports+``redshift_connector`` eagerly; the SDK is loaded only when this module+is first accessed through the lazy-export mechanism in ``__init__.py``.+"""++from __future__ import annotations++import os+from dataclasses import dataclass, field+from datetime import datetime+from typing import Any, Dict, List, Optional, Tuple++from ..utils.exceptions import ProcessingError, ValidationError+from ..utils.logging import get_logger+from ..utils.progress_tracker import get_progress_tracker++# Re-use the repository's established SQL-safety helpers from db_ingestor+# rather than duplicating them. This import is always available (db_ingestor+# has no optional dependency) and keeps validation logic in one place.+from .db_ingestor import _validate_sql_fragment, _validate_sql_identifier++# ---------------------------------------------------------------------------+# Optional-dependency guard+# ---------------------------------------------------------------------------+# The module always imports cleanly. The sentinel fires at instantiation+# time (inside RedshiftConnector.__init__) so that:+# - ``from semantica.ingest import RedshiftData`` succeeds with no SDK+# - ``from semantica.ingest import RedshiftIngestor`` succeeds (lazy)+# - ``RedshiftConnector(...)`` raises a clear ImportError when SDK absent+# ---------------------------------------------------------------------------+try:+ import redshift_connector as _redshift_connector++ REDSHIFT_AVAILABLE = True+except (ImportError, OSError):+ _redshift_connector = None # type: ignore[assignment]+ REDSHIFT_AVAILABLE = False++__all__ = [+ "RedshiftData",+ "RedshiftConnector",+ "RedshiftIngestor",+]++_logger = get_logger("redshift_ingestor")++# ---------------------------------------------------------------------------+# RedshiftData+# ---------------------------------------------------------------------------+++@dataclass+class RedshiftData:+ """Records returned from an Amazon Redshift table or query.++ Attributes:+ data: List of row dictionaries.+ row_count: Number of rows in ``data``.+ columns: Ordered list of column names present in ``data``.+ table_name: Source table name, or ``None`` for raw-query results.+ query: SQL query string used to produce these rows, or ``None``.+ database: Redshift database name used for the ingestion.+ schema: Redshift schema name used for the ingestion.+ metadata: Arbitrary extra metadata (e.g. ``{"query": sql}``).+ ingested_at: Timestamp recorded when this object was created.+ """++ data: List[Dict[str, Any]]+ row_count: int+ columns: List[str]+ table_name: Optional[str] = None+ query: Optional[str] = None+ database: Optional[str] = None+ schema: Optional[str] = None+ metadata: Dict[str, Any] = field(default_factory=dict)+ ingested_at: datetime = field(default_factory=datetime.now)+++# ---------------------------------------------------------------------------+# RedshiftConnector+# ---------------------------------------------------------------------------+++class RedshiftConnector:+ """Manages the ``redshift_connector`` client lifecycle.++ Responsibilities:++ * Reads credentials from constructor arguments, falling back to+ environment variables with ``REDSHIFT_*`` names.+ * Validates that a usable authentication path is configured *before*+ any network call is made.+ * Exposes :meth:`connect`, :meth:`disconnect`, and+ :meth:`test_connection` so the ingestor (and tests) can control the+ connection lifecycle explicitly.+ * **Connection reuse**: :meth:`connect` returns the existing connection+ when one is already open, preventing duplicate round-trips and+ resource leaks when used as a context manager.++ Supported Authentication Modes+ --------------------------------+ **Password / native** — ``host``, ``database``, ``user``, ``password``+ are all required. ``ssl`` defaults to ``True`` and ``timeout`` is+ optional::++ connector = RedshiftConnector(+ host="cluster.abc.us-east-1.redshift.amazonaws.com",+ database="dev",+ user="awsuser",+ password="secret",+ )++ **IAM role** — set ``iam=True``; ``host``, ``database``, ``db_user``,+ and ``cluster_identifier`` are required. AWS credentials are resolved+ in order: explicit kwargs → profile → standard AWS environment variables+ (``AWS_ACCESS_KEY_ID`` etc.) / instance-profile metadata::++ connector = RedshiftConnector(+ host="cluster.abc.us-east-1.redshift.amazonaws.com",+ database="dev",+ iam=True,+ db_user="awsuser",+ cluster_identifier="my-cluster",+ profile="default", # or access_key_id/secret_access_key/region+ )++ Environment Variables+ ----------------------+ Every constructor parameter has an ``REDSHIFT_*`` environment-variable+ fallback:++ * ``REDSHIFT_HOST``+ * ``REDSHIFT_DATABASE``+ * ``REDSHIFT_USER``+ * ``REDSHIFT_PASSWORD``+ * ``REDSHIFT_PORT`` (default: ``5439``)+ * ``REDSHIFT_DB_USER``+ * ``REDSHIFT_CLUSTER_IDENTIFIER``+ * ``REDSHIFT_REGION``+ * ``REDSHIFT_PROFILE``+ * ``REDSHIFT_ACCESS_KEY_ID``+ * ``REDSHIFT_SECRET_ACCESS_KEY``+ * ``REDSHIFT_SESSION_TOKEN``++ Raises:+ ImportError: When ``redshift-connector`` is not installed.+ ValidationError: When required credentials are incomplete.+ """++ def __init__(+ self,+ host: Optional[str] = None,+ port: Optional[int] = None,+ database: Optional[str] = None,+ user: Optional[str] = None,+ password: Optional[str] = None,+ # IAM-role authentication+ iam: bool = False,+ db_user: Optional[str] = None,+ cluster_identifier: Optional[str] = None,+ region: Optional[str] = None,+ profile: Optional[str] = None,+ access_key_id: Optional[str] = None,+ secret_access_key: Optional[str] = None,+ session_token: Optional[str] = None,+ # TLS / misc+ ssl: bool = True,+ timeout: Optional[int] = None,+ **config: Any,+ ) -> None:+ """Initialise the connector and validate credentials.++ Args:+ host: Redshift cluster endpoint hostname.+ port: Redshift port. Defaults to ``5439`` (or+ ``REDSHIFT_PORT`` env var).+ database: Target database name.+ user: Database username (password auth). For IAM auth pass+ an empty string — the SDK requires the kwarg but ignores+ its value.+ password: Database password (password auth). For IAM auth+ pass an empty string.+ iam: Enable IAM-role authentication.+ db_user: Redshift database user to assume (IAM auth).+ cluster_identifier: Redshift cluster identifier (IAM auth).+ region: AWS region where the cluster is located (IAM auth).+ profile: AWS credentials-file profile name (IAM auth).+ access_key_id: Explicit AWS access key (IAM auth).+ secret_access_key: Explicit AWS secret access key (IAM auth).+ session_token: Temporary AWS session token (IAM auth).+ ssl: Enable SSL/TLS. Defaults to ``True``.+ timeout: Connection timeout in seconds.+ **config: Extra keyword arguments forwarded verbatim to+ ``redshift_connector.connect()``.++ Raises:+ ImportError: When ``redshift-connector`` is not installed.+ ValidationError: When required credentials are incomplete.+ """+ if not REDSHIFT_AVAILABLE:+ raise ImportError(+ "redshift-connector is required for RedshiftConnector. "+ 'Install it with: pip install "semantica[db-redshift]"'+ )++ self.logger = _logger++ # ------------------------------------------------------------------+ # Resolve credentials — explicit args override env vars.+ # Secrets (password, access keys, session token) are stored under+ # private attributes and are NEVER included in log messages.+ # ------------------------------------------------------------------+ self.host: Optional[str] = host or os.getenv("REDSHIFT_HOST")+ self.database: Optional[str] = database or os.getenv("REDSHIFT_DATABASE")+ self.user: Optional[str] = user or os.getenv("REDSHIFT_USER")+ self._password: Optional[str] = password or os.getenv("REDSHIFT_PASSWORD")++ # Port: explicit kwarg → env var → default 5439+ _port_env = os.getenv("REDSHIFT_PORT")+ if port is not None:+ self.port: int = port+ elif _port_env is not None:+ try:+ self.port = int(_port_env)+ except ValueError:+ raise ValidationError(+ f"REDSHIFT_PORT must be an integer, got {_port_env!r}."+ )+ else:+ self.port = 5439++ self.iam: bool = iam+ self.db_user: Optional[str] = db_user or os.getenv("REDSHIFT_DB_USER")+ self.cluster_identifier: Optional[str] = cluster_identifier or os.getenv(+ "REDSHIFT_CLUSTER_IDENTIFIER"+ )+ self.region: Optional[str] = region or os.getenv("REDSHIFT_REGION")+ self.profile: Optional[str] = profile or os.getenv("REDSHIFT_PROFILE")+ self._access_key_id: Optional[str] = access_key_id or os.getenv(+ "REDSHIFT_ACCESS_KEY_ID"+ )+ self._secret_access_key: Optional[str] = secret_access_key or os.getenv(+ "REDSHIFT_SECRET_ACCESS_KEY"+ )+ self._session_token: Optional[str] = session_token or os.getenv(+ "REDSHIFT_SESSION_TOKEN"+ )+ self.ssl: bool = ssl+ self.timeout: Optional[int] = timeout+ self._extra_config: Dict[str, Any] = config++ # Internal connection — None until connect() is called.+ self._connection: Optional[Any] = None++ # Validate credentials before any network call.+ self._validate_auth()++ self.logger.debug(+ "Redshift connector initialised (host=%s, database=%s, iam=%s)",+ self.host,+ self.database,+ self.iam,+ )++ # ------------------------------------------------------------------+ # Internal helpers+ # ------------------------------------------------------------------++ def _validate_auth(self) -> None:+ """Raise :class:`~semantica.utils.exceptions.ValidationError` when+ the configured credentials are insufficient to attempt a connection.++ Validates before any network call so failures are immediate and+ never leak partial state.++ Password mode requires: ``host``, ``database``, ``user``,+ ``password``.++ IAM mode requires: ``host``, ``database``, ``db_user``,+ ``cluster_identifier``. AWS credential material (``profile`` or+ ``access_key_id``/``secret_access_key``) is optional when the SDK+ can fall back to the standard AWS credential chain (env vars,+ instance profile, etc.).++ Raises:+ ValidationError: When a required credential is absent.+ """+ if not self.host:+ raise ValidationError(+ "Redshift host is required. "+ "Provide via 'host' or REDSHIFT_HOST environment variable."+ )+ if not self.database:+ raise ValidationError(+ "Redshift database is required. "+ "Provide via 'database' or REDSHIFT_DATABASE environment variable."+ )++ if self.iam:+ if not self.db_user:+ raise ValidationError(+ "Redshift IAM authentication requires 'db_user'. "+ "Provide via 'db_user' or REDSHIFT_DB_USER environment variable."+ )+ if not self.cluster_identifier:+ raise ValidationError(+ "Redshift IAM authentication requires 'cluster_identifier'. "+ "Provide via 'cluster_identifier' or "+ "REDSHIFT_CLUSTER_IDENTIFIER environment variable."+ )+ else:+ # Password / native authentication.+ if not self.user:+ raise ValidationError(+ "Redshift password authentication requires 'user'. "+ "Provide via 'user' or REDSHIFT_USER environment variable."+ )+ if not self._password:+ raise ValidationError(+ "Redshift password authentication requires 'password'. "+ "Provide via 'password' or REDSHIFT_PASSWORD environment variable."+ )++ def _build_connect_kwargs(self) -> Dict[str, Any]:+ """Assemble keyword arguments for ``redshift_connector.connect()``.++ Credentials are assembled here and nowhere else; they are not+ logged.++ Returns:+ Keyword arguments dict ready for+ ``redshift_connector.connect(**kwargs)``.+ """+ kwargs: Dict[str, Any] = {+ "host": self.host,+ "database": self.database,+ "port": self.port,+ "ssl": self.ssl,+ }++ if self.timeout is not None:+ kwargs["timeout"] = self.timeout++ if self.iam:+ # IAM auth — the SDK calls GetClusterCredentials via boto3 to+ # obtain a temporary database password. ``user`` and ``password``+ # must be present as kwargs (SDK requirement) but are ignored+ # when iam=True; pass empty strings to satisfy the signature.+ kwargs["iam"] = True+ kwargs["user"] = ""+ kwargs["password"] = ""+ kwargs["db_user"] = self.db_user+ kwargs["cluster_identifier"] = self.cluster_identifier++ if self.region:+ kwargs["region"] = self.region+ if self.profile:+ kwargs["profile"] = self.profile+ if self._access_key_id:+ kwargs["access_key_id"] = self._access_key_id+ if self._secret_access_key:+ kwargs["secret_access_key"] = self._secret_access_key+ if self._session_token:+ kwargs["session_token"] = self._session_token+ else:+ # Password / native authentication.+ kwargs["user"] = self.user+ kwargs["password"] = self._password++ # Forward any extra config supplied by the caller.+ kwargs.update(self._extra_config)+ return kwargs++ # ------------------------------------------------------------------+ # Public properties+ # ------------------------------------------------------------------++ @property+ def connection(self) -> Optional[Any]:+ """The active ``redshift_connector`` connection, or ``None``."""+ return self._connection++ # ------------------------------------------------------------------+ # Lifecycle+ # ------------------------------------------------------------------++ def connect(self) -> Any:+ """Create and return a ``redshift_connector`` connection.++ If a connection is already open, returns it immediately without+ creating a new one (connection-reuse semantics matching the+ Salesforce and Databricks connectors).++ Returns:+ The ``redshift_connector`` connection object.++ Raises:+ ProcessingError: If the connection attempt fails for any+ reason (network error, bad credentials, SDK error).+ Passwords and secret keys are never included in the+ raised message.+ """+ if self._connection is not None:+ return self._connection++ try:+ kwargs = self._build_connect_kwargs()+ self._connection = _redshift_connector.connect(**kwargs)+ self.logger.info(+ "Connected to Redshift: host=%s database=%s iam=%s",+ self.host,+ self.database,+ self.iam,+ )+ return self._connection++ except Exception as exc:+ # Ensure no partial connection reference leaks out.+ self._connection = None+ # Log only the exception type — never the message, which may+ # contain credential material from the SDK or AWS SDK.+ self.logger.error(+ "Failed to connect to Redshift: %s", type(exc).__name__+ )+ raise ProcessingError(+ f"Failed to connect to Redshift: {type(exc).__name__}"+ ) from exc++ def disconnect(self) -> None:+ """Close the connection and release the reference.++ Safe to call when already disconnected — subsequent calls are+ no-ops.+ """+ if self._connection is not None:+ try:+ self._connection.close()+ except Exception: # noqa: BLE001+ pass+ finally:+ self._connection = None+ self.logger.info("Disconnected from Redshift.")++ def test_connection(self) -> bool:+ """Verify connectivity by opening a transient connection.++ Opens a fresh connection (or reuses an existing one), executes+ ``SELECT 1`` to confirm the session is live, then closes the+ connection if it was opened by this call.++ Returns:+ ``True`` if the ping succeeds, ``False`` for any failure.+ """+ already_connected = self._connection is not None+ try:+ conn = self.connect()+ cursor = conn.cursor()+ try:+ cursor.execute("SELECT 1")+ cursor.fetchone()+ finally:+ cursor.close()+ return True+ except Exception as exc:+ self.logger.debug(+ "Redshift connection test failed: %s", type(exc).__name__+ )+ return False+ finally:+ if not already_connected:+ self.disconnect()+++# ---------------------------------------------------------------------------+# RedshiftIngestor+# ---------------------------------------------------------------------------+++class RedshiftIngestor:+ """Amazon Redshift data ingestor for the Semantica framework.++ Wraps :class:`RedshiftConnector` and exposes table and query+ ingestion, row-to-dict conversion, and SQL-safe query construction.++ Schema-discovery methods (``get_table_schema``, ``list_tables``) and+ document export (``export_as_documents``) are implemented in later+ stages.++ Args:+ host: Redshift cluster endpoint hostname.+ port: Redshift port (default: ``5439``).+ database: Target database name.+ user: Database username (password auth).+ password: Database password (password auth).+ schema: Default schema (default: ``"public"``).+ iam: Use IAM-role authentication when ``True``.+ db_user: Redshift database user to assume (IAM auth).+ cluster_identifier: Redshift cluster identifier (IAM auth).+ region: AWS region (IAM auth).+ profile: AWS credentials-file profile name (IAM auth).+ access_key_id: Explicit AWS access key (IAM auth).+ secret_access_key: Explicit AWS secret access key (IAM auth).+ session_token: Temporary AWS session token (IAM auth).+ ssl: Enable SSL/TLS. Defaults to ``True``.+ timeout: Connection timeout in seconds.+ connector: An existing :class:`RedshiftConnector` to reuse.+ config: Optional extra configuration dict forwarded to the+ connector.+ **kwargs: Additional keyword arguments merged into ``config``.++ Raises:+ ImportError: When ``redshift-connector`` is not installed.+ ValidationError: When required credentials are incomplete.++ Example::++ import os+ from semantica.ingest import RedshiftIngestor++ # Password authentication+ ingestor = RedshiftIngestor(+ host=os.getenv("REDSHIFT_HOST"),+ database=os.getenv("REDSHIFT_DATABASE"),+ user=os.getenv("REDSHIFT_USER"),+ password=os.getenv("REDSHIFT_PASSWORD"),+ )++ # Context manager — preferred for production use+ with RedshiftIngestor(+ host=os.getenv("REDSHIFT_HOST"),+ database=os.getenv("REDSHIFT_DATABASE"),+ user=os.getenv("REDSHIFT_USER"),+ password=os.getenv("REDSHIFT_PASSWORD"),+ ) as ing:+ data = ing.ingest_table("orders", schema="sales", limit=1000)+ query_data = ing.ingest_query(+ "SELECT id, total FROM sales.orders WHERE status = %s",+ params=("shipped",),+ )+ """++ def __init__(+ self,+ host: Optional[str] = None,+ port: Optional[int] = None,+ database: Optional[str] = None,+ user: Optional[str] = None,+ password: Optional[str] = None,+ schema: Optional[str] = None,+ iam: bool = False,+ db_user: Optional[str] = None,+ cluster_identifier: Optional[str] = None,+ region: Optional[str] = None,+ profile: Optional[str] = None,+ access_key_id: Optional[str] = None,+ secret_access_key: Optional[str] = None,+ session_token: Optional[str] = None,+ ssl: bool = True,+ timeout: Optional[int] = None,+ connector: Optional[RedshiftConnector] = None,+ config: Optional[Dict[str, Any]] = None,+ **kwargs: Any,+ ) -> None:+ self.logger = _logger++ self.config: Dict[str, Any] = config or {}+ self.config.update(kwargs)++ # Instantiate a connector unless one is supplied.+ # ImportError propagates when SDK is absent; ValidationError when+ # credentials are incomplete.+ self.connector: RedshiftConnector = connector or RedshiftConnector(+ host=host,+ port=port,+ database=database,+ user=user,+ password=password,+ iam=iam,+ db_user=db_user,+ cluster_identifier=cluster_identifier,+ region=region,+ profile=profile,+ access_key_id=access_key_id,+ secret_access_key=secret_access_key,+ session_token=session_token,+ ssl=ssl,+ timeout=timeout,+ **self.config,+ )++ self.schema: str = schema or os.getenv("REDSHIFT_SCHEMA", "public")++ # Progress tracker — consistent with all other ingestors.+ self.progress_tracker = get_progress_tracker()+ if not self.progress_tracker.enabled:+ self.progress_tracker.enabled = True++ self.logger.debug("Redshift ingestor initialised.")++ # ------------------------------------------------------------------+ # Context-manager support+ # ------------------------------------------------------------------++ def __enter__(self) -> "RedshiftIngestor":+ """Open the Redshift connection on context entry."""+ self.connector.connect()+ return self++ def __exit__(+ self,+ exc_type: Any,+ exc_val: Any,+ exc_tb: Any,+ ) -> None:+ """Close the Redshift connection on context exit."""+ self.close()++ # ------------------------------------------------------------------+ # Lifecycle+ # ------------------------------------------------------------------++ def close(self) -> None:+ """Disconnect from Redshift and release the connection."""+ self.connector.disconnect()++ # ------------------------------------------------------------------+ # SQL-identity helpers+ # ------------------------------------------------------------------++ @staticmethod+ def _escape_identifier(identifier: str) -> str:+ """Escape a SQL identifier using double-quote quoting.++ Redshift (PostgreSQL-compatible) uses ``"identifier"`` quoting.+ Internal double-quote characters are doubled (``""``).++ Args:+ identifier: Already-validated identifier string.++ Returns:+ Double-quoted, safely escaped identifier.+ """+ escaped = identifier.replace('"', '""')+ return f'"{escaped}"'++ @staticmethod+ def _build_table_ref(+ table_name: str,+ schema: Optional[str],+ database: Optional[str],+ ) -> str:+ """Build a fully-qualified, double-quote-escaped table reference.++ Components that are ``None`` or empty are omitted.++ Args:+ table_name: Already-validated table name.+ schema: Optional schema name (already validated).+ database: Optional database name (already validated).++ Returns:+ SQL table reference such as ``"mydb"."myschema"."mytable"``+ or just ``"mytable"``.+ """+ parts = [+ RedshiftIngestor._escape_identifier(part)+ for part in (database, schema, table_name)+ if part+ ]+ return ".".join(parts)++ # ------------------------------------------------------------------+ # Ingestion — ingest_table+ # ------------------------------------------------------------------++ def ingest_table(+ self,+ table_name: str,+ database: Optional[str] = None,+ schema: Optional[str] = None,+ limit: Optional[int] = None,+ offset: Optional[int] = None,+ where: Optional[str] = None,+ order_by: Optional[str] = None,+ **options: Any,+ ) -> RedshiftData:+ """Fetch rows from a Redshift table.++ Builds a ``SELECT * FROM <table>`` query with optional+ ``WHERE``, ``ORDER BY``, ``LIMIT``, and ``OFFSET`` clauses and+ executes it against the Redshift cluster.++ All identifier components (``table_name``, ``schema``,+ ``database``) are validated with+ :func:`~semantica.ingest.db_ingestor._validate_sql_identifier`+ and then escaped with double-quotes before interpolation.+ ``WHERE`` and ``ORDER BY`` fragments are validated with+ :func:`~semantica.ingest.db_ingestor._validate_sql_fragment`.+ Validation is performed before opening any connection so+ malformed inputs fail fast without a network round-trip.++ Connection lifecycle mirrors the Databricks/Snowflake pattern:+ if the ingestor is used as a context manager the existing+ connection is reused; standalone calls open a transient+ connection and close it in a ``finally`` block.++ Args:+ table_name: Name of the Redshift table to query.+ database: Override database name (defaults to the connector's+ configured database).+ schema: Override schema name (defaults to the ingestor's+ default ``schema`` attribute).+ limit: Maximum number of rows to return.+ offset: Number of leading rows to skip.+ where: SQL ``WHERE`` clause fragment (without the+ ``WHERE`` keyword). Trusted / operator input only —+ do not pass raw end-user text.+ order_by: SQL ``ORDER BY`` clause fragment (without the+ keyword). Same trust requirement.+ **options: Reserved for future use.++ Returns:+ :class:`RedshiftData` with ``data``, ``row_count``,+ ``columns``, ``table_name``, ``database``, ``schema``,+ ``query``, and ``metadata`` populated.++ Raises:+ ValidationError: If any identifier or SQL fragment fails+ safety validation.+ ProcessingError: If the query fails or the connection+ cannot be established.+ """+ effective_schema = schema or self.schema+ effective_database = database or self.connector.database++ # ------------------------------------------------------------------+ # Validate all identifiers and SQL fragments BEFORE connecting so+ # invalid inputs are rejected cheaply and without any network call.+ # ------------------------------------------------------------------+ _validate_sql_identifier(table_name, "table_name")+ if effective_schema:+ _validate_sql_identifier(effective_schema, "schema")+ if effective_database:+ _validate_sql_identifier(effective_database, "database")+ if where:+ _validate_sql_fragment(where, "where")+ if order_by:+ _validate_sql_fragment(order_by, "order_by")++ table_ref = self._build_table_ref(+ table_name, effective_schema, effective_database+ )++ tracking_id = self.progress_tracker.start_tracking(+ file=table_ref,+ module="ingest",+ submodule="RedshiftIngestor",+ message=f"Table: {table_ref}",+ )++ try:+ already_connected = self.connector.connection is not None+ conn = self.connector.connect()++ # Capture the previous autocommit state so we can restore it+ # when the connection was already open (caller-owned). We only+ # need to restore it in that case; transient connections are+ # closed in the finally block anyway.+ _prev_autocommit = conn.autocommit if already_connected else None+ try:+ # Enable autocommit for read-only ingestion to avoid leaving+ # idle transactions open on the server (Redshift/Postgres+ # DB-API starts a transaction automatically otherwise).+ conn.autocommit = True++ query = f"SELECT * FROM {table_ref}"++ if where:+ query += f" WHERE {where}"+ if order_by:+ query += f" ORDER BY {order_by}"+ if limit is not None:+ query += f" LIMIT {int(limit)}"+ if offset is not None:+ query += f" OFFSET {int(offset)}"++ self.logger.debug("Executing ingest_table query: %s", query)++ self.progress_tracker.update_tracking(+ tracking_id, message="Executing query..."+ )++ cursor = conn.cursor()+ try:+ cursor.execute(query)++ self.progress_tracker.update_tracking(+ tracking_id, message="Fetching results..."+ )++ columns, data = self._fetch_all(cursor)+ finally:+ cursor.close()++ finally:+ if already_connected and _prev_autocommit is not None:+ conn.autocommit = _prev_autocommit+ if not already_connected:+ self.connector.disconnect()++ self.progress_tracker.stop_tracking(+ tracking_id,+ status="completed",+ message=f"Ingested {len(data)} rows",+ )+ self.logger.info(+ "ingest_table completed: %s — %d row(s)", table_ref, len(data)+ )++ return RedshiftData(+ data=data,+ row_count=len(data),+ columns=columns,+ table_name=table_name,+ database=effective_database,+ schema=effective_schema,+ query=query,+ metadata={"query": query},+ )++ except (ValidationError, ProcessingError):+ self.progress_tracker.stop_tracking(+ tracking_id, status="failed", message="Query failed"+ )+ raise+ except Exception as exc:+ self.progress_tracker.stop_tracking(+ tracking_id, status="failed", message=str(exc)+ )+ self.logger.error(+ "Failed to ingest table %s: %s", table_name, type(exc).__name__+ )+ raise ProcessingError(+ f"Failed to ingest Redshift table '{table_name}': "+ f"{type(exc).__name__}"+ ) from exc++ # ------------------------------------------------------------------+ # Ingestion — ingest_query+ # ------------------------------------------------------------------++ def ingest_query(+ self,+ query: str,+ params: Optional[Any] = None,+ batch_size: Optional[int] = None,+ **options: Any,+ ) -> RedshiftData:+ """Execute a raw SQL query and return all matching rows.++ Passes the query verbatim to the Redshift cursor. The caller+ is responsible for correctness and safety of the query string.+ Use ``params`` for any user-supplied values rather than+ interpolating them directly.++ ``redshift_connector`` cursors support DB-API 2.0 parameter+ binding: pass a tuple or list for positional ``%s`` placeholders,+ or a dict for named ``%(name)s`` style, depending on the cursor's+ ``paramstyle`` setting (default: ``"format"``).++ When ``batch_size`` is provided the rows are fetched in repeated+ ``fetchmany(batch_size)`` calls until the result set is exhausted.++ Connection lifecycle mirrors :meth:`ingest_table`: standalone+ calls open and close a transient connection; calls made inside a+ context manager reuse the open connection.++ Args:+ query: Complete SQL query string.+ params: Query parameters forwarded to+ ``cursor.execute(query, params)``. Accepts any form+ the ``redshift_connector`` cursor accepts (tuple, list,+ dict, or ``None``).+ batch_size: If set, rows are fetched in batches of this+ size using ``cursor.fetchmany(batch_size)``.+ **options: Reserved for future use.++ Returns:+ :class:`RedshiftData` with ``data``, ``row_count``,+ ``columns``, and ``query`` populated.++ Raises:+ ProcessingError: If the query fails or the connection+ cannot be established.+ """+ tracking_id = self.progress_tracker.start_tracking(+ file="query",+ module="ingest",+ submodule="RedshiftIngestor",+ message="Executing query...",+ )++ try:+ already_connected = self.connector.connection is not None+ conn = self.connector.connect()++ _prev_autocommit = conn.autocommit if already_connected else None+ try:+ # Enable autocommit for read-only ingestion.+ conn.autocommit = True++ self.logger.debug(+ "Executing ingest_query: %s", query[:200]+ )++ self.progress_tracker.update_tracking(+ tracking_id, message="Executing query..."+ )++ cursor = conn.cursor()+ try:+ if params is not None:+ cursor.execute(query, params)+ else:+ cursor.execute(query)++ self.progress_tracker.update_tracking(+ tracking_id, message="Fetching results..."+ )++ columns = (+ self._disambiguate_columns(+ [desc[0] for desc in cursor.description]+ )+ if cursor.description+ else []+ )++ if batch_size:+ # Process each batch immediately so raw tuples from+ # the previous batch are eligible for GC before the+ # next fetch. This keeps live memory proportional to+ # one batch of raw rows plus the accumulated converted+ # results, rather than two full copies of all rows.+ data: List[Dict[str, Any]] = []+ while True:+ batch = cursor.fetchmany(batch_size)+ if not batch:+ break+ batch_dicts = self._make_row_dicts(columns, batch)+ data.extend(self._convert_rows(batch_dicts))+ self.progress_tracker.update_tracking(+ tracking_id,+ message=f"Fetched {len(data)} rows...",+ )+ else:+ raw_rows = cursor.fetchall()+ row_dicts = self._make_row_dicts(columns, raw_rows)+ data = self._convert_rows(row_dicts)++ finally:+ cursor.close()++ finally:+ if already_connected and _prev_autocommit is not None:+ conn.autocommit = _prev_autocommit+ if not already_connected:+ self.connector.disconnect()++ self.progress_tracker.stop_tracking(+ tracking_id,+ status="completed",+ message=f"Query completed: {len(data)} rows",+ )+ self.logger.info(+ "ingest_query completed: %d row(s)", len(data)+ )++ return RedshiftData(+ data=data,+ row_count=len(data),+ columns=columns,+ query=query,+ metadata={"params": params} if params is not None else {},+ )++ except (ValidationError, ProcessingError):+ self.progress_tracker.stop_tracking(+ tracking_id, status="failed", message="Query failed"+ )+ raise+ except Exception as exc:+ self.progress_tracker.stop_tracking(+ tracking_id, status="failed", message=str(exc)+ )+ self.logger.error(+ "Failed to execute query: %s", type(exc).__name__+ )+ raise ProcessingError(+ f"Failed to execute Redshift query: {type(exc).__name__}"+ ) from exc++ # ------------------------------------------------------------------+ # Schema discovery+ # ------------------------------------------------------------------++ def get_table_schema(+ self,+ table_name: str,+ database: Optional[str] = None,+ schema: Optional[str] = None,+ ) -> Dict[str, Any]:+ """Return column metadata and primary-key information for a table.++ Queries ``information_schema.COLUMNS`` for column order, names, data+ types, and nullability, and ``information_schema.TABLE_CONSTRAINTS``+ joined with ``information_schema.KEY_COLUMN_USAGE`` for primary-key+ column names.++ ``TABLE_SCHEMA`` and ``TABLE_NAME`` filter values are passed as+ DB-API ``%s`` parameters so they are never interpolated directly into+ the SQL string. The optional *database* prefix is double-quote-escaped+ via :meth:`_escape_identifier` after passing through+ :func:`~semantica.ingest.db_ingestor._validate_sql_identifier`.++ Args:+ table_name: Name of the Redshift table to introspect.+ database: Override database name. Defaults to the connector's+ configured database.+ schema: Override schema name. Defaults to the ingestor's+ ``schema`` attribute (``"public"`` unless changed).++ Returns:+ A dictionary with two keys:++ ``"columns"``+ Ordered list of column dicts, each containing+ ``"name"``, ``"type"``, and ``"nullable"`` (``bool``).++ ``"primary_keys"``+ List of column-name strings that form the primary key, or+ an empty list when no primary key is defined.++ Raises:+ ValidationError: If *table_name*, *database*, or *schema* fails+ identifier validation.+ ProcessingError: If the metadata query fails.+ """+ effective_schema = schema or self.schema+ effective_database = database or self.connector.database++ # Validate identifiers before connecting.+ _validate_sql_identifier(table_name, "table_name")+ if effective_schema:+ _validate_sql_identifier(effective_schema, "schema")+ if effective_database:+ _validate_sql_identifier(effective_database, "database")++ # Build the information_schema reference, qualified by database when+ # available (Redshift allows cross-database queries via the database+ # prefix on information_schema).+ if effective_database:+ info_schema = (+ f"{self._escape_identifier(effective_database)}.information_schema"+ )+ else:+ info_schema = "information_schema"++ # Column query — TABLE_SCHEMA and TABLE_NAME are %s params (values,+ # not identifiers) so they are never interpolated into the SQL string.+ col_query = f"""+ SELECT+ column_name,+ data_type,+ is_nullable+ FROM {info_schema}.columns+ WHERE table_schema = %s+ AND table_name = %s+ ORDER BY ordinal_position+ """++ # Primary-key query — same parameterised approach.+ pk_query = f"""+ SELECT kcu.column_name+ FROM {info_schema}.table_constraints tc+ JOIN {info_schema}.key_column_usage kcu+ ON tc.constraint_name = kcu.constraint_name+ AND tc.table_schema = kcu.table_schema+ AND tc.table_name = kcu.table_name+ WHERE tc.constraint_type = 'PRIMARY KEY'+ AND tc.table_schema = %s+ AND tc.table_name = %s+ ORDER BY kcu.ordinal_position+ """++ params = (effective_schema, table_name)++ try:+ already_connected = self.connector.connection is not None+ conn = self.connector.connect()++ _prev_autocommit = conn.autocommit if already_connected else None+ try:+ conn.autocommit = True++ # --- columns ---+ cursor = conn.cursor()+ try:+ cursor.execute(col_query, params)+ col_rows = cursor.fetchall()+ col_names = [desc[0] for desc in cursor.description]+ finally:+ cursor.close()++ col_dicts = [dict(zip(col_names, row)) for row in col_rows]+ columns = [+ {+ "name": row["column_name"],+ "type": row["data_type"],+ "nullable": str(row["is_nullable"]).upper() == "YES",+ }+ for row in col_dicts+ ]++ # --- primary keys ---+ cursor = conn.cursor()+ try:+ cursor.execute(pk_query, params)+ pk_rows = cursor.fetchall()+ finally:+ cursor.close()++ primary_keys = [row[0] for row in pk_rows]++ finally:+ if already_connected and _prev_autocommit is not None:+ conn.autocommit = _prev_autocommit+ if not already_connected:+ self.connector.disconnect()++ self.logger.debug(+ "get_table_schema: %s — %d column(s), %d PK(s)",+ table_name,+ len(columns),+ len(primary_keys),+ )++ return {"columns": columns, "primary_keys": primary_keys}++ except (ValidationError, ProcessingError):+ raise+ except Exception as exc:+ self.logger.error(+ "Failed to get schema for %s: %s", table_name, type(exc).__name__+ )+ raise ProcessingError(+ f"Failed to get schema for Redshift table '{table_name}': "+ f"{type(exc).__name__}"+ ) from exc++ def list_tables(+ self,+ database: Optional[str] = None,+ schema: Optional[str] = None,+ ) -> List[str]:+ """Return the names of all base tables in the specified schema.++ Queries ``information_schema.tables`` filtering by+ ``table_type = 'BASE TABLE'`` so views are excluded, which matches+ the Snowflake connector's convention. The *schema* filter value is+ passed as a ``%s`` parameter; the optional *database* prefix is+ validated and escaped as an identifier before interpolation.++ Args:+ database: Override database name. Defaults to the connector's+ configured database.+ schema: Override schema name. Defaults to the ingestor's+ ``schema`` attribute (``"public"``).++ Returns:+ Sorted list of table-name strings, or an empty list when the+ schema contains no base tables.++ Raises:+ ValidationError: If *database* or *schema* fails identifier+ validation.+ ProcessingError: If the catalog query fails.+ """+ effective_schema = schema or self.schema+ effective_database = database or self.connector.database++ if effective_schema:+ _validate_sql_identifier(effective_schema, "schema")+ if effective_database:+ _validate_sql_identifier(effective_database, "database")++ if effective_database:+ info_schema = (+ f"{self._escape_identifier(effective_database)}.information_schema"+ )+ else:+ info_schema = "information_schema"++ query = f"""+ SELECT table_name+ FROM {info_schema}.tables+ WHERE table_schema = %s+ AND table_type = 'BASE TABLE'+ ORDER BY table_name+ """++ try:+ already_connected = self.connector.connection is not None+ conn = self.connector.connect()++ _prev_autocommit = conn.autocommit if already_connected else None+ try:+ conn.autocommit = True++ cursor = conn.cursor()+ try:+ cursor.execute(query, (effective_schema,))+ rows = cursor.fetchall()+ finally:+ cursor.close()++ finally:+ if already_connected and _prev_autocommit is not None:+ conn.autocommit = _prev_autocommit+ if not already_connected:+ self.connector.disconnect()++ tables = [row[0] for row in rows]+ self.logger.debug(+ "list_tables: %d table(s) in schema '%s'",+ len(tables),+ effective_schema,+ )+ return tables++ except (ValidationError, ProcessingError):+ raise+ except Exception as exc:+ self.logger.error(+ "Failed to list tables: %s", type(exc).__name__+ )+ raise ProcessingError(+ f"Failed to list Redshift tables: {type(exc).__name__}"+ ) from exc++ # ------------------------------------------------------------------+ # Document export+ # ------------------------------------------------------------------++ def export_as_documents(+ self,+ data: RedshiftData,+ id_field: str = "id",+ text_fields: Optional[List[str]] = None,+ ) -> List[Dict[str, Any]]:+ """Convert :class:`RedshiftData` to the Semantica document format.++ Produces the same ``{"id", "text", "metadata"}`` shape used by+ :class:`~semantica.ingest.snowflake_ingestor.SnowflakeIngestor` and+ :class:`~semantica.ingest.databricks_ingestor.DatabricksIngestor`,+ making :class:`RedshiftData` directly usable with+ :class:`~semantica.kg.graph_builder.GraphBuilder`.++ **ID resolution**+ ``str(row.get(id_field, idx))`` — uses the row-index integer as a+ deterministic fallback when the *id_field* key is absent, cast to+ a string. This matches the Snowflake/Databricks convention exactly.++ **Text composition**+ * When *text_fields* is provided: only those fields are used; each+ non-``None`` value is converted to a string and joined with a+ single space.+ * When *text_fields* is ``None``: only ``str``-typed values are+ joined (integers, floats, booleans, and ``None`` are excluded).+ This matches the Snowflake/Databricks default behavior.++ **Metadata**+ ``{"source": "redshift", "table": ..., "database": ...,+ "schema": ..., "row_data": <full cleaned row dict>}``++ Args:+ data: A :class:`RedshiftData` object returned by+ :meth:`ingest_table` or :meth:`ingest_query`.+ id_field: Row field to use as the document ``"id"``. Defaults+ to ``"id"``.+ text_fields: List of field names whose values form the document+ ``"text"``. When ``None`` all string-typed field values are+ joined.++ Returns:+ List of document dictionaries::++ [+ {+ "id": str,+ "text": str,+ "metadata": {+ "source": "redshift",+ "table": str | None,+ "database": str | None,+ "schema": str | None,+ "row_data": dict,+ },+ },+ ...+ ]+ """+ documents: List[Dict[str, Any]] = []++ for idx, row in enumerate(data.data):+ doc: Dict[str, Any] = {+ "id": str(row.get(id_field, idx)),+ "metadata": {+ "source": "redshift",+ "table": data.table_name,+ "database": data.database,+ "schema": data.schema,+ },+ }++ if text_fields:+ text_parts = [+ str(row[f])+ for f in text_fields+ if f in row and row[f] is not None+ ]+ else:+ text_parts = [+ value+ for value in row.values()+ if isinstance(value, str)+ ]++ doc["text"] = " ".join(text_parts)+ doc["metadata"]["row_data"] = row++ documents.append(doc)++ self.logger.debug(+ "export_as_documents: exported %d document(s)", len(documents)+ )+ return documents++ # ------------------------------------------------------------------+ # Internal helpers+ # ------------------------------------------------------------------++ @staticmethod+ def _disambiguate_columns(columns: List[str]) -> List[str]:+ """Return a new column-name list with duplicate labels made unique.++ When the cursor reports duplicate column labels (e.g. from a JOIN+ that selects the same column name from two tables), a plain+ ``dict(zip(columns, row))`` silently drops every value except the+ last one for each repeated key.++ This method appends a ``_1``, ``_2``, … suffix to each duplicate+ occurrence (the *first* occurrence keeps the original name) so that+ all values are retained in the resulting dict.++ Args:+ columns: Raw column-name list from ``cursor.description``.++ Returns:+ A new list of the same length where every name is unique.+ """+ seen: Dict[str, int] = {}+ result: List[str] = []+ for name in columns:+ if name not in seen:+ seen[name] = 0+ result.append(name)+ else:+ seen[name] += 1+ result.append(f"{name}_{seen[name]}")+ return result++ def _make_row_dicts(+ self,+ columns: List[str],+ rows: List[Any],+ ) -> List[Dict[str, Any]]:+ """Zip *rows* (tuples from the cursor) with *columns* into dicts.++ Duplicate column labels are disambiguated before zipping so that no+ value is silently discarded. The returned column list (already+ stored in ``RedshiftData.columns``) matches the disambiguated names.++ Args:+ columns: Column-name list, already disambiguated via+ :meth:`_disambiguate_columns`.+ rows: Raw cursor rows (each a tuple of values).++ Returns:+ List of ``{column_name: value}`` dicts.+ """+ return [dict(zip(columns, row)) for row in rows]++ def _fetch_all(+ self, cursor: Any+ ) -> Tuple[List[str], List[Dict[str, Any]]]:+ """Fetch all rows from an already-executed cursor.++ Extracts column names from ``cursor.description``, fetches all+ rows as tuples, zips them into dicts, then applies+ :meth:`_convert_rows`.++ Args:+ cursor: An executed ``redshift_connector`` cursor.++ Returns:+ A ``(columns, data)`` tuple where *columns* is the ordered+ list of column-name strings and *data* is the list of+ converted row dictionaries.+ """+ columns = (+ self._disambiguate_columns(+ [desc[0] for desc in cursor.description]+ )+ if cursor.description+ else []+ )+ raw_rows = cursor.fetchall()+ row_dicts = self._make_row_dicts(columns, raw_rows)+ return columns, self._convert_rows(row_dicts)++ def _convert_rows(+ self, rows: List[Dict[str, Any]]+ ) -> List[Dict[str, Any]]:+ """Convert row dicts to JSON-serialisable format.++ Applies the same transformations used by the Databricks and+ Snowflake ingestors:++ * ``datetime`` / date-like values → ISO-8601 string via+ ``.isoformat()``.+ * ``bytes`` → UTF-8 string; falls back to ``str()`` when the+ bytes are not valid UTF-8.+ * All other scalar types (``int``, ``float``, ``str``,+ ``bool``, ``None``, ``Decimal``) are passed through unchanged+ so downstream code can convert them as needed.++ Args:+ rows: List of row dictionaries produced by+ ``dict(zip(columns, row))``.++ Returns:+ New list of row dictionaries with type conversions applied.+ """+ converted = []+ for row in rows:+ converted_row: Dict[str, Any] = {}+ for key, value in row.items():+ if isinstance(value, datetime):+ converted_row[key] = value.isoformat()+ elif isinstance(value, bytes):+ try:+ converted_row[key] = value.decode("utf-8")+ except UnicodeDecodeError:+ converted_row[key] = str(value)+ else:+ converted_row[key] = value+ converted.append(converted_row)+ return converteddiff --git a/tests/ingest/test_optional_imports.py b/tests/ingest/test_optional_imports.pyindex 9a9a6169..76da6cc4 100644--- a/tests/ingest/test_optional_imports.py+++ b/tests/ingest/test_optional_imports.py@@ -272,3 +272,78 @@ else:assert "ConfigurationError" in result.stdoutassert "Salesforce ingestion" in result.stdoutassert "simple-salesforce" in result.stdout+++def test_redshift_package_imports_without_sdk() -> None:+ """``import semantica.ingest`` must not eagerly pull in redshift_connector."""+ result = _run_python_with_blocked_modules(+ """+from semantica.ingest import FileIngestor, ingest_file+print(FileIngestor.__name__, callable(ingest_file))+""",+ ("redshift_connector",),+ )++ assert result.returncode == 0, result.stderr+ assert "FileIngestor True" in result.stdout+++def test_redshift_data_importable_without_sdk() -> None:+ """``RedshiftData`` is a plain dataclass — no SDK needed to import it."""+ result = _run_python_with_blocked_modules(+ """+from semantica.ingest import RedshiftData+print(RedshiftData.__name__)+""",+ ("redshift_connector",),+ )++ assert result.returncode == 0, result.stderr+ assert "RedshiftData" in result.stdout+++def test_redshift_ingestor_probe_fails_without_sdk() -> None:+ """``from semantica.ingest import RedshiftIngestor`` must raise ``ImportError``+ when ``redshift_connector`` is absent."""+ result = _run_python_with_blocked_modules(+ """+try:+ from semantica.ingest import RedshiftIngestor+ has_redshift = True+except ImportError:+ has_redshift = False++assert not has_redshift, (+ "Expected RedshiftIngestor import to fail without redshift-connector"+)+print("RedshiftIngestor probe passed")+""",+ ("redshift_connector",),+ )++ assert result.returncode == 0, result.stderr+ assert "RedshiftIngestor probe passed" in result.stdout+++def test_redshift_connector_reports_missing_dep_with_install_hint() -> None:+ """Constructing ``RedshiftConnector`` without the SDK must raise ``ImportError``+ with a message that names the ``db-redshift`` extra."""+ result = _run_python_with_blocked_modules(+ """+from semantica.ingest.redshift_ingestor import RedshiftConnector++try:+ RedshiftConnector()+except ImportError as exc:+ print(type(exc).__name__, exc)+else:+ raise SystemExit(+ "expected RedshiftConnector() to fail without redshift-connector"+ )+""",+ ("redshift_connector",),+ )++ assert result.returncode == 0, result.stderr+ assert "ImportError" in result.stdout+ assert "db-redshift" in result.stdoutdiff --git a/tests/test_redshift_ingestor.py b/tests/test_redshift_ingestor.pynew file mode 100644index 00000000..7211d190--- /dev/null+++ b/tests/test_redshift_ingestor.py@@ -0,0 +1,3484 @@+"""+Unit tests for RedshiftConnector and RedshiftIngestor.++All redshift_connector API calls are mocked — no real AWS or Redshift+connection is made.++Test structure mirrors tests/test_snowflake_ingestor.py and+tests/test_salesforce_ingestor.py:+ - autouse fixture injects a ``redshift_connector`` stub when the SDK+ is not installed, so the test suite runs in both environments.+ - Each test that needs the connector to appear available patches+ ``REDSHIFT_AVAILABLE`` to ``True`` and ``_redshift_connector`` to+ a ``MagicMock``.+ - Credentials are always supplied so secret values are never embedded+ in assertions.+"""++from __future__ import annotations++import os+from datetime import datetime+from unittest.mock import MagicMock, Mock, call, patch++import pytest++# ---------------------------------------------------------------------------+# Optional SDK detection+# ---------------------------------------------------------------------------+try:+ import redshift_connector as _sdk # noqa: F401++ REDSHIFT_SDK_AVAILABLE = True+except ImportError:+ REDSHIFT_SDK_AVAILABLE = False+++# ---------------------------------------------------------------------------+# autouse fixture — inject stub when SDK is absent+# ---------------------------------------------------------------------------++[email protected](autouse=True)+def _mock_redshift_sdk_if_needed():+ """Inject a minimal redshift_connector stub when the SDK is not installed."""+ if not REDSHIFT_SDK_AVAILABLE:+ stub = MagicMock()+ # Provide a real exception hierarchy so isinstance/except clauses work.+ class _Error(Exception):+ pass++ class _InterfaceError(_Error):+ pass++ class _ProgrammingError(_Error):+ pass++ stub.Error = _Error+ stub.InterfaceError = _InterfaceError+ stub.ProgrammingError = _ProgrammingError+ stub.connect = MagicMock()++ with patch.dict(+ "sys.modules",+ {"redshift_connector": stub},+ ):+ yield+ else:+ yield+++# ---------------------------------------------------------------------------+# Helpers+# ---------------------------------------------------------------------------+++def _make_mock_connection():+ """Return a mock that behaves like a redshift_connector connection."""+ mock_conn = Mock()+ mock_cursor = Mock()+ mock_cursor.execute = Mock()+ mock_cursor.fetchone = Mock(return_value=(1,))+ mock_cursor.fetchall = Mock(return_value=[])+ mock_cursor.description = [("?column?",)]+ mock_cursor.close = Mock()+ mock_conn.cursor = Mock(return_value=mock_cursor)+ mock_conn.close = Mock()+ return mock_conn, mock_cursor+++# ---------------------------------------------------------------------------+# TestRedshiftData+# ---------------------------------------------------------------------------+++class TestRedshiftData:+ """RedshiftData is a plain dataclass — no SDK dependency."""++ def test_creation_with_required_fields(self):+ from semantica.ingest.redshift_ingestor import RedshiftData++ data = RedshiftData(data=[], row_count=0, columns=[])++ assert data.row_count == 0+ assert data.data == []+ assert data.columns == []+ assert data.table_name is None+ assert data.query is None+ assert data.database is None+ assert data.schema is None+ assert data.metadata == {}+ assert isinstance(data.ingested_at, datetime)++ def test_creation_with_all_fields(self):+ from semantica.ingest.redshift_ingestor import RedshiftData++ rows = [{"id": 1, "name": "Alice"}]+ data = RedshiftData(+ data=rows,+ row_count=1,+ columns=["id", "name"],+ table_name="users",+ query="SELECT id, name FROM users",+ database="dev",+ schema="public",+ metadata={"custom": "value"},+ )++ assert data.row_count == 1+ assert data.table_name == "users"+ assert data.query == "SELECT id, name FROM users"+ assert data.database == "dev"+ assert data.schema == "public"+ assert data.metadata["custom"] == "value"++ def test_importable_without_sdk(self):+ """RedshiftData must import even when redshift_connector is absent."""+ # This test intentionally does NOT patch REDSHIFT_AVAILABLE —+ # it relies on the autouse stub (or the real SDK) already in place.+ from semantica.ingest.redshift_ingestor import RedshiftData++ data = RedshiftData(data=[{"col": 1}], row_count=1, columns=["col"])+ assert data.row_count == 1+++# ---------------------------------------------------------------------------+# TestRedshiftConnectorInit — password auth+# ---------------------------------------------------------------------------+++class TestRedshiftConnectorInitPassword:+ """Constructor validation for password / native authentication."""++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_init_explicit_credentials_stored(self):+ from semantica.ingest.redshift_ingestor import RedshiftConnector++ conn = RedshiftConnector(+ host="cluster.example.com",+ database="dev",+ user="awsuser",+ password="s3cr3t",+ )++ assert conn.host == "cluster.example.com"+ assert conn.database == "dev"+ assert conn.user == "awsuser"+ assert conn._password == "s3cr3t"+ assert conn.port == 5439 # default+ assert conn.ssl is True # default+ assert conn.iam is False+ assert conn._connection is None++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_init_explicit_port_overrides_default(self):+ from semantica.ingest.redshift_ingestor import RedshiftConnector++ conn = RedshiftConnector(+ host="cluster.example.com",+ database="dev",+ user="awsuser",+ password="s3cr3t",+ port=5450,+ )++ assert conn.port == 5450++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_init_ssl_false_stored(self):+ from semantica.ingest.redshift_ingestor import RedshiftConnector++ conn = RedshiftConnector(+ host="cluster.example.com",+ database="dev",+ user="awsuser",+ password="s3cr3t",+ ssl=False,+ )++ assert conn.ssl is False++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_init_timeout_stored(self):+ from semantica.ingest.redshift_ingestor import RedshiftConnector++ conn = RedshiftConnector(+ host="cluster.example.com",+ database="dev",+ user="awsuser",+ password="s3cr3t",+ timeout=30,+ )++ assert conn.timeout == 30+++# ---------------------------------------------------------------------------+# TestRedshiftConnectorInitEnvVars+# ---------------------------------------------------------------------------+++class TestRedshiftConnectorInitEnvVars:+ """Environment-variable fallbacks for all credential parameters."""++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_password_auth_from_env_vars(self):+ from semantica.ingest.redshift_ingestor import RedshiftConnector++ env = {+ "REDSHIFT_HOST": "env-cluster.example.com",+ "REDSHIFT_DATABASE": "env_db",+ "REDSHIFT_USER": "env_user",+ "REDSHIFT_PASSWORD": "env_pass",+ }+ with patch.dict(os.environ, env):+ conn = RedshiftConnector()++ assert conn.host == "env-cluster.example.com"+ assert conn.database == "env_db"+ assert conn.user == "env_user"+ assert conn._password == "env_pass"++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_port_from_env_var(self):+ from semantica.ingest.redshift_ingestor import RedshiftConnector++ env = {+ "REDSHIFT_HOST": "cluster.example.com",+ "REDSHIFT_DATABASE": "dev",+ "REDSHIFT_USER": "u",+ "REDSHIFT_PASSWORD": "p",+ "REDSHIFT_PORT": "5450",+ }+ with patch.dict(os.environ, env):+ conn = RedshiftConnector()++ assert conn.port == 5450++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_iam_params_from_env_vars(self):+ from semantica.ingest.redshift_ingestor import RedshiftConnector++ env = {+ "REDSHIFT_HOST": "cluster.example.com",+ "REDSHIFT_DATABASE": "dev",+ "REDSHIFT_DB_USER": "env_db_user",+ "REDSHIFT_CLUSTER_IDENTIFIER": "env-cluster-id",+ "REDSHIFT_REGION": "us-west-2",+ "REDSHIFT_PROFILE": "env-profile",+ "REDSHIFT_ACCESS_KEY_ID": "env_key",+ "REDSHIFT_SECRET_ACCESS_KEY": "env_secret",+ "REDSHIFT_SESSION_TOKEN": "env_token",+ }+ with patch.dict(os.environ, env):+ conn = RedshiftConnector(iam=True)++ assert conn.db_user == "env_db_user"+ assert conn.cluster_identifier == "env-cluster-id"+ assert conn.region == "us-west-2"+ assert conn.profile == "env-profile"+ assert conn._access_key_id == "env_key"+ assert conn._secret_access_key == "env_secret"+ assert conn._session_token == "env_token"++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_explicit_arg_overrides_env_var(self):+ from semantica.ingest.redshift_ingestor import RedshiftConnector++ env = {+ "REDSHIFT_HOST": "env-host.example.com",+ "REDSHIFT_DATABASE": "env_db",+ "REDSHIFT_USER": "env_user",+ "REDSHIFT_PASSWORD": "env_pass",+ }+ with patch.dict(os.environ, env):+ conn = RedshiftConnector(+ host="explicit-host.example.com",+ database="explicit_db",+ user="explicit_user",+ password="explicit_pass",+ )++ assert conn.host == "explicit-host.example.com"+ assert conn.database == "explicit_db"+ assert conn.user == "explicit_user"+ assert conn._password == "explicit_pass"++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_invalid_port_env_var_raises_validation_error(self):+ from semantica.ingest.redshift_ingestor import RedshiftConnector+ from semantica.utils.exceptions import ValidationError++ env = {+ "REDSHIFT_HOST": "cluster.example.com",+ "REDSHIFT_DATABASE": "dev",+ "REDSHIFT_USER": "u",+ "REDSHIFT_PASSWORD": "p",+ "REDSHIFT_PORT": "not-a-number",+ }+ with patch.dict(os.environ, env):+ with pytest.raises(ValidationError, match="REDSHIFT_PORT"):+ RedshiftConnector()+++# ---------------------------------------------------------------------------+# TestRedshiftConnectorValidation — missing credentials+# ---------------------------------------------------------------------------+++class TestRedshiftConnectorValidation:+ """ValidationError is raised for incomplete credentials."""++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_missing_host_raises(self):+ from semantica.ingest.redshift_ingestor import RedshiftConnector+ from semantica.utils.exceptions import ValidationError++ with pytest.raises(ValidationError, match="host"):+ RedshiftConnector(database="dev", user="u", password="p")++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_missing_database_raises(self):+ from semantica.ingest.redshift_ingestor import RedshiftConnector+ from semantica.utils.exceptions import ValidationError++ with pytest.raises(ValidationError, match="database"):+ RedshiftConnector(host="h", user="u", password="p")++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_password_mode_missing_user_raises(self):+ from semantica.ingest.redshift_ingestor import RedshiftConnector+ from semantica.utils.exceptions import ValidationError++ with pytest.raises(ValidationError, match="user"):+ RedshiftConnector(host="h", database="d", password="p")++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_password_mode_missing_password_raises(self):+ from semantica.ingest.redshift_ingestor import RedshiftConnector+ from semantica.utils.exceptions import ValidationError++ with pytest.raises(ValidationError, match="password"):+ RedshiftConnector(host="h", database="d", user="u")++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_iam_mode_missing_db_user_raises(self):+ from semantica.ingest.redshift_ingestor import RedshiftConnector+ from semantica.utils.exceptions import ValidationError++ with pytest.raises(ValidationError, match="db_user"):+ RedshiftConnector(+ host="h",+ database="d",+ iam=True,+ cluster_identifier="c",+ )++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_iam_mode_missing_cluster_identifier_raises(self):+ from semantica.ingest.redshift_ingestor import RedshiftConnector+ from semantica.utils.exceptions import ValidationError++ with pytest.raises(ValidationError, match="cluster_identifier"):+ RedshiftConnector(+ host="h",+ database="d",+ iam=True,+ db_user="awsuser",+ )++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_missing_sdk_raises_import_error_with_hint(self):+ from semantica.ingest.redshift_ingestor import RedshiftConnector++ with patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", False):+ with pytest.raises(ImportError) as exc_info:+ RedshiftConnector(+ host="h", database="d", user="u", password="p"+ )++ msg = str(exc_info.value)+ assert "redshift-connector" in msg+ assert "db-redshift" in msg++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_validation_error_message_does_not_contain_password(self):+ """ValidationError messages must never echo secret values."""+ from semantica.ingest.redshift_ingestor import RedshiftConnector+ from semantica.utils.exceptions import ValidationError++ with pytest.raises(ValidationError) as exc_info:+ # Missing host — but provide a "password" to confirm it's not leaked.+ RedshiftConnector(database="d", user="u", password="super_s3cr3t")++ assert "super_s3cr3t" not in str(exc_info.value)++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_iam_and_password_modes_do_not_conflict(self):+ """iam=True with user/password set does not raise; IAM mode takes over."""+ from semantica.ingest.redshift_ingestor import RedshiftConnector++ # Providing user/password alongside iam=True is harmless;+ # _build_connect_kwargs ignores them when iam=True.+ conn = RedshiftConnector(+ host="h",+ database="d",+ iam=True,+ db_user="awsuser",+ cluster_identifier="c",+ user="ignored",+ password="ignored",+ )+ assert conn.iam is True+ # Verify build kwargs uses IAM path+ kwargs = conn._build_connect_kwargs()+ assert kwargs["iam"] is True+ assert kwargs["user"] == ""+ assert kwargs["password"] == ""+ assert kwargs["db_user"] == "awsuser"+++# ---------------------------------------------------------------------------+# TestRedshiftConnectorConnect — password auth+# ---------------------------------------------------------------------------+++class TestRedshiftConnectorConnectPassword:+ """connect() for password authentication."""++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_connect_calls_sdk_with_correct_kwargs(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftConnector++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn++ conn = RedshiftConnector(+ host="cluster.example.com",+ database="dev",+ user="awsuser",+ password="s3cr3t",+ )+ result = conn.connect()++ assert result is mock_conn+ mock_sdk.connect.assert_called_once()++ call_kwargs = mock_sdk.connect.call_args[1]+ assert call_kwargs["host"] == "cluster.example.com"+ assert call_kwargs["database"] == "dev"+ assert call_kwargs["user"] == "awsuser"+ assert call_kwargs["password"] == "s3cr3t"+ assert call_kwargs["port"] == 5439+ assert call_kwargs["ssl"] is True+ assert "iam" not in call_kwargs++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_connect_passes_custom_port(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftConnector++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn++ conn = RedshiftConnector(+ host="h", database="d", user="u", password="p", port=5450+ )+ conn.connect()++ assert mock_sdk.connect.call_args[1]["port"] == 5450++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_connect_passes_ssl_false(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftConnector++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn++ conn = RedshiftConnector(+ host="h", database="d", user="u", password="p", ssl=False+ )+ conn.connect()++ assert mock_sdk.connect.call_args[1]["ssl"] is False++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_connect_passes_timeout(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftConnector++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn++ conn = RedshiftConnector(+ host="h", database="d", user="u", password="p", timeout=30+ )+ conn.connect()++ assert mock_sdk.connect.call_args[1]["timeout"] == 30++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_connect_stores_connection_reference(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftConnector++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn++ connector = RedshiftConnector(+ host="h", database="d", user="u", password="p"+ )+ assert connector.connection is None+ connector.connect()+ assert connector.connection is mock_conn++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_connect_reuses_existing_connection(self, mock_sdk):+ """Second connect() call returns the same connection without re-calling SDK."""+ from semantica.ingest.redshift_ingestor import RedshiftConnector++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn++ connector = RedshiftConnector(+ host="h", database="d", user="u", password="p"+ )+ first = connector.connect()+ second = connector.connect()++ assert first is second+ mock_sdk.connect.assert_called_once()++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_connect_failure_raises_processing_error(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftConnector+ from semantica.utils.exceptions import ProcessingError++ mock_sdk.connect.side_effect = Exception("connection refused")++ connector = RedshiftConnector(+ host="h", database="d", user="u", password="p"+ )+ with pytest.raises(ProcessingError):+ connector.connect()++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_connect_failure_leaves_connection_none(self, mock_sdk):+ """A failed connect() must not leave a partial connection reference."""+ from semantica.ingest.redshift_ingestor import RedshiftConnector+ from semantica.utils.exceptions import ProcessingError++ mock_sdk.connect.side_effect = RuntimeError("auth failed")++ connector = RedshiftConnector(+ host="h", database="d", user="u", password="p"+ )+ with pytest.raises(ProcessingError):+ connector.connect()++ assert connector.connection is None++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_connect_error_message_does_not_contain_password(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftConnector+ from semantica.utils.exceptions import ProcessingError++ mock_sdk.connect.side_effect = Exception("some sdk error")++ connector = RedshiftConnector(+ host="h", database="d", user="u", password="hunter2"+ )+ with pytest.raises(ProcessingError) as exc_info:+ connector.connect()++ assert "hunter2" not in str(exc_info.value)+++# ---------------------------------------------------------------------------+# TestRedshiftConnectorConnect — IAM auth+# ---------------------------------------------------------------------------+++class TestRedshiftConnectorConnectIAM:+ """connect() for IAM-role authentication."""++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_iam_connect_calls_sdk_with_iam_true(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftConnector++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn++ conn = RedshiftConnector(+ host="cluster.example.com",+ database="dev",+ iam=True,+ db_user="awsuser",+ cluster_identifier="my-cluster",+ profile="default",+ )+ conn.connect()++ kwargs = mock_sdk.connect.call_args[1]+ assert kwargs["iam"] is True+ assert kwargs["db_user"] == "awsuser"+ assert kwargs["cluster_identifier"] == "my-cluster"+ assert kwargs["profile"] == "default"++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_iam_connect_passes_empty_user_and_password(self, mock_sdk):+ """SDK requires user='' and password='' when iam=True."""+ from semantica.ingest.redshift_ingestor import RedshiftConnector++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn++ conn = RedshiftConnector(+ host="h",+ database="d",+ iam=True,+ db_user="awsuser",+ cluster_identifier="c",+ profile="default",+ )+ conn.connect()++ kwargs = mock_sdk.connect.call_args[1]+ assert kwargs["user"] == ""+ assert kwargs["password"] == ""++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_iam_connect_with_explicit_credentials(self, mock_sdk):+ """Explicit AWS credentials are forwarded as access_key_id / secret_access_key."""+ from semantica.ingest.redshift_ingestor import RedshiftConnector++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn++ conn = RedshiftConnector(+ host="h",+ database="d",+ iam=True,+ db_user="awsuser",+ cluster_identifier="c",+ region="us-east-1",+ access_key_id="AKIAIOSFODNN7EXAMPLE",+ secret_access_key="wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",+ session_token="AQoDYXdzEJr//example",+ )+ conn.connect()++ kwargs = mock_sdk.connect.call_args[1]+ assert kwargs["access_key_id"] == "AKIAIOSFODNN7EXAMPLE"+ assert kwargs["secret_access_key"] == "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"+ assert kwargs["session_token"] == "AQoDYXdzEJr//example"+ assert kwargs["region"] == "us-east-1"++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_iam_connect_without_explicit_aws_creds_omits_those_kwargs(self, mock_sdk):+ """When no explicit AWS credentials are given, optional IAM kwargs+ are absent from the SDK call — the SDK resolves them from its own+ credential chain (env vars, instance profile, etc.)."""+ from semantica.ingest.redshift_ingestor import RedshiftConnector++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn++ conn = RedshiftConnector(+ host="h",+ database="d",+ iam=True,+ db_user="awsuser",+ cluster_identifier="c",+ )+ conn.connect()++ kwargs = mock_sdk.connect.call_args[1]+ assert "access_key_id" not in kwargs+ assert "secret_access_key" not in kwargs+ assert "session_token" not in kwargs+ assert "profile" not in kwargs+ assert "region" not in kwargs++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_iam_connect_profile_based(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftConnector++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn++ conn = RedshiftConnector(+ host="h",+ database="d",+ iam=True,+ db_user="awsuser",+ cluster_identifier="c",+ profile="my-profile",+ )+ conn.connect()++ kwargs = mock_sdk.connect.call_args[1]+ assert kwargs["profile"] == "my-profile"+ assert "access_key_id" not in kwargs++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_iam_does_not_include_plain_password_in_sdk_call(self, mock_sdk):+ """The real DB password must not appear in the SDK call for IAM auth."""+ from semantica.ingest.redshift_ingestor import RedshiftConnector++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn++ conn = RedshiftConnector(+ host="h",+ database="d",+ iam=True,+ db_user="awsuser",+ cluster_identifier="c",+ profile="default",+ )+ conn.connect()++ kwargs = mock_sdk.connect.call_args[1]+ # password kwarg must be empty string (SDK requirement), not a real secret+ assert kwargs["password"] == ""+++# ---------------------------------------------------------------------------+# TestRedshiftConnectorDisconnect+# ---------------------------------------------------------------------------+++class TestRedshiftConnectorDisconnect:+ """disconnect() lifecycle behaviour."""++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_disconnect_closes_connection_and_clears_reference(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftConnector++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn++ connector = RedshiftConnector(+ host="h", database="d", user="u", password="p"+ )+ connector.connect()+ assert connector.connection is mock_conn++ connector.disconnect()++ mock_conn.close.assert_called_once()+ assert connector.connection is None++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_disconnect_without_connect_is_safe(self):+ from semantica.ingest.redshift_ingestor import RedshiftConnector++ connector = RedshiftConnector(+ host="h", database="d", user="u", password="p"+ )+ connector.disconnect() # must not raise+ assert connector.connection is None++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_disconnect_is_idempotent(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftConnector++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn++ connector = RedshiftConnector(+ host="h", database="d", user="u", password="p"+ )+ connector.connect()+ connector.disconnect()+ connector.disconnect() # second call — must not raise++ assert connector.connection is None++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_disconnect_clears_reference_even_if_close_raises(self, mock_sdk):+ """Connection reference is set to None even when conn.close() raises."""+ from semantica.ingest.redshift_ingestor import RedshiftConnector++ mock_conn, _ = _make_mock_connection()+ mock_conn.close.side_effect = RuntimeError("already closed")+ mock_sdk.connect.return_value = mock_conn++ connector = RedshiftConnector(+ host="h", database="d", user="u", password="p"+ )+ connector.connect()+ connector.disconnect() # must not raise despite conn.close() failing++ assert connector.connection is None+++# ---------------------------------------------------------------------------+# TestRedshiftConnectorTestConnection+# ---------------------------------------------------------------------------+++class TestRedshiftConnectorTestConnection:+ """test_connection() behaviour."""++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_test_connection_success_returns_true(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftConnector++ mock_conn, mock_cursor = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn++ connector = RedshiftConnector(+ host="h", database="d", user="u", password="p"+ )+ result = connector.test_connection()++ assert result is True+ mock_cursor.execute.assert_called_once_with("SELECT 1")+ mock_cursor.fetchone.assert_called_once()++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_test_connection_closes_transient_connection(self, mock_sdk):+ """test_connection() closes the connection it opens (no leak)."""+ from semantica.ingest.redshift_ingestor import RedshiftConnector++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn++ connector = RedshiftConnector(+ host="h", database="d", user="u", password="p"+ )+ connector.test_connection()++ mock_conn.close.assert_called_once()+ assert connector.connection is None++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_test_connection_failure_returns_false(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftConnector++ mock_sdk.connect.side_effect = Exception("connection refused")++ connector = RedshiftConnector(+ host="h", database="d", user="u", password="p"+ )+ result = connector.test_connection()++ assert result is False++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_test_connection_query_failure_returns_false(self, mock_sdk):+ """Returns False when SELECT 1 raises after a successful connect()."""+ from semantica.ingest.redshift_ingestor import RedshiftConnector++ mock_conn, mock_cursor = _make_mock_connection()+ mock_cursor.execute.side_effect = Exception("query failed")+ mock_sdk.connect.return_value = mock_conn++ connector = RedshiftConnector(+ host="h", database="d", user="u", password="p"+ )+ result = connector.test_connection()++ assert result is False++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_test_connection_does_not_close_pre_existing_connection(self, mock_sdk):+ """test_connection() must not close a connection opened before the call."""+ from semantica.ingest.redshift_ingestor import RedshiftConnector++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn++ connector = RedshiftConnector(+ host="h", database="d", user="u", password="p"+ )+ connector.connect() # open before test_connection+ assert connector.connection is mock_conn++ connector.test_connection()++ # Connection opened externally must still be alive.+ assert connector.connection is mock_conn+ mock_conn.close.assert_not_called()+++# ---------------------------------------------------------------------------+# TestRedshiftConnectorSecurity+# ---------------------------------------------------------------------------+++class TestRedshiftConnectorSecurity:+ """Verify that secret values are never exposed in repr or exceptions."""++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_password_not_in_repr(self):+ from semantica.ingest.redshift_ingestor import RedshiftConnector++ conn = RedshiftConnector(+ host="h", database="d", user="u", password="my_secret_password"+ )+ assert "my_secret_password" not in repr(conn)++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_secret_access_key_not_in_repr(self):+ from semantica.ingest.redshift_ingestor import RedshiftConnector++ conn = RedshiftConnector(+ host="h",+ database="d",+ iam=True,+ db_user="awsuser",+ cluster_identifier="c",+ access_key_id="AKIAIOSFODNN7EXAMPLE",+ secret_access_key="TOP_SECRET_KEY",+ )+ assert "TOP_SECRET_KEY" not in repr(conn)++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_connect_error_does_not_expose_password(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftConnector+ from semantica.utils.exceptions import ProcessingError++ mock_sdk.connect.side_effect = Exception("auth failure")++ conn = RedshiftConnector(+ host="h", database="d", user="u", password="hunter2"+ )+ with pytest.raises(ProcessingError) as exc_info:+ conn.connect()++ assert "hunter2" not in str(exc_info.value)++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_connect_error_does_not_expose_secret_key(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftConnector+ from semantica.utils.exceptions import ProcessingError++ mock_sdk.connect.side_effect = Exception("iam failure")++ conn = RedshiftConnector(+ host="h",+ database="d",+ iam=True,+ db_user="awsuser",+ cluster_identifier="c",+ secret_access_key="SUPER_SECRET_AWS_KEY",+ )+ with pytest.raises(ProcessingError) as exc_info:+ conn.connect()++ assert "SUPER_SECRET_AWS_KEY" not in str(exc_info.value)+++# ---------------------------------------------------------------------------+# TestRedshiftIngestor+# ---------------------------------------------------------------------------+++class TestRedshiftIngestor:+ """Tests for the RedshiftIngestor wrapper."""++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_ingestor_creates_connector(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftConnector, RedshiftIngestor++ ingestor = RedshiftIngestor(+ host="h", database="d", user="u", password="p"+ )++ assert isinstance(ingestor.connector, RedshiftConnector)+ assert ingestor.connector.host == "h"++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_ingestor_accepts_existing_connector(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftConnector, RedshiftIngestor++ connector = RedshiftConnector(+ host="h", database="d", user="u", password="p"+ )+ ingestor = RedshiftIngestor(connector=connector)++ assert ingestor.connector is connector++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_ingestor_missing_credentials_raises_validation_error(self):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor+ from semantica.utils.exceptions import ValidationError++ with pytest.raises(ValidationError):+ RedshiftIngestor(host="h", database="d") # missing user+password++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_ingestor_missing_sdk_raises_import_error(self):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ with patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", False):+ with pytest.raises(ImportError) as exc_info:+ RedshiftIngestor(host="h", database="d", user="u", password="p")++ assert "db-redshift" in str(exc_info.value)++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_context_manager_connects_and_disconnects(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn++ with RedshiftIngestor(host="h", database="d", user="u", password="p") as ing:+ assert ing.connector.connection is mock_conn++ assert ing.connector.connection is None+ mock_conn.close.assert_called_once()++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_context_manager_closes_on_exception(self, mock_sdk):+ """__exit__ closes the connection even when an exception is raised."""+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn++ with pytest.raises(ValueError):+ with RedshiftIngestor(+ host="h", database="d", user="u", password="p"+ ) as ing:+ raise ValueError("something went wrong")++ assert ing.connector.connection is None++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_close_delegates_to_connector(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn++ ingestor = RedshiftIngestor(host="h", database="d", user="u", password="p")+ ingestor.connector.connect()+ ingestor.close()++ assert ingestor.connector.connection is None++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_schema_default_is_public(self):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ ingestor = RedshiftIngestor(+ host="h", database="d", user="u", password="p"+ )+ assert ingestor.schema == "public"++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_schema_custom_value_stored(self):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ ingestor = RedshiftIngestor(+ host="h", database="d", user="u", password="p", schema="analytics"+ )+ assert ingestor.schema == "analytics"+++# ---------------------------------------------------------------------------+# TestPublicAPIInSemanticaIngest+# ---------------------------------------------------------------------------+++class TestPublicAPIInSemanticaIngest:+ """Verify the lazy-import wiring in semantica.ingest is intact."""++ def test_all_contains_redshift_names(self):+ """All three Redshift symbols appear in semantica.ingest.__all__."""+ import semantica.ingest as pkg++ for name in ("RedshiftIngestor", "RedshiftConnector", "RedshiftData"):+ assert name in pkg.__all__, f"{name} missing from __all__"++ def test_redshift_data_importable_via_package(self):+ """RedshiftData is importable from semantica.ingest without the SDK."""+ from semantica.ingest import RedshiftData++ data = RedshiftData(data=[], row_count=0, columns=[])+ assert data.row_count == 0+++# ===========================================================================+# Stage 3 — ingest_table, ingest_query, _convert_rows, connection lifecycle+# ===========================================================================++# ---------------------------------------------------------------------------+# Shared helpers for Stage 3+# ---------------------------------------------------------------------------+++def _make_cursor_with_rows(columns, rows):+ """Return a mock cursor pre-loaded with *columns* and *rows*.++ ``cursor.description`` follows the DB-API 7-tuple convention where+ ``description[i][0]`` is the column name. ``cursor.fetchall`` returns+ the raw tuples exactly as ``redshift_connector`` does (NOT dicts).+ ``cursor.fetchmany(n)`` returns successive slices until exhausted.+ """+ cursor = Mock()+ cursor.description = [(col,) + (None,) * 6 for col in columns]++ # fetchall returns all rows as tuples+ cursor.fetchall = Mock(return_value=list(rows))++ # fetchmany returns successive batches then []+ _remaining = list(rows)++ def _fetchmany(n):+ nonlocal _remaining+ batch = _remaining[:n]+ _remaining = _remaining[n:]+ return batch++ cursor.fetchmany = Mock(side_effect=_fetchmany)+ cursor.execute = Mock()+ cursor.close = Mock()+ return cursor+++# ---------------------------------------------------------------------------+# TestEscapeIdentifier / TestBuildTableRef+# ---------------------------------------------------------------------------+++class TestEscapeIdentifier:+ """Unit tests for _escape_identifier and _build_table_ref."""++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_escape_plain_identifier(self):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ assert RedshiftIngestor._escape_identifier("orders") == '"orders"'++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_escape_identifier_with_internal_quote(self):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ assert RedshiftIngestor._escape_identifier('my"table') == '"my""table"'++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_build_table_ref_table_only(self):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ ref = RedshiftIngestor._build_table_ref("orders", None, None)+ assert ref == '"orders"'++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_build_table_ref_schema_and_table(self):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ ref = RedshiftIngestor._build_table_ref("orders", "sales", None)+ assert ref == '"sales"."orders"'++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_build_table_ref_database_schema_table(self):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ ref = RedshiftIngestor._build_table_ref("orders", "sales", "mydb")+ assert ref == '"mydb"."sales"."orders"'+++# ---------------------------------------------------------------------------+# TestConvertRows+# ---------------------------------------------------------------------------+++class TestConvertRows:+ """Unit tests for RedshiftIngestor._convert_rows."""++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_plain_values_pass_through(self):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ rows = [{"id": 1, "name": "Alice", "score": 9.5, "active": True, "note": None}]+ result = ing._convert_rows(rows)++ assert result[0]["id"] == 1+ assert result[0]["name"] == "Alice"+ assert result[0]["score"] == 9.5+ assert result[0]["active"] is True+ assert result[0]["note"] is None++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_datetime_converted_to_iso_string(self):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ dt = datetime(2024, 6, 15, 12, 0, 0)+ rows = [{"created_at": dt}]+ result = ing._convert_rows(rows)++ assert result[0]["created_at"] == "2024-06-15T12:00:00"++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_bytes_decoded_to_utf8_string(self):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ rows = [{"data": b"hello world"}]+ result = ing._convert_rows(rows)++ assert result[0]["data"] == "hello world"++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_invalid_utf8_bytes_fall_back_to_str(self):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ bad_bytes = b"\xff\xfe"+ rows = [{"blob": bad_bytes}]+ result = ing._convert_rows(rows)++ # fallback: str(bad_bytes) — just verify it's a string, not bytes+ assert isinstance(result[0]["blob"], str)++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_empty_list_returns_empty(self):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ assert ing._convert_rows([]) == []++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_multiple_rows_all_converted(self):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ dt1 = datetime(2024, 1, 1)+ dt2 = datetime(2024, 6, 1)+ rows = [{"ts": dt1, "val": 1}, {"ts": dt2, "val": 2}]+ result = ing._convert_rows(rows)++ assert result[0]["ts"] == "2024-01-01T00:00:00"+ assert result[1]["ts"] == "2024-06-01T00:00:00"+ assert result[0]["val"] == 1+ assert result[1]["val"] == 2+++# ---------------------------------------------------------------------------+# TestIngestTable+# ---------------------------------------------------------------------------+++class TestIngestTable:+ """Tests for RedshiftIngestor.ingest_table()."""++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_basic_table_ingestion(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftData, RedshiftIngestor++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn+ cursor = _make_cursor_with_rows(+ ["id", "name"], [(1, "Alice"), (2, "Bob")]+ )+ mock_conn.cursor.return_value = cursor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ result = ing.ingest_table("customers")++ assert isinstance(result, RedshiftData)+ assert result.row_count == 2+ assert result.table_name == "customers"+ assert result.columns == ["id", "name"]+ assert result.data == [{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}]++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_result_data_fields_populated(self, mock_sdk):+ """RedshiftData.database, .schema, .query are all set."""+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn+ cursor = _make_cursor_with_rows(["id"], [(1,)])+ mock_conn.cursor.return_value = cursor++ ing = RedshiftIngestor(host="h", database="mydb", user="u", password="p")+ result = ing.ingest_table("orders", schema="sales")++ assert result.database == "mydb"+ assert result.schema == "sales"+ assert result.query is not None+ assert '"orders"' in result.query++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_schema_qualified_table_reference(self, mock_sdk):+ """Query uses a double-quoted schema.table reference."""+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn+ cursor = _make_cursor_with_rows(["id"], [])+ mock_conn.cursor.return_value = cursor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ ing.ingest_table("orders", schema="sales")++ executed_sql = cursor.execute.call_args[0][0]+ assert '"sales"."orders"' in executed_sql++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_database_schema_table_reference(self, mock_sdk):+ """Query includes database when explicitly passed."""+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn+ cursor = _make_cursor_with_rows(["id"], [])+ mock_conn.cursor.return_value = cursor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ ing.ingest_table("orders", database="mydb", schema="sales")++ executed_sql = cursor.execute.call_args[0][0]+ assert '"mydb"."sales"."orders"' in executed_sql++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_limit_in_query(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn+ cursor = _make_cursor_with_rows(["id"], [])+ mock_conn.cursor.return_value = cursor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ ing.ingest_table("t", limit=100)++ executed_sql = cursor.execute.call_args[0][0]+ assert "LIMIT 100" in executed_sql++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_offset_in_query(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn+ cursor = _make_cursor_with_rows(["id"], [])+ mock_conn.cursor.return_value = cursor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ ing.ingest_table("t", offset=200)++ executed_sql = cursor.execute.call_args[0][0]+ assert "OFFSET 200" in executed_sql++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_where_clause_in_query(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn+ cursor = _make_cursor_with_rows(["id"], [])+ mock_conn.cursor.return_value = cursor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ ing.ingest_table("t", where="status = 'active'")++ executed_sql = cursor.execute.call_args[0][0]+ assert "WHERE status = 'active'" in executed_sql++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_order_by_in_query(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn+ cursor = _make_cursor_with_rows(["id"], [])+ mock_conn.cursor.return_value = cursor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ ing.ingest_table("t", order_by="created_at DESC")++ executed_sql = cursor.execute.call_args[0][0]+ assert "ORDER BY created_at DESC" in executed_sql++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_all_clauses_order(self, mock_sdk):+ """WHERE comes before ORDER BY before LIMIT before OFFSET."""+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn+ cursor = _make_cursor_with_rows(["id"], [])+ mock_conn.cursor.return_value = cursor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ ing.ingest_table(+ "t",+ where="active = true",+ order_by="id ASC",+ limit=10,+ offset=5,+ )+ sql = cursor.execute.call_args[0][0]+ w = sql.index("WHERE")+ ob = sql.index("ORDER BY")+ lim = sql.index("LIMIT")+ off = sql.index("OFFSET")+ assert w < ob < lim < off++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_autocommit_set_on_connection(self, mock_sdk):+ """conn.autocommit must be set to True before query execution."""+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn+ cursor = _make_cursor_with_rows(["id"], [])+ mock_conn.cursor.return_value = cursor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ ing.ingest_table("t")++ assert mock_conn.autocommit is True++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_cursor_closed_after_fetch(self, mock_sdk):+ """cursor.close() is called even on a successful fetch."""+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn+ cursor = _make_cursor_with_rows(["id"], [(1,)])+ mock_conn.cursor.return_value = cursor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ ing.ingest_table("t")++ cursor.close.assert_called_once()++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_standalone_call_closes_connection(self, mock_sdk):+ """A standalone ingest_table() must close the connection it opens."""+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn+ mock_conn.cursor.return_value = _make_cursor_with_rows(["id"], [])++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ assert ing.connector.connection is None+ ing.ingest_table("t")+ assert ing.connector.connection is None+ mock_conn.close.assert_called_once()++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_context_manager_connection_reused(self, mock_sdk):+ """ingest_table() inside a context manager reuses the open connection."""+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn+ mock_conn.cursor.return_value = _make_cursor_with_rows(["id"], [(1,)])++ with RedshiftIngestor(+ host="h", database="d", user="u", password="p"+ ) as ing:+ ing.ingest_table("t")+ # Connection must still be alive — ingest_table must not have closed it.+ assert ing.connector.connection is mock_conn+ mock_conn.close.assert_not_called()++ # Closed only on __exit__.+ assert ing.connector.connection is None+ mock_conn.close.assert_called_once()++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_query_failure_raises_processing_error(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor+ from semantica.utils.exceptions import ProcessingError++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn+ cursor = Mock()+ cursor.description = None+ cursor.execute.side_effect = RuntimeError("syntax error")+ cursor.close = Mock()+ mock_conn.cursor.return_value = cursor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ with pytest.raises(ProcessingError):+ ing.ingest_table("t")++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_cursor_closed_on_query_failure(self, mock_sdk):+ """cursor.close() is called even when execute() raises."""+ from semantica.ingest.redshift_ingestor import RedshiftIngestor+ from semantica.utils.exceptions import ProcessingError++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn+ cursor = Mock()+ cursor.description = None+ cursor.execute.side_effect = RuntimeError("query error")+ cursor.close = Mock()+ mock_conn.cursor.return_value = cursor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ with pytest.raises(ProcessingError):+ ing.ingest_table("t")++ cursor.close.assert_called_once()++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_connection_closed_on_query_failure(self, mock_sdk):+ """Standalone call must close the connection even when query fails."""+ from semantica.ingest.redshift_ingestor import RedshiftIngestor+ from semantica.utils.exceptions import ProcessingError++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn+ cursor = Mock()+ cursor.description = None+ cursor.execute.side_effect = RuntimeError("failure")+ cursor.close = Mock()+ mock_conn.cursor.return_value = cursor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ with pytest.raises(ProcessingError):+ ing.ingest_table("t")++ mock_conn.close.assert_called_once()+ assert ing.connector.connection is None++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_empty_result_set(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn+ cursor = _make_cursor_with_rows(["id", "name"], [])+ mock_conn.cursor.return_value = cursor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ result = ing.ingest_table("t")++ assert result.row_count == 0+ assert result.data == []+ assert result.columns == ["id", "name"]++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_tuple_rows_converted_to_dicts(self, mock_sdk):+ """Redshift cursor returns tuples; ingest_table must produce dicts."""+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn+ cursor = _make_cursor_with_rows(+ ["id", "name", "score"],+ [(42, "Charlie", 7.5)],+ )+ mock_conn.cursor.return_value = cursor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ result = ing.ingest_table("t")++ assert result.data[0] == {"id": 42, "name": "Charlie", "score": 7.5}++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_datetime_converted_in_result(self, mock_sdk):+ """datetime values in rows are converted to ISO-8601 strings."""+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn+ dt = datetime(2024, 3, 15, 9, 30, 0)+ cursor = _make_cursor_with_rows(["ts"], [(dt,)])+ mock_conn.cursor.return_value = cursor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ result = ing.ingest_table("t")++ assert result.data[0]["ts"] == "2024-03-15T09:30:00"+++# ---------------------------------------------------------------------------+# TestIngestTableValidation+# ---------------------------------------------------------------------------+++class TestIngestTableValidation:+ """SQL-safety validation for ingest_table identifiers and fragments."""++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_invalid_table_name_raises_validation_error(self):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor+ from semantica.utils.exceptions import ValidationError++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ with pytest.raises(ValidationError):+ ing.ingest_table("orders; DROP TABLE orders")++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_invalid_schema_name_raises_validation_error(self):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor+ from semantica.utils.exceptions import ValidationError++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ with pytest.raises(ValidationError):+ ing.ingest_table("orders", schema="sales; DROP")++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_invalid_database_name_raises_validation_error(self):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor+ from semantica.utils.exceptions import ValidationError++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ with pytest.raises(ValidationError):+ ing.ingest_table("t", database="db-bad-name")++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_malicious_where_raises_validation_error(self):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor+ from semantica.utils.exceptions import ValidationError++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ with pytest.raises(ValidationError):+ ing.ingest_table("t", where="1=1; DROP TABLE t")++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_union_in_where_raises_validation_error(self):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor+ from semantica.utils.exceptions import ValidationError++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ with pytest.raises(ValidationError):+ ing.ingest_table("t", where="1=1 UNION SELECT * FROM secrets")++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_malicious_order_by_raises_validation_error(self):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor+ from semantica.utils.exceptions import ValidationError++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ with pytest.raises(ValidationError):+ ing.ingest_table("t", order_by="id; DROP TABLE t")++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_validation_raises_before_connecting(self):+ """ValidationError from a bad identifier must fire before any SDK call."""+ from semantica.ingest.redshift_ingestor import RedshiftIngestor+ from semantica.utils.exceptions import ValidationError++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")++ with patch("semantica.ingest.redshift_ingestor._redshift_connector") as mock_sdk:+ with pytest.raises(ValidationError):+ ing.ingest_table("bad-name!")++ mock_sdk.connect.assert_not_called()++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_valid_where_with_quoted_union_passes(self, mock_sdk):+ """'union' inside a string literal in WHERE must not be rejected."""+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn+ cursor = _make_cursor_with_rows(["id"], [])+ mock_conn.cursor.return_value = cursor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ # "Credit Union" is a literal value, not SQL syntax — must pass.+ ing.ingest_table("t", where="name = 'Credit Union'")++ executed_sql = cursor.execute.call_args[0][0]+ assert "Credit Union" in executed_sql+++# ---------------------------------------------------------------------------+# TestIngestQuery+# ---------------------------------------------------------------------------+++class TestIngestQuery:+ """Tests for RedshiftIngestor.ingest_query()."""++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_basic_query_returns_redshift_data(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftData, RedshiftIngestor++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn+ cursor = _make_cursor_with_rows(["id", "total"], [(1, 99.5), (2, 42.0)])+ mock_conn.cursor.return_value = cursor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ result = ing.ingest_query("SELECT id, total FROM sales")++ assert isinstance(result, RedshiftData)+ assert result.row_count == 2+ assert result.columns == ["id", "total"]+ assert result.query == "SELECT id, total FROM sales"++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_query_stored_verbatim(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn+ mock_conn.cursor.return_value = _make_cursor_with_rows(["n"], [])++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ sql = "SELECT * FROM public.events WHERE type = 'click'"+ result = ing.ingest_query(sql)++ assert result.query == sql++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_tuple_rows_converted_to_dicts(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn+ cursor = _make_cursor_with_rows(["a", "b"], [(10, "x"), (20, "y")])+ mock_conn.cursor.return_value = cursor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ result = ing.ingest_query("SELECT a, b FROM t")++ assert result.data == [{"a": 10, "b": "x"}, {"a": 20, "b": "y"}]++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_params_forwarded_to_cursor_execute(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn+ cursor = _make_cursor_with_rows(["id"], [])+ mock_conn.cursor.return_value = cursor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ ing.ingest_query("SELECT id FROM t WHERE status = %s", params=("active",))++ cursor.execute.assert_called_once_with(+ "SELECT id FROM t WHERE status = %s", ("active",)+ )++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_no_params_calls_execute_without_params(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn+ cursor = _make_cursor_with_rows(["id"], [])+ mock_conn.cursor.return_value = cursor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ ing.ingest_query("SELECT id FROM t")++ cursor.execute.assert_called_once_with("SELECT id FROM t")++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_batch_size_uses_fetchmany(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn+ cursor = _make_cursor_with_rows(+ ["id"],+ [(1,), (2,), (3,), (4,), (5,)],+ )+ mock_conn.cursor.return_value = cursor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ result = ing.ingest_query("SELECT id FROM t", batch_size=2)++ assert result.row_count == 5+ assert result.data == [{"id": i} for i in range(1, 6)]+ # fetchmany called multiple times (3 batches: 2, 2, 1, then empty)+ assert cursor.fetchmany.call_count >= 3++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_batch_size_calls_fetchmany_with_correct_size(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn+ cursor = _make_cursor_with_rows(["id"], [(1,), (2,)])+ mock_conn.cursor.return_value = cursor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ ing.ingest_query("SELECT id FROM t", batch_size=50)++ # All fetchmany calls must use the requested batch size.+ for c in cursor.fetchmany.call_args_list:+ assert c[0][0] == 50++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_empty_result_set(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn+ cursor = _make_cursor_with_rows(["id"], [])+ mock_conn.cursor.return_value = cursor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ result = ing.ingest_query("SELECT id FROM t WHERE 1=0")++ assert result.row_count == 0+ assert result.data == []++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_cursor_closed_after_fetch(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn+ cursor = _make_cursor_with_rows(["id"], [(1,)])+ mock_conn.cursor.return_value = cursor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ ing.ingest_query("SELECT id FROM t")++ cursor.close.assert_called_once()++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_standalone_call_closes_connection(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn+ mock_conn.cursor.return_value = _make_cursor_with_rows(["id"], [])++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ ing.ingest_query("SELECT 1")++ mock_conn.close.assert_called_once()+ assert ing.connector.connection is None++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_context_manager_connection_reused(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn+ mock_conn.cursor.return_value = _make_cursor_with_rows(["n"], [(1,)])++ with RedshiftIngestor(+ host="h", database="d", user="u", password="p"+ ) as ing:+ ing.ingest_query("SELECT 1")+ assert ing.connector.connection is mock_conn+ mock_conn.close.assert_not_called()++ assert ing.connector.connection is None+ mock_conn.close.assert_called_once()++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_query_failure_raises_processing_error(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor+ from semantica.utils.exceptions import ProcessingError++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn+ cursor = Mock()+ cursor.description = None+ cursor.execute.side_effect = RuntimeError("query error")+ cursor.close = Mock()+ mock_conn.cursor.return_value = cursor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ with pytest.raises(ProcessingError):+ ing.ingest_query("SELECT bad query")++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_cursor_closed_on_query_failure(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor+ from semantica.utils.exceptions import ProcessingError++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn+ cursor = Mock()+ cursor.description = None+ cursor.execute.side_effect = RuntimeError("error")+ cursor.close = Mock()+ mock_conn.cursor.return_value = cursor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ with pytest.raises(ProcessingError):+ ing.ingest_query("SELECT 1")++ cursor.close.assert_called_once()++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_connection_closed_on_query_failure(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor+ from semantica.utils.exceptions import ProcessingError++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn+ cursor = Mock()+ cursor.description = None+ cursor.execute.side_effect = RuntimeError("error")+ cursor.close = Mock()+ mock_conn.cursor.return_value = cursor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ with pytest.raises(ProcessingError):+ ing.ingest_query("SELECT 1")++ mock_conn.close.assert_called_once()+ assert ing.connector.connection is None++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_autocommit_set_on_connection(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn+ mock_conn.cursor.return_value = _make_cursor_with_rows(["id"], [])++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ ing.ingest_query("SELECT 1")++ assert mock_conn.autocommit is True++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_params_stored_in_metadata(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn+ mock_conn.cursor.return_value = _make_cursor_with_rows(["id"], [])++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ result = ing.ingest_query("SELECT id FROM t WHERE x = %s", params=("val",))++ assert result.metadata.get("params") == ("val",)++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_no_params_metadata_empty(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn+ mock_conn.cursor.return_value = _make_cursor_with_rows(["id"], [])++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ result = ing.ingest_query("SELECT 1")++ assert result.metadata == {}+++# ---------------------------------------------------------------------------+# TestContextManagerIntegration+# ---------------------------------------------------------------------------+++class TestContextManagerIntegration:+ """End-to-end context manager tests for Stage 3 ingestion."""++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_both_methods_reuse_context_connection(self, mock_sdk):+ """Both ingest_table and ingest_query reuse the context-manager connection."""+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn+ mock_conn.cursor.return_value = _make_cursor_with_rows(["id"], [(1,)])++ with RedshiftIngestor(+ host="h", database="d", user="u", password="p"+ ) as ing:+ ing.ingest_table("t1")+ ing.ingest_query("SELECT id FROM t2")+ # SDK connect must have been called exactly once (by __enter__)+ assert mock_sdk.connect.call_count == 1+ assert ing.connector.connection is mock_conn++ # Closed once on __exit__+ mock_conn.close.assert_called_once()++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_context_manager_closes_on_exception(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn+ mock_conn.cursor.return_value = _make_cursor_with_rows(["id"], [])++ with pytest.raises(ValueError):+ with RedshiftIngestor(+ host="h", database="d", user="u", password="p"+ ) as ing:+ raise ValueError("something went wrong")++ assert ing.connector.connection is None+++# ===========================================================================+# Stage 4 — get_table_schema, list_tables, export_as_documents+# ===========================================================================++# ---------------------------------------------------------------------------+# Helpers for Stage 4+# ---------------------------------------------------------------------------+++def _schema_cursor(columns_rows, pk_rows=None):+ """Return a sequence of cursors matching get_table_schema's two queries.++ First cursor returns *columns_rows* (column metadata tuples).+ Second cursor returns *pk_rows* (primary-key name tuples).++ Each fake cursor exposes .description, .fetchall(), .execute(), .close().+ """+ if pk_rows is None:+ pk_rows = []++ cursors = []+ for rows, col_names in [+ (+ columns_rows,+ ["column_name", "data_type", "is_nullable"],+ ),+ (pk_rows, ["column_name"]),+ ]:+ c = Mock()+ c.description = [(name,) + (None,) * 6 for name in col_names]+ c.fetchall = Mock(return_value=list(rows))+ c.execute = Mock()+ c.close = Mock()+ cursors.append(c)++ conn = Mock()+ conn.autocommit = False+ _call_count = [0]++ def _cursor():+ idx = _call_count[0]+ _call_count[0] += 1+ if idx < len(cursors):+ return cursors[idx]+ # fallback — return empty cursor+ c = Mock()+ c.description = [("col",)]+ c.fetchall = Mock(return_value=[])+ c.execute = Mock()+ c.close = Mock()+ return c++ conn.cursor = Mock(side_effect=_cursor)+ conn.close = Mock()+ return conn, cursors+++def _list_tables_cursor(table_rows):+ """Return a mock connection + cursor for list_tables."""+ mock_conn, mock_cursor = _make_mock_connection()+ cursor = Mock()+ cursor.description = [("table_name",)]+ cursor.fetchall = Mock(return_value=list(table_rows))+ cursor.execute = Mock()+ cursor.close = Mock()+ mock_conn.cursor = Mock(return_value=cursor)+ return mock_conn, cursor+++# ---------------------------------------------------------------------------+# TestGetTableSchema+# ---------------------------------------------------------------------------+++class TestGetTableSchema:+ """Tests for RedshiftIngestor.get_table_schema()."""++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_basic_schema_returned(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ col_rows = [+ ("id", "integer", "NO"),+ ("name", "character varying", "YES"),+ ("score", "numeric", "YES"),+ ]+ mock_conn, cursors = _schema_cursor(col_rows, pk_rows=[("id",)])+ mock_sdk.connect.return_value = mock_conn++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ result = ing.get_table_schema("customers")++ assert "columns" in result+ assert "primary_keys" in result+ assert len(result["columns"]) == 3+ assert result["primary_keys"] == ["id"]++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_column_names_correct(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ col_rows = [("id", "integer", "NO"), ("email", "text", "YES")]+ mock_conn, _ = _schema_cursor(col_rows)+ mock_sdk.connect.return_value = mock_conn++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ result = ing.get_table_schema("users")++ names = [c["name"] for c in result["columns"]]+ assert names == ["id", "email"]++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_column_types_correct(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ col_rows = [("amount", "numeric", "YES")]+ mock_conn, _ = _schema_cursor(col_rows)+ mock_sdk.connect.return_value = mock_conn++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ result = ing.get_table_schema("orders")++ assert result["columns"][0]["type"] == "numeric"++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_nullable_yes_is_true(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ col_rows = [("notes", "text", "YES")]+ mock_conn, _ = _schema_cursor(col_rows)+ mock_sdk.connect.return_value = mock_conn++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ result = ing.get_table_schema("t")++ assert result["columns"][0]["nullable"] is True++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_nullable_no_is_false(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ col_rows = [("id", "integer", "NO")]+ mock_conn, _ = _schema_cursor(col_rows)+ mock_sdk.connect.return_value = mock_conn++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ result = ing.get_table_schema("t")++ assert result["columns"][0]["nullable"] is False++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_column_order_preserved(self, mock_sdk):+ """Columns must appear in ordinal_position order (cursor row order)."""+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ col_rows = [+ ("a", "integer", "NO"),+ ("b", "text", "YES"),+ ("c", "boolean", "YES"),+ ]+ mock_conn, _ = _schema_cursor(col_rows)+ mock_sdk.connect.return_value = mock_conn++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ result = ing.get_table_schema("t")++ assert [c["name"] for c in result["columns"]] == ["a", "b", "c"]++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_multiple_primary_keys(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ col_rows = [+ ("order_id", "integer", "NO"),+ ("item_id", "integer", "NO"),+ ]+ mock_conn, _ = _schema_cursor(col_rows, pk_rows=[("order_id",), ("item_id",)])+ mock_sdk.connect.return_value = mock_conn++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ result = ing.get_table_schema("order_items")++ assert set(result["primary_keys"]) == {"order_id", "item_id"}++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_no_primary_key_returns_empty_list(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ col_rows = [("col1", "text", "YES")]+ mock_conn, _ = _schema_cursor(col_rows, pk_rows=[])+ mock_sdk.connect.return_value = mock_conn++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ result = ing.get_table_schema("t")++ assert result["primary_keys"] == []++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_empty_table_returns_empty_columns(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, _ = _schema_cursor([])+ mock_sdk.connect.return_value = mock_conn++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ result = ing.get_table_schema("empty_t")++ assert result["columns"] == []+ assert result["primary_keys"] == []++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_schema_and_table_passed_as_params(self, mock_sdk):+ """table_name and schema are passed as %s params, not interpolated."""+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, cursors = _schema_cursor([])+ mock_sdk.connect.return_value = mock_conn++ ing = RedshiftIngestor(+ host="h", database="d", user="u", password="p", schema="myschema"+ )+ ing.get_table_schema("mytable")++ # First cursor's execute call should have params=("myschema", "mytable")+ first_execute_params = cursors[0].execute.call_args[0][1]+ assert first_execute_params == ("myschema", "mytable")++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_both_cursors_are_closed(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, cursors = _schema_cursor([("id", "integer", "NO")])+ mock_sdk.connect.return_value = mock_conn++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ ing.get_table_schema("t")++ for c in cursors:+ c.close.assert_called_once()++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_standalone_call_closes_connection(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, _ = _schema_cursor([])+ mock_sdk.connect.return_value = mock_conn++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ ing.get_table_schema("t")++ mock_conn.close.assert_called_once()+ assert ing.connector.connection is None++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_context_manager_connection_reused(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, _ = _schema_cursor([])+ mock_sdk.connect.return_value = mock_conn++ with RedshiftIngestor(+ host="h", database="d", user="u", password="p"+ ) as ing:+ ing.get_table_schema("t")+ assert ing.connector.connection is mock_conn+ mock_conn.close.assert_not_called()++ assert ing.connector.connection is None+ mock_conn.close.assert_called_once()++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_invalid_table_name_raises_validation_error(self):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor+ from semantica.utils.exceptions import ValidationError++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ with pytest.raises(ValidationError):+ ing.get_table_schema("bad-table-name!")++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_invalid_schema_raises_validation_error(self):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor+ from semantica.utils.exceptions import ValidationError++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ with pytest.raises(ValidationError):+ ing.get_table_schema("t", schema="bad schema")++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_validation_fires_before_connecting(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor+ from semantica.utils.exceptions import ValidationError++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ with pytest.raises(ValidationError):+ ing.get_table_schema("bad-name!")++ mock_sdk.connect.assert_not_called()++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_query_failure_raises_processing_error(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor+ from semantica.utils.exceptions import ProcessingError++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn+ bad_cursor = Mock()+ bad_cursor.execute.side_effect = RuntimeError("permission denied")+ bad_cursor.close = Mock()+ mock_conn.cursor = Mock(return_value=bad_cursor)++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ with pytest.raises(ProcessingError):+ ing.get_table_schema("t")++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_cursor_closed_on_failure(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor+ from semantica.utils.exceptions import ProcessingError++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn+ bad_cursor = Mock()+ bad_cursor.execute.side_effect = RuntimeError("error")+ bad_cursor.close = Mock()+ mock_conn.cursor = Mock(return_value=bad_cursor)++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ with pytest.raises(ProcessingError):+ ing.get_table_schema("t")++ bad_cursor.close.assert_called_once()++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_connection_closed_on_failure(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor+ from semantica.utils.exceptions import ProcessingError++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn+ bad_cursor = Mock()+ bad_cursor.execute.side_effect = RuntimeError("error")+ bad_cursor.close = Mock()+ mock_conn.cursor = Mock(return_value=bad_cursor)++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ with pytest.raises(ProcessingError):+ ing.get_table_schema("t")++ mock_conn.close.assert_called_once()+ assert ing.connector.connection is None++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_autocommit_set(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, _ = _schema_cursor([])+ mock_sdk.connect.return_value = mock_conn++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ ing.get_table_schema("t")++ assert mock_conn.autocommit is True+++# ---------------------------------------------------------------------------+# TestListTables+# ---------------------------------------------------------------------------+++class TestListTables:+ """Tests for RedshiftIngestor.list_tables()."""++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_basic_list_returns_table_names(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, cursor = _list_tables_cursor(+ [("customers",), ("orders",), ("products",)]+ )+ mock_sdk.connect.return_value = mock_conn++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ result = ing.list_tables()++ assert result == ["customers", "orders", "products"]++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_schema_filter_passed_as_param(self, mock_sdk):+ """The schema value must be passed as a %s parameter, not interpolated."""+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, cursor = _list_tables_cursor([])+ mock_sdk.connect.return_value = mock_conn++ ing = RedshiftIngestor(+ host="h", database="d", user="u", password="p", schema="analytics"+ )+ ing.list_tables()++ # The execute call should include the schema as a param tuple.+ execute_params = cursor.execute.call_args[0][1]+ assert execute_params == ("analytics",)++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_explicit_schema_override(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, cursor = _list_tables_cursor([])+ mock_sdk.connect.return_value = mock_conn++ ing = RedshiftIngestor(+ host="h", database="d", user="u", password="p", schema="public"+ )+ ing.list_tables(schema="staging")++ execute_params = cursor.execute.call_args[0][1]+ assert execute_params == ("staging",)++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_empty_schema_returns_empty_list(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, _ = _list_tables_cursor([])+ mock_sdk.connect.return_value = mock_conn++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ result = ing.list_tables()++ assert result == []++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_cursor_closed_after_fetch(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, cursor = _list_tables_cursor([("t",)])+ mock_sdk.connect.return_value = mock_conn++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ ing.list_tables()++ cursor.close.assert_called_once()++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_standalone_call_closes_connection(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, _ = _list_tables_cursor([])+ mock_sdk.connect.return_value = mock_conn++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ ing.list_tables()++ mock_conn.close.assert_called_once()+ assert ing.connector.connection is None++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_context_manager_connection_reused(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, _ = _list_tables_cursor([("t1",)])+ mock_sdk.connect.return_value = mock_conn++ with RedshiftIngestor(+ host="h", database="d", user="u", password="p"+ ) as ing:+ ing.list_tables()+ assert ing.connector.connection is mock_conn+ mock_conn.close.assert_not_called()++ assert ing.connector.connection is None+ mock_conn.close.assert_called_once()++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_invalid_schema_raises_validation_error(self):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor+ from semantica.utils.exceptions import ValidationError++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ with pytest.raises(ValidationError):+ ing.list_tables(schema="bad schema!")++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_validation_fires_before_connecting(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor+ from semantica.utils.exceptions import ValidationError++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ with pytest.raises(ValidationError):+ ing.list_tables(schema="bad schema!")++ mock_sdk.connect.assert_not_called()++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_query_failure_raises_processing_error(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor+ from semantica.utils.exceptions import ProcessingError++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn+ bad_cursor = Mock()+ bad_cursor.execute.side_effect = RuntimeError("query error")+ bad_cursor.close = Mock()+ mock_conn.cursor = Mock(return_value=bad_cursor)++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ with pytest.raises(ProcessingError):+ ing.list_tables()++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_cursor_closed_on_failure(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor+ from semantica.utils.exceptions import ProcessingError++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn+ bad_cursor = Mock()+ bad_cursor.execute.side_effect = RuntimeError("error")+ bad_cursor.close = Mock()+ mock_conn.cursor = Mock(return_value=bad_cursor)++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ with pytest.raises(ProcessingError):+ ing.list_tables()++ bad_cursor.close.assert_called_once()++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_autocommit_set(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, _ = _list_tables_cursor([])+ mock_sdk.connect.return_value = mock_conn++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ ing.list_tables()++ assert mock_conn.autocommit is True+++# ---------------------------------------------------------------------------+# TestExportAsDocuments+# ---------------------------------------------------------------------------+++class TestExportAsDocuments:+ """Tests for RedshiftIngestor.export_as_documents()."""++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_basic_conversion(self):+ from semantica.ingest.redshift_ingestor import RedshiftData, RedshiftIngestor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ data = RedshiftData(+ data=[{"id": 1, "name": "Alice", "dept": "Eng"}],+ row_count=1,+ columns=["id", "name", "dept"],+ table_name="employees",+ database="dev",+ schema="public",+ )+ docs = ing.export_as_documents(data, id_field="id", text_fields=["name", "dept"])++ assert len(docs) == 1+ assert docs[0]["id"] == "1"+ assert docs[0]["text"] == "Alice Eng"++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_metadata_source_is_redshift(self):+ from semantica.ingest.redshift_ingestor import RedshiftData, RedshiftIngestor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ data = RedshiftData(+ data=[{"id": 1, "name": "Bob"}],+ row_count=1,+ columns=["id", "name"],+ table_name="t",+ database="mydb",+ schema="myschema",+ )+ docs = ing.export_as_documents(data)++ assert docs[0]["metadata"]["source"] == "redshift"+ assert docs[0]["metadata"]["table"] == "t"+ assert docs[0]["metadata"]["database"] == "mydb"+ assert docs[0]["metadata"]["schema"] == "myschema"++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_row_data_in_metadata(self):+ from semantica.ingest.redshift_ingestor import RedshiftData, RedshiftIngestor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ row = {"id": 7, "name": "Carol", "score": 99}+ data = RedshiftData(data=[row], row_count=1, columns=["id", "name", "score"])+ docs = ing.export_as_documents(data)++ assert docs[0]["metadata"]["row_data"] == row++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_default_id_field_uses_id_key(self):+ from semantica.ingest.redshift_ingestor import RedshiftData, RedshiftIngestor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ data = RedshiftData(+ data=[{"id": 42, "name": "Dave"}], row_count=1, columns=["id", "name"]+ )+ docs = ing.export_as_documents(data)++ assert docs[0]["id"] == "42"++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_custom_id_field(self):+ from semantica.ingest.redshift_ingestor import RedshiftData, RedshiftIngestor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ data = RedshiftData(+ data=[{"order_id": "ORD-001", "item": "Widget"}],+ row_count=1,+ columns=["order_id", "item"],+ )+ docs = ing.export_as_documents(data, id_field="order_id")++ assert docs[0]["id"] == "ORD-001"++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_missing_id_field_falls_back_to_row_index(self):+ """When id_field is absent from a row, the row index is used."""+ from semantica.ingest.redshift_ingestor import RedshiftData, RedshiftIngestor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ data = RedshiftData(+ data=[{"name": "Eve"}, {"name": "Frank"}],+ row_count=2,+ columns=["name"],+ )+ docs = ing.export_as_documents(data, id_field="id")++ # Index fallback: row 0 → "0", row 1 → "1"+ assert docs[0]["id"] == "0"+ assert docs[1]["id"] == "1"++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_text_fields_explicit_join(self):+ from semantica.ingest.redshift_ingestor import RedshiftData, RedshiftIngestor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ data = RedshiftData(+ data=[{"id": 1, "first": "Ada", "last": "Lovelace", "year": 1815}],+ row_count=1,+ columns=["id", "first", "last", "year"],+ )+ docs = ing.export_as_documents(data, text_fields=["first", "last"])++ assert docs[0]["text"] == "Ada Lovelace"++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_text_fields_none_value_excluded(self):+ from semantica.ingest.redshift_ingestor import RedshiftData, RedshiftIngestor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ data = RedshiftData(+ data=[{"id": 1, "name": "Alice", "notes": None}],+ row_count=1,+ columns=["id", "name", "notes"],+ )+ docs = ing.export_as_documents(data, text_fields=["name", "notes"])++ assert docs[0]["text"] == "Alice"++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_default_text_uses_only_string_values(self):+ """When text_fields=None, only str-typed values contribute to text."""+ from semantica.ingest.redshift_ingestor import RedshiftData, RedshiftIngestor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ data = RedshiftData(+ data=[{"id": 1, "name": "Grace", "count": 10, "active": True}],+ row_count=1,+ columns=["id", "name", "count", "active"],+ )+ docs = ing.export_as_documents(data)++ # Only "Grace" is str — integers and bools are excluded.+ assert docs[0]["text"] == "Grace"++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_multiple_rows_all_exported(self):+ from semantica.ingest.redshift_ingestor import RedshiftData, RedshiftIngestor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ data = RedshiftData(+ data=[+ {"id": 1, "name": "Alice"},+ {"id": 2, "name": "Bob"},+ {"id": 3, "name": "Charlie"},+ ],+ row_count=3,+ columns=["id", "name"],+ )+ docs = ing.export_as_documents(data)++ assert len(docs) == 3+ assert docs[1]["id"] == "2"++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_empty_data_returns_empty_list(self):+ from semantica.ingest.redshift_ingestor import RedshiftData, RedshiftIngestor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ data = RedshiftData(data=[], row_count=0, columns=[])+ docs = ing.export_as_documents(data)++ assert docs == []++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_numeric_id_is_string_in_output(self):+ from semantica.ingest.redshift_ingestor import RedshiftData, RedshiftIngestor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ data = RedshiftData(+ data=[{"id": 99}], row_count=1, columns=["id"]+ )+ docs = ing.export_as_documents(data)++ assert isinstance(docs[0]["id"], str)+ assert docs[0]["id"] == "99"++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_table_name_none_when_from_query(self):+ from semantica.ingest.redshift_ingestor import RedshiftData, RedshiftIngestor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ data = RedshiftData(+ data=[{"id": 1, "val": "x"}],+ row_count=1,+ columns=["id", "val"],+ table_name=None,+ query="SELECT id, val FROM t",+ )+ docs = ing.export_as_documents(data)++ assert docs[0]["metadata"]["table"] is None++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_ids_are_deterministic(self):+ """Same data produces same IDs on every call — no random UUIDs."""+ from semantica.ingest.redshift_ingestor import RedshiftData, RedshiftIngestor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ data = RedshiftData(+ data=[{"id": 5, "name": "Dave"}], row_count=1, columns=["id", "name"]+ )+ docs1 = ing.export_as_documents(data)+ docs2 = ing.export_as_documents(data)++ assert docs1[0]["id"] == docs2[0]["id"]++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_no_credentials_in_documents(self):+ """No password, access key, or session token may appear in documents."""+ from semantica.ingest.redshift_ingestor import RedshiftData, RedshiftIngestor++ ing = RedshiftIngestor(+ host="h", database="d", user="awsuser", password="hunter2"+ )+ data = RedshiftData(+ data=[{"id": 1, "name": "Test"}],+ row_count=1,+ columns=["id", "name"],+ )+ docs = ing.export_as_documents(data)++ import json+ serialized = json.dumps(docs)+ assert "hunter2" not in serialized++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_text_fields_numeric_value_stringified(self):+ """Numeric values in explicit text_fields are str()-converted."""+ from semantica.ingest.redshift_ingestor import RedshiftData, RedshiftIngestor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ data = RedshiftData(+ data=[{"id": 1, "score": 42}],+ row_count=1,+ columns=["id", "score"],+ )+ docs = ing.export_as_documents(data, text_fields=["score"])++ assert docs[0]["text"] == "42"++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_text_fields_datetime_value_stringified(self):+ from semantica.ingest.redshift_ingestor import RedshiftData, RedshiftIngestor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ # _convert_rows already converted datetime → ISO string before export+ data = RedshiftData(+ data=[{"id": 1, "ts": "2024-06-15T12:00:00"}],+ row_count=1,+ columns=["id", "ts"],+ )+ docs = ing.export_as_documents(data, text_fields=["ts"])++ assert "2024-06-15" in docs[0]["text"]+++# ---------------------------------------------------------------------------+# TestGraphBuilderContract+# ---------------------------------------------------------------------------+++class TestGraphBuilderContract:+ """Verify that documents produced by export_as_documents are accepted+ by GraphBuilder without modification.++ The contract (from reading GraphBuilder._process_item) is:+ - A dict with ``"id"`` key is routed to all_entities.+ - A dict with ``"text"`` key (and no ``"source"``/``"target"``) is+ passed to _extract_from_text.+ - Both can coexist in the same dict.++ We verify the shape without invoking a live GraphBuilder+ (no NLP models, no network).+ """++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_document_has_id_key(self):+ from semantica.ingest.redshift_ingestor import RedshiftData, RedshiftIngestor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ data = RedshiftData(+ data=[{"id": 1, "description": "A widget"}],+ row_count=1,+ columns=["id", "description"],+ )+ docs = ing.export_as_documents(data)++ assert "id" in docs[0], "GraphBuilder requires 'id' for entity recognition"++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_document_has_text_key(self):+ from semantica.ingest.redshift_ingestor import RedshiftData, RedshiftIngestor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ data = RedshiftData(+ data=[{"id": 1, "description": "A widget"}],+ row_count=1,+ columns=["id", "description"],+ )+ docs = ing.export_as_documents(data)++ assert "text" in docs[0], "GraphBuilder uses 'text' for NLP extraction"++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_document_id_is_string(self):+ """GraphBuilder uses entity IDs as dict keys — must be hashable strings."""+ from semantica.ingest.redshift_ingestor import RedshiftData, RedshiftIngestor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ data = RedshiftData(+ data=[{"id": 123}], row_count=1, columns=["id"]+ )+ docs = ing.export_as_documents(data)++ assert isinstance(docs[0]["id"], str)++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_document_text_is_string(self):+ from semantica.ingest.redshift_ingestor import RedshiftData, RedshiftIngestor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ data = RedshiftData(+ data=[{"id": 1, "name": "test"}], row_count=1, columns=["id", "name"]+ )+ docs = ing.export_as_documents(data)++ assert isinstance(docs[0]["text"], str)++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_document_shape_matches_snowflake_convention(self):+ """The shape must exactly match Snowflake/Databricks so GraphBuilder+ processes them identically."""+ from semantica.ingest.redshift_ingestor import RedshiftData, RedshiftIngestor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ data = RedshiftData(+ data=[{"id": 1, "name": "Alice", "role": "Engineer"}],+ row_count=1,+ columns=["id", "name", "role"],+ table_name="employees",+ database="dev",+ schema="public",+ )+ docs = ing.export_as_documents(+ data, id_field="id", text_fields=["name", "role"]+ )++ doc = docs[0]+ # Top-level keys exactly {id, text, metadata}+ assert set(doc.keys()) == {"id", "text", "metadata"}+ # id is a string+ assert doc["id"] == "1"+ # text is the joined fields+ assert doc["text"] == "Alice Engineer"+ # metadata keys matching Snowflake convention (source, table, database,+ # schema, row_data) — Redshift adds "redshift" as source.+ assert doc["metadata"]["source"] == "redshift"+ assert doc["metadata"]["table"] == "employees"+ assert doc["metadata"]["database"] == "dev"+ assert doc["metadata"]["schema"] == "public"+ assert "row_data" in doc["metadata"]++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_graphbuilder_processes_documents_without_error(self):+ """GraphBuilder.build() must accept a list of Redshift documents+ without raising. No live service calls; text extraction is+ explicitly disabled via extract=False."""+ from semantica.ingest.redshift_ingestor import RedshiftData, RedshiftIngestor+ from semantica.kg.graph_builder import GraphBuilder++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ data = RedshiftData(+ data=[+ {"id": 1, "product": "Widget A", "category": "Hardware"},+ {"id": 2, "product": "Widget B", "category": "Software"},+ ],+ row_count=2,+ columns=["id", "product", "category"],+ table_name="products",+ database="dev",+ schema="public",+ )+ docs = ing.export_as_documents(data, text_fields=["product", "category"])++ builder = GraphBuilder(resolve_conflicts=False)+ # extract=False: skip NLP extraction so the test needs no spaCy model.+ graph = builder.build(docs, extract=False)++ assert "entities" in graph+ assert "relationships" in graph+ assert graph["metadata"]["num_entities"] == 2+++# ===========================================================================+# Code-review fix tests+# ===========================================================================+++# ---------------------------------------------------------------------------+# Fix #1 — autocommit state restored on caller-owned connections+# ---------------------------------------------------------------------------+++class TestAutocommitRestoration:+ """autocommit must be restored to its previous value when the connection+ was already open (caller-owned) before the ingestion call."""++ def _make_ingesting_connection(self, initial_autocommit: bool):+ """Return a mock connection with autocommit pre-set."""+ mock_conn = Mock()+ mock_conn.autocommit = initial_autocommit+ return mock_conn++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_ingest_table_restores_autocommit_false(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn = self._make_ingesting_connection(initial_autocommit=False)+ mock_sdk.connect.return_value = mock_conn+ mock_conn.cursor.return_value = _make_cursor_with_rows(["id"], [(1,)])++ with RedshiftIngestor(+ host="h", database="d", user="u", password="p"+ ) as ing:+ # autocommit is False on the shared connection+ assert ing.connector.connection is mock_conn+ ing.ingest_table("t")+ # must be restored to False after the call+ assert mock_conn.autocommit is False++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_ingest_table_restores_autocommit_true(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn = self._make_ingesting_connection(initial_autocommit=True)+ mock_sdk.connect.return_value = mock_conn+ mock_conn.cursor.return_value = _make_cursor_with_rows(["id"], [])++ with RedshiftIngestor(+ host="h", database="d", user="u", password="p"+ ) as ing:+ ing.ingest_table("t")+ # was True before → must still be True after+ assert mock_conn.autocommit is True++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_ingest_query_restores_autocommit(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn = self._make_ingesting_connection(initial_autocommit=False)+ mock_sdk.connect.return_value = mock_conn+ mock_conn.cursor.return_value = _make_cursor_with_rows(["n"], [])++ with RedshiftIngestor(+ host="h", database="d", user="u", password="p"+ ) as ing:+ ing.ingest_query("SELECT 1")+ assert mock_conn.autocommit is False++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_get_table_schema_restores_autocommit(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, _ = _schema_cursor([])+ mock_conn.autocommit = False+ mock_sdk.connect.return_value = mock_conn++ with RedshiftIngestor(+ host="h", database="d", user="u", password="p"+ ) as ing:+ ing.get_table_schema("t")+ assert mock_conn.autocommit is False++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_list_tables_restores_autocommit(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, _ = _list_tables_cursor([])+ mock_conn.autocommit = False+ mock_sdk.connect.return_value = mock_conn++ with RedshiftIngestor(+ host="h", database="d", user="u", password="p"+ ) as ing:+ ing.list_tables()+ assert mock_conn.autocommit is False++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_transient_connection_sets_autocommit_true(self, mock_sdk):+ """A standalone (transient) call must still enable autocommit for+ the duration of the query."""+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, _ = _make_mock_connection()+ mock_conn.autocommit = False+ mock_sdk.connect.return_value = mock_conn+ mock_conn.cursor.return_value = _make_cursor_with_rows(["id"], [])++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ ing.ingest_table("t")+ # Transient connection is closed; autocommit was set True during the+ # call (we verify it was set by confirming the query completed).+ # The connection is gone, so we just verify no error occurred.+ mock_conn.close.assert_called_once()++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_autocommit_restored_even_when_query_fails(self, mock_sdk):+ """autocommit must be restored on caller-owned connections even when+ the query raises."""+ from semantica.ingest.redshift_ingestor import RedshiftIngestor+ from semantica.utils.exceptions import ProcessingError++ mock_conn = Mock()+ mock_conn.autocommit = False+ mock_sdk.connect.return_value = mock_conn+ bad_cursor = Mock()+ bad_cursor.execute.side_effect = RuntimeError("boom")+ bad_cursor.close = Mock()+ mock_conn.cursor = Mock(return_value=bad_cursor)++ with RedshiftIngestor(+ host="h", database="d", user="u", password="p"+ ) as ing:+ with pytest.raises(ProcessingError):+ ing.ingest_table("t")+ # autocommit restored despite the exception+ assert mock_conn.autocommit is False+++# ---------------------------------------------------------------------------+# Fix #2 — batch_size processes rows per-batch (memory behaviour)+# ---------------------------------------------------------------------------+++class TestBatchFetchMemoryBehaviour:+ """With batch_size, each batch must be converted before the next fetch+ so raw tuples from prior batches are eligible for garbage collection."""++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_batch_produces_correct_total_count(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn+ # 5 rows, batch_size=2 → 3 fetches (2, 2, 1) + empty+ cursor = _make_cursor_with_rows(+ ["id"], [(1,), (2,), (3,), (4,), (5,)]+ )+ mock_conn.cursor.return_value = cursor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ result = ing.ingest_query("SELECT id FROM t", batch_size=2)++ assert result.row_count == 5+ assert result.data == [{"id": i} for i in range(1, 6)]++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_batch_fetchmany_called_per_batch(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn+ cursor = _make_cursor_with_rows(["id"], [(1,), (2,), (3,)])+ mock_conn.cursor.return_value = cursor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ ing.ingest_query("SELECT id FROM t", batch_size=2)++ # fetchmany called 3 times: [1,2], [3], [] (stop)+ assert cursor.fetchmany.call_count == 3+ for c in cursor.fetchmany.call_args_list:+ assert c[0][0] == 2++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_non_batch_path_still_works(self, mock_sdk):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn+ cursor = _make_cursor_with_rows(["id"], [(10,), (20,)])+ mock_conn.cursor.return_value = cursor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ result = ing.ingest_query("SELECT id FROM t")++ assert result.data == [{"id": 10}, {"id": 20}]+ cursor.fetchall.assert_called_once()+++# ---------------------------------------------------------------------------+# Fix #3 — REDSHIFT_SCHEMA env var is honoured when schema arg is omitted+# ---------------------------------------------------------------------------+++class TestSchemaEnvVarFallback:+ """REDSHIFT_SCHEMA must supply the default when no schema= arg is given."""++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_schema_from_env_var(self):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ with patch.dict(+ os.environ,+ {+ "REDSHIFT_HOST": "h",+ "REDSHIFT_DATABASE": "d",+ "REDSHIFT_USER": "u",+ "REDSHIFT_PASSWORD": "p",+ "REDSHIFT_SCHEMA": "analytics",+ },+ ):+ ing = RedshiftIngestor()++ assert ing.schema == "analytics"++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_explicit_schema_arg_overrides_env_var(self):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ with patch.dict(os.environ, {"REDSHIFT_SCHEMA": "analytics"}):+ ing = RedshiftIngestor(+ host="h", database="d", user="u", password="p",+ schema="overridden",+ )++ assert ing.schema == "overridden"++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_schema_defaults_to_public_when_no_env_var(self):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ env = {k: v for k, v in os.environ.items() if k != "REDSHIFT_SCHEMA"}+ with patch.dict(os.environ, env, clear=True):+ ing = RedshiftIngestor(+ host="h", database="d", user="u", password="p"+ )++ assert ing.schema == "public"++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_env_schema_used_in_ingest_table_query(self, mock_sdk):+ """The schema from REDSHIFT_SCHEMA is used when building the table ref."""+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn+ mock_conn.cursor.return_value = _make_cursor_with_rows(["id"], [])++ with patch.dict(os.environ, {"REDSHIFT_SCHEMA": "staging"}):+ ing = RedshiftIngestor(+ host="h", database="d", user="u", password="p"+ )+ # Ensure schema was picked up+ assert ing.schema == "staging"++ ing.ingest_table("orders")+ executed_sql = mock_conn.cursor.return_value.execute.call_args[0][0]+ assert '"staging"."orders"' in executed_sql+++# ---------------------------------------------------------------------------+# Fix #4 — db-all includes db-redshift+# ---------------------------------------------------------------------------+++class TestDbAllIncludesRedshift:+ """pyproject.toml db-all aggregate must reference db-redshift."""++ def test_db_all_references_db_redshift(self):+ """Read pyproject.toml and verify db-all includes db-redshift."""+ import tomllib+ from pathlib import Path++ toml_path = Path(__file__).resolve().parents[1] / "pyproject.toml"+ with open(toml_path, "rb") as f:+ data = tomllib.load(f)++ db_all = data["project"]["optional-dependencies"]["db-all"]+ # db-all is a list of strings like ["semantica[db-snowflake,...]"]+ combined = " ".join(db_all)+ assert "db-redshift" in combined, (+ f"db-all does not reference db-redshift. Got: {db_all}"+ )+++# ---------------------------------------------------------------------------+# Fix #5 — duplicate column labels are disambiguated+# ---------------------------------------------------------------------------+++class TestDisambiguateColumns:+ """_disambiguate_columns must make every label unique."""++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_no_duplicates_unchanged(self):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ result = ing._disambiguate_columns(["id", "name", "score"])+ assert result == ["id", "name", "score"]++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_single_duplicate_gets_suffix(self):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ result = ing._disambiguate_columns(["id", "name", "id"])+ # First occurrence keeps original name; second gets _1+ assert result == ["id", "name", "id_1"]++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_triple_duplicate_gets_incrementing_suffixes(self):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ result = ing._disambiguate_columns(["x", "x", "x"])+ assert result == ["x", "x_1", "x_2"]++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ def test_all_unique_after_disambiguation(self):+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ cols = ["a", "b", "a", "c", "b", "a"]+ result = ing._disambiguate_columns(cols)+ assert len(result) == len(set(result)), "Result contains duplicates"++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_ingest_query_with_duplicate_columns_retains_all_values(self, mock_sdk):+ """A JOIN-style query with two columns named 'id' must not lose the+ second value — it must appear as 'id_1' in the row dict."""+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn++ # Simulate cursor.description with duplicate 'id' labels+ cursor = Mock()+ cursor.description = [+ ("id",) + (None,) * 6,+ ("name",) + (None,) * 6,+ ("id",) + (None,) * 6, # duplicate!+ ]+ cursor.fetchall = Mock(return_value=[(1, "Alice", 99)])+ cursor.execute = Mock()+ cursor.close = Mock()+ mock_conn.cursor = Mock(return_value=cursor)++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ result = ing.ingest_query("SELECT t1.id, name, t2.id FROM ...")++ assert result.columns == ["id", "name", "id_1"]+ assert result.data[0]["id"] == 1+ assert result.data[0]["name"] == "Alice"+ assert result.data[0]["id_1"] == 99++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_ingest_table_with_duplicate_columns_retains_all_values(self, mock_sdk):+ """ingest_table uses _fetch_all which must also disambiguate."""+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn++ cursor = Mock()+ cursor.description = [+ ("val",) + (None,) * 6,+ ("val",) + (None,) * 6, # duplicate!+ ]+ cursor.fetchall = Mock(return_value=[(10, 20)])+ cursor.execute = Mock()+ cursor.close = Mock()+ mock_conn.cursor = Mock(return_value=cursor)++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ result = ing.ingest_table("t")++ assert result.columns == ["val", "val_1"]+ assert result.data[0]["val"] == 10+ assert result.data[0]["val_1"] == 20++ @patch("semantica.ingest.redshift_ingestor.REDSHIFT_AVAILABLE", True)+ @patch("semantica.ingest.redshift_ingestor._redshift_connector")+ def test_batch_with_duplicate_columns(self, mock_sdk):+ """Duplicate disambiguation must also apply when batch_size is set."""+ from semantica.ingest.redshift_ingestor import RedshiftIngestor++ mock_conn, _ = _make_mock_connection()+ mock_sdk.connect.return_value = mock_conn++ cursor = Mock()+ cursor.description = [+ ("score",) + (None,) * 6,+ ("score",) + (None,) * 6, # duplicate!+ ]+ _batches = [[(1, 2)], [(3, 4)], []]+ cursor.fetchmany = Mock(side_effect=_batches)+ cursor.execute = Mock()+ cursor.close = Mock()+ mock_conn.cursor = Mock(return_value=cursor)++ ing = RedshiftIngestor(host="h", database="d", user="u", password="p")+ result = ing.ingest_query("SELECT ...", batch_size=1)++ assert result.columns == ["score", "score_1"]+ assert result.data[0] == {"score": 1, "score_1": 2}+ assert result.data[1] == {"score": 3, "score_1": 4}