From 4eed6c8d6dea19fe84de11c6d14678618c7f5374 Mon Sep 17 00:00:00 2001 From: Chyroc Date: Tue, 25 Jul 2017 22:38:33 +0800 Subject: [PATCH 1/2] add error html --- test/file/search-gaokao-gzh-error.html | 104 ++++++++++++++++++ ...uest.py => test_gen_search_article_url.py} | 60 +--------- test/test_gen_search_gzh_url.py | 28 +++++ test/test_search_request.py | 51 +++++++++ wechatsogou/refactor_request.py | 4 +- 5 files changed, 188 insertions(+), 59 deletions(-) create mode 100644 test/file/search-gaokao-gzh-error.html rename test/{test_api_request.py => test_gen_search_article_url.py} (57%) create mode 100644 test/test_gen_search_gzh_url.py create mode 100644 test/test_search_request.py diff --git a/test/file/search-gaokao-gzh-error.html b/test/file/search-gaokao-gzh-error.html new file mode 100644 index 0000000..fb574a9 --- /dev/null +++ b/test/file/search-gaokao-gzh-error.html @@ -0,0 +1,104 @@ + + + + + + + 搜狗搜索 + + + + + + +
+ +
您的访问出错了返回首页>>
+
+
+

IP:123.116.247.15
访问时间:2017.07.25 22:36:19

+

用户您好,您的访问过于频繁,为确认本次访问为正常用户行为,需要您协助验证。

+

+
+

+ + + + + + + 请输入图中的验证码 + + + 换一张 + +

+
+

+ 提交 + 提交后没解决问题?欢迎反馈 +

+
+
企业推广关于搜狗免责声明意见反馈
 © 2017 SOGOU - 京ICP证050897号 - 京公网安备110000000025号
