Skip to content

Commit dc2620f

Browse files
committed
Fix tmepfile issue in tests
1 parent 2f53f0c commit dc2620f

1 file changed

Lines changed: 16 additions & 13 deletions

File tree

test/test_quadratic_constraint.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1691,12 +1691,13 @@ def test_lp_file_with_progress(self) -> None:
16911691
m.add_quadratic_constraints(x * x, "<=", 10, name="qc")
16921692
m.add_objective(x)
16931693

1694-
with tempfile.NamedTemporaryFile(suffix=".lp", delete=False) as f:
1694+
with tempfile.TemporaryDirectory() as tmpdir:
1695+
filepath = Path(tmpdir) / "model.lp"
16951696
# Write with progress (tests the progress branch)
1696-
m.to_file(f.name, progress=True)
1697-
content = Path(f.name).read_text()
1697+
m.to_file(filepath, progress=True)
1698+
content = filepath.read_text()
16981699

1699-
assert "qc" in content.lower() or "QCMATRIX" in content
1700+
assert "qc" in content.lower() or "QCMATRIX" in content
17001701

17011702
def test_lp_file_only_quadratic_constraints(self) -> None:
17021703
"""Test LP file with only quadratic constraints (no linear)."""
@@ -1706,12 +1707,13 @@ def test_lp_file_only_quadratic_constraints(self) -> None:
17061707
m.add_quadratic_constraints(x * x, "<=", 10, name="qc")
17071708
m.add_objective(x)
17081709

1709-
with tempfile.NamedTemporaryFile(suffix=".lp", delete=False) as f:
1710-
m.to_file(f.name)
1711-
content = Path(f.name).read_text()
1710+
with tempfile.TemporaryDirectory() as tmpdir:
1711+
filepath = Path(tmpdir) / "model.lp"
1712+
m.to_file(filepath)
1713+
content = filepath.read_text()
17121714

1713-
# Should have "s.t." section header
1714-
assert "s.t." in content or "Subject" in content
1715+
# Should have "s.t." section header
1716+
assert "s.t." in content or "Subject" in content
17151717

17161718
def test_lp_file_explicit_coordinate_names(self) -> None:
17171719
"""Test LP file with explicit coordinate names."""
@@ -1720,11 +1722,12 @@ def test_lp_file_explicit_coordinate_names(self) -> None:
17201722
m.add_quadratic_constraints(x * x, "<=", 10, name="qc")
17211723
m.add_objective(x.sum())
17221724

1723-
with tempfile.NamedTemporaryFile(suffix=".lp", delete=False) as f:
1724-
m.to_file(f.name, explicit_coordinate_names=True)
1725-
content = Path(f.name).read_text()
1725+
with tempfile.TemporaryDirectory() as tmpdir:
1726+
filepath = Path(tmpdir) / "model.lp"
1727+
m.to_file(filepath, explicit_coordinate_names=True)
1728+
content = filepath.read_text()
17261729

1727-
assert "qc" in content.lower()
1730+
assert "qc" in content.lower()
17281731

17291732
def test_netcdf_roundtrip_with_linear_terms(self) -> None:
17301733
"""Test netCDF roundtrip for QC with both quadratic and linear terms."""

0 commit comments

Comments
 (0)