pgmock
raw JSON → 1.3.7 verified Fri May 01 auth: no python
A library for mocking Postgres queries in Python tests. Current version 1.3.7, stable release cadence.
pip install pgmock Common errors
error pgmock.exceptions.MockDBNotConnectedError: No connection established. Please create a MockPGClient instance first. ↓
cause Trying to use execute() before adding any query or without calling connect() if using context manager.
fix
Ensure you instantiate MockPGClient and add queries before calling execute().
error ModuleNotFoundError: No module named 'pgmock' ↓
cause pgmock is not installed or installed in a different environment.
fix
Run 'pip install pgmock' in the correct Python environment.
error TypeError: 'MockPGClient' object does not support item assignment ↓
cause Trying to assign to client['table'] or similar dict-like usage instead of using add_query().
fix
Use add_query() method to register queries, e.g., client.add_query('SELECT * FROM table', result=[(1,)]).
Warnings
gotcha MockPGClient.execute() requires queries to be added with add_query() before execution, otherwise raises MockDBNotConnectedError ↓
fix Always register expected queries with add_query() or use autospec feature if available.
deprecated The old import from pgmock import PGMock has been deprecated and will be removed. ↓
fix Use from pgmock import MockPGClient instead.
breaking In version 1.3.0, the .execute() method now returns a ResultProxy instead of a list. Old code expecting list will break. ↓
fix Use .fetchall(), .fetchone() or .fetchmany() methods on the returned result.
Imports
- MockPGClient
from pgmock import MockPGClient - MockDBNotConnectedError
from pgmock import MockDBNotConnectedError - MockError
from pgmock import MockError
Quickstart
from pgmock import MockPGClient
client = MockPGClient()
# Simulate a query result
client.add_query("SELECT 1", result=[("1",)])
result = client.execute("SELECT 1")
print(result.fetchall()) # [('1',)]