我有以下JSON模式,我想驗證和Python單元測試。可以引用JSON整數屬性嗎?
{
"properties": {
"traffic_parameters" {
"capacity": {
"oneOf": [
{
"max_percentage": {
"type": "integer",
"minimum" : 1,
"maximum" : 150
},
"min_percentage": {
"type": "integer",
"minimum" : 1,
"maximum" : {
"$ref": "#/properties/traffic_parameters/capacity/oneOf/max_percentage/minimum"
}
}
},
{
"percentage_range": {
"type": "array",
"minItems": 1,
"maxItems": 10,
"items": {
"type": "integer"
}
}
}
]
}
}
}
}
使用jsonschema我驗證整個模式文件確定。 但是,在編寫unittests時,我得到以下錯誤;
capacity = {'oneOf': [{'max_percentage': {'type': 'integer', 'minimum': 1, 'maximum': 150}, 'min_percentage': {'type': 'integer', 'minimum': 1, 'maximum': {'$ref': '#/properties/traffic_parameters/capacity/oneOf/max_percentage/minimum'}}}, {'percentage_range': {'type': 'array', 'minItems': 1, 'maxItems': 10, 'items': {'type': 'integer'}}}]}
-----------------------------------------
index0 = {'max_percentage': {'type': 'integer', 'minimum': 1, 'maximum': 150}, 'min_percentage': {'type': 'integer', 'minimum': 1, 'maximum': {'$ref': '#/properties/traffic_parameters/capacity/oneOf/max_percentage/minimum'}}}
-----------------------------------------
min_percentage = {'type': 'integer', 'minimum': 1, 'maximum': {'$ref': '#/properties/traffic_parameters/capacity/oneOf/max_percentage/minimum'}}
E.........
======================================================================
ERROR: test_invalid_minimum__traffic_parameters__capacity__min_percentage (__main__.SchemaTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test_test-variables_schema.py", line 160, in test_invalid_minimum__traffic_parameters__capacity__min_percentage
validate(0, min_percentage)
File "/local/tools/PACKAGES/python3/lib/python3.6/site-packages/jsonschema/validators.py", line 540, in validate
cls.check_schema(schema)
File "/local/tools/PACKAGES/python3/lib/python3.6/site-packages/jsonschema/validators.py", line 83, in check_schema
raise SchemaError.create_from(error)
jsonschema.exceptions.SchemaError: {'$ref': '#/properties/traffic_parameters/capacity/oneOf/max_percentage/minimum'} is not of type 'number'
Failed validating 'type' in schema['properties']['maximum']:
{'type': 'number'}
On instance['maximum']:
{'$ref': '#/properties/traffic_parameters/capacity/oneOf/max_percentage/minimum'}
FAILED (errors=1)
我的測試是測試屬性的最小值和最大值的Python單元測試。最大值是對上面直接定義的另一個屬性的引用。
顯示的測試測試了最小值,但它是錯誤的最大值。如果我正在測試最大值,錯誤將是相同的,這正是我正在處理的內容。
我試着重新指定類型爲「整數」爲「最大」,但錯誤依然存在。
如何在不更改屬性類型的情況下過濾此錯誤?該引用很重要,因爲我希望此屬性的最大值與前一個屬性的最小值直接相關。
此外,還有另一種(更簡單)的方式來引用這些變量在unittests?
這裏的功能
def test_invalid_minimum__traffic_parameters__capacity__min_percentage(self):
global test_schema
capacity = test_schema["traffic_parameters"]["capacity"]
print ("capacity = " + str(capacity))
print ("-----------------------------------------")
index0 = capacity["oneOf"][0]
print ("index0 = " + str(index0))
print ("-----------------------------------------")
min_percentage = index0["min_percentage"]
print ("min_percentage = " + str(min_percentage))
with self.assertRaises(ValidationError):
validate(0, min_percentage)
感謝。