Skip to content

Commit

Permalink
coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
bqpd committed Jul 15, 2020
1 parent f2a528c commit 7e48be7
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 20 deletions.
2 changes: 1 addition & 1 deletion docs/source/examples/beam.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def setup(self, N=4):
assert max(abs(w_gp - w_exact)) <= 1.1*ureg.cm

PLOT = False
if PLOT:
if PLOT: # pragma: no cover
import matplotlib.pyplot as plt
x_exact = np.linspace(0, L, 1000)
w_exact = q/(24*EI) * x_exact**2 * (x_exact**2 - 4*L*x_exact + 6*L**2)
Expand Down
4 changes: 4 additions & 0 deletions gpkit/keydict.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ def __setitem__(self, key, value):
dict.__setitem__(self, key, np.full(key.shape, np.nan, **dty))
self.owned.add(key)
if idx:
if is_sweepvar(value):
old = super().__getitem__(key)
super().__setitem__(key, np.array(old, "object"))
self.owned.add(key)
self._copyonwrite(key)
super().__getitem__(key)[idx] = value
return # succefully set a single index!
Expand Down
17 changes: 4 additions & 13 deletions gpkit/tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ class that inherits from `unittest.TestCase`
import_dict = {}
if newtest_fn is None:
newtest_fn = new_test
if solvers is None:
import gpkit
solvers = [gpkit.settings["default_solver"]]
tests = []
for testclass in testclasses:
if os.path.isdir(path):
Expand Down Expand Up @@ -55,10 +52,7 @@ def test(self):

import gpkit
with NewDefaultSolver(solver):
try:
testfn(name, import_dict, path)(self)
except FutureWarning as fw:
print(fw)
testfn(name, import_dict, path)(self)

# clear modelnums to ensure deterministic script-like output!
gpkit.globals.NamedVariables.reset_modelnumbers()
Expand All @@ -70,7 +64,7 @@ def test(self):
("signomials enabled", gpkit.SignomialsEnabled),
("vectorization", gpkit.Vectorize.vectorization),
("namedvars", gpkit.NamedVariables.namedvars)]:
if global_thing:
if global_thing: # pragma: no cover
raise ValueError("global attribute %s should have been"
" falsy after the test, but was instead %s"
% (globname, global_thing))
Expand All @@ -90,10 +84,7 @@ def test(self):
filepath = ("".join([path, os.sep, "%s_output.txt" % name])
if name not in imported else None)
with StdoutCaptured(logfilepath=filepath):
if name not in imported:
imported[name] = importlib.import_module(name)
else:
importlib.reload(imported[name])
imported[name] = importlib.import_module(name)
getattr(self, name)(imported[name])
return test

Expand All @@ -117,7 +108,7 @@ def run_tests(tests, xmloutput=None, verbosity=2):
if xmloutput:
import xmlrunner # pylint: disable=import-error
xmlrunner.XMLTestRunner(output=xmloutput).run(suite)
else:
else: # pragma: no cover
unittest.TextTestRunner(verbosity=verbosity).run(suite)


Expand Down
14 changes: 8 additions & 6 deletions gpkit/tests/t_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,9 @@ def test_zeroing(self):
self.assertAlmostEqual(sol["cost"], 0.1, self.ndig)
self.assertTrue(sol.almost_equal(sol))

def test_singular(self):
def test_singular(self): # pragma: no cover
"Create and solve GP with a singular A matrix"
if self.solver == "cvxopt": # pragma: no cover
if self.solver == "cvxopt":
# cvxopt can"t solve this problem
# (see https://github.com/cvxopt/cvxopt/issues/36)
return
Expand Down Expand Up @@ -328,9 +328,10 @@ def test_sp_bounded(self):
m = Model(x, Bounded([x + y >= 1], verbosity=0))
sol = m.localsolve(verbosity=0, solver=self.solver)
boundedness = sol["boundedness"]
if "value near lower bound" in boundedness:
# depends on solver, platform, whims of the numerical deities
if "value near lower bound" in boundedness: # pragma: no cover
self.assertIn(x.key, boundedness["value near lower bound"])
if "value near upper bound" in boundedness:
else: # pragma: no cover
self.assertIn(y.key, boundedness["value near upper bound"])

def test_values_vs_subs(self):
Expand Down Expand Up @@ -629,9 +630,10 @@ def test_unbounded_debugging(self):
m = Model(x*y, Bounded(m, verbosity=0))
sol = m.solve(self.solver, verbosity=0)
bounds = sol["boundedness"]
if "sensitive to upper bound" in bounds:
# depends on solver, platform, whims of the numerical deities
if "sensitive to upper bound" in bounds: # pragma: no cover
self.assertIn(y.key, bounds["sensitive to upper bound"])
if "sensitive to lower bound" in bounds:
else: # pragma: no cover
self.assertIn(x.key, bounds["sensitive to lower bound"])


Expand Down
5 changes: 5 additions & 0 deletions gpkit/tests/t_sub.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ def test_vector_sweep(self):
a = m.solve(verbosity=0)["cost"]
b = [6, 10]
self.assertTrue(all(abs(a-b)/(a+b) < 1e-7))
# create a numpy float array, then insert a sweep element
m.substitutions.update({y: [2, 3]})
m.substitutions.update({y[1]: ("sweep", [3, 5])})
a = m.solve(verbosity=0)["cost"]
self.assertTrue(all(abs(a-b)/(a+b) < 1e-7))

def test_calcconst(self):
x = Variable("x", "hours")
Expand Down

0 comments on commit 7e48be7

Please sign in to comment.