Giter VIP home page Giter VIP logo

pytest-async-mongodb's Introduction

What is this?

This is a pytest plugin based on pytest-mongodb and mongomock that enables you to test your code that relies on a callback- or Future-based API for non-blocking access to a MongoDB and expects 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 database fixtures on the top-level directory of your package you have to specify a directory where pytest-async-mongodb 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]
async_mongodb_fixture_dir =
  tests/unit/fixtures

async_mongodb_fixtures =
  fixture_1
  fixture_2

pytest-async-mongodb would then look for files ending in .yaml or .json in that directory.

Unlike pytest-mongodb, you cannot specify a real MongoDB connection with the pymongo client.

Basic usage

After you configured pytest-async-mongodb 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 fixture-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 your test-function by using the async_mongodb fixture like so:

@pytest.mark.asyncio
async def test_players(async_mongodb):
    manuel = await async_mongodb.players.find_one({'name': 'Manuel'})
    assert manuel['surname'] == 'Neuer'

For further information refer to the mongomock documentation.

pytest-async-mongodb's People

Contributors

gonzaloverussa avatar assigdev avatar xzased avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.