|
1
|
|
|
import typing as t |
|
2
|
|
|
|
|
3
|
|
|
import pytest |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
@pytest.mark.runner_setup(mix_stderr=False) |
|
7
|
|
|
def test_cli_demo(test_suite, toy_nst_algorithm, isolated_cli_runner, monkeypatch): |
|
8
|
|
|
"""Verify process exits with 0 after calling the CLI as `nst demo -it 4`. |
|
9
|
|
|
|
|
10
|
|
|
Test that verifies the process exits with 0 when the CLI is invoked as |
|
11
|
|
|
`nst demo -it 4`. |
|
12
|
|
|
|
|
13
|
|
|
This means the NST receives as input Content and Style Images the 2 images |
|
14
|
|
|
shipped with the Source Distribution for demoing purposes. |
|
15
|
|
|
|
|
16
|
|
|
The NST is expected to iterate/learn (number of epochs to run) for 4 times. |
|
17
|
|
|
|
|
18
|
|
|
The process is run in isolation, meaning that the process' stdout and |
|
19
|
|
|
stderr are not mixed with the pytest's stdout and stderr. |
|
20
|
|
|
""" |
|
21
|
|
|
from pathlib import Path |
|
22
|
|
|
from artificial_artwork.cli import entry_point as main |
|
23
|
|
|
from artificial_artwork import _demo |
|
24
|
|
|
|
|
25
|
|
|
# monkey patch _demo module to trick the _demo module in believing it is |
|
26
|
|
|
# inside the Test Suite dir ('tests/'), so that it properly locates the demo |
|
27
|
|
|
# Content and Style Images |
|
28
|
|
|
monkeypatch.setattr(_demo, 'source_root_dir', Path(test_suite) / '..') |
|
29
|
|
|
|
|
30
|
|
|
# Defer from using Production Pretrained Weights, and instead use the Toy Network |
|
31
|
|
|
# That way this Test Case runs as a Unit Test, and does not need to integrate |
|
32
|
|
|
# with the Production VGG Image Model. |
|
33
|
|
|
# We achieve that by monkeypatching at runtime all the necessary objects, so that |
|
34
|
|
|
# the program uses the Toy Network, which has but 1 Conv Layer (with very small |
|
35
|
|
|
# dimensions too), with weights to use for the NST (as pretrained weights) |
|
36
|
|
|
toy_nst_algorithm() # use fixture callable, which leverages monkeypatch under the hood |
|
37
|
|
|
|
|
38
|
|
|
# Call CLI as `nst demo -it 4` in isolation |
|
39
|
|
|
result = isolated_cli_runner.invoke( |
|
40
|
|
|
main, |
|
41
|
|
|
args=['demo', '-it', '4'], |
|
42
|
|
|
input=None, |
|
43
|
|
|
env=None, |
|
44
|
|
|
catch_exceptions=False, |
|
45
|
|
|
color=False, |
|
46
|
|
|
# **kwargs, |
|
47
|
|
|
) |
|
48
|
|
|
assert result.exit_code == 0 |
|
49
|
|
|
# GIVEN we can capture the stdout of the CLI (ie as a User would see if |
|
50
|
|
|
# calling the CLI in an interactive shell) |
|
51
|
|
|
assert type(result.stdout) == str |
|
52
|
|
|
|
|
53
|
|
|
# WHEN we inspect the stdout of the CLI |
|
54
|
|
|
string_to_inspect = result.stdout |
|
55
|
|
|
|
|
56
|
|
|
# THEN we expect to see the following: VGG Mat Weights Mock Loader Called 1 time |
|
57
|
|
|
# (ie the CLI called the VGG Mat Weights Mock Loader 1 time) |
|
58
|
|
|
exp_str = 'VGG Mat Weights Mock Loader Called' |
|
59
|
|
|
|
|
60
|
|
|
stdout_lines: t.List[str] = string_to_inspect.split('\n') |
|
61
|
|
|
exp_str_appearances = stdout_lines.count(exp_str) |
|
62
|
|
|
assert exp_str_appearances == 1 |
|
63
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
@pytest.mark.runner_setup(mix_stderr=False) |
|
67
|
|
|
def test_cli_main(test_suite, |
|
68
|
|
|
toy_nst_algorithm, isolated_cli_runner): |
|
69
|
|
|
from pathlib import Path |
|
70
|
|
|
from artificial_artwork.cli import entry_point as main |
|
71
|
|
|
|
|
72
|
|
|
# Monkey Patch Prod NST (Prod Pretrained Weights) to use Toy Network (Toy Pretrained Weights) |
|
73
|
|
|
toy_nst_algorithm() # use fixture callable, which leverages monkeypatch under the hood |
|
74
|
|
|
|
|
75
|
|
|
result = isolated_cli_runner.invoke( |
|
76
|
|
|
main, |
|
77
|
|
|
args=[ |
|
78
|
|
|
'run', |
|
79
|
|
|
str(Path(test_suite) / 'data' / 'canoe_water_w300-h225.jpg'), |
|
80
|
|
|
str(Path(test_suite) / 'data' / 'blue-red_w300-h225.jpg'), |
|
81
|
|
|
'--iterations', |
|
82
|
|
|
'6', |
|
83
|
|
|
'--location', # output folder to store snapshots of Gen Image |
|
84
|
|
|
'/tmp', # TODO use os native pytest fixture for tempdir |
|
85
|
|
|
], |
|
86
|
|
|
input=None, |
|
87
|
|
|
env=None, |
|
88
|
|
|
catch_exceptions=False, |
|
89
|
|
|
color=False, |
|
90
|
|
|
# **kwargs, |
|
91
|
|
|
) |
|
92
|
|
|
assert result.exit_code == 0 |
|
93
|
|
|
|