+ + + diff --git a/test/test_api_request.py b/test/test_gen_search_article_url.py similarity index 57% rename from test/test_api_request.py rename to test/test_gen_search_article_url.py index 7e6d9a4..5ee966f 100644 --- a/test/test_api_request.py +++ b/test/test_gen_search_article_url.py @@ -2,12 +2,9 @@ from __future__ import absolute_import, unicode_literals, print_function -import io -import os import unittest -from nose.tools import assert_raises, assert_equal, assert_in, assert_not_equal, assert_not_in +from nose.tools import assert_raises, assert_equal, assert_in, assert_not_in -import httpretty from hypothesis import given, strategies as st from wechatsogou.refactor_request import WechatSogouRequest @@ -37,14 +34,14 @@ def test_gen_search_article_url_timesn(self, timesn, ft, et): assert_in('type=2&page=1&ie=utf8&query=', url) assert_not_in('ft=&et=', url) - url = WechatSogouRequest._gen_search_article_url(gaokao_keyword, timesn=timesn, ft=str(ft)) + url = WechatSogouRequest._gen_search_article_url(gaokao_keyword, timesn=timesn, ft=ft) assert_in('type=2&page=1&ie=utf8&query=', url) assert_not_in('ft=&et=', url) elif timesn in [1, 2, 3, 4]: url = WechatSogouRequest._gen_search_article_url(gaokao_keyword, timesn=timesn) assert_in('tsn={}&ft=&et='.format(timesn), url) - url = WechatSogouRequest._gen_search_article_url(gaokao_keyword, timesn=timesn, ft=str(ft)) + url = WechatSogouRequest._gen_search_article_url(gaokao_keyword, timesn=timesn, ft=ft) assert_in('tsn={}&ft=&et='.format(timesn), url) elif timesn == 5: if ft <= et: @@ -70,54 +67,3 @@ def test_gen_search_article_url_article_type(self): url = WechatSogouRequest._gen_search_article_url(gaokao_keyword, article_type=WechatSogouRequest.TYPE_RICH) assert_in('interation=458754%2C458756', url) - - -class TestBasicGenSearchGzhURL(unittest.TestCase): - def test_gen_search_article_url_keyword(self): - url = WechatSogouRequest._gen_search_gzh_url(gaokao_keyword) - assert_equal('http://weixin.sogou.com/weixin?type=1&page=1&ie=utf8&query=%E9%AB%98%E8%80%83', url) - - @given(st.integers(min_value=-20000, max_value=20000)) - def test_gen_search_gzh_url_page(self, page): - if page > 0: - url = WechatSogouRequest._gen_search_gzh_url(gaokao_keyword, page) - assert_in('page={}'.format(page), url) - else: - with assert_raises(AssertionError): - WechatSogouRequest._gen_search_gzh_url(gaokao_keyword, page) - - -class TestBasicSearchArticle(unittest.TestCase): - @httpretty.activate - def test_search_article_keyword(self): - url = WechatSogouRequest._gen_search_article_url(gaokao_keyword) - file_name = '{}/{}'.format(os.getcwd(), 'test/file/search-gaokao-article.html') - with io.open(file_name, encoding='utf-8') as f: - search_gaokao_article = f.read() - httpretty.register_uri(httpretty.GET, url, body=search_gaokao_article) - - r = WechatSogouRequest._search_article(gaokao_keyword) - assert_equal(search_gaokao_article, r.text) - assert_equal(url, r.url) - assert_not_equal(WechatSogouRequest._gen_search_article_url(gaokao_keyword, 2), r.url) - assert_not_equal(WechatSogouRequest._search_gzh(gaokao_keyword), r.url) - - -class TestBasicSearchGzh(unittest.TestCase): - @httpretty.activate - def test_search_gzh_keyword(self): - url = WechatSogouRequest._gen_search_gzh_url(gaokao_keyword) - file_name = '{}/{}'.format(os.getcwd(), 'test/file/search-gaokao-gzh.html') - with io.open(file_name, encoding='utf-8') as f: - search_gaokao_gzh = f.read() - httpretty.register_uri(httpretty.GET, url, body=search_gaokao_gzh) - - r = WechatSogouRequest._search_gzh(gaokao_keyword) - assert_equal(search_gaokao_gzh, r.text) - assert_equal(url, r.url) - assert_not_equal(WechatSogouRequest._search_gzh(gaokao_keyword, 2), r.url) - assert_not_equal(WechatSogouRequest._search_article(gaokao_keyword), r.url) - - -if __name__ == '__main__': - unittest.main() diff --git a/test/test_gen_search_gzh_url.py b/test/test_gen_search_gzh_url.py new file mode 100644 index 0000000..e313faa --- /dev/null +++ b/test/test_gen_search_gzh_url.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- + +from __future__ import absolute_import, unicode_literals, print_function + +import unittest +from nose.tools import assert_raises, assert_equal, assert_in + +from hypothesis import given, strategies as st + +from wechatsogou.refactor_request import WechatSogouRequest + +gaokao_keyword = '高考' +ws = WechatSogouRequest() + + +class TestBasicGenSearchGzhURL(unittest.TestCase): + def test_gen_search_article_url_keyword(self): + url = WechatSogouRequest._gen_search_gzh_url(gaokao_keyword) + assert_equal('http://weixin.sogou.com/weixin?type=1&page=1&ie=utf8&query=%E9%AB%98%E8%80%83', url) + + @given(st.integers(min_value=-20000, max_value=20000)) + def test_gen_search_gzh_url_page(self, page): + if page > 0: + url = WechatSogouRequest._gen_search_gzh_url(gaokao_keyword, page) + assert_in('page={}'.format(page), url) + else: + with assert_raises(AssertionError): + WechatSogouRequest._gen_search_gzh_url(gaokao_keyword, page) diff --git a/test/test_search_request.py b/test/test_search_request.py new file mode 100644 index 0000000..ba85d15 --- /dev/null +++ b/test/test_search_request.py @@ -0,0 +1,51 @@ +# -*- coding: utf-8 -*- + +from __future__ import absolute_import, unicode_literals, print_function + +import io +import os +import unittest +from nose.tools import assert_equal, assert_not_equal + +import httpretty + +from wechatsogou.refactor_request import WechatSogouRequest + +gaokao_keyword = '高考' +ws = WechatSogouRequest() + + +class TestBasicSearchArticle(unittest.TestCase): + @httpretty.activate + def test_search_article_keyword(self): + url = WechatSogouRequest._gen_search_article_url(gaokao_keyword) + file_name = '{}/{}'.format(os.getcwd(), 'test/file/search-gaokao-article.html') + with io.open(file_name, encoding='utf-8') as f: + search_gaokao_article = f.read() + httpretty.register_uri(httpretty.GET, url, body=search_gaokao_article) + + r = WechatSogouRequest._search_article(gaokao_keyword) + assert_equal(search_gaokao_article, r.text) + assert_equal(url, r.url) + assert_not_equal(WechatSogouRequest._gen_search_article_url(gaokao_keyword, 2), r.url) + assert_not_equal(WechatSogouRequest._search_gzh(gaokao_keyword), r.url) + + +class TestBasicSearchGzh(unittest.TestCase): + @httpretty.activate + def test_search_gzh_keyword(self): + url = WechatSogouRequest._gen_search_gzh_url(gaokao_keyword) + file_name = '{}/{}'.format(os.getcwd(), 'test/file/search-gaokao-gzh.html') + with io.open(file_name, encoding='utf-8') as f: + search_gaokao_gzh = f.read() + httpretty.register_uri(httpretty.GET, url, body=search_gaokao_gzh) + + r = WechatSogouRequest._search_gzh(gaokao_keyword) + assert_equal(search_gaokao_gzh, r.text) + assert_equal(url, r.url) + assert_not_equal(WechatSogouRequest._search_gzh(gaokao_keyword, 2), r.url) + assert_not_equal(WechatSogouRequest._search_article(gaokao_keyword), r.url) + + +if __name__ == '__main__': + unittest.main() diff --git a/wechatsogou/refactor_request.py b/wechatsogou/refactor_request.py index f75089b..e4aab16 100644 --- a/wechatsogou/refactor_request.py +++ b/wechatsogou/refactor_request.py @@ -138,7 +138,7 @@ def _search_article(keyword, page=1, timesn=0, article_type=None, wxid=None, usi Returns ------- - requests + requests.models.Response return of requests """ @@ -162,7 +162,7 @@ def _search_gzh(keyword, page=1): Returns ------- - requests + requests.models.Response return of requests """ url = WechatSogouRequest._gen_search_gzh_url(keyword, page) From 42af377fa1213d3bbd8cd4667506beb43b30828e Mon Sep 17 00:00:00 2001 From: Chyroc Date: Tue, 25 Jul 2017 22:52:13 +0800 Subject: [PATCH 2/2] add api interface --- test/test_api.py | 13 ++++++++++ test/test_api_structuring.py | 4 +--- ...st_api_html.py => test_check_html_file.py} | 0 test/test_gen_search_article_url.py | 4 +--- test/test_gen_search_gzh_url.py | 4 +--- test/test_main.py | 12 ++++++++++ test/test_search_request.py | 24 +++++++++++++++---- wechatsogou/refactor_api.py | 7 ++++++ 8 files changed, 54 insertions(+), 14 deletions(-) create mode 100644 test/test_api.py rename test/{test_api_html.py => test_check_html_file.py} (100%) create mode 100644 test/test_main.py create mode 100644 wechatsogou/refactor_api.py diff --git a/test/test_api.py b/test/test_api.py new file mode 100644 index 0000000..43346bc --- /dev/null +++ b/test/test_api.py @@ -0,0 +1,13 @@ +# -*- coding: utf-8 -*- + +import unittest + + +class TestAPI(unittest.TestCase): + @unittest.skip + def test_api(self): + pass + + +if __name__ == '__main__': + unittest.main() diff --git a/test/test_api_structuring.py b/test/test_api_structuring.py index 3854101..371cb07 100644 --- a/test/test_api_structuring.py +++ b/test/test_api_structuring.py @@ -10,12 +10,10 @@ from nose.tools import assert_equal, assert_in, assert_true, assert_greater_equal from wechatsogou.refactor_structuring import WechatSogouStructuring +from test.test_main import fake_data_path assert_equal.__self__.maxDiff = None -ws_structuring = WechatSogouStructuring() -fake_data_path = 'test/file' - class TestStructuringGzh(unittest.TestCase): def test_get_gzh_by_search(self): diff --git a/test/test_api_html.py b/test/test_check_html_file.py similarity index 100% rename from test/test_api_html.py rename to test/test_check_html_file.py diff --git a/test/test_gen_search_article_url.py b/test/test_gen_search_article_url.py index 5ee966f..f34985e 100644 --- a/test/test_gen_search_article_url.py +++ b/test/test_gen_search_article_url.py @@ -8,9 +8,7 @@ from hypothesis import given, strategies as st from wechatsogou.refactor_request import WechatSogouRequest - -gaokao_keyword = '高考' -ws = WechatSogouRequest() +from test.test_main import gaokao_keyword class TestBasicGenSearchArticleURL(unittest.TestCase): diff --git a/test/test_gen_search_gzh_url.py b/test/test_gen_search_gzh_url.py index e313faa..6c63f01 100644 --- a/test/test_gen_search_gzh_url.py +++ b/test/test_gen_search_gzh_url.py @@ -8,9 +8,7 @@ from hypothesis import given, strategies as st from wechatsogou.refactor_request import WechatSogouRequest - -gaokao_keyword = '高考' -ws = WechatSogouRequest() +from test.test_main import gaokao_keyword class TestBasicGenSearchGzhURL(unittest.TestCase): diff --git a/test/test_main.py b/test/test_main.py new file mode 100644 index 0000000..2447c8c --- /dev/null +++ b/test/test_main.py @@ -0,0 +1,12 @@ +# -*- coding: utf-8 -*- + +from __future__ import absolute_import, unicode_literals, print_function + +from wechatsogou.refactor_request import WechatSogouRequest +from wechatsogou.refactor_structuring import WechatSogouStructuring + +ws = WechatSogouRequest() +ws_structuring = WechatSogouStructuring() + +gaokao_keyword = '高考' +fake_data_path = 'test/file' diff --git a/test/test_search_request.py b/test/test_search_request.py index ba85d15..6b9648a 100644 --- a/test/test_search_request.py +++ b/test/test_search_request.py @@ -10,16 +10,14 @@ import httpretty from wechatsogou.refactor_request import WechatSogouRequest - -gaokao_keyword = '高考' -ws = WechatSogouRequest() +from test.test_main import gaokao_keyword, fake_data_path class TestBasicSearchArticle(unittest.TestCase): @httpretty.activate def test_search_article_keyword(self): url = WechatSogouRequest._gen_search_article_url(gaokao_keyword) - file_name = '{}/{}'.format(os.getcwd(), 'test/file/search-gaokao-article.html') + file_name = '{}/{}/{}'.format(os.getcwd(), fake_data_path, 'search-gaokao-article.html') with io.open(file_name, encoding='utf-8') as f: search_gaokao_article = f.read() httpretty.register_uri(httpretty.GET, url, body=search_gaokao_article) @@ -35,7 +33,7 @@ class TestBasicSearchGzh(unittest.TestCase): @httpretty.activate def test_search_gzh_keyword(self): url = WechatSogouRequest._gen_search_gzh_url(gaokao_keyword) - file_name = '{}/{}'.format(os.getcwd(), 'test/file/search-gaokao-gzh.html') + file_name = '{}/{}/{}'.format(os.getcwd(), fake_data_path, 'search-gaokao-gzh.html') with io.open(file_name, encoding='utf-8') as f: search_gaokao_gzh = f.read() httpretty.register_uri(httpretty.GET, url, body=search_gaokao_gzh) @@ -46,6 +44,22 @@ def test_search_gzh_keyword(self): assert_not_equal(WechatSogouRequest._search_gzh(gaokao_keyword, 2), r.url) assert_not_equal(WechatSogouRequest._search_article(gaokao_keyword), r.url) + @httpretty.activate + def test_search_gzh_keyword_error(self): + url = WechatSogouRequest._gen_search_gzh_url(gaokao_keyword) + file_name = '{}/{}/{}'.format(os.getcwd(), fake_data_path, 'search-gaokao-gzh-error.html') + with io.open(file_name, encoding='utf-8') as f: + search_gaokao_gzh_error = f.read() + httpretty.register_uri(httpretty.GET, url, body=search_gaokao_gzh_error) + + r = WechatSogouRequest._search_gzh(gaokao_keyword) + assert_equal(search_gaokao_gzh_error, r.text) + assert_equal(url, r.url) + assert_not_equal(WechatSogouRequest._search_gzh(gaokao_keyword, 2), r.url) + assert_not_equal(WechatSogouRequest._search_article(gaokao_keyword), r.url) + + + if __name__ == '__main__': unittest.main() diff --git a/wechatsogou/refactor_api.py b/wechatsogou/refactor_api.py new file mode 100644 index 0000000..c211e4c --- /dev/null +++ b/wechatsogou/refactor_api.py @@ -0,0 +1,7 @@ +# -*- coding: utf-8 -*- + +from __future__ import absolute_import, unicode_literals, print_function + + +class WechatSogouAPI(object): + pass