|
| 1 | +import pytest |
| 2 | + |
| 3 | +import fastjsonschema |
| 4 | +from fastjsonschema import JsonSchemaValueException |
| 5 | + |
| 6 | + |
| 7 | +def _composition_example(composition="oneOf"): |
| 8 | + return { |
| 9 | + "definitions": { |
| 10 | + "multiple-of-3": {"type": "number", "multipleOf": 3}, |
| 11 | + "multiple-of-5": {"type": "number", "multipleOf": 5}, |
| 12 | + }, |
| 13 | + composition: [ |
| 14 | + {"$ref": "#/definitions/multiple-of-3"}, |
| 15 | + {"$ref": "#/definitions/multiple-of-5"}, |
| 16 | + ], |
| 17 | + } |
| 18 | + |
| 19 | + |
| 20 | +@pytest.mark.parametrize( |
| 21 | + "composition, value", |
| 22 | + [("oneOf", 10), ("allOf", 15), ("anyOf", 9)] |
| 23 | +) |
| 24 | +def test_composition(asserter, composition, value): |
| 25 | + asserter(_composition_example(composition), value, value) |
| 26 | + |
| 27 | + |
| 28 | +@pytest.mark.parametrize( |
| 29 | + "composition, value", |
| 30 | + [("oneOf", 2), ("anyOf", 2), ("allOf", 3)] |
| 31 | +) |
| 32 | +def test_ref_is_expanded_on_composition_error(composition, value): |
| 33 | + with pytest.raises(JsonSchemaValueException) as exc: |
| 34 | + fastjsonschema.validate(_composition_example(composition), value) |
| 35 | + |
| 36 | + if composition in exc.value.definition: |
| 37 | + assert exc.value.definition[composition] == [ |
| 38 | + {"type": "number", "multipleOf": 3}, |
| 39 | + {"type": "number", "multipleOf": 5}, |
| 40 | + ] |
| 41 | + else: |
| 42 | + # allOf will fail on the first invalid item, |
| 43 | + # so the error message will not refer to the entire composition |
| 44 | + assert composition == "allOf" |
| 45 | + assert "$ref" not in exc.value.definition |
| 46 | + assert exc.value.definition["type"] == "number" |
| 47 | + |
| 48 | + |
| 49 | +@pytest.mark.parametrize( |
| 50 | + "composition, value", |
| 51 | + [("oneOf", 2), ("anyOf", 2), ("allOf", 3)] |
| 52 | +) |
| 53 | +def test_ref_is_expanded_with_resolver(composition, value): |
| 54 | + repo = { |
| 55 | + "sch://multiple-of-3": {"type": "number", "multipleOf": 3}, |
| 56 | + "sch://multiple-of-5": {"type": "number", "multipleOf": 5}, |
| 57 | + } |
| 58 | + schema = { |
| 59 | + composition: [ |
| 60 | + {"$ref": "sch://multiple-of-3"}, |
| 61 | + {"$ref": "sch://multiple-of-5"}, |
| 62 | + ] |
| 63 | + } |
| 64 | + |
| 65 | + with pytest.raises(JsonSchemaValueException) as exc: |
| 66 | + fastjsonschema.validate(schema, value, handlers={"sch": repo.__getitem__}) |
| 67 | + |
| 68 | + if composition in exc.value.definition: |
| 69 | + assert exc.value.definition[composition] == [ |
| 70 | + {"type": "number", "multipleOf": 3}, |
| 71 | + {"type": "number", "multipleOf": 5}, |
| 72 | + ] |
| 73 | + else: |
| 74 | + # allOf will fail on the first invalid item, |
| 75 | + # so the error message will not refer to the entire composition |
| 76 | + assert composition == "allOf" |
| 77 | + assert "$ref" not in exc.value.definition |
| 78 | + assert exc.value.definition["type"] == "number" |
0 commit comments