test_wordcloud_cli.py 3.87 KB
Newer Older
Raphael Boidol's avatar
Raphael Boidol committed
1
import argparse
2
import os
Raphael Boidol's avatar
Raphael Boidol committed
3
4
5
from collections import namedtuple
from tempfile import NamedTemporaryFile

6
7
8
9
10
11
import wordcloud as wc
from wordcloud import wordcloud_cli as cli
from mock import patch
from nose.tools import assert_equal, assert_greater, assert_true, assert_in, assert_not_in


Raphael Boidol's avatar
Raphael Boidol committed
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
temp = NamedTemporaryFile()
ArgOption = namedtuple('ArgOption', ['cli_name', 'init_name', 'pass_value', 'fail_value'])
ARGUMENT_SPEC_TYPED = [
    ArgOption(cli_name='width', init_name='width', pass_value=13, fail_value=1.),
    ArgOption(cli_name='height', init_name='height', pass_value=15, fail_value=1.),
    ArgOption(cli_name='margin', init_name='margin', pass_value=17, fail_value=1.),
    ArgOption(cli_name='relative_scaling', init_name='relative_scaling', pass_value=1, fail_value='c')
]
ARGUMENT_SPEC_REMAINING = [
    ArgOption(cli_name='stopwords', init_name='stopwords', pass_value=temp.name, fail_value=None),
    ArgOption(cli_name='mask', init_name='mask', pass_value=temp.name, fail_value=None),
    ArgOption(cli_name='fontfile', init_name='font_path', pass_value=temp.name, fail_value=None),
    ArgOption(cli_name='color', init_name='color_func', pass_value='red', fail_value=None),
    ArgOption(cli_name='background', init_name='background_color', pass_value='grey', fail_value=None)
]

def all_arguments():
    arguments = []
    arguments.extend(ARGUMENT_SPEC_TYPED)
    arguments.extend(ARGUMENT_SPEC_REMAINING)
    return arguments


def test_main_passes_arguments_through():
36
37
38
    temp_imagefile = NamedTemporaryFile()

    args = argparse.Namespace(text='some long text', imagefile=open(temp_imagefile.name, 'w'))
Raphael Boidol's avatar
Raphael Boidol committed
39
40
41
    for option in all_arguments():
        setattr(args, option.init_name, option.pass_value)

42
43
    with patch('wordcloud.wordcloud_cli.wc.WordCloud', autospec=True) as mock_word_cloud:
        cli.main(args)
Raphael Boidol's avatar
Raphael Boidol committed
44
45
46
47
48
49
50
51
52

    posargs, kwargs = mock_word_cloud.call_args
    for option in all_arguments():
        assert_in(option.init_name, kwargs)


def check_argument(name, result_name, value):
    text = NamedTemporaryFile()

53
    args = cli.parse_args(['--text', text.name, '--' + name, str(value)])
Raphael Boidol's avatar
Raphael Boidol committed
54
55
56
57
58
59
60
    assert_in(result_name, vars(args))


def check_argument_type(name, value):
    text = NamedTemporaryFile()

    try:
61
62
        with patch('sys.stderr') as mock_stderr:
            args = cli.parse_args(['--text', text.name, '--' + name, str(value)])
Raphael Boidol's avatar
Raphael Boidol committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
        raise AssertionError('argument "{}" was accepted even though the type did not match'.format(name))
    except SystemExit:
        pass
    except ValueError:
        pass


def test_parse_args_are_passed_along():
    for option in all_arguments():
        if option.cli_name != 'mask':
            yield check_argument, option.cli_name, option.init_name, option.pass_value


def test_parse_arg_types():
    for option in ARGUMENT_SPEC_TYPED:
        yield check_argument_type, option.cli_name, option.fail_value


def test_check_duplicate_color_error():
    color_mask_file = NamedTemporaryFile()
    text_file = NamedTemporaryFile()

    try:
86
        cli.parse_args(['--color', 'red', '--colormask', color_mask_file.name, '--text', text_file.name])
Raphael Boidol's avatar
Raphael Boidol committed
87
88
89
90
91
92
93
94
        raise AssertionError('parse_args(...) didn\'t raise')
    except ValueError as e:
        assert_true('specify either' in str(e), msg='expecting the correct error message, instead got: ' + str(e))


def test_parse_args_defaults_to_random_color():
    text = NamedTemporaryFile()

95
    args = cli.parse_args(['--text', text.name])
Raphael Boidol's avatar
Raphael Boidol committed
96
    assert_equal(args.color_func, wc.random_color_func)
97
98
99
100
101
102
103
104
105
106
107
108
109


def test_cli_writes_image():
    # ensure writting works with all python versions
    temp_imagefile = NamedTemporaryFile()
    temp_textfile = NamedTemporaryFile()
    temp_textfile.write(b'some text')
    temp_textfile.flush()

    args = cli.parse_args(['--text', temp_textfile.name, '--imagefile', temp_imagefile.name])
    cli.main(args)

    assert_greater(os.path.getsize(temp_imagefile.name), 0, msg='expecting image to be written')