Giter Club home page Giter Club logo

db-mock's Introduction

dbmock

This is dbmock, the convenient DB-API 2.0 and async database mocking library. With dbmock you can test your database layer code without having to have a database running for your tests. Have a look at the following example:

# First we have a function
def get_book_by_author_name(db, name):
    autor_id = db.execute(
        "SELECT id FROM users where name = :name;",
        {'name': name}).fetchval()
    return db.execute(
        "select title from books where author_id = :id limit 1",
        {'id': author_id}
    ).fetchval()

# Then we define our expectations to the function. Asynchronous database
# drivers are also supported by the `DBMockAsync` and `DBMockDatabases`
# classes.
from db_mock import DBMockSync
mock = (
    DBMockSync()
    .expect_stmt("select id from users where name = :name",
                 {'name': 'Douglas Adams'})
    .expect_result([(42,)])
    .expect_stmt("select title from books where author_id = :id limit 1",
                 {'id': 42})
    .expect_result([('The Hitchhikers Guide To The Galaxy',)])
)

# Then we run the function. If not all SQL queries are executed in the
# expected order, an exception will be raised.
with mock as db:
    title = get_book_by_author_name(db, 'Douglas Adams')
    assert title == u'The Hitchikers Guide To The Galaxy'

Table of Contents

Installation

db-mock is distributed on PyPI as a universal wheel and is available on Linux/macOS (and possibly Windows) and supports Python 2.7/3.5+ and PyPy/PyPy3.

$ pip install db-mock

License

db-mock is distributed under the terms of both

at your option.

db-mock's People

Contributors

smokey42 avatar

Watchers

 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.