/
test_agc_law.py
173 lines (136 loc) · 5.42 KB
/
test_agc_law.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
__author__ = 'lowks'
import unittest
import json
import requests_mock
import agc_law
import sys
from agc_law import Law, LawPages
from mock import patch, Mock, call
from cStringIO import StringIO
class AGCTests(unittest.TestCase):
def setUp(self):
self.law = Law()
self.input_html = """<div class="article-content">
<table><a href="hoho">hoho</a></table>
<table><a href="hehe">hehe</a></table></div>"""
self.lp = LawPages(self.input_html)
@patch("agc_law.requests")
@patch("agc_law.LawPages")
def test_find_laws(self, mock_law_pages, mock_requests):
"""Test for find_laws using mock 200 request"""
mock_requests.get.return_value.status_code = 200
mock_law_pages.return_value.give_pages.return_value = 'hulahoop'
self.assertEqual(self.law.find_laws(), 'hulahoop')
@patch("agc_law.requests")
def test_find_laws_exception(self, mock_requests):
"""Test for find_laws exception using mock 400 status_code"""
mock_requests.get.return_value.status_code = 400
with self.assertRaises(SystemExit):
self.law.find_laws()
@patch("agc_law.Law.fetch")
def test_dump_to_json(self, mock_fetch):
"""Test for dump_to_json using mock_fetch value"""
mock_fetch.return_value = "mypage"
self.assertTrue(self.law.dump_to_json(),
json.dumps({'lom': list("mypage")}))
def test_lp_give_pages(self):
self.assertListEqual(self.lp.give_pages(),
[(u'hehe', 'http://www.agc.gov.myhehe')])
@patch("agc_law.LawPages.extract")
def test_extract_to_json(self, mock_extract):
"""Test for extract_to_json using mock extract value"""
mock_extract.return_value = "gula"
data = self.lp.extract_to_json()
self.assertEqual(data, '{\n "lom": "gula"\n}')
@patch("agc_law.requests")
def test_private_fetch_law_exception(self, mock_requests):
"""Testing _fetch_law with mock 404 request to raise exception"""
mock_requests.get.return_value.status_code = 404
setattr(self.law, 'silent', True)
with self.assertRaises(SystemExit):
self.law._fetch_law(('test', 'test'), Mock())
@patch("agc_law.LawPages.extract")
def test_private_fetch_law(self, mock_law_pages):
"""Testing _fetch_law with mock requests and capturing stdout"""
with requests_mock.mock() as mock_requests:
test_text = (self.input_html.rstrip('</div>') +
"""><table><a href="gigi">gigi</table>
<table><tbody><a href="gogo">gogo</table></tbody></div>""")
mock_requests.get(agc_law.FIRST_PAGE, text=test_text, status_code=200)
mock_storage = Mock()
mock_law_pages.return_value = "hulahoop"
sys.stdout = captured = StringIO()
self.law._fetch_law(('test', agc_law.FIRST_PAGE), mock_storage)
self.assertEqual(captured.getvalue(),
'Requesting page test\n')
self.assertIn(call('hulahoop'), mock_storage.extend.call_args_list)
sys.stdout = sys.__stdout__
@patch("agc_law.LawPages._get_rows")
@patch("agc_law.LawPages._extract_row")
def test_law_pages_extract(self, mock_extract_rows, mock_get_rows):
"""Test to verify extract method of LawPages"""
mock_get_rows.return_value = ["row1", "row2", "row3"]
mock_extract_rows.return_value = {'number': 'number1', 'docs': 'doc1'}
result = self.lp.extract()
self.assertEqual(result, [{'docs': 'doc1', 'number': 'number1'}])
def test_law_pages_private_extract_row(self):
"""Test asserting correct calls are made in _extract_row"""
input_html = """<div class="article-content">
<table>
<a href="hoho">
<p>hoho</p>
</a>
</table>
<table>
<a href="hehe">
<p>hehe</p>
</a>
</table>
<table>
<a href="hoho">
<tbody>
<td>
<p>hoho</p>
</td>
</tbody>
</a>
</table>
<table>
<a href="hoho">
<tbody>
<td>
<p>
<a href="www.harhar.com">harhar</a>
</p>
</td>
<a href="hoho">
<td>
<p>
<a href="www.herher.com">herher</a>
</p>
</td>
</tbody>
</table>
</div>"""
law_pages = LawPages(input_html)
rows = law_pages._get_rows()
real_value = agc_law.DOMAIN
agc_law.DOMAIN = "http://abc.com"
result = self.lp._extract_row(rows[0], rows[1])
self.assertEqual(result, {'docs': [{'link': 'http://abc.comwww.herher.com',
'name': u'herher'}],
'number': u'harhar'})
agc_law.DOMAIN = real_value
@patch("agc_law.multiprocessing.Manager")
@patch("agc_law.multiprocessing.Process.start")
@patch("agc_law.multiprocessing.Process.join")
@patch("agc_law.Law.find_laws")
def test_fetch(self, mock_find_law, mock_process_join,
mock_process_start, mock_process_manager):
"""Test to verify fetch method of Law"""
mock_find_law.return_value = [1, 2, 3]
mock_process_manager.list.return_value = [3, 4, 5]
self.law.fetch()
self.assertEqual(mock_process_join.call_count, 3)
self.assertEqual(mock_process_start.call_count, 3)
self.assertEqual(mock_process_manager.call_count, 1)