Skip to main content

Pytest helpers and viur-core stand-ins for testing ViUR packages without App Engine.

Project description

viur-light-mock

Pytest helpers and viur.core.* stand-ins for testing ViUR packages without App Engine.

Tests License: MIT

What it does

viur-light-mock is a pytest plugin that injects lightweight stand-ins for the viur.core.* modules into sys.modules before your tests are collected. That lets a package import from viur.core import db, utils, errors, … in production code while the tests run hermetically — no Google App Engine stack, no Datastore connection, fast cold-start.

The plugin auto-discovers via the pytest11 entry-point. No conftest.py boilerplate, no pytest_plugins = line — install the package and the fixtures and mocks are there.

Requirements

  • Python ≥ 3.12
  • pytest ≥ 8

Install

The PyPI distribution name is spltz-viur-light-mock (the experimental spltz- prefix marks it pre-1.0). The Python import path stays viur.light_mock — namespace package, no rename in user code.

pip install spltz-viur-light-mock

For a ViUR-based package that wants to use it:

[project.optional-dependencies]
test = ["pytest", "pytest-cov", "spltz-viur-light-mock>=0.1"]

Usage

Just write tests as if viur.core were real:

# tests/test_my_module.py
def test_something(db_state, freeze_time, make_query):
    from viur.core.db import Entity, Key
    from my_package import MyAdapter

    existing = Entity(Key("order_revision", 1))
    existing["revision_index"] = 7
    make_query(single=existing)

    MyAdapter().do_something()

    assert any(p["revision_index"] == 8 for p in db_state.put_calls)

The fixtures used here (db_state, freeze_time, make_query) are auto-discovered from the plugin — you don't need to import them or wire them up in a conftest.py.

What's mocked

Module Stand-in surface
viur.core Plus identity decorators exposed, force_post, skey
viur.core.db Key, Entity, Query, SortOrder, Get, Put, Delete, AllocateIDs
viur.core.utils utcNow() — freezable
viur.core.errors Unauthorized, Forbidden, BadRequest, NotFound
viur.core.current user, request slots with .get/.set
viur.core.skeleton Skeleton, SkeletonInstance, DatabaseAdapter
viur.core.tasks PeriodicTask decorator (no-op, but tags the wrapped function)
viur.core.render.json.default CustomJsonEncoder aliased to stdlib json.JSONEncoder

Fixtures

Fixture Purpose
db_state Handle on the in-memory datastore (store, put_calls, delete_calls, …)
freeze_time Pin utils.utcNow() to a controllable value
make_query Pre-populate the next db.Query(...) with single/many results
patched_user Set current.user.get() to a fake user dict
_viur_light_mock_reset_state Autouse — resets the fake datastore singleton between tests

Public API

If you need to drive the mocks from your own conftest.py (for example to add a project-specific stand-in), import directly:

from viur.light_mock import (
    install_viur_core_mocks,
    FakeKey, FakeEntity, FakeQuery, FakeSortOrder, DbState,
)

Why not just use unittest.mock?

You can — but the fake viur.core modules here are real Python modules in sys.modules, so from viur.core import db resolves naturally without patching every test, and isinstance checks against db.Entity etc. work out of the box.

Development

git clone https://github.com/sprengplatz/viur-light-mock
cd viur-light-mock
pip install -e ".[dev]"
pytest                  # 100% coverage required

License

MIT — see LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

spltz_viur_light_mock-0.1.0.tar.gz (15.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

spltz_viur_light_mock-0.1.0-py3-none-any.whl (13.5 kB view details)

Uploaded Python 3

File details

Details for the file spltz_viur_light_mock-0.1.0.tar.gz.

File metadata

  • Download URL: spltz_viur_light_mock-0.1.0.tar.gz
  • Upload date:
  • Size: 15.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for spltz_viur_light_mock-0.1.0.tar.gz
Algorithm Hash digest
SHA256 cf85133f9278e7351d09ffeaef7f4f1b327e0a6381662f0125e59adba4f583f0
MD5 6c5ed44041c2d59a229fe47fa58ebe9b
BLAKE2b-256 4a4c831a47e8f44f364e0a8772a39d30c46b27ebb06573173fb1f0f5a1500781

See more details on using hashes here.

Provenance

The following attestation bundles were made for spltz_viur_light_mock-0.1.0.tar.gz:

Publisher: release.yml on sprengplatz/viur-light-mock

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file spltz_viur_light_mock-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for spltz_viur_light_mock-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 17580b8af6695b3ba2c7640380e19fd0e820176fa942faf52f35b5e4d86898f2
MD5 f654afe959bf1f13c645d0f6ff1d8292
BLAKE2b-256 e411570b80a5121247d03d7b9263837d02188b064b43f3519c45b4532891d20e

See more details on using hashes here.

Provenance

The following attestation bundles were made for spltz_viur_light_mock-0.1.0-py3-none-any.whl:

Publisher: release.yml on sprengplatz/viur-light-mock

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page