test_tools.test_annuity_exceptions()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
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/tool_tests.py
8
9
SPDX-License-Identifier: MIT
10
"""
11
12
import logging
13
import os
14
import warnings
15
16
import pytest
17
18
from oemof.tools import debugging
19
from oemof.tools import economics
20
from oemof.tools.logger import define_logging
21
from oemof.tools.logger import extend_basic_path
22
from oemof.tools.logger import get_basic_path
23
24
25
def test_helpers():
26
    get_basic_path()
27
    assert os.path.isdir(os.path.join(os.path.expanduser("~"), ".oemof"))
28
    new_dir = extend_basic_path("test_xf67456_dir")
29
    assert os.path.isdir(new_dir)
30
    get_basic_path()  # get basic path should not overwrite path
31
    assert os.path.isdir(new_dir)
32
    os.rmdir(new_dir)
33
    assert not os.path.isdir(new_dir)
34
35
36
def test_define_logging_format():
37
    file_format = "%(module)s - %(message)s"
38
    screen_format = "%(levelname)s-%(message)s"
39
    my_logpath = extend_basic_path("test_xf4hz4345456_dir")
40
    my_logfile = define_logging(
41
        logpath=my_logpath,
42
        log_path=False,
43
        screen_format=screen_format,
44
        file_format=file_format,
45
        file_level=logging.INFO,
46
    )
47
    logging.info("basdfuio")
48
    f = open(my_logfile, "r")
49
    log_content = f.read()
50
    assert "test_tools - basdfuio" in log_content
51
    assert "DEBUG" not in log_content
52
    os.remove(my_logfile)
53
    os.rmdir(my_logpath)
54
55
56
def test_logg_file_in_new_path():
57
    my_logpath = extend_basic_path("test_xf4hz4u67456_dir")
58
    my_logfile = define_logging(logpath=my_logpath, file_level=logging.DEBUG)
59
    assert os.path.join(my_logpath, "oemof.log") == my_logfile
60
    assert os.path.isfile(my_logfile)
61
    logging.debug("Tester345")
62
    f = open(my_logfile, "r")
63
    log_content = f.read()
64
    assert "Tester345" in log_content
65
    assert "DEBUG" in log_content
66
    os.remove(my_logfile)
67
    os.rmdir(my_logpath)
68
69
70
def test_logger():
71
    filepath = define_logging()
72
    assert isinstance(filepath, str)
73
    assert filepath[-9:] == "oemof.log"
74
    assert os.path.isfile(filepath)
75
76
77
def test_annuity():
78
    """Test annuity function of economics tool."""
79
    assert round(economics.annuity(1000, 10, 0.1)) == 163
80
    assert round(economics.annuity(capex=1000, wacc=0.1, n=10, u=5)) == 264
81
    assert (
82
        round(economics.annuity(1000, 10, 0.1, u=5, cost_decrease=0.1)) == 222
83
    )
84
85
86
def test_annuity_exceptions():
87
    """Test out-of-bounds-error of the annuity tool."""
88
    pytest.raises(ValueError, economics.annuity, 1000, 10, 2)
89
    pytest.raises(ValueError, economics.annuity, 1000, 0.5, 1)
90
    pytest.raises(ValueError, economics.annuity, 1000, 10, 0.1, u=0.3)
91
    pytest.raises(
92
        ValueError, economics.annuity, 1000, 10, 0.1, cost_decrease=-1
93
    )
94
95
96
def test_suspicious_usage_warning():
97
    msg = "My message."
98
    with warnings.catch_warnings(record=True) as w:
99
        warnings.warn(msg, debugging.SuspiciousUsageWarning)
100
        assert len(w) == 1
101
        assert msg, str(w[-1].message)
102