Skip to content

Commit

Permalink
Fix unit tests that fail without internet access (#3487)
Browse files Browse the repository at this point in the history
## Changes
Fix unit tests that fail without internet access

### Linked issues

Resolves #3486

### Tests

- [x] added unit tests
  • Loading branch information
JCZuurmond authored Jan 7, 2025
1 parent 505a062 commit 5c02968
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 33 deletions.
11 changes: 10 additions & 1 deletion tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,17 @@ def ws():

@pytest.fixture
def simple_dependency_resolver(mock_path_lookup: PathLookup) -> DependencyResolver:

def mock_pip_install_always_successful(_) -> tuple[int, str, str]:
"""Mock an always successful pip install.
Pip installs require internet access which we want to avoid during unit testing for speed and reliability.
While using the simple dependency resolver, we mock any library install as successful.
"""
return 0, "", ""

allow_list = KnownList()
library_resolver = PythonLibraryResolver(allow_list)
library_resolver = PythonLibraryResolver(allow_list, mock_pip_install_always_successful)
notebook_resolver = NotebookResolver(NotebookLoader())
import_resolver = ImportFileResolver(FileLoader(), allow_list)
return DependencyResolver(library_resolver, notebook_resolver, import_resolver, import_resolver, mock_path_lookup)
Expand Down
26 changes: 7 additions & 19 deletions tests/unit/source_code/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,28 @@
import pytest

from databricks.labs.ucx.source_code.base import CurrentSessionState
from databricks.labs.ucx.source_code.linters.files import FileLoader, ImportFileResolver, FolderLoader
from databricks.labs.ucx.source_code.linters.files import FileLoader, FolderLoader
from databricks.labs.ucx.source_code.graph import (
Dependency,
DependencyGraph,
DependencyResolver,
InheritedContext,
DependencyGraphWalker,
)
from databricks.labs.ucx.source_code.notebooks.loaders import NotebookResolver, NotebookLoader
from databricks.labs.ucx.source_code.notebooks.loaders import NotebookLoader
from databricks.labs.ucx.source_code.path_lookup import PathLookup
from databricks.labs.ucx.source_code.python.python_ast import Tree
from databricks.labs.ucx.source_code.python_libraries import PythonLibraryResolver
from databricks.labs.ucx.source_code.known import KnownList


def test_dependency_graph_registers_library(mock_path_lookup) -> None:
def test_dependency_graph_registers_library_from_egg(mock_path_lookup, simple_dependency_resolver) -> None:
egg_path = Path(__file__).parent / "samples/distribution/dist/thingy-0.0.1-py3.10.egg"
dependency = Dependency(FileLoader(), Path("test"))
file_loader = FileLoader()
allow_list = KnownList()
session_state = CurrentSessionState()
import_file_resolver = ImportFileResolver(file_loader, allow_list)
dependency_resolver = DependencyResolver(
PythonLibraryResolver(allow_list),
NotebookResolver(NotebookLoader()),
import_file_resolver,
import_file_resolver,
mock_path_lookup,
)
graph = DependencyGraph(dependency, None, dependency_resolver, mock_path_lookup, session_state)
graph = DependencyGraph(dependency, None, simple_dependency_resolver, mock_path_lookup, session_state)

problems = graph.register_library("demo-egg") # installs pkgdir
problems = graph.register_library(egg_path.as_posix())

assert len(problems) == 0
lookup_resolve = graph.path_lookup.resolve(Path("pkgdir"))
lookup_resolve = graph.path_lookup.resolve(Path("thingy"))
assert lookup_resolve is not None
assert lookup_resolve.exists()

Expand Down
13 changes: 0 additions & 13 deletions tests/unit/source_code/test_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,6 @@ def test_workflow_task_container_builds_dependency_graph_empty_task(mock_path_lo
ws.assert_not_called()


def test_workflow_task_container_builds_dependency_graph_pytest_pypi_library(mock_path_lookup, graph) -> None:
ws = create_autospec(WorkspaceClient)
libraries = [compute.Library(pypi=compute.PythonPyPiLibrary(package="demo-egg"))] # installs pkgdir
task = jobs.Task(task_key="test", libraries=libraries)

workflow_task_container = WorkflowTaskContainer(ws, task, Job())
problems = workflow_task_container.build_dependency_graph(graph)

assert len(problems) == 0
assert graph.path_lookup.resolve(Path("pkgdir")).exists()
ws.assert_not_called()


def test_workflow_task_container_builds_dependency_graph_unknown_pypi_library(mock_path_lookup, graph) -> None:
ws = create_autospec(WorkspaceClient)
libraries = [compute.Library(pypi=compute.PythonPyPiLibrary(package="unknown-library-name"))]
Expand Down
1 change: 1 addition & 0 deletions tests/unit/source_code/test_known.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def test_error_on_missing_known_json() -> None:
KnownList._get_known() # pylint: disable=protected-access


@pytest.mark.skip(reason="Interferes with tests reading known list at the same time of rebuilding (writing)")
def test_rebuild_trivial() -> None:
# No-op: the known.json file is already up-to-date
cwd = Path.cwd()
Expand Down

0 comments on commit 5c02968

Please sign in to comment.