{ "info": { "author": "See AUTHORS", "author_email": "maarten@adimian.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 4 - Beta", "License :: OSI Approved :: MIT License", "Operating System :: MacOS :: MacOS X", "Operating System :: Microsoft :: Windows", "Operating System :: POSIX", "Programming Language :: Python", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3.4", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6" ], "description": "[![Build Status](https://travis-ci.com/maarten-dp/requests-flask-adapter.svg?branch=master)](https://travis-ci.com/maarten-dp/requests-flask-adapter)\n\n### Purpose\n\nFlaskAdapter is a requests adapter intended to allow its user to call Flask app endpoints, with requests, without having to run a Flask server.\n\nIts main uses include building integration tests and client tests without having to resort to multithreading/multiprocessing/running an instance in another shell/docker to spawn a running Flask app. In doing so, you are able to to call the endpoints you wish to test with your client. It can also be used as an alternative to the Flask test_client, unlocking the well-known and well-loved interface of requests in your unittests, because god knows I love those `json.loads(res.data.decode('utf-8'))` statements in my tests.\n\n### Using FlaskAdapter as a test client\n\nYou can swap out the flask test client for a requests interface in two ways.\n\nThe first would be to import the requests_flask_adapter session, which is basically a session subclassed from a requests Session, but allows the registering of apps.\n\n```python\nfrom requests_flask_adapter import Session\nfrom my_production_code import setup_my_app\nfrom pytest import fixture\n\n@fixture\ndef session():\n app = setup_my_app()\n Session.register('http://my_app', app)\n return Session()\n\n\ndef test_it_runs_my_test(session):\n result = session.get(\"http://my_app/my_endpoint\", params={'some': 'params'})\n assert result.json() == {'nailed': 'it'}\n\n```\n\nif you don't want to or, for some reason, can't rely on the `requests_flask_adapter.Session`, you can also use the requests_flask_adapter helper function to monkey patch the requests Session. For now, it heavily depends on import order, so make sure to patch it before importing the Session for your tests.\n\n```python\nfrom requests_flask_adapter.helpers import patch_requests\npatch_requests([\n ('http://patched_app', app),\n ('http://another_patched_app', another_app)\n])\n\n```\n\nAnd in your tests you can now run code that imports the requests.Session\n\n```python\ndef test_it_runs_code_that_imports_requests():\n result = my_code_that_imports_requests_and_does_something()\n assert result == [':ok_hand:']\n\n```\n\n### Using FlaskAdapter for client testing\n\nSimilarly, FlaskAdapter is very effective for testing a client that is written with requests.\nAnd once again, without having to run a live server of your flask app.\n\n```python\nfrom requests_flask_adapter import Session\nfrom my_production_code import setup_my_app, User\nfrom my_client import Client\nfrom pytest import fixture\n\n\n@fixture\ndef client():\n app = setup_my_app()\n Session.register('http://my_app', app)\n return Client(\n base_url='http://my_app',\n session=Session(), # monkeypatch if your client isn't accepting another session.\n auth=('Scanlan', 'b3st_b4rd_Exandr!a'),\n )\n\n\ndef test_it_gets_a_user_list(client):\n users = client.users()\n assert users == ['vex', 'vax']\n\n\ndef test_it_can_upload_a_timesheet(client):\n with open('data/timesheet.xls', 'r') as fh:\n client.upload_timesheet(fh)\n user = User.query.get(1)\n assert user.hours_worked_this_month == 8\n\n```\n\n### Using FlaskAdapter for cross app integration tests\n\nAnd just because I need to bloat this readme a bit to validate this project, I'm throwing in \"integration testing\" as one of its functionalities.\nOf course, these integration tests require you to have access to the source code of the flask apps you're trying to test.\n\nSo, here's an example.\n\nLet's assume the your team owns and maintains the following codebases:\n - A webshop application that's also keeping track of sales, users visited and other stats from the last hour. These stats are accessible though an endpoint in your app.\n - An ETL script that periodically runs and collects realtime stats from your webshop.\n - A timeseries database that stores the data extracted by your ETL script\n\nUsing the data stored in your timeseries database, you have a reporting script that you run once per month to determine peak hours, what product is most popular and during which hours, which amount of users showed interest in which products, which products are falling in and out of trending, etc.\n\nSeeing as these codebases are still actively under construction, you want to make sure future implementations don't introduce regressions in the entire chain.\n\n```python\nfrom datetime import datetime, timedelta\n\nfrom pytest import fixture\nfrom requests_flask_adapter import Session\nfrom my_webshop_app import app as feeder_app\nfrom my_timeseries_database_app import app as timeseries_app, Series\nfrom my_etl_project import (ETLWorker, FeederClient, TSWriter,\n ConfigLoader)\n\nfrom .helpers import populate_webshop\n\n\nSession.register('http://feeder_app', feeder_app)\nSession.register('http://timeseries_app', timeseries_app)\npopulate_webshop(feeder_app)\n\n\n@fixture\ndef feeder_client():\n config = ConfigLoader(location='environ')\n return FeederClient(\n base_url='http://feeder_app',\n session=Session(),\n username=config['feeder_username'],\n password=contig['feeder_password']\n )\n\n\n@fixture\ndef writer():\n config = ConfigLoader(location='environ')\n return TSWriter(\n base_url='http://timeseries_app',\n session=Session(),\n username=config['writer_username'],\n password=contig['writer_password']\n )\n\n\ndef test_it_can_go_end_to_end(feeder_client, writer)\n now = datetime.now()\n worker = ETLWorker(\n feeder=feeder_client,\n writer=writer,\n )\n worker.run()\n result = Series.sum('my_serie_name', start=now, end=now + timedelta(days=1))\n assert result == 42\n\n```\n\n\n\n", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/maarten-dp/requests-flask-adapter", "keywords": "", "license": "MIT/Expat", "maintainer": "", "maintainer_email": "", "name": "requests-flask-adapter", "package_url": "https://pypi.org/project/requests-flask-adapter/", "platform": "", "project_url": "https://pypi.org/project/requests-flask-adapter/", "project_urls": { "Homepage": "https://github.com/maarten-dp/requests-flask-adapter" }, "release_url": "https://pypi.org/project/requests-flask-adapter/0.0.4/", "requires_dist": [ "flask", "requests" ], "requires_python": "", "summary": "Adapter for requests session to access flask apps", "version": "0.0.4" }, "last_serial": 5916847, "releases": { "0.0.0": [ { "comment_text": "", "digests": { "md5": "e908ce72bf02072c85c13bb07759ba7b", "sha256": "f16aaa4fbb4e05e7a105e9c8193fbced2867d48752bcdb4252ed205f380cc946" }, "downloads": -1, "filename": "requests_flask_adapter-0.0.0-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "e908ce72bf02072c85c13bb07759ba7b", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": null, "size": 6417, "upload_time": "2019-01-25T10:10:50", "url": "https://files.pythonhosted.org/packages/90/e9/dd85913fa56da75b34fb9febcc9a8b1dce7f46e5bf91cbd95e5f9a928dd7/requests_flask_adapter-0.0.0-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "aba276096e1a86d58a1d5feb28749883", "sha256": "46b8c7fc15e88a1b9ae1a801bfaad8c37855915738155dd34fff3bd91d7e233e" }, "downloads": -1, "filename": "requests-flask-adapter-0.0.0.tar.gz", "has_sig": false, "md5_digest": "aba276096e1a86d58a1d5feb28749883", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6475, "upload_time": "2019-01-25T10:02:32", "url": "https://files.pythonhosted.org/packages/91/7c/0634143db1e73f79aa78631ea0fc19149107496b7d1e2879c60ecdb64546/requests-flask-adapter-0.0.0.tar.gz" } ], "0.0.1": [ { "comment_text": "", "digests": { "md5": "e7db9cfc55b642e9843ec9d669813a89", "sha256": "fc1eeeeab370b1d4f3c9e93867486ac6e5db8adcb044ab006ea195f5b186c7db" }, "downloads": -1, "filename": "requests_flask_adapter-0.0.1-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "e7db9cfc55b642e9843ec9d669813a89", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": null, "size": 6490, "upload_time": "2019-01-25T10:24:31", "url": "https://files.pythonhosted.org/packages/96/d9/36ca723e3842039ffc71b97fccf1c5fcef44c6b5962159961a93c80330ae/requests_flask_adapter-0.0.1-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "6a98de86b6145a2558641c96da8a555f", "sha256": "8edc8b3ce5e8ca2a7d221c22a63679b0b07bcce52dc6d149f94cc53cc8b7f759" }, "downloads": -1, "filename": "requests-flask-adapter-0.0.1.tar.gz", "has_sig": false, "md5_digest": "6a98de86b6145a2558641c96da8a555f", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6597, "upload_time": "2019-01-25T10:24:33", "url": "https://files.pythonhosted.org/packages/3e/08/adcfd91a58e4b09942278dcf50bd1c5e1ddd1a43bc4d71042a008edc55da/requests-flask-adapter-0.0.1.tar.gz" } ], "0.0.3": [ { "comment_text": "", "digests": { "md5": "acda0ce6b703e6c4480ee22b54cdb04b", "sha256": "1a4f0b949d50ff1d1b771fe4889199f8e9e6622922f23d4e4d68df78fd2dcb25" }, "downloads": -1, "filename": "requests_flask_adapter-0.0.3-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "acda0ce6b703e6c4480ee22b54cdb04b", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": null, "size": 6531, "upload_time": "2019-05-24T19:48:57", "url": "https://files.pythonhosted.org/packages/27/f2/cdf8a541996f66c48109354ae6bdfb7d8abd36d728fa3a1d20807abe1499/requests_flask_adapter-0.0.3-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "6d0493dc772b34dcd8ffc2e32492134e", "sha256": "fd916c8cfa6ce969f9fff17e76e78bae556e6c233bf92d634975014e07dd2f5d" }, "downloads": -1, "filename": "requests-flask-adapter-0.0.3.tar.gz", "has_sig": false, "md5_digest": "6d0493dc772b34dcd8ffc2e32492134e", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7943, "upload_time": "2019-05-24T19:48:59", "url": "https://files.pythonhosted.org/packages/ba/fe/4f4cd505f43c238d039565587f038836f79fdc6a7739ff573d88f7135da0/requests-flask-adapter-0.0.3.tar.gz" } ], "0.0.4": [ { "comment_text": "", "digests": { "md5": "71b32d305990ccdf7151dd806bb7e4a0", "sha256": "b75c2c083d1844f01fc62c2e60463569394798ce9d319f9c6f4d4a8a4e784dce" }, "downloads": -1, "filename": "requests_flask_adapter-0.0.4-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "71b32d305990ccdf7151dd806bb7e4a0", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": null, "size": 6570, "upload_time": "2019-10-02T08:56:00", "url": "https://files.pythonhosted.org/packages/36/7c/6f702ee315ca0d554ad35cdb50aa4becc9d2945d936565c0ebe9069fe0f6/requests_flask_adapter-0.0.4-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "1c90bc713f8d38f5310542138b7833fb", "sha256": "e2156d64afbeefd1d3b9d206ff7c7c21e3ef4d8446371affaf9dfd249d547733" }, "downloads": -1, "filename": "requests-flask-adapter-0.0.4.tar.gz", "has_sig": false, "md5_digest": "1c90bc713f8d38f5310542138b7833fb", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7957, "upload_time": "2019-10-02T08:56:01", "url": "https://files.pythonhosted.org/packages/ac/2b/083cc6b19ae8d342a37007f6326d3723f8b0ff56fb48df2a355ef1ebef87/requests-flask-adapter-0.0.4.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "71b32d305990ccdf7151dd806bb7e4a0", "sha256": "b75c2c083d1844f01fc62c2e60463569394798ce9d319f9c6f4d4a8a4e784dce" }, "downloads": -1, "filename": "requests_flask_adapter-0.0.4-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "71b32d305990ccdf7151dd806bb7e4a0", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": null, "size": 6570, "upload_time": "2019-10-02T08:56:00", "url": "https://files.pythonhosted.org/packages/36/7c/6f702ee315ca0d554ad35cdb50aa4becc9d2945d936565c0ebe9069fe0f6/requests_flask_adapter-0.0.4-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "1c90bc713f8d38f5310542138b7833fb", "sha256": "e2156d64afbeefd1d3b9d206ff7c7c21e3ef4d8446371affaf9dfd249d547733" }, "downloads": -1, "filename": "requests-flask-adapter-0.0.4.tar.gz", "has_sig": false, "md5_digest": "1c90bc713f8d38f5310542138b7833fb", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7957, "upload_time": "2019-10-02T08:56:01", "url": "https://files.pythonhosted.org/packages/ac/2b/083cc6b19ae8d342a37007f6326d3723f8b0ff56fb48df2a355ef1ebef87/requests-flask-adapter-0.0.4.tar.gz" } ] }