Passed
Push — master ( c9b83a...258af3 )
by George
02:04
created

test_version()   A

Complexity

Conditions 3

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
dl 0
loc 4
rs 10
1
# -*- coding: utf-8 -*-
2
# vi:si:et:sw=4:sts=4:ts=4
3
4
import json
5
from unittest import mock
6
7
from click.testing import CliRunner
0 ignored issues
show
Configuration introduced by
The import click.testing could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
8
9
import pytest
10
11
from loafer import conf
12
from loafer.cli import cli
13
14
15
@pytest.fixture
16
def runner():
17
    return CliRunner()
18
19
20
@pytest.fixture
21
def local_settings():
22
    return conf.Settings()
23
24
25
def test_verbose(runner, local_settings):
26
    with mock.patch('loafer.cli.settings', local_settings) as settings:
27
        runner.invoke(cli, ['-v'])
28
        assert settings.LOAFER_LOGLEVEL == 'INFO'
29
30
31
def test_very_verbose(runner, local_settings):
32
    with mock.patch('loafer.cli.settings', local_settings) as settings:
33
        runner.invoke(cli, ['-vv'])
34
        assert settings.LOAFER_LOGLEVEL == 'DEBUG'
35
36
37
def test_max_jobs(runner, local_settings):
38
    with mock.patch('loafer.cli.settings', local_settings) as settings:
39
        runner.invoke(cli, ['--max-jobs', '20'])
40
        assert settings.LOAFER_MAX_JOBS == 20
41
42
43
def test_max_jobs_error(runner, local_settings):
44
    result = runner.invoke(cli, ['--max-jobs', 'a'])
45
    assert isinstance(result.exception, SystemExit)
46
47
    with mock.patch('loafer.cli.settings', local_settings) as settings:
48
        result = runner.invoke(cli, ['--max-jobs', '0'])
49
        # preserves default value
50
        assert settings.LOAFER_MAX_JOBS == 10
51
52
53
def test_max_threads(runner, local_settings):
54
    with mock.patch('loafer.cli.settings', local_settings) as settings:
55
        runner.invoke(cli, ['--max-threads', '20'])
56
        assert settings.LOAFER_MAX_THREAD_POOL == 20
57
58
59
def test_max_threads_error(runner, local_settings):
60
    result = runner.invoke(cli, ['--max-threads', 'a'])
61
    assert isinstance(result.exception, SystemExit)
62
63
    with mock.patch('loafer.cli.settings', local_settings) as settings:
64
        result = runner.invoke(cli, ['--max-threads', '0'])
65
        # preserves default value
66
        assert settings.LOAFER_MAX_THREAD_POOL is None
67
68
69
def test_source(runner, local_settings):
70
    with mock.patch('loafer.cli.settings', local_settings) as settings:
71
        runner.invoke(cli, ['--source', 'foobar'])
72
        routes = settings.LOAFER_ROUTES
73
        assert routes[0]['source'] == 'foobar'
74
75
76
def test_handler(runner, local_settings):
77
    with mock.patch('loafer.cli.settings', local_settings) as settings:
78
        runner.invoke(cli, ['--handler', 'foobar'])
79
        routes = settings.LOAFER_ROUTES
80
        assert routes[0]['handler'] == 'foobar'
81
82
83
def test_translator(runner, local_settings):
84
    with mock.patch('loafer.cli.settings', local_settings) as settings:
85
        runner.invoke(cli, ['--translator', 'foobar'])
86
        routes = settings.LOAFER_ROUTES
87
        assert routes[0]['message_translator'] == 'foobar'
88
89
90
def test_consumer(runner, local_settings):
91
    with mock.patch('loafer.cli.settings', local_settings) as settings:
92
        runner.invoke(cli, ['--consumer', 'foobar'])
93
        assert settings.LOAFER_DEFAULT_CONSUMER_CLASS == 'foobar'
94
95
96
def test_consumer_opts(runner, local_settings):
97
    with mock.patch('loafer.cli.settings', local_settings) as settings:
98
        for opt in [{}, '1', [], '{"key": "value"}', '""']:
99
            runner.invoke(cli, ['--consumer-opts', str(opt)])
100
            assert settings.LOAFER_DEFAULT_CONSUMER_OPTIONS == json.loads(str(opt))
101
102
103
def test_consumer_opts_error(runner):
104
    for opt in ['test', '123:123']:
105
        result = runner.invoke(cli, ['--consumer-opts', opt])
106
        assert isinstance(result.exception, json.decoder.JSONDecodeError)
107