| Conditions | 1 |
| Total Lines | 57 |
| Code Lines | 21 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | import typing as t |
||
| 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 | |||
| 93 |