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

test_nst_observer   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 41
dl 0
loc 61
rs 10
c 0
b 0
f 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
A subject_container() 0 23 1
A styling_observer_data() 0 6 1
A test_styling_observer() 0 14 1
A nst_observer() 0 6 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