Skip to main content

Generate samples for various schemas like json schema, xml schema and regex

Project description

Fences

Tests

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 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:

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)
Output
Valid:
cbbb
Valid:
acccbbbbbbb

JSON schema

Generate samples for json schema:

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))
Output
Valid:
{
    "foo": "",
    "bar": true
}

Valid:
{}

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

Invalid:
{
    "foo": null
}

Invalid:
{
    "bar": 42
}

Invalid:
{
    "bar": null
}

Invalid:
{
    "foo": "",
    "bar": "INVALID"
}

XML Schema

Generate samples for XML schema:

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="   "))
Output
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>

Grammar

Generate samples for a grammar:

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)
Output
0
91.0901E0901
-0e+9
-10901.0
9E-0109

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fences-1.1.0.tar.gz (59.0 kB view details)

Uploaded Source

Built Distribution

fences-1.1.0-py3-none-any.whl (63.5 kB view details)

Uploaded Python 3

File details

Details for the file fences-1.1.0.tar.gz.

File metadata

  • Download URL: fences-1.1.0.tar.gz
  • Upload date:
  • Size: 59.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.19

File hashes

Hashes for fences-1.1.0.tar.gz
Algorithm Hash digest
SHA256 91a7cde75e28ad02e3ab05865ba61dd23392b6ca0da0bc1237263df4b28f18e0
MD5 235af5591b619ed81ef7932daf6c15d5
BLAKE2b-256 53d6f10401c9dd22ec1958e59a63be54d46c7dee1f697fcf397b25595909729b

See more details on using hashes here.

Provenance

File details

Details for the file fences-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: fences-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 63.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.19

File hashes

Hashes for fences-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dd332212adbac7acfe7f581847dcabaac510712e1ab5269ec40317c7f6c16b77
MD5 108ef0198bd2af7a98b3f16dd0b62fe3
BLAKE2b-256 4a4b7008a4ac17500297801dbeb25372346c993a0a63cc177abbc97a06c51370

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page