Skip to main content

A pure-python in-memory mock of rethinkdb (formerly MockThink)

Project description

build coverage python rethinkdb PyPI version

RethinkDB Mock

Formerly known as MockThink by Scivey, this is a continuation of that work released under a new name.

Rethinkdb-mock is an in-process Python clone of RethinkDB's API. For testing.

MockThink provides a stub connection object which can be passed to normal ReQL queries. Instead of being serialized and sent to the server, the ReQL AST is run through an interpreter in the same process. "Tables" and "databases" are based on data given to the MockThink constructor.

Avoiding network calls (for tests themselves as well as setup/teardown) makes testing queries with MockThink orders of magnitude faster.

Install

pip install rethinkdb-mock
# or
pipenv install --dev rethinkdb-mock

Usage

Basic

    from pprint import pprint
    from rethinkdb_mock import MockThink
    import rethinkdb as r

    db = MockThink({
        'dbs': {
            'tara': {
                'tables': {
                    'people': [
                        {'id': 'john-id', 'name': 'John'},
                        {'id': 'sam-id', 'name': 'Sam'}
                    ]
                }
            }
        }
    })

    with db.connect() as conn:
        result = r.db('tara').table('people').map(
            lambda doc: doc.merge({'also_name': doc['name']})
        ).run(conn)
        pprint(list(result))

        # [
        #    {'also_name': 'John', 'id': 'john-id', 'name': 'John'},
        #    {'also_name': 'Sam', 'id': 'sam-id', 'name': 'Sam'}
        # ]

        r.db('tara').table('people').update(
            {'likes_fonz': True}
        ).run(conn)

        result = r.db('tara').table('people').run(conn)
        pprint(list(result))

        # [
        #    {'id': 'john-id', 'likes_fonz': True, 'name': 'John'},
        #    {'id': 'sam-id', 'likes_fonz': True, 'name': 'Sam'}
        # ]

    # data is reset at exit of context manager above

    with db.connect() as conn:
        result = r.db('tara').table('people').run(conn)
        pprint(list(result))
        # [
        #    {'id': 'john-id', 'name': 'John'},
        #    {'id': 'sam-id', 'name': 'Sam'}
        # ]

Full support for secondary indexes

    from pprint import pprint
    from rethinkdb_mock import MockThink
    import rethinkdb as r

    db = MockThink({
        'dbs': {
            'tara': {
                'tables': {
                    'people': [
                        {'id': 'john-id', 'first_name': 'John', 'last_name': 'Generic'},
                        {'id': 'sam-id', 'first_name': 'Sam', 'last_name': 'Dull'},
                        {'id': 'adam-id', 'first_name': 'Adam', 'last_name': 'Average'}
                    ]
                }
            }
        }
    })

    with db.connect() as conn:

        r.db('tara').table('people').index_create(
            'full_name',
            lambda doc: doc['last_name'] + doc['first_name']
        ).run(conn)

        r.db('tara').table('people').index_wait().run(conn)

        result = r.db('tara').table('people').get_all(
            'GenericJohn', 'AverageAdam', index='full_name'
        ).run(conn)
        pprint(list(result))
        # {'id': 'john-id', 'first_name': 'John', 'last_name': 'Generic'},
        # {'id': 'adam-id', 'first_name': 'Adam', 'last_name': 'Average'}

Testing

The most confusing test failures are those caused by errors in test frameworks and harnesses themselves. This means they need to be tested very thoroughly.

The main testing is a suite of functional tests which are targeted at the individual query level, e.g. testing all the ways in which r.merge might be called.

These are all complete ReQL queries, but avoid complexity beyond the target query to make failures easier to diagnose.

The integration tests cover more complicated queries, e.g. eq_join->map->eq_join->map.

Both the functional and integration tests have two modes of execution, rethinkdb_mock and rethink. The second mode runs the same tests against a running RethinkDB instance, and is much slower due to the network calls. rethinkdb_mock mode is for testing MockThink's behavior against our expectations; rethink mode is for testing our expectations against reality.

Run tests

# Install dependencies
pip install pipenv
pipenv sync --dev

# Run the unit tests
pipenv run test

Run tests against a live server

The docker folder contains a dockerfile that grabs rethinkdb and runs the python tests

Simply running the below command outputs the results

docker-compose up --build

Code formatting

The tox.ini file contains the configuration for code formatting

# Install dependencies
pip install pipenv
pipenv sync --dev

# Format imports
pipenv run isort

# Check styling with Flake8
pipenv run lint

License

The MIT License (MIT)

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

rethinkdb_mock-0.10.1.tar.gz (23.0 kB view details)

Uploaded Source

Built Distribution

rethinkdb_mock-0.10.1-py3-none-any.whl (23.4 kB view details)

Uploaded Python 3

File details

Details for the file rethinkdb_mock-0.10.1.tar.gz.

File metadata

  • Download URL: rethinkdb_mock-0.10.1.tar.gz
  • Upload date:
  • Size: 23.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12

File hashes

Hashes for rethinkdb_mock-0.10.1.tar.gz
Algorithm Hash digest
SHA256 8110a7d4b077dd08fd5ad320e9a5d12c8eda1c7bb814e88b8aa861c7cb958e64
MD5 910fedd51c2a7bc02244868851f50a16
BLAKE2b-256 85f3eb8ffdb9e926fbe87226a09a34dd922bca5d35821312b9062f48c31de4ba

See more details on using hashes here.

File details

Details for the file rethinkdb_mock-0.10.1-py3-none-any.whl.

File metadata

  • Download URL: rethinkdb_mock-0.10.1-py3-none-any.whl
  • Upload date:
  • Size: 23.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12

File hashes

Hashes for rethinkdb_mock-0.10.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9873ea9d63c6aee4c9ef6c0e5a438d993ffd79c92fc0dda1ad6301209aebf2da
MD5 fccf0884e7b81f2fa38de473d3805e28
BLAKE2b-256 3008d012fd494837ded5373edf75c0eafaafbbeedcf8137026b8d7ad5847cef0

See more details on using hashes here.

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