forked from limebrains/scrapper-helpers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
124 lines (96 loc) · 3.52 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import pytest
from bs4 import BeautifulSoup
import scrapper_helpers.utils as utils
if sys.version_info < (3, 3):
from mock import mock
else:
from unittest import mock
@pytest.mark.parametrize(
'text,dic,expected_value', [
('ala', {'a': 'b'}, 'blb'),
(u'Gdańsk', {u'ń': u'n'}, u'Gdansk')
])
def test_replace_all(text, dic, expected_value):
assert utils.replace_all(text, dic) == expected_value
@pytest.mark.parametrize("list1", [[[2], [[3], [1]], [4, [0]]]])
def test_flatten(list1):
result = utils.flatten(list1)
for element in result:
assert not isinstance(element, list)
@pytest.mark.parametrize(
'text,expected_value', [
('ala MA KoTa', 'ala_ma_kota'),
('Gdańsk', 'gdansk')
])
def test_normalize_text(text, expected_value):
assert utils.normalize_text(text) == expected_value
def test_key_md5():
with mock.patch("scrapper_helpers.utils.hashlib.md5") as md5:
utils.key_md5("test")
assert md5.called
def test_key_sha1():
with mock.patch("scrapper_helpers.utils.hashlib.sha1") as sha1:
utils.key_sha1("test")
assert sha1.called
@pytest.mark.parametrize(
'text,expected_value', [
('ala MA KoTa', 'ala MA KoTa'),
('ala:MA/KoTa', 'alaMAKoTa')
])
def test_default_key_func(text, expected_value):
assert utils.default_key_func(text) == expected_value
@pytest.mark.parametrize(
'text,expected_value', [
('Mac>Windows', 'Mac>Windows'),
('ala ma kota', 'ala ma kota')
])
def test_html_decode(text, expected_value):
assert utils.html_decode(text) == expected_value
@pytest.mark.parametrize(
'debug_value', [1, 0])
def test_caching(debug_value):
with mock.patch("scrapper_helpers.utils.Cache.set") as set, \
mock.patch("scrapper_helpers.utils.Cache.get") as get:
utils.DEBUG = debug_value
utils.caching()(list)("test")
assert (set.called or get.called) or (not set.called and not get.called)
@pytest.mark.skipif(sys.version_info < (3, 5), reason="requires Python35")
def test_set():
with mock.patch("scrapper_helpers.utils.pickle.dump") as dump, \
mock.patch("scrapper_helpers.utils.open") as opn:
utils.Cache.set("test", "test")
assert dump.called and opn.called
@pytest.mark.skipif(sys.version_info < (3, 5), reason="requires Python35")
def test_get():
with mock.patch("scrapper_helpers.utils.pickle.load") as load, \
mock.patch("scrapper_helpers.utils.open") as opn:
utils.Cache.get("test")
assert load.called and opn.called
@pytest.mark.parametrize('markup, finder_args, finder_kwargs, expected_text', [
(
BeautifulSoup('<div><h1>text to find</h1></div>', 'html.parser'),
('h1',), {'many': False},
'text to find',
),
(
BeautifulSoup('<div><span class="find_me">text to find</span></div>', 'html.parser'),
(), {'class_': "find_me", 'many': False},
'text to find',
),
(
BeautifulSoup('<div><span class="find_me">text to find</span></div>', 'html.parser'),
(), {'class_': "find_me", 'many': True},
'text to find',
),
])
def test_finder(markup, finder_args, finder_kwargs, expected_text):
many = finder_kwargs.pop('many', False)
@utils.finder(many, *finder_args, **finder_kwargs)
def get_tag(item, *args, **kwargs):
if many:
return item[0].text
return item.text
assert get_tag(markup) == expected_text