|
| 1 | +import json |
| 2 | +from unittest import TestCase |
| 3 | +from search_tags_count import SearchTagsCount |
| 4 | +from elasticsearch import Elasticsearch |
| 5 | +from unittest.mock import patch, MagicMock |
| 6 | + |
| 7 | + |
| 8 | +class TestSearchTagsCount(TestCase): |
| 9 | + elasticsearch = Elasticsearch( |
| 10 | + hosts=[{'host': 'localhost'}] |
| 11 | + ) |
| 12 | + |
| 13 | + def setUp(self): |
| 14 | + items = [ |
| 15 | + { |
| 16 | + 'article_id': 'test1', |
| 17 | + 'created_at': 1530112710, |
| 18 | + 'title': 'abc1', |
| 19 | + 'published_at': 1530112710, |
| 20 | + 'sort_key': 1530112710000000, |
| 21 | + 'body': 'huga test', |
| 22 | + 'tags': ['A', 'B', 'C'] |
| 23 | + }, |
| 24 | + { |
| 25 | + 'article_id': 'test2', |
| 26 | + 'created_at': 1530112720, |
| 27 | + 'title': 'abc2', |
| 28 | + 'published_at': 1530112710 + 1, |
| 29 | + 'sort_key': 1530112720000000, |
| 30 | + 'body': 'foo bar', |
| 31 | + 'tags': ['A', 'B'] |
| 32 | + }, |
| 33 | + { |
| 34 | + 'article_id': 'test3', |
| 35 | + 'created_at': 1530112730, |
| 36 | + 'title': 'abc3', |
| 37 | + 'published_at': 1530112710 + 2, |
| 38 | + 'sort_key': 1530112730000000, |
| 39 | + 'body': 'foo bar', |
| 40 | + 'tags': ['A'] |
| 41 | + }, |
| 42 | + { |
| 43 | + 'article_id': 'test4', |
| 44 | + 'created_at': 1530112740, |
| 45 | + 'title': 'abc4', |
| 46 | + 'published_at': 1530112710 - 1, |
| 47 | + 'sort_key': 1530112700000000, |
| 48 | + 'body': 'foo bar', |
| 49 | + 'tags': ['A', 'B', 'C'] |
| 50 | + }, |
| 51 | + { |
| 52 | + 'article_id': 'test5', |
| 53 | + 'created_at': 1530112750, |
| 54 | + 'title': 'abc5', |
| 55 | + 'published_at': 1530112710, |
| 56 | + 'sort_key': 1530112700000000, |
| 57 | + 'body': 'foo bar', |
| 58 | + 'tags': ['ハンカク', '&$%!”#', '𪚲🍣𪚲', 'aaa-aaa', 'abcde vwxyz'] |
| 59 | + }, |
| 60 | + ] |
| 61 | + for item in items: |
| 62 | + self.elasticsearch.index( |
| 63 | + index="articles", |
| 64 | + doc_type="article", |
| 65 | + id=item["article_id"], |
| 66 | + body=item |
| 67 | + ) |
| 68 | + self.elasticsearch.indices.refresh(index="articles") |
| 69 | + |
| 70 | + def tearDown(self): |
| 71 | + self.elasticsearch.indices.delete(index="articles", ignore=[404]) |
| 72 | + |
| 73 | + @patch('time.time', MagicMock(return_value=1530112710 + 86400 * 7)) |
| 74 | + def test_search_request(self): |
| 75 | + params = { |
| 76 | + 'queryStringParameters': { |
| 77 | + 'tags': ['A', 'B', 'C', 'D'] |
| 78 | + } |
| 79 | + } |
| 80 | + response = SearchTagsCount(params, {}, elasticsearch=self.elasticsearch).main() |
| 81 | + actual = json.loads(response['body']) |
| 82 | + expected = [ |
| 83 | + {'count': 3, 'tag': 'A'}, |
| 84 | + {'count': 2, 'tag': 'B'}, |
| 85 | + {'count': 1, 'tag': 'C'}, |
| 86 | + {'count': 0, 'tag': 'D'} |
| 87 | + ] |
| 88 | + self.assertEqual(response['statusCode'], 200) |
| 89 | + self.assertEqual(expected, actual) |
| 90 | + |
| 91 | + def test_search_request_not_exists(self): |
| 92 | + params = { |
| 93 | + 'queryStringParameters': { |
| 94 | + 'tags': ['あ', 'い', 'う'] |
| 95 | + } |
| 96 | + } |
| 97 | + response = SearchTagsCount(params, {}, elasticsearch=self.elasticsearch).main() |
| 98 | + actual = json.loads(response['body']) |
| 99 | + expected = [ |
| 100 | + {'count': 0, 'tag': 'あ'}, |
| 101 | + {'count': 0, 'tag': 'い'}, |
| 102 | + {'count': 0, 'tag': 'う'} |
| 103 | + ] |
| 104 | + self.assertEqual(response['statusCode'], 200) |
| 105 | + self.assertEqual(expected, actual) |
| 106 | + |
| 107 | + @patch('time.time', MagicMock(return_value=1530112710 + 86400 * 7)) |
| 108 | + def test_search_with_tag_half_kana(self): |
| 109 | + params = { |
| 110 | + 'queryStringParameters': { |
| 111 | + 'tags': ['ハンカク', '&$%!”#', '𪚲🍣𪚲', 'aaa-aaa', 'abcde vwxyz'] |
| 112 | + } |
| 113 | + } |
| 114 | + response = SearchTagsCount(params, {}, elasticsearch=self.elasticsearch).main() |
| 115 | + actual = json.loads(response['body']) |
| 116 | + expected = [ |
| 117 | + {'count': 1, 'tag': 'ハンカク'}, |
| 118 | + {'count': 1, 'tag': '&$%!”#'}, |
| 119 | + {'count': 1, 'tag': '𪚲🍣𪚲'}, |
| 120 | + {'count': 1, 'tag': 'aaa-aaa'}, |
| 121 | + {'count': 1, 'tag': 'abcde vwxyz'} |
| 122 | + ] |
| 123 | + self.assertEqual(response['statusCode'], 200) |
| 124 | + self.assertEqual(expected, actual) |
| 125 | + |
| 126 | + def test_search_request_tags_over150(self): |
| 127 | + # tags 数150超 |
| 128 | + temp_tags = [] |
| 129 | + for i in range(151): |
| 130 | + temp_tags.append('AAA') |
| 131 | + |
| 132 | + params = { |
| 133 | + 'queryStringParameters': { |
| 134 | + 'tags': temp_tags, |
| 135 | + } |
| 136 | + } |
| 137 | + response = SearchTagsCount(params, {}, elasticsearch=self.elasticsearch).main() |
| 138 | + self.assertEqual(response['statusCode'], 400) |
| 139 | + |
| 140 | + def test_search_no_params(self): |
| 141 | + params = { |
| 142 | + 'queryStringParameters': {} |
| 143 | + } |
| 144 | + response = SearchTagsCount(params, {}, elasticsearch=self.elasticsearch).main() |
| 145 | + self.assertEqual(response['statusCode'], 400) |
| 146 | + |
| 147 | + def test_invalid_tag_parmas(self): |
| 148 | + params = { |
| 149 | + 'queryStringParameters': { |
| 150 | + 'tags': [''] |
| 151 | + } |
| 152 | + } |
| 153 | + response = SearchTagsCount(params, {}, elasticsearch=self.elasticsearch).main() |
| 154 | + self.assertEqual(response['statusCode'], 400) |
| 155 | + |
| 156 | + params = { |
| 157 | + 'queryStringParameters': { |
| 158 | + 'tags': ['A' * 26] |
| 159 | + } |
| 160 | + } |
| 161 | + response = SearchTagsCount(params, {}, elasticsearch=self.elasticsearch).main() |
| 162 | + self.assertEqual(response['statusCode'], 400) |
0 commit comments