{"id":9954,"library":"mock-alchemy","title":"Mock Alchemy","description":"mock-alchemy provides mock helpers for SQLAlchemy, allowing developers to test SQLAlchemy-dependent code without needing a live database connection. The current version is 0.2.6, offering compatibility with SQLAlchemy 2.0 and Python 3.7+. Releases are made periodically to add new features, support newer SQLAlchemy versions, and fix bugs.","status":"active","version":"0.2.6","language":"en","source_language":"en","source_url":"https://github.com/rajivsarvepalli/mock-alchemy","tags":["SQLAlchemy","mocking","testing","unit-testing"],"install":[{"cmd":"pip install mock-alchemy","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Core dependency for mocking its objects and API.","package":"SQLAlchemy","optional":false}],"imports":[{"note":"The main mocking class resides within the `mocking` submodule, not directly under `mock_alchemy`.","wrong":"from mock_alchemy import UnifiedAlchemyMagicMock","symbol":"UnifiedAlchemyMagicMock","correct":"from mock_alchemy.mocking import UnifiedAlchemyMagicMock"}],"quickstart":{"code":"from mock_alchemy.mocking import UnifiedAlchemyMagicMock\nfrom sqlalchemy.orm import declarative_base, Mapped, mapped_column\nfrom sqlalchemy import String, Integer\n\n# Define a simple SQLAlchemy model using SQLAlchemy 2.0+ syntax\nBase = declarative_base()\n\nclass User(Base):\n    __tablename__ = \"users\"\n    id: Mapped[int] = mapped_column(Integer, primary_key=True)\n    name: Mapped[str] = mapped_column(String)\n\n# Create a mock session instance\nsession = UnifiedAlchemyMagicMock()\n\n# Add some mock data to the session\nsession.add(User(id=1, name=\"Alice\"))\nsession.add(User(id=2, name=\"Bob\"))\nsession.add(User(id=3, name=\"Charlie\"))\n\n# Perform mock queries\nuser_alice = session.query(User).filter(User.name == \"Alice\").first()\nprint(f\"Found user by name: {user_alice.name}\")\n\nall_users = session.query(User).all()\nprint(f\"All users: {[u.name for u in all_users]}\")\n\nuser_count = session.query(User).count()\nprint(f\"Total users: {user_count}\")\n\n# Simulate a deletion\nsession.query(User).filter(User.id == 2).delete()\nremaining_users = session.query(User).all()\nprint(f\"Users after deletion: {[u.name for u in remaining_users]}\")\n","lang":"python","description":"This quickstart demonstrates how to set up a mock SQLAlchemy session using `UnifiedAlchemyMagicMock`, add mock data, and perform common query operations like `filter`, `first`, `all`, `count`, and `delete` without connecting to a real database. It uses SQLAlchemy 2.0+ declarative syntax for the model."},"warnings":[{"fix":"Upgrade your Python interpreter to 3.7 or newer. For projects requiring Python 2.7, use `mock-alchemy` versions <0.2.0 (e.g., `0.1.x`).","message":"Version 0.2.0 dropped support for Python 2.7 and older Python 3 versions (<3.7). Using `mock-alchemy>=0.2.0` with these Python versions will result in errors.","severity":"breaking","affected_versions":">=0.2.0"},{"fix":"Ensure `mock-alchemy` is version 0.2.6 or newer when working with SQLAlchemy 2.0+.","message":"Support for SQLAlchemy 2.0's API changes was fully implemented in version 0.2.6. Using SQLAlchemy 2.0+ with older `mock-alchemy` versions might lead to compatibility issues or unexpected behavior.","severity":"breaking","affected_versions":"<0.2.6"},{"fix":"Upgrade to `mock-alchemy` version 0.2.5 or later for reliable `scalar()` method mocking.","message":"The `scalar()` method for mocking queries was introduced in `v0.2.4`, and its implementation was improved in `v0.2.5`. Older versions will lack this functionality or have incorrect behavior.","severity":"gotcha","affected_versions":"<0.2.5"},{"fix":"Upgrade to `mock-alchemy` version 0.2.2 or later to ensure `get()` works with various singular value types.","message":"A bug in `get()` that caused it to fail with non-integer singular values was fixed in `v0.2.2`. Prior versions might not correctly handle such queries.","severity":"gotcha","affected_versions":"<0.2.2"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Upgrade your Python environment to 3.7 or newer. If downgrading `mock-alchemy` is not an option, consider using `mock-alchemy` 0.1.x for Python 2.7 support.","cause":"Attempting to run `mock-alchemy` 0.2.0 or newer on Python 2.7 or an unsupported Python 3 version that doesn't fully support type hints.","error":"TypeError: 'type' object is not subscriptable"},{"fix":"Upgrade `mock-alchemy` to version 0.2.4 or later. For improved `scalar()` behavior, upgrade to 0.2.5 or later.","cause":"Trying to use the `.scalar()` method on a mock session with a `mock-alchemy` version older than 0.2.4.","error":"AttributeError: 'MagicMock' object has no attribute 'scalar'"},{"fix":"Upgrade `mock-alchemy` to version 0.2.6 or newer to ensure full compatibility with SQLAlchemy 2.0's API.","cause":"This error or similar SQLAlchemy API compatibility issues can occur when using SQLAlchemy 2.0+ with `mock-alchemy` versions older than 0.2.6.","error":"TypeError: Class 'Mapper' is not a collection type and is not a mapped class"}]}