Skip to content

Commit

Permalink
Set default type when generating docs to object (#417)
Browse files Browse the repository at this point in the history
* Parametrize docs tests
  • Loading branch information
nina-ctrlv authored Apr 13, 2020
1 parent 810aa0e commit c2f4ff2
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 61 deletions.
10 changes: 6 additions & 4 deletions src/rpdk/core/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,17 +416,19 @@ def _set_docs_properties(self, propname, prop, proppath):
):
prop["readonly"] = True

if prop["type"] in BASIC_TYPE_MAPPINGS:
mapped = BASIC_TYPE_MAPPINGS[prop["type"]]
prop_type = prop.get("type", "object")

if prop_type in BASIC_TYPE_MAPPINGS:
mapped = BASIC_TYPE_MAPPINGS[prop_type]
prop["jsontype"] = prop["yamltype"] = prop["longformtype"] = mapped
elif prop["type"] == "array":
elif prop_type == "array":
prop["arrayitems"] = arrayitems = self._set_docs_properties(
propname, prop["items"], proppath
)
prop["jsontype"] = f'[ {arrayitems["jsontype"]}, ... ]'
prop["yamltype"] = f'\n - {arrayitems["longformtype"]}'
prop["longformtype"] = f'List of {arrayitems["longformtype"]}'
elif prop["type"] == "object":
elif prop_type == "object":
template = self.env.get_template("docs-subproperty.md")
docs_path = self.root / "docs"

Expand Down
23 changes: 23 additions & 0 deletions tests/data/schema/valid/valid_no_type.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"typeName": "AWS::Valid::TypeName",
"description": "a test schema",
"definitions": {
"obj1def": {
"properties": {
"str1": {
"type": "string",
"minLength": 2
}
}
}
},
"properties": {
"property1": {
"$ref": "#/definitions/obj1def"
}
},
"primaryIdentifier": [
"/properties/property1"
],
"additionalProperties": false
}
78 changes: 21 additions & 57 deletions tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,38 +177,29 @@ def test_generate_no_handlers(project):
mock_plugin.generate.assert_called_once_with(project)


def test_generate_with_docs(project, tmp_path_factory):
project.schema = resource_json(
__name__, "data/schema/valid/valid_type_complex.json"
)
project.type_name = "AWS::Color::Red"
# tmpdir conflicts with other tests, make a unique one
project.root = tmp_path_factory.mktemp("generate_with_docs")
mock_plugin = MagicMock(spec=["generate"])
with patch.object(project, "_plugin", mock_plugin):
project.generate()
project.generate_docs()
mock_plugin.generate.assert_called_once_with(project)

docs_dir = project.root / "docs"
readme_file = project.root / "docs" / "README.md"

assert docs_dir.is_dir()
assert readme_file.is_file()
with patch.object(project, "_plugin", mock_plugin):
project.generate()
project.generate_docs()
readme_contents = readme_file.read_text(encoding="utf-8")
assert project.type_name in readme_contents


def test_generate_with_docs_nested_object(project, tmp_path_factory):
project.schema = resource_json(
__name__, "data/schema/valid/valid_nested_property_object.json"
)
@pytest.mark.parametrize(
"schema_path,path",
[
("data/schema/valid/valid_no_type.json", "generate_with_no_type_defined"),
(
"data/schema/valid/valid_type_complex.json",
"generate_with_docs_type_complex",
),
(
"data/schema/valid/valid_nested_property_object.json",
"generate_with_docs_nested_object",
),
(
"data/schema/valid/valid_type_composite_primary_identifier.json",
"generate_with_docs_composite_primary_identifier",
),
],
)
def test_generate_with_docs(project, tmp_path_factory, schema_path, path):
project.schema = resource_json(__name__, schema_path)
project.type_name = "AWS::Color::Red"
# tmpdir conflicts with other tests, make a unique one
project.root = tmp_path_factory.mktemp("generate_with_docs_nested_object")
project.root = tmp_path_factory.mktemp(path)
mock_plugin = MagicMock(spec=["generate"])
with patch.object(project, "_plugin", mock_plugin):
project.generate()
Expand Down Expand Up @@ -251,33 +242,6 @@ def test_generate_with_docs_invalid_property_type(project, tmp_path_factory):
assert project.type_name in readme_contents


def test_generate_with_docs_composite_primary_identifier(project, tmp_path_factory):
project.schema = resource_json(
__name__, "data/schema/valid/valid_type_composite_primary_identifier.json"
)
project.type_name = "AWS::Color::Red"
# tmpdir conflicts with other tests, make a unique one
project.root = tmp_path_factory.mktemp(
"generate_with_docs_composite_primary_identifier"
)
mock_plugin = MagicMock(spec=["generate"])
with patch.object(project, "_plugin", mock_plugin):
project.generate()
project.generate_docs()
mock_plugin.generate.assert_called_once_with(project)

docs_dir = project.root / "docs"
readme_file = project.root / "docs" / "README.md"

assert docs_dir.is_dir()
assert readme_file.is_file()
with patch.object(project, "_plugin", mock_plugin):
project.generate()
project.generate_docs()
readme_contents = readme_file.read_text(encoding="utf-8")
assert project.type_name in readme_contents


def test_generate_with_docs_no_type(project, tmp_path_factory):
project.schema = {"properties": {}}
# tmpdir conflicts with other tests, make a unique one
Expand Down

0 comments on commit c2f4ff2

Please sign in to comment.