Skip to content

wyfdev/mitest

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mitest - Mongo Inspired policy test

Travis status for master branch codecov.io status for master branch pypi status

Introduction

Test if data match policy, inspired by MongoDB query format

A policy express the expected relationship between variable and data uesing json (or python dict direct).

Key beginning with "$" to indicate relationship like $and, $lt, $gte.

Example

policy, variable "a" less then 10:

{"a": {"$lt": 10}}

test, if given data match policy:

>>> policy = TestPolicy({'a': {'$lt': 10}})
>>>
>>> result = policy.test({'a': 9})
>>> bool(result)
True
>>> result.how
[('a', {'$lt': 10}, 9)]
>>>
>>> result = policy.test({'a': 11})
>>> bool(result)
False
>>> result.how
[('a', {'$lt': 10}, 11)]

Arrtibute how express how data match or not match policy. For example, ("a", {"$lt": 10}, 9) mean that for variable a, policy is lessthen 10, and (or but, if not match) given data is 9.

Policy format

MongoDB query format is a little complicated, but "Simple is better than complex".

For this MongoDB query:

{"$or":[
    {"$and":[
        {"a":"23"},
        {"b":6}
    ]},
    {"$and":[
        {"c":{"$gte":2}},
        {"c":{"$lt":4}}
    ]}
]}

"$and" is default for each items in list:

{"$or":[
    [
        {"a":"23"},
        {"b":6}
    ],
    [
        {"c":{"$gte":2}},
        {"c":{"$lt":4}}
    ]
]}

Merge items to single dict:

{"$or":[
    {"a":"23", "b":6},
    {"c":{"$gte":2, "$lt":4}}
]}

Much simpler :-)


Wish this can help you.

About

No description or website provided.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages