def test_change_object_name(self): var = Variable('blub') self.model.add(var) self.model.update() var.name = 'blurg' self.assertEqual(self.container.keys(), ['blurg']) self.assertEqual(self.container['blurg'], var)
def test_setitem(self): var = Variable('blub') self.assertRaises(IndexError, self.container.__setitem__, 0, var) self.container.append(var) self.assertEqual(self.container[0], var) var2 = Variable('blib') self.container[0] = var2 self.assertEqual(self.container[0], var2) var3 = Variable("blab") self.assertRaises(ValueError, self.container.__setitem__, "blub", var3) self.container["blab"] = var3 self.assertIs(self.container["blab"], var3) self.assertIs(self.container[1], var3) var4 = Variable("blab") self.container["blab"] = var4 self.assertFalse(var3 in self.container) self.assertIs(self.container["blab"], var4) self.assertIs(self.container[1], var4) self.assertRaises(ValueError, self.container.__setitem__, 1, var2) self.container[1] = var3 self.assertIs(self.container["blab"], var3) self.assertIs(self.container[1], var3) self.container.update_key("blab")
def test_add_already_existing_item_raises(self): var = Variable('blub') self.container.append(var) self.assertRaises(Exception, self.container.append, var) variables = [Variable("v" + str(i), lb=10, ub=100) for i in range(100)] self.container.extend(variables) self.assertRaises(Exception, self.container.extend, variables)
def setUp(self): self.model = Model() x = Variable('x', lb=0, ub=10) y = Variable('y', lb=0, ub=10) constr = Constraint(1. * x + y, lb=3, name="constr1") obj = Objective(2 * x + y) self.model.add(x) self.model.add(y) self.model.add(constr) self.model.objective = obj
def test_variable_independence(self): model = Model() x = Variable('x', lb=0, ub=20) self.assertNotEqual(id(x), id(self.model.variables['x'])) y = Variable('y', lb=0, ub=10) constr = Constraint(1. * x + y, lb=3, name="constr1") model.add(constr) self.assertNotEqual(id(self.model.variables['x']), id(model.variables['x'])) self.assertNotEqual(id(self.model.variables['y']), id(model.variables['y'])) self.assertNotEqual(self.model.variables['y'].problem, model) self.assertNotEqual(self.model.variables['x'].problem, model) x.lb = -10 self.assertNotEqual(self.model.variables['x'].lb, model.variables['x'].lb)
def test_init(self): self.assertEqual(self.var.name, 'x') self.assertEqual(self.var.lb, None) self.assertEqual(self.var.ub, None) self.assertEqual(self.var.type, 'continuous') self.assertEqual(self.var.problem, None) y = Variable('y', lb=-10) self.assertEqual(y.lb, -10) self.assertEqual(y.ub, None) u = Variable('u', ub=-10) self.assertEqual(u.lb, None) self.assertEqual(u.ub, -10)
def test_has_key(self): self.assertFalse('blurb' in self.container) self.assertFalse(self.container.has_key('blurb')) # noqa: W601 var = Variable('blurb') self.container.append(var) self.assertTrue('blurb' in self.container) self.assertTrue(self.container.has_key('blurb')) # noqa: W601
def test_values(self): variables = [ Variable("v" + str(i), lb=10, ub=100) for i in range(1000) ] self.container.extend(variables) values = self.container.values() self.assertEqual(values, variables)
def test_itervalues(self): variables = [ Variable("v" + str(i), lb=10, ub=100) for i in range(1000) ] self.container.extend(variables) generator = self.container.itervalues() self.assertEqual(list(generator), variables)
def test_keys(self): variables = [ Variable("v" + str(i), lb=10, ub=100) for i in range(1000) ] self.container.extend(variables) keys = self.container.keys() self.assertEqual(keys, [item.name for item in self.container])
def test_container_from_iterable(self): variables_iterable = [ Variable("v" + str(i), lb=10, ub=100) for i in range(10000) ] container = Container(variables_iterable) self.assertEqual(len(container), 10000) for index, variable in enumerate(variables_iterable): self.assertEqual(container[index], variable) self.assertEqual(container[variable.name], variable)
def test_set_wrong_bounds_on_integer_raises(self): self.assertRaises(ValueError, Variable, 'x', lb=-33, ub=0.3, type='integer') x = Variable('x', type='integer') self.assertRaises(ValueError, setattr, x, 'lb', -3.3) self.assertRaises(ValueError, setattr, x, 'ub', 3.3)
def test_iter_container_len_change_raises(self): def _(container): for item in container: del container[item.name] variables_iterable = [ Variable("v" + str(i), lb=10, ub=100) for i in range(10) ] container = Container(variables_iterable) self.assertRaises(RuntimeError, _, container)
def test_clear(self): variables = [ Variable("v" + str(i), lb=10, ub=100) for i in range(1000) ] self.container.extend(variables) self.container.clear() self.assertEqual(len(self.container), 0) self.assertEqual(self.container._object_list, []) self.assertEqual(self.container._indices, {}) self.assertEqual(self.container._dict, {})
def test_del_by_key(self): variables_iterable = [ Variable("v" + str(i), lb=10, ub=100) for i in range(1000) ] container = Container(variables_iterable) del container["v333"] for i, variable in enumerate(container): if i < 333: self.assertEqual(int(variable.name.replace('v', '')), i) else: self.assertEqual(int(variable.name.replace('v', '')) - 1, i)
def test_fromkeys(self): variables = [ Variable("v" + str(i), lb=10, ub=100) for i in range(1000) ] self.container.extend(variables) sub_container = self.container.fromkeys(('v1', 'v66', 'v999')) print(sub_container._object_list) lookalike = Container([variables[i] for i in (1, 66, 999)]) print(lookalike._object_list) self.assertEqual(sub_container._object_list, lookalike._object_list) # self.assertEqual(sub_container._name_list, lookalike._name_list) self.assertEqual(sub_container._dict, lookalike._dict)
def test_add_variable(self): var = Variable('z') self.model.add(var) self.assertTrue(var in self.model.variables.values()) self.assertEqual(self.model.variables.values().count(var), 1) self.assertEqual(self.model.variables['z'].problem, var.problem) var = Variable( 'asdfasdflkjasdflkjasdlkjsadflkjasdflkjasdlfkjasdlfkjasdlkfjasdf', lb=-13) self.model.add(var) self.assertTrue(var in self.model.variables.values()) self.assertEqual(self.model.variables['z'].lb, None) self.assertEqual(self.model.variables['z'].ub, None) self.assertEqual( self.model.variables[ 'asdfasdflkjasdflkjasdlkjsadflkjasdflkjasdlfkjasdlfkjasdlkfjasdf'] .lb, -13) self.assertEqual( self.model.variables[ 'asdfasdflkjasdflkjasdlkjsadflkjasdflkjasdlfkjasdlfkjasdlkfjasdf'] .ub, None)
def test_dir(self): var = Variable('blub') self.container.append(var) print(dir(self.container)) self.assertEqual(dir(self.container), [ '__contains__', '__delitem__', '__dict__', '__dir__', '__doc__', '__getattr__', '__getitem__', '__getstate__', '__init__', '__iter__', '__len__', '__module__', '__setitem__', '__setstate__', '__weakref__', '_check_for_name_attribute', '_reindex', 'append', 'blub', 'clear', 'extend', 'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys', 'update_key', 'values' ])
def setUp(self): self.x = Variable("x")
def test_pickle_ability(self): var = Variable('name', type='binary') pickle_var = pickle.loads(pickle.dumps(var)) keys = var.__dict__.keys() self.assertEqual([getattr(var, k) for k in keys], [getattr(pickle_var, k) for k in keys])
def setUp(self): self.x = Variable('x', type='binary') self.y = Variable('y', lb=-181133.3, ub=12000., type='continuous') self.z = Variable('z', lb=0., ub=3., type='integer')
def setUp(self): self.vars = [Variable(name) for name in ["x", "y", "z", "v", "w"]]
def test_extend(self): variables = [ Variable("v" + str(i), lb=10, ub=100) for i in range(1000) ] self.container.extend(variables)
def test_get(self): var = Variable('blub') self.container.append(var) self.assertEqual(self.container.get('blub'), var) self.assertEqual(self.container.get('blurb', None), None)
def test_container_append(self): var = Variable('blub') self.container.append(var) self.assertEqual(len(self.container), 1) self.assertEqual(self.container['blub'], var) self.assertEqual(self.container[0], var)
def test_getattr(self): var = Variable('variable1') self.container.append(var) self.assertEqual(self.container.variable1, var) self.assertRaises(AttributeError, getattr, self.container, 'not_existing_variable')
def setUp(self): self.a = Variable("a") self.b = Variable("b") self.c = Variable("c") self.d = Variable("d")
def test_pickle(self): variables = [Variable("v" + str(i), lb=10, ub=100) for i in range(100)] self.container.extend(variables) unpickled = pickle.loads(pickle.dumps(self.container)) self.assertEquals(unpickled[0].name, variables[0].name)
def test_add_variable_twice_raises(self): var = Variable('x') self.model.add(var) self.assertRaises(ContainerAlreadyContains, self.model.update)
def test_white_space_name_raises(self): with self.assertRaises(ValueError): Variable('White Space')