Package: eve-mocker
Mocking tool forEve powered REST API, based on the excellentHTTPretty, aimed to be used in your unit tests, when you rely on an Eve API.FeaturesEve-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 requestsHandle 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 withrequests.It renders only JSON, no XML yet.Installing$ pip install eve-mockerUsageYou should readHTTPrettydocumentation before starting.Here is a basic example:fromhttprettyimportHTTPrettyimportrequestsHTTPretty.enable()EveMocker("http://myapi.com/api/")response=requests.get("http://myapi.com/api/mymodel/")assertresponse.status_code==200assertresponse.json()=={"_items":[]}HTTPretty.disable()You can add data directly withEveMocker.set_resource:fromhttprettyimportHTTPrettyimportrequestsHTTPretty.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/")assertresponse.status_code==200assertresponse.json()=={"_items":mymodel_data}HTTPretty.disable()Alternatively, you useEveMockerwithin a context manager, and it will automatically callHTTPretty.enable()andHTTPretty.disable().fromeve_mockerimportEveMockerimportrequestswithEveMocker("http://myapi.com/api/"):response=requests.get("http://myapi.com/api/mymodel/")assertresponse.status_code==200assertresponse.json()=={"_items":[]}Check the tests (intest_eve_mocker.py) if you want to check more examples.Getting startedLet’s say you want to test the following class stored inremote_items.pythat need to call an Eve powered REST API:fromurlparseimporturljoinfromfunctoolsimportpartialimportrequestsAPI_URL="http://my-eve-api.com/api/"classRemoteItems(object):def__init__(self,api_url=API_URL):self.api_url=api_urlself.endpoint_url=partial(urljoin,self.api_url)defget_latest(self):r=requests.get(self.endpoint_url("items"))r.raise_for_status()returnr.json().get("_items",[])Here is how you can do it with Eve-Mocker:fromeve_mockerimportEveMockerimportunittestfromremote_itemsimportRemoteItems,API_URLclassTestRemoteItems(unittest.TestCase):deftestGetLatestItems(self):items=[{"_id":"fakeid","content":"my content"},{"_id":"fakeid2","content":"another_content"}]withEveMocker(API_URL)aseve_mocker:# We feed EveMocker DB with some itemseve_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 theexamplesdirectory.Advanced UsageEveMockertakes two additonals arguments,default_pkif you need a primary key other than_id, andpk_mapswhich is a mapping resource => primary key:{"resource": "pk_field", "resource2": "pk_field"}.License (MIT)Copyright (c) 2013 Thomas SileoPermission 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.