test_linear_scale()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 10
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nop 3
dl 0
loc 10
rs 9.95
c 0
b 0
f 0
1
import pytest
2
from so_magic.utils import MapOnLinearSpace
3
from so_magic.utils.linear_mapping import LinearScale
4
5
6
@pytest.mark.parametrize('lower_bound, upper_bound, wrong_index', [
7
    (20, 100, 2),
8
    pytest.param(20, 20, 2, marks=pytest.mark.xfail(raises=ValueError, reason="Client supplied illogical arguments. lower_bound < upper_bound should be true; The lower bound of a linear scale is restricted to be strictly smaller than the upper bound")),
9
    pytest.param(80, 20, 2, marks=pytest.mark.xfail(raises=ValueError, reason="Client supplied illogical arguments. lower_bound < upper_bound should be true; The lower bound of a linear scale is restricted to be strictly smaller than the upper bound")),
10
])
11
def test_linear_scale(lower_bound, upper_bound, wrong_index):
12
    scale = LinearScale(lower_bound, upper_bound)
13
    assert scale.lower_bound == lower_bound
14
    assert scale.upper_bound == upper_bound
15
    assert len(scale) == 2
16
17
18
@pytest.mark.parametrize('from_scale, target_scale, reverse, input_value, output_value', [
19
    ([0, 400], [0, 100], True, 50, 87.5),
20
    ([0, 400], [0, 100], True, 100, 75),
21
    ([0, 400], [0, 100], True, 25, 93.75),
22
    ([0, 400], [0, 100], True, 10, 97.5),
23
    ([0, 400], [0, 100], True, 400, 0),
24
    ([0, 400], [0, 100], True, 0, 100),
25
    ([0, 400], [0, 100], False, 50, 12.5),
26
    ([0, 400], [0, 100], False, 100, 25),
27
    ([0, 400], [0, 100], False, 25, 6.25),
28
    ([0, 400], [0, 100], False, 10, 2.5),
29
    ([0, 400], [0, 100], False, 400, 100),
30
    ([0, 400], [0, 100], False, 0, 0),
31
])
32
def test_linear_transformation(from_scale, target_scale, reverse, input_value, output_value):
33
    linear_transformer = MapOnLinearSpace.universal_constructor(from_scale, target_scale, reverse=reverse)
34
    assert all(hasattr(linear_transformer, x) for x in ('from_scale', 'target_scale', 'reverse'))
35
    assert hasattr(linear_transformer, 'from_scale')
36
    assert linear_transformer.transform(input_value) == output_value
37
38
    linear_transformer.reverse = not linear_transformer.reverse
39
    assert linear_transformer.transform(input_value) == target_scale[1] - output_value
40
41
    linear_transformer.reverse = False
42
    linear_transformer.from_scale = [0, 10]
43
    linear_transformer.target_scale = [0, 100]
44
    assert linear_transformer.transform(7) == 70
45