Skip to content

Commit

Permalink
Test Solver and UnSolvable
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoinePrv committed Feb 9, 2024
1 parent d028733 commit c369884
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 11 deletions.
12 changes: 6 additions & 6 deletions libmamba/src/solver/libsolv/unsolvable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,19 @@ namespace mamba::solver::libsolv
return *m_solver;
}

auto UnSolvable::problems(Database& mpool) const -> std::vector<std::string>
auto UnSolvable::problems(Database& db) const -> std::vector<std::string>
{
auto& pool = Database::Impl::get(mpool);
auto& pool = Database::Impl::get(db);
std::vector<std::string> problems;
solver().for_each_problem_id([&](solv::ProblemId pb)
{ problems.emplace_back(solver().problem_to_string(pool, pb)); }
);
return problems;
}

auto UnSolvable::problems_to_str(Database& mpool) const -> std::string
auto UnSolvable::problems_to_str(Database& db) const -> std::string
{
auto& pool = Database::Impl::get(mpool);
auto& pool = Database::Impl::get(db);
std::stringstream problems;
problems << "Encountered problems while solving:\n";
solver().for_each_problem_id(
Expand All @@ -63,9 +63,9 @@ namespace mamba::solver::libsolv
return problems.str();
}

auto UnSolvable::all_problems_to_str(Database& mpool) const -> std::string
auto UnSolvable::all_problems_to_str(Database& db) const -> std::string
{
auto& pool = Database::Impl::get(mpool);
auto& pool = Database::Impl::get(db);
std::stringstream problems;
solver().for_each_problem_id(
[&](solv::ProblemId pb)
Expand Down
15 changes: 10 additions & 5 deletions libmambapy/src/libmambapy/bindings/solver_libsolv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,16 @@ namespace mambapy
);

py::class_<UnSolvable>(m, "UnSolvable")
.def("problems", &UnSolvable::problems)
.def("problems_to_str", &UnSolvable::problems_to_str)
.def("all_problems_to_str", &UnSolvable::all_problems_to_str)
.def("problems_graph", &UnSolvable::problems_graph)
.def("explain_problems", &UnSolvable::explain_problems);
.def("problems", &UnSolvable::problems, py::arg("database"))
.def("problems_to_str", &UnSolvable::problems_to_str, py::arg("database"))
.def("all_problems_to_str", &UnSolvable::all_problems_to_str, py::arg("database"))
.def("problems_graph", &UnSolvable::problems_graph, py::arg("database"))
.def(
"explain_problems",
&UnSolvable::explain_problems,
py::arg("database"),
py::arg("palette")
);

constexpr auto solver_flags_v2_migrator = [](Solver&, py::args, py::kwargs) {
throw std::runtime_error("All flags need to be passed in the libmambapy.solver.Request.");
Expand Down
37 changes: 37 additions & 0 deletions libmambapy/tests/test_solver_libsolv.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,40 @@ def test_Database_RepoInfo_from_repodata_error():
db.add_repo_from_native_serialization(
path="/does/not/exists", expected=libsolv.RepodataOrigin()
)


def test_Solver_UnSolvable():
Request = libmambapy.solver.Request

db = libsolv.Database(libmambapy.specs.ChannelResolveParams())

request = Request([Request.Install(libmambapy.specs.MatchSpec.parse("a>1.0"))])

solver = libsolv.Solver()
outcome = solver.solve(db, request)

assert isinstance(outcome, libsolv.UnSolvable)
assert "nothing provides" in "\n".join(outcome.problems(db))
assert "nothing provides" in outcome.problems_to_str(db)
assert "nothing provides" in outcome.all_problems_to_str(db)
assert "The following package could not be installed" in outcome.explain_problems(
db, libmambapy.Palette.no_color()
)
assert outcome.problems_graph(db).graph() is not None


def test_Solver_Solution():
Request = libmambapy.solver.Request

db = libsolv.Database(libmambapy.specs.ChannelResolveParams())
db.add_repo_from_packages(
[libmambapy.specs.PackageInfo(name="foo")],
)

request = Request([Request.Install(libmambapy.specs.MatchSpec.parse("foo"))])

solver = libsolv.Solver()
outcome = solver.solve(db, request)

assert isinstance(outcome, libmambapy.solver.Solution)
assert len(outcome.actions) == 1

0 comments on commit c369884

Please sign in to comment.