Example #1
0
def test_shorthand_property_storage(solver):
    # test shorthand assignment of constant properties
    model = Model(solver=solver)
    node = Storage(model, 'node')
    for attr in ('min_volume', 'max_volume', 'cost', 'level'):
        # should except int, float or Paramter
        setattr(node, attr, 123)
        if attr == 'conversion_factor':
            with pytest.raises(ValueError):
                setattr(node, attr, Parameter(model))
        else:
            setattr(node, attr, Parameter(model))

        with pytest.raises(TypeError):
            setattr(node, attr, '123')
            setattr(node, attr, None)
Example #2
0
def test_shorthand_property_storage():
    # test shorthand assignment of constant properties
    model = Model()
    node = Storage(model, "node")
    for attr in ("min_volume", "max_volume", "cost", "level"):
        # should except int, float or Paramter
        setattr(node, attr, 123)
        if attr == "conversion_factor":
            with pytest.raises(ValueError):
                setattr(node, attr, Parameter(model))
        else:
            setattr(node, attr, Parameter(model))

        with pytest.raises(TypeError):
            setattr(node, attr, "123")
            setattr(node, attr, None)
Example #3
0
def test_parameter_child_variables(model):

    p1 = Parameter(model)
    # Default parameter
    assert len(p1.parents) == 0
    assert len(p1.children) == 0

    c1 = Parameter(model)
    c1.parents.add(p1)
    assert len(p1.children) == 1
    assert c1 in p1.children
    assert p1 in c1.parents

    # Test third level
    c2 = Parameter(model)
    c2.parents.add(c1)

    # Disable parent
    c1.parents.clear()

    assert len(p1.children) == 0
Example #4
0
def test_parameter_child_variables():

    p1 = Parameter()
    # Default parameter
    assert p1 not in p1.variables
    assert len(p1.variables) == 0
    assert len(p1.parents) == 0
    assert len(p1.children) == 0

    c1 = Parameter()
    c1.parents.add(p1)
    assert len(p1.children) == 1
    assert c1 in p1.children
    assert p1 in c1.parents

    assert p1 not in p1.variables
    assert len(p1.variables) == 0

    c1.is_variable = True
    # Now parent should see find child as a variable
    assert p1 not in p1.variables
    assert c1 in p1.variables
    assert len(p1.variables) == 1

    # Test third level
    c2 = Parameter()
    c2.parents.add(c1)
    c2.is_variable = True
    assert p1 not in p1.variables
    assert c1 in p1.variables
    assert c2 in p1.variables
    assert len(p1.variables) == 2

    # Disable middle parameter as variable
    c1.is_variable = False
    assert p1 not in p1.variables
    assert c1 not in p1.variables
    assert c2 in p1.variables
    assert len(p1.variables) == 1

    # Disable parent
    c1.parents.clear()

    assert len(p1.variables) == 0
    assert len(p1.children) == 0
    # Child variables still OK.
    assert c1 not in c1.variables
    assert c2 in c1.variables
    assert len(c1.variables) == 1