Пример #1
0
def test_save_pickled_sklearn_object_and_version_handles_existing_file(
        fs, sklearn_scaler_object):
    """
    Exception is raised if file already exists and overwrite mode is not set, and that file is
    overwritten if it is.
    """

    # Create a fake file
    pre_existing_pathname = '/fake_directory/pre_existing_file'
    fs.create_file(pre_existing_pathname)

    # Check that exception is raised if file already exists and user did not specify it should be over-written
    with pytest.raises(FileExistsError, match=r'File .* already exists.*'):
        sk_io.save_pickled_sklearn_object_and_version(
            sklearn_object=sklearn_scaler_object,
            filename_or_path=pre_existing_pathname,
            overwrite=False,
        )

    # Check that existing file is overwritten if requested
    existing_file_last_modified_time = os.stat(pre_existing_pathname).st_mtime

    sk_io.save_pickled_sklearn_object_and_version(
        sklearn_object=sklearn_scaler_object,
        filename_or_path=pre_existing_pathname,
        overwrite=True,
    )

    overwritten_file_last_modified_time = os.stat(
        pre_existing_pathname).st_mtime

    assert overwritten_file_last_modified_time > existing_file_last_modified_time
Пример #2
0
def test_get_python_files_from_dir(fs):
    for f in [
            "dir/" + f for f in ["a.py", "b.py", "c.py", "d.txt", "sub/e.py"]
    ]:
        fs.create_file(f)

    result = load._get_python_files_from_dir("dir", "output")
    assert result == [
        PyTestGenInputFile("a.py", f"dir"),
        PyTestGenInputFile("b.py", f"dir"),
        PyTestGenInputFile("c.py", f"dir"),
        PyTestGenInputFile("e.py", f"dir{sep}sub")
    ]
Пример #3
0
def test_directory(fs):
    for f in [
            "dir/" + f for f in ["a.py", "b.py", "c.py", "d.txt", "sub/e.py"]
    ]:
        fs.create_file(f)

    result = load.directory("dir", "output")
    assert result == PyTestGenInputSet("output", [
        PyTestGenInputFile("a.py", f"dir"),
        PyTestGenInputFile("b.py", f"dir"),
        PyTestGenInputFile("c.py", f"dir"),
        PyTestGenInputFile("e.py", f"dir{sep}sub")
    ])
Пример #4
0
def test_get_encryption_error(mock_encryption, fs):
    config_yml = """storage_providers: {}
logging_config:
  version: 1
encryption_provider:
  provider: non exist
  config: {}
plugins_config: {}
plugins_config_location: {}"""
    fs.create_file("victoria.yaml", contents=config_yml)
    config = victoria.config.load("victoria.yaml")
    with pytest.raises(ValueError):
        config.get_encryption()
Пример #5
0
def test_get_storage_invalid_config(mock_storage, fs):
    config_yml = """storage_providers:
  azure:
    wrong: values
logging_config:
  version: 1
encryption_provider: null
plugins_config: {}
plugins_config_location: {}"""
    fs.create_file("victoria.yaml", contents=config_yml)
    config = victoria.config.load("victoria.yaml")
    with pytest.raises(TypeError):
        config.get_storage("azure")
Пример #6
0
def test_get_encryption_invalid_config(mock_encryption, fs):
    config_yml = """storage_providers: {}
logging_config:
  version: 1
encryption_provider:
  provider: azure
  config:
    bad: config
plugins_config: {}
plugins_config_location: {}"""
    fs.create_file("victoria.yaml", contents=config_yml)
    config = victoria.config.load("victoria.yaml")
    with pytest.raises(TypeError):
        config.get_encryption()
Пример #7
0
def test_output_parsed_file_exists(fs, mock_parsed_file, monkeypatch):
    test_file_path = path.join("output", "a_dir", "test_a_file.py")
    fs.create_file(mock_parsed_file.input_file.get_test_file_path("output"))
    pytestgen.output._output_parsed_file(mock_parsed_file, "output")
    assert path.exists(test_file_path) == True, "test file did not exist"

    # we need to patch FunctionDef back in, it was patched out in the
    # 'mock_class_testable_func' fixture used in 'mock_parsed_set'
    # otherwise isinstance() for FunctionDef will fail in
    # get_existing_test_functions()
    monkeypatch.setattr(pytestgen.parse.ast, "FunctionDef", FunctionDef)

    outputted_funcs = get_existing_test_functions(test_file_path)
    assert outputted_funcs == [
        "test_a_test_function", "test_testclass_a_class_test_function"
    ]
Пример #8
0
def test_load_plugin_config_storage_provider(fs):
    config_yml = """storage_providers:
  local:
    container: container
logging_config:
  version: 1
encryption_provider: null
plugins_config: {}
plugins_config_location:
  config: local://config.yaml"""
    fs.create_file("victoria.yaml", contents=config_yml)
    fs.create_file("container/config.yaml", contents="test_field: 2")
    config = victoria.config.load("victoria.yaml")
    plugin_def = victoria.plugin.Plugin("config", None, PluginSchema())
    plugin_config = victoria.config.load_plugin_config(plugin_def, config)
    assert plugin_config["test_field"] == 2
Пример #9
0
def config_fixture(mock_storage, mock_encryption, fs):
    config_yml = """logging_config:
  version: 1
  disable_existing_loggers: True
  formatters:
    default:
      format: "%(message)s"
  handlers:
    console:
      class: logging.StreamHandler
      level: INFO
      formatter: default
      stream: ext://sys.stdout
  root:
    level: INFO
    handlers: [console]
  loggers:
    azure.core.pipeline.policies.http_logging_policy:
      level: CRITICAL
encryption_provider:
  provider: azure
  config:
    vault_url: "https://your-vault.vault.azure.net/"
    key: keyencryptionkey
    tenant_id: tenant-id-here
    client_id: sp-client-id-here
    client_secret: sp-client-secret-here
storage_providers:
  azure:
    connection_string: conn_str
    container: victoria
plugins_config:
  config:
    indent: 2
plugins_config_location: {}"""
    fs.create_file("victoria.yaml", contents=config_yml)
    return victoria.config.load("victoria.yaml")
Пример #10
0
def test_pytestgeninputfile_has_test_file(fs):
    fs.create_file("output/dir/test_b.py")
    instance = PyTestGenInputFile("b.py", "dir")
    assert instance.has_test_file("output") == True
Пример #11
0
def test_filename(fs, file, expected, raises):
    fs.create_file("/dir/a_file.py")

    with raises:
        result = load.filename(file, "output")
        assert result == expected