Passed
Pull Request — dev (#1231)
by Patrik
01:46
created

tests.test_options.test_check_age_and_lifetime()   A

Complexity

Conditions 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 2
nop 0
1
"""Tests of the _options module.
2
3
This file is part of project oemof (github.com/oemof/oemof). It's copyrighted
4
by the contributors recorded in the version control history of the file,
5
available from its original location oemof/tests/test_components.py
6
7
SPDX-License-Identifier: MIT
8
"""
9
10
import pytest
11
from oemof.tools import debugging
12
13
from oemof import solph
14
15
16
def test_check_age_and_lifetime():
17
    """Check error being thrown if age > lifetime"""
18
    msg = "A unit's age must be smaller than its expected lifetime."
19
    with pytest.raises(AttributeError, match=msg):
20
        solph.Flow(nominal_capacity=solph.Investment(age=41, lifetime=40))
21
22
23
def test_check_invest_attributes_nonconvex():
24
    """Check error being thrown if nonconvex parameter is not of type bool"""
25
    msg = (
26
        "The `nonconvex` parameter of the `Investment` class has to be of type"
27
        + " boolean, not <class 'oemof.solph._options.NonConvex'>."
28
    )
29
    with pytest.raises(AttributeError, match=msg):
30
        solph.Flow(
31
            nominal_capacity=solph.Investment(
32
                maximum=1, nonconvex=solph.NonConvex()
33
            )
34
        )
35
36
37
def test_check_nonconvex():
38
    """
39
    Check warning being thrown if minimum and offset are zero and nonconvex
40
    is set
41
    """
42
    with pytest.warns(debugging.SuspiciousUsageWarning):
43
        solph.Flow(
44
            nominal_capacity=solph.Investment(
45
                maximum=1, minimum=0, offset=0, nonconvex=True
46
            )
47
        )
48
49
50
def test_investment_custom_properties():
51
    investment1 = solph.Investment(custom_properties={"prop": 1})
52
    assert investment1.custom_properties["prop"] == 1
53
54
    # --- BEGIN: The following code can be removed for versions >= v0.7 ---
55
    with pytest.warns(
56
        FutureWarning,
57
        match="For backward compatibility,",
58
    ):
59
        investment2 = solph.Investment(custom_attributes={"attribute": 1})
60
        assert investment2.attribute == 1
61
        assert investment2.custom_properties["attribute"] == 1
62
63
    with pytest.raises(
64
        AttributeError,
65
        match="Both options cannot be set at the same time.",
66
    ):
67
        solph.Investment(
68
            custom_attributes={"attribute": 1},
69
            custom_properties={"prop": 1},
70
        )
71
    # --- END ---
72