Skip to content

testing

Hika van den Hoven edited this page May 5, 2017 · 6 revisions

Testing your data_def

As of version 1.2 there is a module to test your data_defs. To use it create a script as included in the package like:

test_data_def.py

#!/usr/bin/env python2
# -*- coding: utf-8 -*-

import sys
import test_json_struct

testjson = test_json_struct.test_JSON()
# This value holds the struct files and is None on any load errors
if testjson.struct_tree in (None, []):
    sys.exit(3)

cmd = sys.argv
if len(cmd) < 2:
    testjson.log('Please give the name of the json file to test.\n')
    sys.exit(1)

if len(cmd) == 2:
    sys.exit(testjson.test_file(cmd[1], report_level = (1+2+4+8+16+128+256)))

if len(cmd) >2:
    sys.exit(testjson.test_file(cmd[1], report_level = int(cmd[2])))

The return values are:

  • 0: The file was recognized and tested without serious errors
  • 1: Unable to open the file
  • 2: JSON errors were issued
  • 3: No valid struct was found to test the file
  • 4: The file was recognized and tested but serious errors were found

The testfile function accepts up to three parameters:

  • file_name
  • struct_name: If you leave this None, the program tries to determine the kind of JSON file it is testing. The available structs are listed in json_struct-files.json which is downloaded to ~/.json_struct, so check there for the available structs. At present they not by far are complete, but updates will get downloaded automatically. If you are interested in the struct syntax look at the description in test_json_struct.py.
  • report_level: This defaults to -1 or ALL. Add the desired ones or subtract the undesired ones from -1
    • 1: Missing required keys
    • 2: Errors on required values and on either selection
    • 4: Errors on not required keys
    • 8: Missing sugested keys without a default
    • 16: Missing sugested keys
    • 32: Missing optional keys without a default
    • 64: Missing optional keys
    • 128: Unused keys
    • 256: Unknown keys
    • 512: Report on either selection (without errors)

Errors and the like are written to stderr, the report is written to stdout. You can adapt this by replacing respectively the log and the report class function or you can pipe the report to a file.

On initializing the test_JSON() class you can add the following parameters:

  • encoding: defaulting to 'utf-8'
  • struct_file: defaulting to 'json_struct-files', being the json file holding a list of structs and the file containing them.
  • struct_path: defaulting to '~/.json_struct, being the path to locally store the struct files.