Allow special marker ANY inside JSON examples
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
from functools import singledispatch
|
from functools import singledispatch
|
||||||
|
from jsonexample import ANY
|
||||||
|
|
||||||
|
|
||||||
def assert_same_type(f):
|
def assert_same_type(f):
|
||||||
@@ -40,3 +41,8 @@ def _(model, obj):
|
|||||||
@assert_same_type
|
@assert_same_type
|
||||||
def _(model, obj):
|
def _(model, obj):
|
||||||
return model <= obj
|
return model <= obj
|
||||||
|
|
||||||
|
|
||||||
|
@is_subset.register(ANY)
|
||||||
|
def _(model, obj):
|
||||||
|
return True
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
import json
|
||||||
|
from functools import partial
|
||||||
|
import re
|
||||||
|
|
||||||
|
|
||||||
|
class ANY:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class ExampleDecoder(json.JSONDecoder):
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
super().__init__(*args, object_hook=self.object_hook, **kwargs)
|
||||||
|
|
||||||
|
def decode(self, s, *args, **kwargs):
|
||||||
|
s = re.sub(r'(\W)(ANY)(\W)', r'\1{"_type": "ANY"}\3', s)
|
||||||
|
return super().decode(s, *args, **kwargs)
|
||||||
|
|
||||||
|
def object_hook(self, dct):
|
||||||
|
if dct.get('_type', None) == 'ANY':
|
||||||
|
return ANY()
|
||||||
|
else:
|
||||||
|
return dct
|
||||||
|
|
||||||
|
loads = partial(json.loads, cls=ExampleDecoder)
|
||||||
@@ -8,6 +8,7 @@ import subprocess
|
|||||||
import requests
|
import requests
|
||||||
from environconfig import EnvironConfig, StringVar, IntVar, BooleanVar
|
from environconfig import EnvironConfig, StringVar, IntVar, BooleanVar
|
||||||
from comparedict import is_subset
|
from comparedict import is_subset
|
||||||
|
import jsonexample
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
@@ -134,7 +135,7 @@ def step_impl(context, reason):
|
|||||||
|
|
||||||
@then('I get the following response body')
|
@then('I get the following response body')
|
||||||
def step_impl(context):
|
def step_impl(context):
|
||||||
assert is_subset(json.loads(context.text), context.response.json())
|
assert is_subset(jsonexample.loads(context.text), context.response.json())
|
||||||
|
|
||||||
|
|
||||||
@then('I get an empty response body')
|
@then('I get an empty response body')
|
||||||
|
|||||||
Reference in New Issue
Block a user