Skip to content

Commit 1082ec9

Browse files
committed
add test case & delete unreachable code
1 parent be12131 commit 1082ec9

File tree

3 files changed

+156
-57
lines changed

3 files changed

+156
-57
lines changed

src/common/es_util.py

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,6 @@
33

44
class ESUtil:
55

6-
@staticmethod
7-
def post_article(elasticsearch, article_info, article_content):
8-
id = article_info['article_id']
9-
body = article_info
10-
body.update(article_content)
11-
elasticsearch.index(
12-
index="articles",
13-
doc_type="article",
14-
id=id,
15-
body=body
16-
)
17-
18-
@staticmethod
19-
def delete_article(elasticsearch, article_id):
20-
elasticsearch.delete(
21-
index="articles",
22-
doc_type="article",
23-
id=article_id,
24-
ignore=[404]
25-
)
26-
276
@staticmethod
287
def search_article(elasticsearch, word, limit, page):
298
body = {
@@ -105,13 +84,3 @@ def search_user(elasticsearch, word, limit, page):
10584
body=body
10685
)
10786
return res
108-
109-
@staticmethod
110-
def post_user(elasticsearch, user):
111-
id = user['user_id']
112-
elasticsearch.index(
113-
index="users",
114-
doc_type="user",
115-
id=id,
116-
body=user
117-
)

tests/handlers/search/articles/test_search_articles.py

Lines changed: 87 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,38 @@ class TestSearchArticles(TestCase):
1010
)
1111

1212
def setUp(self):
13-
body = {
14-
'article_id': "test",
13+
items = [
14+
{
15+
'article_id': "test1",
16+
'created_at': 1530112753,
17+
'title': "abc1",
18+
"published_at": 1530112753,
19+
'body': "huga test"
20+
},
21+
{
22+
'article_id': "test2",
23+
'created_at': 1530112753,
24+
'title': "abc2",
25+
"published_at": 1530112753,
26+
'body': "foo bar"
27+
}
28+
]
29+
for dummy in range(30):
30+
items.append({
31+
'article_id': f"dummy{dummy}",
1532
'created_at': 1530112753,
16-
'title': "abc",
17-
"published_at": 1530112753,
18-
'body': "huga test"
19-
}
20-
self.elasticsearch.index(
21-
index="articles",
22-
doc_type="article",
23-
id="test",
24-
body=body
25-
)
26-
self.elasticsearch.indices.refresh(index="articles")
33+
'title': f"abc{dummy}",
34+
"published_at": 1530112753 + dummy,
35+
'body': "dummy article{dummy}"
36+
})
37+
for item in items:
38+
self.elasticsearch.index(
39+
index="articles",
40+
doc_type="article",
41+
id=item["article_id"],
42+
body=item
43+
)
44+
self.elasticsearch.indices.refresh(index="articles")
2745

2846
def tearDown(self):
2947
self.elasticsearch.indices.delete(index="articles", ignore=[404])
@@ -38,3 +56,59 @@ def test_search_request(self):
3856
response = SearchArticles(params, {}, elasticsearch=self.elasticsearch).main()
3957
result = json.loads(response['body'])
4058
self.assertEqual(len(result), 1)
59+
60+
def test_search_request_limit(self):
61+
# limit 指定なし
62+
params = {
63+
'queryStringParameters': {
64+
'query': 'dummy'
65+
}
66+
}
67+
response = SearchArticles(params, {}, elasticsearch=self.elasticsearch).main()
68+
result = json.loads(response['body'])
69+
self.assertEqual(len(result), 20)
70+
# limit 指定
71+
params = {
72+
'queryStringParameters': {
73+
'limit': '10',
74+
'query': 'dummy'
75+
}
76+
}
77+
response = SearchArticles(params, {}, elasticsearch=self.elasticsearch).main()
78+
result = json.loads(response['body'])
79+
self.assertEqual(len(result), 10)
80+
81+
def test_search_request_page(self):
82+
# page 指定なし
83+
params = {
84+
'queryStringParameters': {
85+
'limit': '10',
86+
'query': 'dummy'
87+
}
88+
}
89+
response = SearchArticles(params, {}, elasticsearch=self.elasticsearch).main()
90+
result = json.loads(response['body'])
91+
self.assertEqual(len(result), 10)
92+
self.assertEqual(result[0]['article_id'], 'dummy29')
93+
# page 指定
94+
params = {
95+
'queryStringParameters': {
96+
'page': '2',
97+
'limit': '10',
98+
'query': 'dummy'
99+
}
100+
}
101+
response = SearchArticles(params, {}, elasticsearch=self.elasticsearch).main()
102+
result = json.loads(response['body'])
103+
self.assertEqual(result[0]['article_id'], 'dummy19')
104+
self.assertEqual(len(result), 10)
105+
106+
def test_search_match_zero(self):
107+
params = {
108+
'queryStringParameters': {
109+
'query': 'def'
110+
}
111+
}
112+
response = SearchArticles(params, {}, elasticsearch=self.elasticsearch).main()
113+
result = json.loads(response['body'])
114+
self.assertEqual(len(result), 0)

