def test_overwrite(): mock_path = MagicMock(spec=Path) Project.overwrite(mock_path, LANGUAGE) mock_path.open.assert_called_once_with("w", encoding="utf-8") mock_f = mock_path.open.return_value.__enter__.return_value mock_f.write.assert_called_once_with(LANGUAGE)
def test_generate(project: Project): project.load_schema() before = get_files_in_project(project) project.generate() after = get_files_in_project(project) files = after.keys() - before.keys() - {"resource-role.yaml"} assert files == {"src/models.ts"}
def project(tmpdir): project = Project(root=tmpdir) with patch.dict( "rpdk.core.plugin_registry.PLUGIN_REGISTRY", {"test": lambda: JavaLanguagePlugin}, clear=True, ), patch( "rpdk.java.codegen.input_with_validation", autospec=True, return_value=("software", "amazon", "foo", RESOURCE.lower()), ): project.init("AWS::Foo::{}".format(RESOURCE), "test") return project
def project(tmp_path): project = Project(root=tmp_path) patch_plugins = patch.dict( "rpdk.core.plugin_registry.PLUGIN_REGISTRY", {PythonLanguagePlugin.NAME: lambda: PythonLanguagePlugin}, clear=True, ) patch_wizard = patch("rpdk.python.codegen.input_with_validation", autospec=True, side_effect=[False]) with patch_plugins, patch_wizard: project.init(TYPE_NAME, PythonLanguagePlugin.NAME) return project
def test_package_local(project: Project): project.load_schema() project.generate() zip_path = project.root / "foo-bar-baz.zip" with zip_path.open("wb") as f, ZipFile(f, mode="w") as zip_file: project._plugin.package(project, zip_file) with zip_path.open("rb") as f, ZipFile(f, mode="r") as zip_file: assert sorted(zip_file.namelist()) == [ "ResourceProvider.zip", "src/handlers.ts", "src/models.ts", ]
def project(tmpdir, request): def mock_input_with_validation(prompt, validate): # pylint: disable=unused-argument if prompt.startswith("Enter a package name"): return ("software", "amazon", "foo", RESOURCE.lower()) if prompt.startswith("Choose codegen model"): return request.param return "" project = Project(root=tmpdir) mock_cli = MagicMock(side_effect=mock_input_with_validation) with patch.dict( "rpdk.core.plugin_registry.PLUGIN_REGISTRY", {"test": lambda: JavaLanguagePlugin}, clear=True, ), patch("rpdk.java.codegen.input_with_validation", new=mock_cli): project.init("AWS::Foo::{}".format(RESOURCE), "test") return project
def test__get_docs_gettable_atts_good_path(): getatt = Project._get_docs_gettable_atts( { "properties": {"Id2": {"description": "foo"}}, "readOnlyProperties": ["/properties/Id2"], } ) assert getatt == [{"name": "Id2", "description": "foo"}]
def test_package_local(project: Project): project.load_schema() project.generate() zip_path = project.root / "foo-bar-baz.zip" # pylint: disable=unexpected-keyword-arg with zip_path.open("wb") as f, ZipFile( f, mode="w", strict_timestamps=False) as zip_file: project._plugin.package(project, zip_file) with zip_path.open("rb") as f, ZipFile( f, mode="r", strict_timestamps=False) as zip_file: assert sorted(zip_file.namelist()) == [ "ResourceProvider.zip", "src/handlers.ts", "src/models.ts", ]
def project(tmp_path: str): project = Project(root=tmp_path) patch_plugins = patch.dict( "rpdk.core.plugin_registry.PLUGIN_REGISTRY", {TypescriptLanguagePlugin.NAME: lambda: TypescriptLanguagePlugin}, clear=True, ) patch_wizard = patch( "rpdk.typescript.codegen.input_with_validation", autospec=True, side_effect=[False], ) with patch_plugins, patch_wizard: current_path = os.path.abspath(__file__) lib_abspath = os.path.abspath(os.path.join(current_path, "..", "..", "..")) TypescriptLanguagePlugin.SUPPORT_LIB_URI = f"file:{lib_abspath}" project.init(TYPE_NAME, TypescriptLanguagePlugin.NAME) return project
def project(tmpdir): unique_dir = "".join(random.choices(string.ascii_uppercase, k=12)) return Project(root=tmpdir.mkdir(unique_dir))
def test__get_docs_gettable_atts_bad_path(docs_schema): getatt = Project._get_docs_gettable_atts(docs_schema) assert getatt == [{ "name": "Id2", "description": "Returns the <code>Id2</code> value." }]
def test__get_docs_gettable_atts_empty(): getatt = Project._get_docs_gettable_atts({}) assert getatt == []
def test__get_docs_primary_identifier_good_path(): ref = Project._get_docs_primary_identifier( {"primaryIdentifier": ["/properties/Id1"]}) assert ref == "Id1"
def test__get_docs_primary_identifier_bad_path(docs_schema): ref = Project._get_docs_primary_identifier(docs_schema) assert ref is None
def test_generate_with_type_configuration(tmp_path): type_name = "schema::with::typeconfiguration" project = Project(root=tmp_path) patch_plugins = patch.dict( "rpdk.core.plugin_registry.PLUGIN_REGISTRY", {PythonLanguagePlugin.NAME: lambda: PythonLanguagePlugin}, clear=True, ) patch_wizard = patch("rpdk.python.codegen.input_with_validation", autospec=True, side_effect=[False]) with patch_plugins, patch_wizard: project.init(type_name, PythonLanguagePlugin.NAME) copyfile( str(Path.cwd() / "tests/data/schema-with-typeconfiguration.json"), str(project.root / "schema-with-typeconfiguration.json"), ) project.type_info = ("schema", "with", "typeconfiguration") project.load_schema() project.load_configuration_schema() project.generate() # assert TypeConfigurationModel is added to generated directory models_path = project.root / "src" / "schema_with_typeconfiguration" / "models.py" # this however loads the module spec = importlib.util.spec_from_file_location("foo_bar_baz.models", models_path) module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) assert hasattr(module.ResourceModel, "_serialize") assert hasattr(module.ResourceModel, "_deserialize") assert hasattr(module.TypeConfigurationModel, "_serialize") assert hasattr(module.TypeConfigurationModel, "_deserialize") type_configuration_schema_file = ( project.root / "schema-with-typeconfiguration-configuration.json") assert type_configuration_schema_file.is_file()