aboutsummaryrefslogtreecommitdiff
path: root/tests/test_utils_py_manager.py
blob: e41680d5ea8a5ff71713903f937160ec9bb765cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# SPDX-FileCopyrightText: Copyright 2022, Arm Limited and/or its affiliates.
# SPDX-License-Identifier: Apache-2.0
"""Tests for python package manager."""
import sys
from unittest.mock import MagicMock

import pytest

from mlia.utils.py_manager import get_package_manager
from mlia.utils.py_manager import PyPackageManager


def test_get_package_manager() -> None:
    """Test function get_package_manager."""
    manager = get_package_manager()
    assert isinstance(manager, PyPackageManager)


@pytest.fixture(name="mock_check_call")
def mock_check_call_fixture(monkeypatch: pytest.MonkeyPatch) -> MagicMock:
    """Mock check_call function."""
    mock_check_call = MagicMock()
    monkeypatch.setattr("mlia.utils.py_manager.check_call", mock_check_call)

    return mock_check_call


def test_py_package_manager_metadata() -> None:
    """Test getting package status."""
    manager = PyPackageManager()
    assert manager.package_installed("pytest")
    assert manager.packages_installed(["pytest", "mlia"])


def test_py_package_manager_install(mock_check_call: MagicMock) -> None:
    """Test package installation."""
    manager = PyPackageManager()
    with pytest.raises(ValueError, match="No package names provided"):
        manager.install([])

    manager.install(["mlia", "pytest"])
    mock_check_call.assert_called_once_with(
        [
            sys.executable,
            "-m",
            "pip",
            "--disable-pip-version-check",
            "install",
            "mlia",
            "pytest",
        ]
    )


def test_py_package_manager_uninstall(mock_check_call: MagicMock) -> None:
    """Test package removal."""
    manager = PyPackageManager()
    with pytest.raises(ValueError, match="No package names provided"):
        manager.uninstall([])

    manager.uninstall(["mlia", "pytest"])
    mock_check_call.assert_called_once_with(
        [
            sys.executable,
            "-m",
            "pip",
            "--disable-pip-version-check",
            "uninstall",
            "--yes",
            "mlia",
            "pytest",
        ]
    )