Passed
Pull Request — master (#1)
by Konstantinos
59s
created

test_nst_observer.subject_container()   A

Complexity

Conditions 1

Size

Total Lines 23
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

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