Passed
Pull Request — dev (#1208)
by Patrik
04:04 queued 02:13
created

TestConverterClass.setup_class()   A

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nop 1
1
# -*- coding: utf-8 -
2
3
"""Test the created constraints against approved constraints.
4
5
This file is part of project oemof (github.com/oemof/oemof). It's copyrighted
6
by the contributors recorded in the version control history of the file,
7
available from its original location oemof/tests/test_solph_network_classes.py
8
9
SPDX-License-Identifier: MIT
10
"""
11
12
import warnings
13
14
import pytest
15
from oemof.tools.debugging import SuspiciousUsageWarning
16
17
from oemof import solph
18
19
20
class TestConverterClass:
21
    @classmethod
22
    def setup_class(cls):
23
        """Setup default values"""
24
        cls.bus = solph.buses.Bus()
25
26
    @pytest.mark.filterwarnings("ignore::UserWarning")
27
    def test_empty_converter(self):
28
        converter = solph.components.Converter()
29
        assert isinstance(converter.conversion_factors, dict)
30
        assert len(converter.conversion_factors.keys()) == 0
31
32
    def test_default_conversion_factor(self):
33
        converter = solph.components.Converter(
34
            inputs={self.bus: solph.flows.Flow()},
35
            outputs={self.bus: solph.flows.Flow()},
36
        )
37
        assert converter.conversion_factors[self.bus][2] == 1
38
39
    def test_sequence_conversion_factor_from_scalar(self):
40
        converter = solph.components.Converter(
41
            inputs={self.bus: solph.flows.Flow()},
42
            outputs={self.bus: solph.flows.Flow()},
43
            conversion_factors={self.bus: 2},
44
        )
45
        assert converter.conversion_factors[self.bus][6] == 2
46
47
    def test_sequence_conversion_factor_from_list_correct_length(self):
48
        converter = solph.components.Converter(
49
            inputs={self.bus: solph.flows.Flow()},
50
            outputs={self.bus: solph.flows.Flow()},
51
            conversion_factors={self.bus: [2]},
52
        )
53
        assert len(converter.conversion_factors[self.bus]) == 1
54
55
    def test_sequence_conversion_factor_from_list_wrong_length(self):
56
        converter = solph.components.Converter(
57
            inputs={self.bus: solph.flows.Flow()},
58
            outputs={self.bus: solph.flows.Flow()},
59
            conversion_factors={self.bus: [2]},
60
        )
61
        with pytest.raises(IndexError):
62
            self.a = converter.conversion_factors[self.bus][6]
63
64
    def test_converter_missing_output_create_empty_dict(self):
65
        with pytest.warns(SuspiciousUsageWarning):
66
            converter = solph.components.Converter(inputs={})
67
            assert converter.outputs == {}
68
69
    def test_converter_missing_input_create_empty_dict(self):
70
        with pytest.warns(SuspiciousUsageWarning):
71
            converter = solph.components.Converter(outputs={})
72
            assert converter.inputs == {}
73
74
75
def test_fixed_costs_warning():
76
    msg = (
77
        "Be aware that the fixed costs attribute is only\n"
78
        "meant to be used for multi-period models to depict "
79
        "fixed costs that occur on a yearly basis.\n"
80
        "If you wish to set up a multi-period model, explicitly "
81
        "set the `periods` attribute of your energy system.\n"
82
        "It has been decided to remove the `fixed_costs` "
83
        "attribute with v0.2 for regular uses.\n"
84
        "If you specify `fixed_costs` for a regular model, "
85
        "this will simply be silently ignored."
86
    )
87
    with warnings.catch_warnings(record=True) as w:
88
        solph.flows.Flow(fixed_costs=34)
89
        assert len(w) != 0
90
        assert msg == str(w[-1].message)
91
92
93
def test_flow_with_fix_and_min_max():
94
    msg = "It is not allowed to define `min`/`max` if `fix` is defined."
95
    with pytest.raises(AttributeError, match=msg):
96
        solph.flows.Flow(fix=[1, 3], min=[0, 5])
97
    with pytest.raises(AttributeError, match=msg):
98
        solph.flows.Flow(fix=[1, 3], max=[0, 5])
99
    with pytest.raises(AttributeError, match=msg):
100
        solph.flows.Flow(fix=[1, 3], max=[0, 5], min=[4, 9])
101
102
103
def test_infinite_values():
104
    msg1 = "nominal_capacity must be a finite value"
105
    msg2 = "max must be a finite value"
106
    with pytest.raises(ValueError, match=msg1):
107
        solph.flows.Flow(nominal_capacity=float("+inf"))
108
    with pytest.raises(ValueError, match=msg2):
109
        solph.flows.Flow(nominal_capacity=1, max=float("+inf"))
110
111
112
def test_attributes_needing_nominal_value_get_it():
113
    with pytest.raises(AttributeError, match="If fix is set in a flow "):
114
        solph.flows.Flow(fix=0.3)
115
116
    with pytest.raises(AttributeError, match="If max is set in a flow "):
117
        solph.flows.Flow(max=0.3)
118
119
    with pytest.raises(AttributeError, match="If min is set in a flow "):
120
        solph.flows.Flow(min=0.3)
121
122
    with pytest.raises(
123
        AttributeError, match="If full_load_time_max is set in a flow "
124
    ):
125
        solph.flows.Flow(full_load_time_max=0.3)
126
127
    with pytest.raises(
128
        AttributeError, match="If full_load_time_min is set in a flow "
129
    ):
130
        solph.flows.Flow(full_load_time_min=0.3)
131
132
133
def test_min_max_values_for_bidirectional_flow():
134
    a = solph.flows.Flow(bidirectional=True)  # use default values
135
    b = solph.flows.Flow(
136
        bidirectional=True, nominal_capacity=1, min=-0.8, max=0.9
137
    )
138
    assert a.bidirectional
139
    assert a.max[0] == 1
140
    assert a.min[0] == -1
141
    assert b.bidirectional
142
    assert b.max[0] == 0.9
143
    assert b.min[0] == -0.8
144