|
2 | 2 |
|
3 | 3 | from __future__ import absolute_import, division, print_function, unicode_literals |
4 | 4 |
|
| 5 | +import math |
| 6 | +import re |
5 | 7 | from unittest import TestCase |
6 | 8 |
|
7 | 9 | from atwiki.core import AtWikiAPI |
@@ -59,3 +61,51 @@ def test_search_or(self): |
59 | 61 | def test_search_none(self): |
60 | 62 | results = list(self._api.search('no_result_expected_for_this')) |
61 | 63 | self.assertEqual(len(results), 0) |
| 64 | + |
| 65 | + |
| 66 | +class PagerizeTest(TestCase): |
| 67 | + def setUp(self): |
| 68 | + self._uri = AtWikiURI('https://w.atwiki.jp/hmiku') |
| 69 | + self._api = AtWikiAPI(self._uri) |
| 70 | + |
| 71 | + def test_get_list(self): |
| 72 | + top_page = next(self._api.get_list()) |
| 73 | + assert top_page == {'id': 1, 'name': 'トップページ'} |
| 74 | + |
| 75 | + soup = self._api._request(self._uri.list(sort='create', index=1)) |
| 76 | + text = soup.find('div', class_='pagelist').text |
| 77 | + m = re.search(r'計 (\d+) ページ / 1 から 100 を表示', text) |
| 78 | + assert m is not None |
| 79 | + count = int(m.group(1)) |
| 80 | + assert 45000 < count < 90000 |
| 81 | + last_index = math.ceil(count / 100) |
| 82 | + |
| 83 | + # Get list from the last page. |
| 84 | + # N.B. The page counter is not updated immediately. |
| 85 | + pages = list(self._api.get_list(_start=last_index)) |
| 86 | + expected = (count % 100) |
| 87 | + assert (expected - 5) < len(pages) < (expected + 5) |
| 88 | + |
| 89 | + top_page = next(self._api.get_list(_start=last_index + 1)) |
| 90 | + assert top_page == {'id': 1, 'name': 'トップページ'} |
| 91 | + |
| 92 | + def test_get_list_tag(self): |
| 93 | + soup = self._api._request(self._uri.tag('曲', index=1)) |
| 94 | + last_index = 1 |
| 95 | + for link in soup.find('div', class_='cmd_tag').find_all('a'): |
| 96 | + if not link.attrs['href'].endswith('&p={}'.format(last_index + 1)): |
| 97 | + break |
| 98 | + last_index += 1 |
| 99 | + pages = list(self._api.get_list('曲', _start=last_index)) |
| 100 | + assert 1 <= len(pages) <= 50 |
| 101 | + |
| 102 | + pages = list(self._api.get_list('曲', _start=last_index + 1)) |
| 103 | + assert len(pages) == 0 |
| 104 | + |
| 105 | + def test_get_tags(self): |
| 106 | + song = next(self._api.get_tags('num')) |
| 107 | + assert song['name'] == '曲' |
| 108 | + assert 35000 < song['weight'] < 70000 |
| 109 | + |
| 110 | + not_song = next(self._api.get_tags('num', _start=2)) |
| 111 | + assert not_song['name'] != '曲' |
0 commit comments