{ "info": { "author": "H. Chase Stevens", "author_email": "chase@chasestevens.com", "bugtrack_url": null, "classifiers": [ "Framework :: Hypothesis", "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", "Programming Language :: Python", "Programming Language :: Python :: 2", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.6", "Topic :: Software Development :: Libraries :: Python Modules" ], "description": "# hypothesis-protobuf\n[![Build Status](https://travis-ci.org/hchasestevens/hypothesis-protobuf.svg?branch=master)](https://travis-ci.org/hchasestevens/hypothesis-protobuf)\n[![PyPI version](https://badge.fury.io/py/hypothesis-pb.svg)](https://pypi.org/project/hypothesis-pb)\n![PyPI - Python Version](https://img.shields.io/pypi/pyversions/hypothesis-pb.svg) \n\n[Hypothesis](http://hypothesis.works/) extension to allow generating [protobuf](https://developers.google.com/protocol-buffers/) messages matching a schema.\n\n## Installation\n```\npip install hypothesis-pb\n```\n\n## Usage\nGiven a compiled protobuf schema module, `hypothesis-protobuf` allows for hypothesis strategies to be generated which match the types of the protobuf messages.\n\n### Simple example\nUsing an example protobuf schema for an instant messaging application:\n```proto\nsyntax = \"proto3\";\npackage im;\n\nenum Client {\n CLIENT_UNKNOWN = 0;\n CLIENT_NATIVE_APP = 1;\n CLIENT_WEB_APP = 2;\n CLIENT_API = 3;\n}\n\nmessage User {\n uint64 id = 1;\n string screen_name = 2;\n}\n\nmessage InstantMessage {\n uint64 timestamp = 1;\n Client client = 2;\n fixed32 sender_ip = 3;\n User sender = 4;\n User recipient = 5;\n string message = 6;\n repeated bytes image_attachments = 7;\n}\n```\na strategy for `InstantMessage` can be generated from the compiled schema (`im_pb2.py`) by executing:\n```python\nfrom hypothesis_protobuf import modules_to_strategies\nimport im_pb2\n\nprotobuf_strategies = modules_to_strategies(im_pb2)\ninstant_message_strategy = protobuf_strategies[im_pb2.InstantMessage]\n```\nwhich in turn can be used to generate `InstantMessage` examples:\n```python\n>>> instant_message_strategy.example()\ntimestamp: 14420265017158477352\nclient: CLIENT_NATIVE_APP\nsender_ip: 1465109037\nsender {\n id: 9509488734701077048\n screen_name: \"\\364\\210\\240\\2233\\007\\352\\212\\222i\\354\\217\\251\"\n}\nrecipient {\n id: 14863054719025962687\n screen_name: \"\\351\\274\\240\"\n}\nmessage: \"M\\361\\265\\247\\224\\310\\224\\362\\202\\r\\347\\227\\245\\n\\352\\202M]\\361\\253\\237\\2700\"\nimage_attachments: \"\\236rN\\267\\252\\363-s\\235\"\nimage_attachments: \"\\256\\376ZP-\"\nimage_attachments: \"\\340\"\n\n```\nor as a strategy for use in testing (see the [hypothesis quick-start guide](https://hypothesis.readthedocs.io/en/latest/quickstart.html)):\n```python\nfrom hypothesis import given\n\n@given(instant_message=protobuf_strategies[im_pb2.InstantMessage])\ndef test_instant_message_processor(instant_message):\n assert process_message(instant_message) # will be run using multiple InstantMessage examples\n```\n\n### Overriding strategies\nWhen generating strategies for a given protobuf module, field-specific overrides can be provided. These overrides must be mappings from full field names to strategies, like so:\n```python\nfrom hypothesis_protobuf import modules_to_strategies\nfrom hypothesis import strategies as st\nimport im_pb2\n\nstrategy_overrides = {\n 'im.InstantMessage.timestamp': st.floats(\n min_value=0, \n max_value=2e9\n )\n}\nprotobuf_strategies = modules_to_strategies(im_pb2, **strategy_overrides)\ninstant_message_strategy = protobuf_strategies[im_pb2.InstantMessage]\n```\n`hypothesis-protobuf` also offers a `full_field_name` utility, allowing the above override to be specified as:\n```python\nfrom hypothesis_protobuf import full_field_name\nfrom hypothesis import strategies as st\nimport im_pb2\n\nstrategy_overrides = {\n full_field_name(im_pb2.InstantMessage, 'timestamp'): st.floats(\n min_value=0,\n max_value=2e9\n )\n}\n```\nIn cases where the message strategy should choose either from the override provided or from the default field value, the `optional` function can be used:\n```python\nfrom hypothesis_protobuf import optional\nfrom hypothesis import strategies as st\n\nstrategy_overrides = {\n 'im.InstantMessage.timestamp': optional(\n st.floats(min_value=0, max_value=2e9)\n )\n}\n```\nFinally, overrides can also be provided as functions, taking the field's default strategy and returning a new strategy. Using this method, the above can be rewritten as:\n```python\nstrategy_overrides = {\n 'im.InstantMessage.timestamp': (\n lambda strategy: strategy.filter(lambda value: value <= 2e9)\n )\n}\n```\n\n## License\n`hypothesis-protobuf` is available under the MIT license.", "description_content_type": "", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/hchasestevens/hypothesis-protobuf", "keywords": "", "license": "MIT", "maintainer": "", "maintainer_email": "", "name": "hypothesis-pb", "package_url": "https://pypi.org/project/hypothesis-pb/", "platform": "any", "project_url": "https://pypi.org/project/hypothesis-pb/", "project_urls": { "Homepage": "https://github.com/hchasestevens/hypothesis-protobuf" }, "release_url": "https://pypi.org/project/hypothesis-pb/1.2.0/", "requires_dist": null, "requires_python": "", "summary": "Hypothesis extension to allow generating protobuf messages matching a schema.", "version": "1.2.0" }, "last_serial": 4688662, "releases": { "1.0.0": [ { "comment_text": "", "digests": { "md5": "6aab9888b9045a680deba8b8baf840f4", "sha256": "d39f794cecafd5c9ca9dec3201813477e3790b13f1f0fd929a698f74bb94a4ec" }, "downloads": -1, "filename": "hypothesis-pb-1.0.0.tar.gz", "has_sig": false, "md5_digest": "6aab9888b9045a680deba8b8baf840f4", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5026, "upload_time": "2017-11-13T15:48:09", "url": "https://files.pythonhosted.org/packages/2e/6c/47951e9d08f56bc18bec5691218559dafb651bf66fff8cdc052b54494793/hypothesis-pb-1.0.0.tar.gz" } ], "1.0.1": [ { "comment_text": "", "digests": { "md5": "2b13c34b37f0d394802a327f75f43a6b", "sha256": "8d4446f8bbfd2d319b3b5d802314b888ec423b0cf2ad8cf8e3bb92060967dbb1" }, "downloads": -1, "filename": "hypothesis-pb-1.0.1.tar.gz", "has_sig": false, "md5_digest": "2b13c34b37f0d394802a327f75f43a6b", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5056, "upload_time": "2017-11-21T17:21:01", "url": "https://files.pythonhosted.org/packages/db/f2/07b45740b12b229f3cdd39e4ed2f89ce09408d68de9c43bf654c9f8f4b5e/hypothesis-pb-1.0.1.tar.gz" } ], "1.1.0": [ { "comment_text": "", "digests": { "md5": "ceac4d251a65c8dbf8ccdbbc63e05add", "sha256": "b8a79cbab51a9837128ae77fd67c9ea897f22e77e1c5bbd79a38278367617e9e" }, "downloads": -1, "filename": "hypothesis-pb-1.1.0.tar.gz", "has_sig": false, "md5_digest": "ceac4d251a65c8dbf8ccdbbc63e05add", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5119, "upload_time": "2018-03-29T00:41:17", "url": "https://files.pythonhosted.org/packages/ba/b3/35e5d268e980174ef2960721b29561e2085ce45dc3711e789ace0fe7fea2/hypothesis-pb-1.1.0.tar.gz" } ], "1.2.0": [ { "comment_text": "", "digests": { "md5": "aea1fad77fcb4995fcd3088f1e1694fe", "sha256": "6361add3371277959c7cc2dc8a0d225dbbcb99e49f670f405304ac0f1ad5ff3d" }, "downloads": -1, "filename": "hypothesis-pb-1.2.0.tar.gz", "has_sig": false, "md5_digest": "aea1fad77fcb4995fcd3088f1e1694fe", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5509, "upload_time": "2019-01-12T15:10:12", "url": "https://files.pythonhosted.org/packages/00/9b/837eef75deb3d517bb0c2cf7373884bc98e67065278bfa0c01ae66bd5edc/hypothesis-pb-1.2.0.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "aea1fad77fcb4995fcd3088f1e1694fe", "sha256": "6361add3371277959c7cc2dc8a0d225dbbcb99e49f670f405304ac0f1ad5ff3d" }, "downloads": -1, "filename": "hypothesis-pb-1.2.0.tar.gz", "has_sig": false, "md5_digest": "aea1fad77fcb4995fcd3088f1e1694fe", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5509, "upload_time": "2019-01-12T15:10:12", "url": "https://files.pythonhosted.org/packages/00/9b/837eef75deb3d517bb0c2cf7373884bc98e67065278bfa0c01ae66bd5edc/hypothesis-pb-1.2.0.tar.gz" } ] }