|
1 | 1 | import os |
| 2 | + |
| 3 | +from boto3.dynamodb.conditions import Key |
2 | 4 | from db_util import DBUtil |
3 | 5 | from tests_util import TestsUtil |
4 | 6 | from unittest import TestCase |
@@ -53,6 +55,50 @@ def setUpClass(cls): |
53 | 55 | ] |
54 | 56 | TestsUtil.create_table(cls.dynamodb, os.environ['COMMENT_TABLE_NAME'], cls.comment_items) |
55 | 57 |
|
| 58 | + article_pv_user_items = [ |
| 59 | + { |
| 60 | + 'article_id': 'article01', |
| 61 | + 'user_id': 'one_day_before_user1', |
| 62 | + 'article_user_id': 'article_user_1', |
| 63 | + 'target_date': '2018-05-01', |
| 64 | + 'created_at': 1520035200, |
| 65 | + 'sort_key': 1520035200000000 |
| 66 | + }, |
| 67 | + { |
| 68 | + 'article_id': 'one_day_before_article', |
| 69 | + 'user_id': 'one_day_before_user1', |
| 70 | + 'article_user_id': 'article_user_1', |
| 71 | + 'target_date': '2018-05-01', |
| 72 | + 'created_at': 1520035200, |
| 73 | + 'sort_key': 1520035200000000 |
| 74 | + }, |
| 75 | + { |
| 76 | + 'article_id': 'article01', |
| 77 | + 'user_id': 'a1_user1', |
| 78 | + 'article_user_id': 'article_user_1', |
| 79 | + 'target_date': '2018-05-01', |
| 80 | + 'created_at': 1520121600, |
| 81 | + 'sort_key': 1520121600000000 |
| 82 | + }, |
| 83 | + { |
| 84 | + 'article_id': 'article01', |
| 85 | + 'user_id': 'a1_user2', |
| 86 | + 'article_user_id': 'article_user_1', |
| 87 | + 'target_date': '2018-05-01', |
| 88 | + 'created_at': 1520125200, |
| 89 | + 'sort_key': 1520125200000000 |
| 90 | + }, |
| 91 | + { |
| 92 | + 'article_id': 'article02', |
| 93 | + 'user_id': 'a1_user2', |
| 94 | + 'article_user_id': 'article_user_2', |
| 95 | + 'target_date': '2018-05-02', |
| 96 | + 'created_at': 1520125200, |
| 97 | + 'sort_key': 1520125200000000 |
| 98 | + } |
| 99 | + ] |
| 100 | + TestsUtil.create_table(cls.dynamodb, os.environ['ARTICLE_PV_USER_TABLE_NAME'], article_pv_user_items) |
| 101 | + |
56 | 102 | @classmethod |
57 | 103 | def tearDownClass(cls): |
58 | 104 | TestsUtil.delete_all_tables(cls.dynamodb) |
@@ -236,3 +282,28 @@ def test_items_values_empty_to_none_ok(self): |
236 | 282 |
|
237 | 283 | self.assertEqual(values['test'], 'test') |
238 | 284 | self.assertEqual(values['empty'], None) |
| 285 | + |
| 286 | + def test_query_all_items_with_limit(self): |
| 287 | + article_pv_user_table = self.dynamodb.Table(os.environ['ARTICLE_PV_USER_TABLE_NAME']) |
| 288 | + # ユースケースとしては1MBを超え、レスポンスにLastEvaluatedKeyが付与されて返ってくる場合だが |
| 289 | + # Limitを付与した際も同じレスポンスなのでLimitで代用している |
| 290 | + query_params = { |
| 291 | + 'IndexName': 'target_date-sort_key-index', |
| 292 | + 'KeyConditionExpression': Key('target_date').eq('2018-05-01'), |
| 293 | + 'Limit': 1 |
| 294 | + } |
| 295 | + |
| 296 | + response = DBUtil.query_all_items(article_pv_user_table, query_params) |
| 297 | + |
| 298 | + self.assertEqual(len(response), 4) |
| 299 | + |
| 300 | + def test_query_all_items_with_no_limit(self): |
| 301 | + article_pv_user_table = self.dynamodb.Table(os.environ['ARTICLE_PV_USER_TABLE_NAME']) |
| 302 | + query_params = { |
| 303 | + 'IndexName': 'target_date-sort_key-index', |
| 304 | + 'KeyConditionExpression': Key('target_date').eq('2018-05-01') |
| 305 | + } |
| 306 | + |
| 307 | + response = DBUtil.query_all_items(article_pv_user_table, query_params) |
| 308 | + |
| 309 | + self.assertEqual(len(response), 4) |
0 commit comments