|
1
|
|
|
import requests_mock |
|
2
|
|
|
|
|
3
|
|
|
from flask_jsondash.data_utils import wordcloud |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
def test_get_word_freq_distribution(): |
|
7
|
|
|
words = ['foo', 'foo', 'bar', 'baz'] |
|
8
|
|
|
res = wordcloud.get_word_freq_distribution(words) |
|
9
|
|
|
assert dict(res) == {'foo': 2, 'bar': 1, 'baz': 1} |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
def test_get_word_freq_distribution_no_stopwords(): |
|
13
|
|
|
words = ['foo', 'bar', 'baz', 'the'] |
|
14
|
|
|
res = wordcloud.get_word_freq_distribution(words) |
|
15
|
|
|
assert dict(res) == {'foo': 1, 'bar': 1, 'baz': 1} |
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
def test_get_word_freq_distribution_none(): |
|
19
|
|
|
words = [] |
|
20
|
|
|
res = wordcloud.get_word_freq_distribution(words) |
|
21
|
|
|
assert dict(res) == {} |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
def test_format_4_wordcloud(): |
|
25
|
|
|
words = [('foo', 1), ('bar', 1)] |
|
26
|
|
|
res = wordcloud.format_4_wordcloud(words) |
|
27
|
|
|
assert res == [ |
|
28
|
|
|
{'size': 2, 'text': 'foo'}, {'size': 2, 'text': 'bar'} |
|
29
|
|
|
] |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
def test_format_4_wordcloud_sizing(): |
|
33
|
|
|
words = [('foo', 2), ('bar', 2)] |
|
34
|
|
|
res = wordcloud.format_4_wordcloud(words, size_multiplier=3) |
|
35
|
|
|
assert res == [ |
|
36
|
|
|
{'size': 6, 'text': 'foo'}, {'size': 6, 'text': 'bar'} |
|
37
|
|
|
] |
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
def test_format_4_wordcloud_none(): |
|
41
|
|
|
words = [] |
|
42
|
|
|
res = wordcloud.format_4_wordcloud(words) |
|
43
|
|
|
assert res == [] |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
def test_url2wordcloud_exclude_punct(monkeypatch): |
|
47
|
|
|
url = 'http://www.test.com' |
|
48
|
|
|
content = '<html><body>; great words $ &!</body></html>' |
|
49
|
|
|
with requests_mock.Mocker() as m: |
|
50
|
|
|
m.get(url, content=content.encode('utf-8')) |
|
51
|
|
|
res = wordcloud.url2wordcloud(url, exclude_punct=True) |
|
52
|
|
|
assert isinstance(res, list) |
|
53
|
|
|
words = [w['text'] for w in res] |
|
54
|
|
|
for punct in [';', '$', '&!']: |
|
55
|
|
|
assert punct not in words |
|
56
|
|
|
for w in ['great', 'words']: |
|
57
|
|
|
assert w in words |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
def test_url2wordcloud_limit(monkeypatch): |
|
61
|
|
|
url = 'http://www.test.com' |
|
62
|
|
|
content = '<html><body>these are are great great great words</body></html>' |
|
63
|
|
|
with requests_mock.Mocker() as m: |
|
64
|
|
|
m.get(url, content=content.encode('utf-8')) |
|
65
|
|
|
res = wordcloud.url2wordcloud(url, limit=2) |
|
66
|
|
|
assert isinstance(res, list) |
|
67
|
|
|
words = [w['text'] for w in res] |
|
68
|
|
|
assert any([ |
|
69
|
|
|
words == ['words', 'great'], |
|
70
|
|
|
words == ['great', 'words'] |
|
71
|
|
|
]) |
|
72
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
def test_url2wordcloud_min_len_none(monkeypatch): |
|
75
|
|
|
url = 'http://www.test.com' |
|
76
|
|
|
content = '<html><body>; great words short word $</body></html>' |
|
77
|
|
|
with requests_mock.Mocker() as m: |
|
78
|
|
|
m.get(url, content=content.encode('utf-8')) |
|
79
|
|
|
res = wordcloud.url2wordcloud(url, min_len=10) |
|
80
|
|
|
assert isinstance(res, list) |
|
81
|
|
|
words = [w['text'] for w in res] |
|
82
|
|
|
assert words == [] |
|
83
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
def test_url2wordcloud_min_len_none_one(monkeypatch): |
|
86
|
|
|
url = 'http://www.test.com' |
|
87
|
|
|
content = '<html><body>a a prettylongwordfortest sort word</body></html>' |
|
88
|
|
|
with requests_mock.Mocker() as m: |
|
89
|
|
|
m.get(url, content=content.encode('utf-8')) |
|
90
|
|
|
res = wordcloud.url2wordcloud(url, min_len=10) |
|
91
|
|
|
assert isinstance(res, list) |
|
92
|
|
|
words = [w['text'] for w in res] |
|
93
|
|
|
assert words == ['prettylongwordfortest'] |
|
94
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
def test_url2wordcloud_bad_url(monkeypatch): |
|
97
|
|
|
url = 'http://www.test.com' |
|
98
|
|
|
content = '<html><body>500 SERVER ERROR.</body></html>' |
|
99
|
|
|
with requests_mock.Mocker() as m: |
|
100
|
|
|
m.get(url, content=content.encode('utf-8'), status_code=404) |
|
101
|
|
|
res = wordcloud.url2wordcloud(url, min_len=10) |
|
102
|
|
|
assert res == [] |
|
103
|
|
|
|