Welcome, guest | Sign In | My Account | Store | Cart

Notice! PyPM is being replaced with the ActiveState Platform, which enhances PyPM’s build and deploy capabilities. Create your free Platform account to download ActivePython or customize Python with the packages you require and get automatic updates.

Download
ActivePython
INSTALL>
pypm install eve-mocker

How to install eve-mocker

  1. Download and install ActivePython
  2. Open Command Prompt
  3. Type pypm install eve-mocker
 Python 2.7Python 3.2Python 3.3
Windows (32-bit)
Windows (64-bit)
Mac OS X (10.5+)
Linux (32-bit)
Linux (64-bit)
0.1.0 Available View build log
 
Author
License
MIT
Dependencies
Imports
Lastest release
version 0.1.0 on Jan 9th, 2014

Mocking tool for Eve powered REST API, based on the excellent HTTPretty, aimed to be used in your unit tests, when you rely on an Eve API.

Features

Eve-Mocker doesn't try to replicate every Eve features, by design, it doesn't need any Eve settings files, doesn't support schema validation and more advanced features. Don't hesitate to contribute if you need more complex features.

  • Everything is stored in memory (self.items)
  • Support all methods except HEAD requests
  • Handle ETags, and always return meaningful status code, like Eve.
  • Partial support of filtering and sorting (only mongo query syntax)
  • No need to change your code for testing, HTTPretty does everything for you, it works well with requests.
  • It renders only JSON, no XML yet.

Installing

System Message: ERROR/3 (<string>, line 22)

Unknown directive type "code-block".

.. code-block::

    $ pip install eve-mocker


Usage

You should read HTTPretty documentation before starting.

Here is a basic example:

System Message: ERROR/3 (<string>, line 34)

Unknown directive type "code-block".

.. code-block:: python

    from httpretty import HTTPretty
    import requests

    HTTPretty.enable()
    EveMocker("http://myapi.com/api/")

    response = requests.get("http://myapi.com/api/mymodel/")
    assert response.status_code == 200
    assert response.json() == {"_items": []}

    HTTPretty.disable()

You can add data directly with EveMocker.set_resource:

System Message: ERROR/3 (<string>, line 50)

Unknown directive type "code-block".

.. code-block:: python

    from httpretty import HTTPretty
    import requests

    HTTPretty.enable()
    eve_mocker = EveMocker("http://myapi.com/api/")

    mymodel_data = [{"_id": "fakeid", "content": "mycontent"}]
    eve_mocker.set_resource("mymodel", mymodel_data)

    response = requests.get("http://myapi.com/api/mymodel/")
    assert response.status_code == 200
    assert response.json() == {"_items": mymodel_data}

    HTTPretty.disable()


Alternatively, you use EveMocker within a context manager, and it will automatically call HTTPretty.enable() and HTTPretty.disable().

System Message: ERROR/3 (<string>, line 70)

Unknown directive type "code-block".

.. code-block:: python

    from eve_mocker import EveMocker
    import requests

    with EveMocker("http://myapi.com/api/"):
        response = requests.get("http://myapi.com/api/mymodel/")
        assert response.status_code == 200
        assert response.json() == {"_items": []}

Check the tests (in test_eve_mocker.py) if you want to check more examples.

Getting started

Let's say you want to test the following class stored in remote_items.py that need to call an Eve powered REST API:

System Message: ERROR/3 (<string>, line 87)

Unknown directive type "code-block".

.. code-block:: python

    from urlparse import urljoin
    from functools import partial
    import requests

    API_URL = "http://my-eve-api.com/api/"


    class RemoteItems(object):
        def __init__(self, api_url=API_URL):
            self.api_url = api_url
            self.endpoint_url = partial(urljoin, self.api_url)

        def get_latest(self):
            r = requests.get(self.endpoint_url("items"))
            r.raise_for_status()
            return r.json().get("_items", [])


Here is how you can do it with Eve-Mocker:

System Message: ERROR/3 (<string>, line 109)

Unknown directive type "code-block".

.. code-block:: python

    from eve_mocker import EveMocker
    import unittest
    from remote_items import RemoteItems, API_URL

    class TestRemoteItems(unittest.TestCase):
        def testGetLatestItems(self):
            items = [{"_id": "fakeid", "content": "my content"},
                     {"_id": "fakeid2", "content": "another_content"}]
            with EveMocker(API_URL) as eve_mocker:
                # We feed EveMocker DB with some items
                eve_mocker.set_resource("items", items)

                remote_items = RemoteItems()
                latest_items = remote_items.get_latest()

                self.assertEqual(sorted(latest_items), sorted(items))

    if __name__ == '__main__':
        unittest.main()


You can find these two files in the examples directory.

Advanced Usage

EveMocker takes two additonals arguments, default_pk if you need a primary key other than _id, and pk_maps which is a mapping resource => primary key: {"resource": "pk_field", "resource2": "pk_field"}.

License (MIT)

Copyright (c) 2013 Thomas Sileo

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Subscribe to package updates

Last updated Jan 9th, 2014

What does the lock icon mean?

Builds marked with a lock icon are only available via PyPM to users with a current ActivePython Business Edition subscription.

Need custom builds or support?

ActivePython Enterprise Edition guarantees priority access to technical support, indemnification, expert consulting and quality-assured language builds.

Plan on re-distributing ActivePython?

Get re-distribution rights and eliminate legal risks with ActivePython OEM Edition.