fences


Namefences JSON
Version 1.1.0 PyPI version JSON
download
home_pageNone
SummaryGenerate samples for various schemas like json schema, xml schema and regex
upload_time2024-04-13 15:51:42
maintainerNone
docs_urlNone
authorNone
requires_python>=3.6
licenseMIT License Copyright (c) 2023 Institut für Automation und Kommunikation e.V. 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.
keywords xml json regex schema
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Fences
[![Tests](https://github.com/ifak/fences/actions/workflows/check.yml/badge.svg)](https://github.com/ifak/fences/actions/workflows/check.yml)

Fences is a python tool which lets you create test data based on schemas.

For this, it generates a set of *valid samples* which fullfil your schema.
Additionally, it generates a set of *invalid samples* which intentionally violate your schema.
You can then feed these samples into your software to test.
If your software is implemented correctly, it must accept all valid samples and reject all invalid ones.

Unlike other similar tools, fences generate samples systematically instead of randomly.
This way, the valid / invalid samples systematically cover all boundaries of your input schema (like placing *fences*, hence the name).

## Installation

Use pip to install Fences:

```
python -m pip install fences
```

Fences is a self contained library without any external dependencies.
It uses [Lark](https://github.com/lark-parser/lark) for regex parsing, but in the standalone version where a python file is generated from the grammar beforehand (Mozilla Public License, v. 2.0).

## Usage

### Regular Expressions

Generate samples for regular expressions:

```python
from fences import parse_regex

graph = parse_regex("a?(c+)b{3,7}")

for i in graph.generate_paths():
    sample = graph.execute(i.path)
    print("Valid:" if i.is_valid else "Invalid:")
    print(sample)
```

<details>
<summary>Output</summary>

```
Valid:
cbbb
Valid:
acccbbbbbbb
```
</details>

### JSON schema

Generate samples for json schema:

```python
from fences import parse_json_schema
import json

graph = parse_json_schema({
    'properties': {
        'foo': {
            'type': 'string'
        },
        'bar': {
            'type': 'boolean'
        }
    }
})

for i in graph.generate_paths():
    sample = graph.execute(i.path)
    print("Valid:" if i.is_valid else "Invalid:")
    print(json.dumps(sample, indent=4))
```

<details>
<summary>Output</summary>

```json
Valid:
{
    "foo": "",
    "bar": true
}

Valid:
{}

Valid:
{
    "foo": "",
    "bar": false
}

Invalid:
{
    "foo": null
}

Invalid:
{
    "bar": 42
}

Invalid:
{
    "bar": null
}

Invalid:
{
    "foo": "",
    "bar": "INVALID"
}
```
</details>

### XML Schema

Generate samples for XML schema:

```python
from fences import parse_xml_schema
from xml.etree import ElementTree
from xml.dom import minidom

et = ElementTree.fromstring("""<?xml version="1.0" encoding="UTF-8" ?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <xs:element name = 'class'>
            <xs:complexType>
                <xs:sequence>
                    <xs:element name = 'student' type = 'StudentType' minOccurs = '0' maxOccurs = 'unbounded' />
                </xs:sequence>
            </xs:complexType>
        </xs:element>
        <xs:complexType name = "StudentType">
            <xs:sequence>
                <xs:element name = "firstname" type = "xs:string"/>
                <xs:element name = "lastname" type = "xs:string"/>
                <xs:element name = "nickname" type = "xs:string"/>
                <xs:element name = "marks" type = "xs:positiveInteger"/>
            </xs:sequence>
            <xs:attribute name = 'rollno' type = 'xs:positiveInteger'/>
        </xs:complexType>
    </xs:schema>""")

graph = parse_xml_schema(et)
for i in graph.generate_paths():
    sample = graph.execute(i.path)
    s = ElementTree.tostring(sample.getroot())
    print("Valid:" if i.is_valid else "Invalid:")
    print(minidom.parseString(s).toprettyxml(indent="   "))
```

<details>
<summary>Output</summary>

```xml
Valid:
<?xml version="1.0" ?>
<class/>

Valid:
<?xml version="1.0" ?>
<class>
   <student>
      <firstname>foo</firstname>
      <lastname>foo</lastname>
      <nickname>foo</nickname>
      <marks>780</marks>
   </student>
</class>

Valid:
<?xml version="1.0" ?>
<class>
   <student rollno="533">
      <firstname>x</firstname>
      <lastname>x</lastname>
      <nickname>x</nickname>
      <marks>780</marks>
   </student>
</class>

Invalid:
<?xml version="1.0" ?>
<class>
   <student>
      <firstname>foo</firstname>
      <lastname>foo</lastname>
      <nickname>foo</nickname>
      <marks>-10</marks>
   </student>
</class>

Invalid:
<?xml version="1.0" ?>
<class>
   <student rollno="533">
      <firstname>x</firstname>
      <lastname>x</lastname>
      <nickname>x</nickname>
      <marks>foo</marks>
   </student>
</class>

Invalid:
<?xml version="1.0" ?>
<class>
   <student rollno="-10">
      <firstname>foo</firstname>
      <lastname>foo</lastname>
      <nickname>foo</nickname>
      <marks>780</marks>
   </student>
</class>

Invalid:
<?xml version="1.0" ?>
<class>
   <student rollno="foo">
      <firstname>x</firstname>
      <lastname>x</lastname>
      <nickname>x</nickname>
      <marks>780</marks>
   </student>
</class>
```

</details>

### Grammar

Generate samples for a grammar:

```python
from fences.grammar.types import NonTerminal, CharacterRange
from fences import parse_grammar

number = NonTerminal("number")
integer = NonTerminal("integer")
fraction = NonTerminal("fraction")
exponent = NonTerminal("exponent")
digit = NonTerminal("digit")
digits = NonTerminal("digits")
one_to_nine = NonTerminal("one_to_nine")
sign = NonTerminal("sign")

grammar = {
    number:      integer + fraction + exponent,
    integer:     digit
                 | one_to_nine + digits
                 | '-' + digit
                 | '-' + one_to_nine + digits,
    digit:       '0'
                 | one_to_nine,
    digits:      digit*(1, None),
    one_to_nine: CharacterRange('1', '9'),
    fraction:    ""
                 | "." + digits,
    exponent:    ""
                 | 'E' + sign + digits
                 | "e" + sign + digits,
    sign:        ["", "+", "-"]
}

graph = parse_grammar(grammar, number)
for i in graph.generate_paths():
    sample = graph.execute(i.path)
    print(sample)
```

<details>
<summary>Output</summary>

```
0
91.0901E0901
-0e+9
-10901.0
9E-0109
```

</details>

## Real-World Examples

Find some real-world examples in the `examples` folder.

## Limitations

General:

Fences does not check if your schema is syntactically correct.
Fences is designed to be as permissive as possible when parsing a schema but will complain if there is an aspect it does not understand.

For XML:

Python's default XML implementation `xml.etree.ElementTree` has a very poor support for namespaces (https://docs.python.org/3/library/xml.etree.elementtree.html#parsing-xml-with-namespaces).
This might lead to problems when using the `targetNamespace` attribute in your XML schema.

For Grammars:

Fences currently does not generate invalid samples for grammars.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "fences",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": null,
    "keywords": "xml, json, regex, schema",
    "author": null,
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/53/d6/f10401c9dd22ec1958e59a63be54d46c7dee1f697fcf397b25595909729b/fences-1.1.0.tar.gz",
    "platform": null,
    "description": "# Fences\n[![Tests](https://github.com/ifak/fences/actions/workflows/check.yml/badge.svg)](https://github.com/ifak/fences/actions/workflows/check.yml)\n\nFences is a python tool which lets you create test data based on schemas.\n\nFor this, it generates a set of *valid samples* which fullfil your schema.\nAdditionally, it generates a set of *invalid samples* which intentionally violate your schema.\nYou can then feed these samples into your software to test.\nIf your software is implemented correctly, it must accept all valid samples and reject all invalid ones.\n\nUnlike other similar tools, fences generate samples systematically instead of randomly.\nThis way, the valid / invalid samples systematically cover all boundaries of your input schema (like placing *fences*, hence the name).\n\n## Installation\n\nUse pip to install Fences:\n\n```\npython -m pip install fences\n```\n\nFences is a self contained library without any external dependencies.\nIt uses [Lark](https://github.com/lark-parser/lark) for regex parsing, but in the standalone version where a python file is generated from the grammar beforehand (Mozilla Public License, v. 2.0).\n\n## Usage\n\n### Regular Expressions\n\nGenerate samples for regular expressions:\n\n```python\nfrom fences import parse_regex\n\ngraph = parse_regex(\"a?(c+)b{3,7}\")\n\nfor i in graph.generate_paths():\n    sample = graph.execute(i.path)\n    print(\"Valid:\" if i.is_valid else \"Invalid:\")\n    print(sample)\n```\n\n<details>\n<summary>Output</summary>\n\n```\nValid:\ncbbb\nValid:\nacccbbbbbbb\n```\n</details>\n\n### JSON schema\n\nGenerate samples for json schema:\n\n```python\nfrom fences import parse_json_schema\nimport json\n\ngraph = parse_json_schema({\n    'properties': {\n        'foo': {\n            'type': 'string'\n        },\n        'bar': {\n            'type': 'boolean'\n        }\n    }\n})\n\nfor i in graph.generate_paths():\n    sample = graph.execute(i.path)\n    print(\"Valid:\" if i.is_valid else \"Invalid:\")\n    print(json.dumps(sample, indent=4))\n```\n\n<details>\n<summary>Output</summary>\n\n```json\nValid:\n{\n    \"foo\": \"\",\n    \"bar\": true\n}\n\nValid:\n{}\n\nValid:\n{\n    \"foo\": \"\",\n    \"bar\": false\n}\n\nInvalid:\n{\n    \"foo\": null\n}\n\nInvalid:\n{\n    \"bar\": 42\n}\n\nInvalid:\n{\n    \"bar\": null\n}\n\nInvalid:\n{\n    \"foo\": \"\",\n    \"bar\": \"INVALID\"\n}\n```\n</details>\n\n### XML Schema\n\nGenerate samples for XML schema:\n\n```python\nfrom fences import parse_xml_schema\nfrom xml.etree import ElementTree\nfrom xml.dom import minidom\n\net = ElementTree.fromstring(\"\"\"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n    <xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n        <xs:element name = 'class'>\n            <xs:complexType>\n                <xs:sequence>\n                    <xs:element name = 'student' type = 'StudentType' minOccurs = '0' maxOccurs = 'unbounded' />\n                </xs:sequence>\n            </xs:complexType>\n        </xs:element>\n        <xs:complexType name = \"StudentType\">\n            <xs:sequence>\n                <xs:element name = \"firstname\" type = \"xs:string\"/>\n                <xs:element name = \"lastname\" type = \"xs:string\"/>\n                <xs:element name = \"nickname\" type = \"xs:string\"/>\n                <xs:element name = \"marks\" type = \"xs:positiveInteger\"/>\n            </xs:sequence>\n            <xs:attribute name = 'rollno' type = 'xs:positiveInteger'/>\n        </xs:complexType>\n    </xs:schema>\"\"\")\n\ngraph = parse_xml_schema(et)\nfor i in graph.generate_paths():\n    sample = graph.execute(i.path)\n    s = ElementTree.tostring(sample.getroot())\n    print(\"Valid:\" if i.is_valid else \"Invalid:\")\n    print(minidom.parseString(s).toprettyxml(indent=\"   \"))\n```\n\n<details>\n<summary>Output</summary>\n\n```xml\nValid:\n<?xml version=\"1.0\" ?>\n<class/>\n\nValid:\n<?xml version=\"1.0\" ?>\n<class>\n   <student>\n      <firstname>foo</firstname>\n      <lastname>foo</lastname>\n      <nickname>foo</nickname>\n      <marks>780</marks>\n   </student>\n</class>\n\nValid:\n<?xml version=\"1.0\" ?>\n<class>\n   <student rollno=\"533\">\n      <firstname>x</firstname>\n      <lastname>x</lastname>\n      <nickname>x</nickname>\n      <marks>780</marks>\n   </student>\n</class>\n\nInvalid:\n<?xml version=\"1.0\" ?>\n<class>\n   <student>\n      <firstname>foo</firstname>\n      <lastname>foo</lastname>\n      <nickname>foo</nickname>\n      <marks>-10</marks>\n   </student>\n</class>\n\nInvalid:\n<?xml version=\"1.0\" ?>\n<class>\n   <student rollno=\"533\">\n      <firstname>x</firstname>\n      <lastname>x</lastname>\n      <nickname>x</nickname>\n      <marks>foo</marks>\n   </student>\n</class>\n\nInvalid:\n<?xml version=\"1.0\" ?>\n<class>\n   <student rollno=\"-10\">\n      <firstname>foo</firstname>\n      <lastname>foo</lastname>\n      <nickname>foo</nickname>\n      <marks>780</marks>\n   </student>\n</class>\n\nInvalid:\n<?xml version=\"1.0\" ?>\n<class>\n   <student rollno=\"foo\">\n      <firstname>x</firstname>\n      <lastname>x</lastname>\n      <nickname>x</nickname>\n      <marks>780</marks>\n   </student>\n</class>\n```\n\n</details>\n\n### Grammar\n\nGenerate samples for a grammar:\n\n```python\nfrom fences.grammar.types import NonTerminal, CharacterRange\nfrom fences import parse_grammar\n\nnumber = NonTerminal(\"number\")\ninteger = NonTerminal(\"integer\")\nfraction = NonTerminal(\"fraction\")\nexponent = NonTerminal(\"exponent\")\ndigit = NonTerminal(\"digit\")\ndigits = NonTerminal(\"digits\")\none_to_nine = NonTerminal(\"one_to_nine\")\nsign = NonTerminal(\"sign\")\n\ngrammar = {\n    number:      integer + fraction + exponent,\n    integer:     digit\n                 | one_to_nine + digits\n                 | '-' + digit\n                 | '-' + one_to_nine + digits,\n    digit:       '0'\n                 | one_to_nine,\n    digits:      digit*(1, None),\n    one_to_nine: CharacterRange('1', '9'),\n    fraction:    \"\"\n                 | \".\" + digits,\n    exponent:    \"\"\n                 | 'E' + sign + digits\n                 | \"e\" + sign + digits,\n    sign:        [\"\", \"+\", \"-\"]\n}\n\ngraph = parse_grammar(grammar, number)\nfor i in graph.generate_paths():\n    sample = graph.execute(i.path)\n    print(sample)\n```\n\n<details>\n<summary>Output</summary>\n\n```\n0\n91.0901E0901\n-0e+9\n-10901.0\n9E-0109\n```\n\n</details>\n\n## Real-World Examples\n\nFind some real-world examples in the `examples` folder.\n\n## Limitations\n\nGeneral:\n\nFences does not check if your schema is syntactically correct.\nFences is designed to be as permissive as possible when parsing a schema but will complain if there is an aspect it does not understand.\n\nFor XML:\n\nPython's default XML implementation `xml.etree.ElementTree` has a very poor support for namespaces (https://docs.python.org/3/library/xml.etree.elementtree.html#parsing-xml-with-namespaces).\nThis might lead to problems when using the `targetNamespace` attribute in your XML schema.\n\nFor Grammars:\n\nFences currently does not generate invalid samples for grammars.\n",
    "bugtrack_url": null,
    "license": "MIT License  Copyright (c) 2023 Institut f\u00fcr Automation und Kommunikation e.V.  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. ",
    "summary": "Generate samples for various schemas like json schema, xml schema and regex",
    "version": "1.1.0",
    "project_urls": {
        "Homepage": "https://github.com/ifak/fences"
    },
    "split_keywords": [
        "xml",
        " json",
        " regex",
        " schema"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "4a4b7008a4ac17500297801dbeb25372346c993a0a63cc177abbc97a06c51370",
                "md5": "108ef0198bd2af7a98b3f16dd0b62fe3",
                "sha256": "dd332212adbac7acfe7f581847dcabaac510712e1ab5269ec40317c7f6c16b77"
            },
            "downloads": -1,
            "filename": "fences-1.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "108ef0198bd2af7a98b3f16dd0b62fe3",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6",
            "size": 63495,
            "upload_time": "2024-04-13T15:51:40",
            "upload_time_iso_8601": "2024-04-13T15:51:40.499888Z",
            "url": "https://files.pythonhosted.org/packages/4a/4b/7008a4ac17500297801dbeb25372346c993a0a63cc177abbc97a06c51370/fences-1.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "53d6f10401c9dd22ec1958e59a63be54d46c7dee1f697fcf397b25595909729b",
                "md5": "235af5591b619ed81ef7932daf6c15d5",
                "sha256": "91a7cde75e28ad02e3ab05865ba61dd23392b6ca0da0bc1237263df4b28f18e0"
            },
            "downloads": -1,
            "filename": "fences-1.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "235af5591b619ed81ef7932daf6c15d5",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 58977,
            "upload_time": "2024-04-13T15:51:42",
            "upload_time_iso_8601": "2024-04-13T15:51:42.142911Z",
            "url": "https://files.pythonhosted.org/packages/53/d6/f10401c9dd22ec1958e59a63be54d46c7dee1f697fcf397b25595909729b/fences-1.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-04-13 15:51:42",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "ifak",
    "github_project": "fences",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "fences"
}
        
Elapsed time: 0.24643s