Skip to main content

Mongo database mocking with fixtures

Project description

https://img.shields.io/pypi/v/humongous.svg https://travis-ci.org/mdomke/humongous.svg?branch=master https://img.shields.io/pypi/l/humongous.svg

What is this?

This is a pytest plugin, that enables you to test your code that relies on a database connection to a MongoDB and expectes certain data to be present. It allows you to specify fixtures for database collections in JSON or YAML format. Under the hood we use the mongomock library, that you should consult for documentation on how to use MongoDB mock objects.

Configuration

If you don’t want to put your fixtures on the top-level directory of your package you have to specify a directory where humongous looks for your data definitions.

To do so put a line like the following under the pytest section of your pytest.ini-file put a:

[pytest]
humongous_basedir =
  tests/unit/fixtures

humongous would then look for files ending in .yaml or .json in that directory.

Basic usage

After you configured humongous so that it can find your fixtures you’re ready to specify some data. Regardless of the markup language you choose, the data is provided as a list of documents (dicts). The collection that these documents are being inserted into is given by the filename of your fixutre-file. E.g.: If you had a file named players.yaml with the following content:

-
  name: Mario
  surname: Götze
  position: striker

-
  name: Manuel
  surname: Neuer
  position: keeper

you’d end up with a collection players that has the above player definitions inserted.

You get ahold of the database in you test-function by using the humongous fixture like so:

def test_players(humongous):
    assert "players" in humongous.collection_names()
    manuel = humongous.players.find_one({"name": "Manuel"})
    assert manuel["surname"] == "Neuer"

For further information refer to the mongomock documentation.

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

humongous-1.0.3.tar.gz (5.1 kB view hashes)

Uploaded source

Supported by

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