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 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 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 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()