|
1
|
|
|
""" Specific tests for the `oemof.groupings` module. |
|
2
|
|
|
|
|
3
|
|
|
Most parts of the `groupings` module are tested via other tests, but certain |
|
4
|
|
|
code paths don't get covered by those, which is what this module is for. |
|
5
|
|
|
|
|
6
|
|
|
This file is part of project oemof (github.com/oemof/oemof). It's copyrighted |
|
7
|
|
|
by the contributors recorded in the version control history of the file, |
|
8
|
|
|
available from its original location oemof/tests/tests_groupings.py |
|
9
|
|
|
|
|
10
|
|
|
SPDX-License-Identifier: MIT |
|
11
|
|
|
""" |
|
12
|
|
|
|
|
13
|
|
|
from types import MappingProxyType as MaProTy |
|
14
|
|
|
|
|
15
|
|
|
from nose.tools import assert_raises |
|
16
|
|
|
from nose.tools import eq_ |
|
17
|
|
|
|
|
18
|
|
|
from oemof.network.groupings import Grouping |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
def test_initialization_argument_checks(): |
|
22
|
|
|
"""`Grouping` constructor should raise `TypeError` on bad arguments.""" |
|
23
|
|
|
|
|
24
|
|
|
message = "\n`Grouping` constructor did not check mandatory arguments." |
|
25
|
|
|
with assert_raises(TypeError, msg=message): |
|
26
|
|
|
Grouping() |
|
27
|
|
|
|
|
28
|
|
|
message = "\n`Grouping` constructor did not check conflicting arguments." |
|
29
|
|
|
with assert_raises(TypeError, msg=message): |
|
30
|
|
|
Grouping(key=lambda x: x, constant_key="key") |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
def test_notimplementederrors(): |
|
34
|
|
|
"""`Grouping` should raise an error when reaching unreachable code.""" |
|
35
|
|
|
|
|
36
|
|
|
message = "\n`Grouping.key` not overriden, but no error raised." |
|
37
|
|
|
with assert_raises(NotImplementedError, msg=message): |
|
38
|
|
|
g = Grouping(key="key") |
|
39
|
|
|
del g.key |
|
40
|
|
|
g.key("dummy argument") |
|
41
|
|
|
|
|
42
|
|
|
message = "\n`Grouping.filter` not overriden, but no error raised." |
|
43
|
|
|
with assert_raises(NotImplementedError, msg=message): |
|
44
|
|
|
g = Grouping(key="key") |
|
45
|
|
|
del g.filter |
|
46
|
|
|
g.filter("dummy argument") |
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
def test_mutable_mapping_groups(): |
|
50
|
|
|
g = Grouping( |
|
51
|
|
|
key=lambda x: len(x), |
|
52
|
|
|
value=lambda x: {y: len([z for z in x if z == y]) for y in x}, |
|
53
|
|
|
) |
|
54
|
|
|
groups = {} |
|
55
|
|
|
expected = {3: {"o": 2, "f": 1}} |
|
56
|
|
|
g("foo", groups) |
|
57
|
|
|
eq_( |
|
58
|
|
|
groups, |
|
59
|
|
|
expected, |
|
60
|
|
|
"\n Expected: {} \n Got : {}".format(expected, groups), |
|
61
|
|
|
) |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
def test_immutable_mapping_groups(): |
|
65
|
|
|
g = Grouping( |
|
66
|
|
|
key=lambda x: len(x), |
|
67
|
|
|
value=lambda x: MaProTy({y: len([z for z in x if z == y]) for y in x}), |
|
68
|
|
|
) |
|
69
|
|
|
groups = {} |
|
70
|
|
|
expected = {3: MaProTy({"o": 2, "f": 1})} |
|
71
|
|
|
g("foo", groups) |
|
72
|
|
|
eq_( |
|
73
|
|
|
groups, |
|
74
|
|
|
expected, |
|
75
|
|
|
"\n Expected: {} \n Got : {}".format(expected, groups), |
|
76
|
|
|
) |
|
77
|
|
|
|