Skip to content

Commit 7e4561c

Browse files
committed
Add initial get_preference unit test
1 parent 415a43b commit 7e4561c

File tree

1 file changed

+103
-2
lines changed

1 file changed

+103
-2
lines changed

tests/unit/resolution_resolvelib/test_provider.py

Lines changed: 103 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,34 @@
1-
from typing import TYPE_CHECKING, List, Optional
1+
from typing import (
2+
TYPE_CHECKING,
3+
Dict,
4+
Iterable,
5+
Iterator,
6+
List,
7+
Mapping,
8+
Optional,
9+
Sequence,
10+
)
11+
from unittest.mock import Mock
212

13+
import pytest
314
from pip._vendor.resolvelib.resolvers import RequirementInformation
415

516
from pip._internal.models.candidate import InstallationCandidate
617
from pip._internal.models.link import Link
718
from pip._internal.req.constructors import install_req_from_req_string
19+
from pip._internal.resolution.resolvelib.base import Candidate, Constraint, Requirement
20+
from pip._internal.resolution.resolvelib.candidates import (
21+
REQUIRES_PYTHON_IDENTIFIER,
22+
RequiresPythonCandidate,
23+
)
824
from pip._internal.resolution.resolvelib.factory import Factory
925
from pip._internal.resolution.resolvelib.provider import PipProvider
1026
from pip._internal.resolution.resolvelib.requirements import SpecifierRequirement
1127

1228
if TYPE_CHECKING:
13-
from pip._internal.resolution.resolvelib.provider import PreferenceInformation
29+
from pip._vendor.resolvelib.providers import Preference
30+
31+
PreferenceInformation = RequirementInformation[Requirement, Candidate]
1432

1533

1634
def build_requirement_information(
@@ -76,3 +94,86 @@ def test_provider_known_depths(factory: Factory) -> None:
7694
transitive_requirement_name: 2.0,
7795
root_requirement_name: 1.0,
7896
}
97+
98+
99+
def create_mock_factory() -> Factory:
100+
# Mock the required components for the Factory initialization
101+
finder = Mock()
102+
preparer = Mock()
103+
make_install_req = Mock()
104+
wheel_cache = Mock()
105+
use_user_site = False
106+
force_reinstall = False
107+
ignore_installed = False
108+
ignore_requires_python = False
109+
110+
# Create a Factory instance with mock components
111+
return Factory(
112+
finder=finder,
113+
preparer=preparer,
114+
make_install_req=make_install_req,
115+
wheel_cache=wheel_cache,
116+
use_user_site=use_user_site,
117+
force_reinstall=force_reinstall,
118+
ignore_installed=ignore_installed,
119+
ignore_requires_python=ignore_requires_python,
120+
)
121+
122+
123+
@pytest.mark.parametrize(
124+
"identifier, resolutions, candidates, information, backtrack_causes, expected",
125+
[
126+
(
127+
REQUIRES_PYTHON_IDENTIFIER,
128+
{},
129+
{REQUIRES_PYTHON_IDENTIFIER: iter([RequiresPythonCandidate((3, 7))])},
130+
{REQUIRES_PYTHON_IDENTIFIER: build_requirement_information("python", None)},
131+
[],
132+
(
133+
False,
134+
True,
135+
True,
136+
True,
137+
1.0,
138+
float("inf"),
139+
True,
140+
REQUIRES_PYTHON_IDENTIFIER,
141+
),
142+
),
143+
],
144+
)
145+
def test_get_preference(
146+
identifier: str,
147+
resolutions: Mapping[str, Candidate],
148+
candidates: Mapping[str, Iterator[Candidate]],
149+
information: Mapping[str, Iterable["PreferenceInformation"]],
150+
backtrack_causes: Sequence["PreferenceInformation"],
151+
expected: "Preference",
152+
) -> None:
153+
# Create the factory with mock components
154+
factory = create_mock_factory()
155+
constraints: Dict[str, Constraint] = {}
156+
user_requested = {"requested-package": 0}
157+
ignore_dependencies = False
158+
upgrade_strategy = "to-satisfy-only"
159+
160+
# Initialize PipProvider
161+
provider = PipProvider(
162+
factory=factory,
163+
constraints=constraints,
164+
ignore_dependencies=ignore_dependencies,
165+
upgrade_strategy=upgrade_strategy,
166+
user_requested=user_requested,
167+
)
168+
169+
# Get the preference for the test case
170+
preference = provider.get_preference(
171+
identifier,
172+
resolutions,
173+
candidates,
174+
information,
175+
backtrack_causes,
176+
)
177+
178+
# Assert the calculated preference matches the expected preference
179+
assert preference == expected, f"Expected {expected}, got {preference}"

0 commit comments

Comments
 (0)