|
1
|
|
|
"""Test filter methods""" |
|
2
|
|
|
# pylint: disable=protected-access |
|
3
|
1 |
|
import pytest |
|
4
|
1 |
|
from unittest.mock import MagicMock |
|
5
|
|
|
|
|
6
|
1 |
|
from napps.kytos.pathfinder.graph.filters import (TypeCheckPreprocessor, |
|
7
|
|
|
TypeDifferentiatedProcessor) |
|
8
|
1 |
|
from napps.kytos.pathfinder.graph import KytosGraph |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
1 |
|
class TestLazyFilter: |
|
12
|
|
|
"""Tests for the Main class.""" |
|
13
|
|
|
|
|
14
|
1 |
|
def setup_method(self): |
|
15
|
|
|
"""Execute steps before each test.""" |
|
16
|
1 |
|
self.graph = KytosGraph() |
|
17
|
|
|
|
|
18
|
1 |
|
def test_type_error(self): |
|
19
|
|
|
"""Test filtering with invalid minimum type.""" |
|
20
|
1 |
|
wrong_type = "wrong_type" |
|
21
|
1 |
|
right_type = 3 |
|
22
|
1 |
|
preprocessor = TypeCheckPreprocessor(int) |
|
23
|
1 |
|
with pytest.raises(TypeError): |
|
24
|
1 |
|
preprocessor(wrong_type) |
|
25
|
1 |
|
preprocessor(right_type) |
|
26
|
|
|
|
|
27
|
1 |
|
def test_type_error2(self): |
|
28
|
|
|
"""Test filtering with invalid minimum type.""" |
|
29
|
1 |
|
wrong_type = "wrong_type" |
|
30
|
1 |
|
right_type = 3 |
|
31
|
1 |
|
fake_inner = MagicMock() |
|
32
|
1 |
|
preprocessor = TypeDifferentiatedProcessor({ |
|
33
|
|
|
int: fake_inner |
|
34
|
|
|
}) |
|
35
|
1 |
|
with pytest.raises(TypeError): |
|
36
|
1 |
|
preprocessor(wrong_type) |
|
37
|
1 |
|
fake_inner.assert_not_called() |
|
38
|
1 |
|
preprocessor(right_type) |
|
39
|
1 |
|
fake_inner.assert_called_once() |
|
40
|
|
|
|
|
41
|
1 |
|
def test_filter_functions_in(self): |
|
42
|
|
|
"""Test _filter_function that are expected to use the filter_in""" "" |
|
43
|
|
|
|
|
44
|
1 |
|
attr = "ownership" |
|
45
|
1 |
|
nx_edge_values = [ |
|
46
|
|
|
(None, None, {attr: {"blue": {}, "red": {}}}), |
|
47
|
|
|
(None, None, {attr: {"green": {}}}), |
|
48
|
|
|
] |
|
49
|
|
|
|
|
50
|
1 |
|
target = "blue" |
|
51
|
1 |
|
ownership_filter = self.graph._filter_functions[attr] |
|
52
|
1 |
|
filtered = list(ownership_filter(target, nx_edge_values)) |
|
53
|
1 |
|
assert filtered |
|
54
|
|
|
|
|
55
|
1 |
|
for item in filtered: |
|
56
|
1 |
|
assert target in item[2][attr] |
|
57
|
|
|
|
|
58
|
1 |
View Code Duplication |
def test_filter_functions_ge(self): |
|
|
|
|
|
|
59
|
|
|
"""Test _filter_function that are expected to use the filter_ge.""" |
|
60
|
|
|
|
|
61
|
1 |
|
for attr in ("bandwidth", "reliability"): |
|
62
|
1 |
|
nx_edge_values = [ |
|
63
|
|
|
(None, None, {attr: 20}), |
|
64
|
|
|
(None, None, {attr: 10}), |
|
65
|
|
|
] |
|
66
|
|
|
|
|
67
|
1 |
|
target = 15 |
|
68
|
1 |
|
func = self.graph._filter_functions[attr] |
|
69
|
1 |
|
filtered = list(func(target, nx_edge_values)) |
|
70
|
1 |
|
assert filtered |
|
71
|
|
|
|
|
72
|
1 |
|
for item in filtered: |
|
73
|
1 |
|
assert item[2][attr] >= target |
|
74
|
|
|
|
|
75
|
1 |
|
target = 21 |
|
76
|
1 |
|
filter_func = self.graph._filter_functions[attr] |
|
77
|
1 |
|
filtered = list(filter_func(target, nx_edge_values)) |
|
78
|
1 |
|
assert not filtered |
|
79
|
|
|
|
|
80
|
1 |
View Code Duplication |
def test_filter_functions_le(self): |
|
|
|
|
|
|
81
|
|
|
"""Test _filter_function that are expected to use the filter_le.""" |
|
82
|
|
|
|
|
83
|
1 |
|
for attr in ("priority", "delay", "utilization"): |
|
84
|
1 |
|
nx_edge_values = [ |
|
85
|
|
|
(None, None, {attr: 20}), |
|
86
|
|
|
(None, None, {attr: 10}), |
|
87
|
|
|
] |
|
88
|
|
|
|
|
89
|
1 |
|
target = 15 |
|
90
|
1 |
|
func = self.graph._filter_functions[attr] |
|
91
|
1 |
|
filtered = list(func(target, nx_edge_values)) |
|
92
|
1 |
|
assert filtered |
|
93
|
|
|
|
|
94
|
1 |
|
for item in filtered: |
|
95
|
1 |
|
assert item[2][attr] <= target |
|
96
|
|
|
|
|
97
|
1 |
|
target = 9 |
|
98
|
1 |
|
filter_func = self.graph._filter_functions[attr] |
|
99
|
1 |
|
filtered = list(filter_func(target, nx_edge_values)) |
|
100
|
|
|
assert not filtered |
|
101
|
|
|
|