tests/handlers/search/users/test_search_users.py

Lines changed: 69 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,21 @@ class TestSearchUsers(TestCase):
1010
)
1111

1212
def setUp(self):
13-
body = {
14-
'user_id': 'testuser',
15-
'user_display_name': 'testuser',
13+
items = []
14+
for dummy in range(30):
15+
items.append({
16+
'user_id': f"testuser{dummy}",
17+
'user_display_name': f"testuser",
1618
'updated_at': 1530112753,
17-
'sync_elasticsearch': 0
18-
}
19-
self.elasticsearch.index(
20-
index="users",
21-
doc_type="user",
22-
id="test",
23-
body=body
24-
)
25-
self.elasticsearch.indices.refresh(index="users")
19+
})
20+
for item in items:
21+
self.elasticsearch.index(
22+
index="users",
23+
doc_type="user",
24+
id=item["user_id"],
25+
body=item
26+
)
27+
self.elasticsearch.indices.refresh(index="users")
2628

2729
def tearDown(self):
2830
self.elasticsearch.indices.delete(index="users", ignore=[404])
@@ -31,9 +33,63 @@ def test_search_request(self):
3133
params = {
3234
'queryStringParameters': {
3335
'limit': '1',
34-
'query': 'testuser'
36+
'query': 'testuser1'
3537
}
3638
}
3739
response = SearchUsers(params, {}, elasticsearch=self.elasticsearch).main()
3840
result = json.loads(response['body'])
3941
self.assertEqual(len(result), 1)
42+
43+
def test_search_request_limit(self):
44+
# limit 指定なし
45+
params = {
46+
'queryStringParameters': {
47+
'query': 'testuser'
48+
}
49+
}
50+
response = SearchUsers(params, {}, elasticsearch=self.elasticsearch).main()
51+
result = json.loads(response['body'])
52+
self.assertEqual(len(result), 20)
53+
# limit 指定なし
54+
params = {
55+
'queryStringParameters': {
56+
'limit': '10',
57+
'query': 'testuser'
58+
}
59+
}
60+
response = SearchUsers(params, {}, elasticsearch=self.elasticsearch).main()
61+
result = json.loads(response['body'])
62+
self.assertEqual(len(result), 10)
63+
64+
def test_search_request_page(self):
65+
# page 指定なし
66+
params = {
67+
'queryStringParameters': {
68+
'limit': '20',
69+
'query': 'testuser'
70+
}
71+
}
72+
response = SearchUsers(params, {}, elasticsearch=self.elasticsearch).main()
73+
result = json.loads(response['body'])
74+
self.assertEqual(len(result), 20)
75+
# page 指定
76+
params = {
77+
'queryStringParameters': {
78+
'page': '2',
79+
'limit': '20',
80+
'query': 'testuser'
81+
}
82+
}
83+
response = SearchUsers(params, {}, elasticsearch=self.elasticsearch).main()
84+
result = json.loads(response['body'])
85+
self.assertEqual(len(result), 10)
86+
87+
def test_search_match_zero(self):
88+
params = {
89+
'queryStringParameters': {
90+
'query': 'hogehoge'
91+
}
92+
}
93+
response = SearchUsers(params, {}, elasticsearch=self.elasticsearch).main()
94+
result = json.loads(response['body'])
95+
self.assertEqual(len(result), 0)

0 commit comments

Comments
 (0)