Example #1
0
def test_bad_shortcut():
    cli = CLI(prog="cartography-detectdrift")
    directory = "tests/data/test_cli_detectors/bad_shortcut"
    start_state = "1.json"
    end_state = "invalid-shortcut"
    with pytest.raises(FileNotFoundError):
        cli.main([
            "get-drift", "--query-directory", directory, "--start-state",
            start_state, "--end-state", end_state
        ])
Example #2
0
def test_cli_main(mock_run):
    """
    Tests that CLI runs update.
    """
    cli = CLI(prog="cartography-detectdrift")
    cli.main([
        "get-state", "--neo4j-uri",
        settings.get("NEO4J_URL"), "--drift-detection-directory",
        "tests/data/test_update_detectors"
    ])
    mock_run.assert_called_once()
Example #3
0
def test_cli_shortcuts(mock_run_add_shortcut):
    """
    Tests that the CLI calls add shortcuts.
    """
    file = "1.json"
    shortcut = "most-recent"
    directory = "tests/data/test_cli_detectors/detector"
    cli = CLI(prog="cartography-detectdrift")
    cli.main([
        "add-shortcut", "--query-directory", directory, "--shortcut", shortcut,
        "--file", file
    ])
    mock_run_add_shortcut.assert_called_once()
Example #4
0
def test_cli_get_drift(mock_run_drift_detection):
    """
    Tests that get_drift is called.
    """
    start_state = "1.json"
    end_state = "2.json"
    directory = "tests/data/test_cli_detectors/detector"
    cli = CLI(prog="cartography-detectdrift")
    cli.main([
        "get-drift", "--query-directory", directory, "--start-state",
        start_state, "--end-state", end_state
    ])
    mock_run_drift_detection.assert_called_once()
Example #5
0
def test_nonexistent_shortcuts():
    cli = CLI(prog="cartography-detectdrift")
    directory = "tests/data/test_cli_detectors/detector"
    alias = "test_shortcut"
    file = "3.json"
    shortcut_path = os.path.join(directory, "shortcut.json")
    cli.main([
        "add-shortcut", "--query-directory", directory, "--shortcut", alias,
        "--file", file
    ])
    shortcut_data = FileSystem.load(shortcut_path)
    shortcut = ShortcutSchema().load(shortcut_data)
    with pytest.raises(KeyError):
        shortcut.shortcuts[alias]
Example #6
0
def test_basic_add_shortcuts():
    """
    Tests that the CLI can add shortcuts.
    """
    cli = CLI(prog="cartography-detectdrift")
    directory = "tests/data/test_cli_detectors/detector"
    alias = "test_shortcut"
    file = "1.json"
    shortcut_path = directory + '/shortcut.json'
    cli.main([
        "add-shortcut", "--query-directory", directory, "--shortcut", alias,
        "--file", file
    ])
    shortcut_data = FileSystem.load(shortcut_path)
    shortcut = ShortcutSchema().load(shortcut_data)
    assert shortcut.shortcuts[alias] == file
    shortcut.shortcuts.pop(alias)
    shortcut_data = ShortcutSchema().dump(shortcut)
    FileSystem.write(shortcut_data, shortcut_path)
Example #7
0
def test_shortcut_fails_when_shortcut_exists():
    """
    Tests add_shortcut fails when shortcuts exist.
    """
    cli = CLI(prog="cartography-detectdrift")
    directory = "tests/data/test_cli_detectors/detector"
    alias = "2.json"
    filename = "1.json"
    cli.main([
        "add-shortcut",
        "--query-directory",
        directory,
        "--shortcut",
        alias,
        "--file",
        filename,
    ])
    shortcut_path = directory + '/shortcut.json'
    shortcut_data = FileSystem.load(shortcut_path)
    shortcut = ShortcutSchema().load(shortcut_data)
    with pytest.raises(KeyError):
        shortcut.shortcuts[alias]
Example #8
0
def test_use_shortcuts_for_shortcuts():
    """
    Tests add_shortcut can parse shortcuts.
    """
    cli = CLI(prog="cartography-detectdrift")
    directory = "tests/data/test_cli_detectors/detector"
    alias = "test_shortcut"
    alias_2 = "test_shortcut_2"
    filename = "1.json"
    shortcut_path = directory + '/shortcut.json'
    cli.main([
        "add-shortcut",
        "--query-directory",
        directory,
        "--shortcut",
        alias,
        "--file",
        filename,
    ])
    cli.main([
        "add-shortcut",
        "--query-directory",
        directory,
        "--shortcut",
        alias_2,
        "--file",
        alias,
    ])
    shortcut_data = FileSystem.load(shortcut_path)
    shortcut = ShortcutSchema().load(shortcut_data)
    assert shortcut.shortcuts[alias] == filename
    assert shortcut.shortcuts[alias_2] == filename

    # Return shortcut back to its original state.
    shortcut.shortcuts.pop(alias)
    shortcut.shortcuts.pop(alias_2)
    shortcut_data = ShortcutSchema().dump(shortcut)
    FileSystem.write(shortcut_data, shortcut_path)
Example #9
0
def test_cli_main(mock_run):
    cli = CLI(prog="driftdetect")
    cli.main(["--neo4j-uri", settings.get("NEO4J_URL"), "--drift-detector-directory", "tests/data/detectors"])
    mock_run.assert_called_once()