Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (pypi.python.org).
Help us improve Python packaging - Donate today!

Mongo database mocking with fixtures

Project Description

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/BSON or YAML format. Under the hood we use the mongomock library, that you should consult for documentation on how to use MongoDB mock objects. If suitable you can also use a real MongoDb server.

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.

You can also choose to use a real MongoDB server for your tests. In that case you might also want to configure the hostname and/or the credentials if you don’t want to stick with the default (localhost and no credentials). Use the following configuration values in your pytest.ini to adapt the settings to your needs:

[pytest]
humongous_engine = pymongo
humongous_host = mongodb://user:passwd@server.tld
humongous_dbname = mydbname

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. If your fixture file is in JSON/BSON format you can also use BSON specific types like $oid, $date, etc.

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.

Release History

Release History

This version
History Node

1.3.0

History Node

1.2.4

History Node

1.2.3

History Node

1.2.2

History Node

1.1.0

History Node

1.0.5

History Node

1.0.3

Download Files

Download Files

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

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
humongous-1.3.0-py2.py3-none-any.whl (6.6 kB) Copy SHA256 Checksum SHA256 py2.py3 Wheel Oct 16, 2015

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting