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

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)

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

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

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.

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-0.0.3.tar.gz (48.2 kB view details)

Uploaded Source

Built Distribution

fences-0.0.3-py3-none-any.whl (52.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fences-0.0.3.tar.gz
  • Upload date:
  • Size: 48.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for fences-0.0.3.tar.gz
Algorithm Hash digest
SHA256 cb5a67273f2d97893c643dfe575a0d6cd299e8c9878a3b7874aa4edf6a4a4e67
MD5 6864b46240ed56838fffad68ded4b3d1
BLAKE2b-256 17d756d784c9a79953054644bac604d8064856d5ea7d8a8973d6aabfab8569e3

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: fences-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 52.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for fences-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 6705d43521c575e66760a6681f1126f6c0b4f3408e0de4242325a28b7c06322e
MD5 349254ab9226577eb7c6238281c0e9b1
BLAKE2b-256 b4d5ccb673f35c39bf7dc549b33384757e0714cf06c518b19ff58989e687f7c3

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