def test_load_resource_spec_invalid_ref(): copy = json.loads(json.dumps(BASIC_SCHEMA)) copy["properties"]["foo"] = {"$ref": "#/bar"} with pytest.raises(SpecValidationError) as excinfo: load_resource_spec(json_s(copy)) cause = excinfo.value.__cause__ assert cause assert isinstance(cause, RefResolutionError) assert "bar" in str(cause)
def test_load_resource_spec_inliner_produced_invalid_schema(): with patch("rpdk.core.data_loaders.RefInliner", autospec=True) as mock_inliner: mock_inliner.return_value.inline.return_value = {} with pytest.raises(InternalError) as excinfo: load_resource_spec(json_s(BASIC_SCHEMA)) mock_inliner.assert_called_once_with(ANY, BASIC_SCHEMA) cause = excinfo.value.__cause__ assert cause assert isinstance(cause, ValidationError)
def test_load_resource_spec_remote_key_is_invalid(): schema = { "typeName": "AWS::FOO::BAR", "description": "test schema", "properties": {"foo": {"type": "string"}}, "primaryIdentifier": ["/properties/foo"], "remote": {}, } with pytest.raises(SpecValidationError) as excinfo: load_resource_spec(json_s(schema)) assert "remote" in str(excinfo.value)
def test_load_resource_spec_non_public_properties_and_definitions(): schema = { "typeName": "AWS::FOO::BAR", "description": "test schema", "additionalProperties": False, "definitions": {"bar": {"type": "string"}}, "properties": {"foo": {"type": "string"}}, "primaryIdentifier": ["/properties/foo"], "readOnlyProperties": ["/properties/foo"], "nonPublicProperties": ["/properties/foo"], "nonPublicDefinitions": ["/definitions/bar"], } load_resource_spec(json_s(schema))
def test_load_resource_spec_conditionally_create_only_match_read_only(): schema = { "typeName": "AWS::FOO::BAR", "description": "test schema", "additionalProperties": False, "properties": {"foo": {"type": "string"}}, "primaryIdentifier": ["/properties/foo"], "readOnlyProperties": ["/properties/foo"], "conditionalCreateOnlyProperties": ["/properties/foo"], } with pytest.raises(SpecValidationError) as excinfo: load_resource_spec(json_s(schema)) assert ( str(excinfo.value) == "readOnlyProperties and conditionalCreateOnlyProperties MUST NOT have common properties" )
def test_load_resource_spec_object_property_missing_additional_properties( schema, caplog ): schema = BASEDIR / "data" / "schema" / "valid" / schema with schema.open("r", encoding="utf-8") as f: assert load_resource_spec(f) assert "Resource spec validation would fail from next major version" in caplog.text
def test_load_resource_spec_unmodeled_object_property_missing_additional_properties( caplog, ): schema = BASEDIR / "data" / "schema" / "valid" / "valid_no_properties.json" with schema.open("r", encoding="utf-8") as f: assert load_resource_spec(f) assert ( "Resource spec validation would fail from next major version" not in caplog.text )
def test_load_resource_spec_uses_id_if_id_is_set(ref_fn): @Request.application def application(_request): return Response(json.dumps({"type": "string"}), mimetype="application/json") with wsgi_serve(application) as server: schema = { **BASIC_SCHEMA, "$id": server.url + "/foo", "properties": {"foo": {"$ref": ref_fn(server)}}, } inlined = load_resource_spec(json_s(schema)) assert inlined["remote"]["schema0"]["type"] == "string"
def test_load_resource_spec_example_spec_is_valid(example): with example.open("r", encoding="utf-8") as f: assert load_resource_spec(f)
def test_load_resource_spec_empty_object_is_invalid(): with pytest.raises(SpecValidationError): load_resource_spec(json_s({}))
def test_load_resource_spec_boolean_is_invalid(): with pytest.raises(SpecValidationError): load_resource_spec(json_s(True))
def test_load_resource_spec_empty_is_invalid(): with pytest.raises(SpecValidationError): load_resource_spec(StringIO(""))
def test_load_resource_spec_invalid_json(): with pytest.raises(SpecValidationError) as excinfo: load_resource_spec(StringIO('{"foo": "aaaaa}')) assert "line 1" in str(excinfo.value) assert "column 9" in str(excinfo.value)
def test_load_resource_spec_invalid_snippets(example): with example.open("r", encoding="utf-8") as f: with pytest.raises(SpecValidationError): load_resource_spec(f)
def test_load_resource_spec_object_property_additional_properties_true(schema): schema = BASEDIR / "data" / "schema" / "invalid" / schema with schema.open("r", encoding="utf-8") as f: with pytest.raises(SpecValidationError) as excinfo: load_resource_spec(f) assert "False was expected" in str(excinfo.value)