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 ])
def test_configurate(): cli = CLI(prog="driftdetect") config = cli.configure([ "--neo4j-uri", settings.get("NEO4J_URL"), "--drift-detector-directory", "tests/data/detectors" ]) assert config.drift_detector_directory == "tests/data/detectors" assert config.neo4j_uri == settings.get("NEO4J_URL")
def test_run(mock_perform_drift_detection, neo4j_session): cli = CLI(prog="driftdetect") config = cli.configure([ "--neo4j-uri", settings.get("NEO4J_URL"), "--drift-detector-directory", "tests/data/detectors" ]) run(config) assert mock_perform_drift_detection.called_once_with(neo4j_session, config.drift_detector_directory, False)
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()
def test_configure(): """ Tests that the configure method correctly parses args. """ cli = CLI(prog="cartography-detectdrift") config = cli.configure([ "get-state", "--neo4j-uri", settings.get("NEO4J_URL"), "--drift-detection-directory", "tests/data/detectors" ]) assert config.drift_detection_directory == "tests/data/detectors" assert config.neo4j_uri == settings.get("NEO4J_URL")
def test_run(neo4j_session): """ Test that a full run through with the CLI works :param neo4j_session: :return: """ data = [ ["1", "8", ["15", "22", "29"]], ["2", "9", ["16", "23", "30"]], ["3", "10", ["17", "24", "31"]], ["4", "11", ["18", "25", "32"]], ["5", "12", ["19", "26", "33"]], ["6", "13", ["20", "27", "34"]], ["7", "14", ["21", "28", "35"]], ["36", "37", ["38", "39", "40"]] ] ingest_nodes = """ MERGE (person:Person{test: {test}}) SET person.test2 = {test2}, person.test3 = {test3} """ for node in data: test = node[0] test2 = node[1] test3 = node[2] neo4j_session.run( ingest_nodes, test=test, test2=test2, test3=test3 ) cli = CLI(prog="driftdetect") # Test that run does not work with no directory specified config = cli.configure([ "--neo4j-uri", settings.get("NEO4J_URL") ]) assert not run(config) config = cli.configure([ "--neo4j-uri", settings.get("NEO4J_URL"), "--drift-detector-directory", "tests/data/detectors" ]) drift_pairs = run(config) drift_info = [pair[0] for pair in drift_pairs] assert {"d.test": "7"} in drift_info assert {"d.test": "7", "d.test2": "14"} in drift_info assert {"d.test": "7", "d.test2": "14", "d.test3": ["21", "28", "35"]} in drift_info assert {"d.test": "36", "d.test2": "37", "d.test3": ["38", "39", "40"]} in drift_info
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()
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()
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]
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)
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]
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)
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()