Skip to content

Add unit tests for PostgresSchemaInspector #3

Description

@NoMo-101

`tests/test_inspector.py` currently exists but is empty. We need real unit test coverage for `PostgresSchemaInspector` now that the db layer has been refactored to be database-agnostic (see models.py, interfaces.py, postgres_connector.py, postgres_inspector.py).

What to test

Three methods on `PostgresSchemaInspector`:

  • `fetch_tables()` — returns `list[TableInfo]`
  • `fetch_columns()` — returns `list[ColumnInfo]`, including the `is_nullable` string-to-bool conversion (`'YES'`/`'NO'` → `True`/`False`)
  • `fetch_foreign_keys()` — returns `list[ForeignKeyInfo]`

Approach

`PostgresSchemaInspector.init` takes anything implementing `DBConnectorInterface` — not necessarily a real `PostgresConnector`. Rather than testing against a live Postgres database, build a `MockConnector` that implements `DBConnectorInterface` and returns hardcoded rows from `execute_query()`. This lets tests run without any real database connection.

Example shape:

class MockConnector(DBConnectorInterface):
    def __init__(self, fake_rows):
        self.fake_rows = fake_rows
    def execute_query(self, sql, params=None):
        return self.fake_rows
    def connect(self): return True
    def disconnect(self): pass
    @property
    def is_connected(self): return True

Then feed `PostgresSchemaInspector` a `MockConnector` with known fake rows, and assert the returned dataclass objects have the correct fields.

What "done" looks like

  • At least one test per method (`fetch_tables`, `fetch_columns`, `fetch_foreign_keys`)
  • A test specifically confirming `is_nullable` converts `'YES'` → `True` and `'NO'` → `False`
  • Tests run with pytest and don't require a live database connection

Metadata

Metadata

Assignees

Labels

good first issueGood for newcomerstestingWriting or updating automated tests

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions