Passed
Push — master ( c3d045...3525ae )
by Konstantinos
01:55 queued 43s
created

test_nst_observer.test_styling_observer()   A

Complexity

Conditions 1

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nop 4
dl 0
loc 15
rs 9.8
c 0
b 0
f 0
1
import pytest
2
3
4
@pytest.fixture(scope='module')
5
def nb_iterations():
6
    return 100
7
8
9
@pytest.fixture
10
def nst_observer(disk, nb_iterations):
11
    """An instance object of StylingObserver class."""
12
    from artificial_artwork.styling_observer import StylingObserver
13
    from artificial_artwork.image.image_operations import convert_to_uint8
14
    return StylingObserver(disk.save_image, convert_to_uint8, nb_iterations)
15
16
17
18
@pytest.fixture
19
def subject_container(tmpdir):
20
    import numpy as np
21
    class TestSubject:
22
        def __init__(self, subject):
23
            self.subject = subject
24
        
25
        def build_state(self, runtime_data):
26
            return {
27
                'output_path': tmpdir,
28
                'content_image_path': 'c',
29
                'style_image_path': 's',
30
                'metrics': {
31
                    'iterations': runtime_data
32
                },
33
                'matrix': np.random.randint(0, high=255, size=(30, 40), dtype=np.uint8)
34
            }
35
36
        def notify(self, runtime_data):
37
            self.subject.state = type('Subject', (), self.build_state(runtime_data))
38
            self.subject.notify()
39
40
    return TestSubject
41
42
43
@pytest.fixture
44
def styling_observer_data(subject_container, nst_observer):
45
    from software_patterns import Subject
46
    return type('TestData', (), {
47
        'broadcaster': subject_container(Subject()),
48
        'observer': nst_observer,
49
    })
50
51
52
def test_styling_observer(styling_observer_data, subscribe, nb_iterations, tmpdir):
53
    d = styling_observer_data
54
    # Subscribe observer/listener to subject/broadcaster at runtime
55
    subscribe(
56
        d.broadcaster.subject,
57
        [d.observer]
58
    )
59
60
    import os
61
    assert nb_iterations == 100
62
    zeroes = '00'
63
    d.broadcaster.notify(1)
64
    assert os.path.isfile(os.path.join(tmpdir, f'c+s-{zeroes}1.png'))
65
    d.broadcaster.notify(2)
66
    assert os.path.isfile(os.path.join(tmpdir, f'c+s-{zeroes}2.png'))
67