Skip to content

Commit

Permalink
missed bits
Browse files Browse the repository at this point in the history
  • Loading branch information
jGaboardi committed Apr 28, 2024
1 parent c7e669a commit 15f4f2b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 26 deletions.
6 changes: 6 additions & 0 deletions spopt/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,9 @@ def loc_raises_diff_crs() -> _pytest.python_api.RaisesContext:
def loc_raises_infeasible() -> _pytest.python_api.RaisesContext:
"""`locate` error"""
return pytest.raises(RuntimeError, match="Model is not solved: Infeasible.")


@pytest.fixture
def loc_raises_fac_constr() -> _pytest.python_api.RaisesContext:
"""`locate` error"""
return pytest.raises(AttributeError, match="Before setting facility constraint")
31 changes: 11 additions & 20 deletions spopt/tests/test_c_p_median.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import os

import numpy
import pandas
import pulp
import pytest

Expand All @@ -14,8 +11,6 @@
class TestSyntheticLocate:
@pytest.fixture(autouse=True)
def setup_method(self, network_instance) -> None:
self.dirpath = os.path.join(os.path.dirname(__file__), "./data/")

client_count, facility_count = 2, 3
(
self.clients_snapped,
Expand Down Expand Up @@ -66,7 +61,9 @@ def test_c_p_median_with_predefined_facilities_from_cost_matrix(self):
observed = result.fac2cli
assert known == observed

def test_c_p_median_with_predefined_facilities_infeasible(self):
def test_c_p_median_with_predefined_facilities_infeasible(
self, loc_raises_infeasible
):
facility_capacity = numpy.array([5, 7, 10])
demand_quantity = numpy.array([4, 10])
predefine = numpy.array([0])
Expand All @@ -78,17 +75,15 @@ def test_c_p_median_with_predefined_facilities_infeasible(self):
predefined_facilities_arr=predefine,
fulfill_predefined_fac=True,
)
with pytest.raises(RuntimeError, match="Model is not solved: Infeasible."):
with loc_raises_infeasible:
p_median.solve(pulp.PULP_CBC_CMD(msg=False))


class TestRealWorldLocate:
def setup_method(self) -> None:
self.dirpath = os.path.join(os.path.dirname(__file__), "./data/")
@pytest.fixture(autouse=True)
def setup_method(self, load_test_data) -> None:
time_table = load_test_data("example_subject_student_school_journeys.csv")

time_table = pandas.read_csv(
self.dirpath + "example_subject_student_school_journeys.csv"
)
self.cost_matrix = (
time_table.pivot_table(
columns="school",
Expand All @@ -101,12 +96,8 @@ def setup_method(self) -> None:
.values
)

self.demand_points = pandas.read_csv(
self.dirpath + "example_subject_students.csv"
)
self.facility_points = pandas.read_csv(
self.dirpath + "example_subject_schools.csv"
)
self.demand_points = load_test_data("example_subject_students.csv")
self.facility_points = load_test_data("example_subject_schools.csv")

self.p_facility = 10
self.demand = numpy.ones(len(self.demand_points))
Expand All @@ -128,11 +119,11 @@ def test_optimality_capacitated_pmedian_with_predefined_facilities(self):
pmedian = pmedian.solve(pulp.PULP_CBC_CMD(msg=False))
assert pmedian.problem.status == pulp.LpStatusOptimal

def test_infeasibility_capacitated_pmedian(self):
def test_infeasibility_capacitated_pmedian(self, loc_raises_infeasible):
pmedian = PMedian.from_cost_matrix(
self.cost_matrix, self.demand, 0, facility_capacities=self.capacities_arr
)
with pytest.raises(RuntimeError, match="Model is not solved: Infeasible."):
with loc_raises_infeasible:
pmedian.solve(pulp.PULP_CBC_CMD(msg=False))

def test_mixin_mean_time(self):
Expand Down
4 changes: 2 additions & 2 deletions spopt/tests/test_mclp.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,8 @@ def setup_method(self, loc_warns_geo_crs) -> None:
with loc_warns_geo_crs:
self.gdf_dem_buffered["geometry"] = self.gdf_dem.buffer(2)

def test_attribute_error_add_facility_constraint(self):
with pytest.raises(AttributeError, match="Before setting facility constraint"):
def test_attribute_error_add_facility_constraint(self, loc_raises_fac_constr):
with loc_raises_fac_constr:
dummy_class = MCLP("dummy", pulp.LpProblem("name"))
dummy_p_facility = 1
FacilityModelBuilder.add_facility_constraint(dummy_class, dummy_p_facility)
Expand Down
10 changes: 6 additions & 4 deletions spopt/tests/test_p_dispersion.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ def setup_method(self) -> None:

self.gdf_fac = geopandas.GeoDataFrame(polygon_dict, crs="EPSG:4326")

def test_attribute_error_add_facility_constraint(self):
with pytest.raises(AttributeError, match="Before setting facility constraint"):
def test_attribute_error_add_facility_constraint(self, loc_raises_fac_constr):
with loc_raises_fac_constr:
dummy_p_facility = 1
dummy_class = PDispersion("dummy", pulp.LpProblem("name"), dummy_p_facility)
FacilityModelBuilder.add_facility_constraint(dummy_class, dummy_p_facility)
Expand All @@ -167,8 +167,10 @@ def test_attribute_error_add_p_dispersion_interfacility_constraint(self):
dummy_range,
)

def test_attribute_error_add_predefined_facility_constraint(self):
with pytest.raises(AttributeError, match="Before setting facility constraint"):
def test_attribute_error_add_predefined_facility_constraint(
self, loc_raises_fac_constr
):
with loc_raises_fac_constr:
dummy_p_facility = 1
dummy_matrix = numpy.array([])
dummy_class = PDispersion("dummy", pulp.LpProblem("name"), dummy_p_facility)
Expand Down

0 comments on commit 15f4f2b

Please sign in to comment.