def assert_bad_request(self, params): function = ArticlesTipRanking(params, {}, dynamodb=self.dynamodb, elasticsearch=self.elasticsearch) response = function.main() self.assertEqual(response['statusCode'], 400)
def test_main_ok_with_topic(self): params = {'queryStringParameters': {'topic': 'fashion'}} response = ArticlesTipRanking(params, {}, dynamodb=self.dynamodb, elasticsearch=self.elasticsearch).main() expected_items = [{ 'article_id': 'testid000004', 'user_id': 'matsumatsu20', 'created_at': 1520150272, 'title': 'title05', 'overview': 'overview05', 'status': 'public', 'topic': 'fashion', 'sort_tip_value': 18000000000000.1, 'sort_key': 1520150272000003 }, { 'article_id': 'testid000002', 'user_id': 'matsumatsu20', 'created_at': 1520150272, 'title': 'title03', 'overview': 'overview03', 'status': 'public', 'topic': 'fashion', 'sort_tip_value': 18000000000000, 'sort_key': 1520150272000002 }] self.assertEqual(response['statusCode'], 200) self.assertEqual(json.loads(response['body'])['Items'], expected_items)
def test_main_ok_with_no_limit(self): for i in range(21): self.elasticsearch.index( index=settings.ARTICLE_TIP_RANKING_INDEX_NAME, doc_type="article_tip_ranking", id='test_limit_number' + str(i), body={ 'article_id': 'test_limit_number' + str(i), 'user_id': 'matsumatsu20', 'created_at': 1520150272, 'title': 'title03', 'overview': 'overview03', 'status': 'public', 'topic': 'crypto', 'sort_tip_value': 18000000000000, 'sort_key': 1520150273000000 + i }) self.elasticsearch.indices.refresh( settings.ARTICLE_TIP_RANKING_INDEX_NAME) params = {'queryStringParameters': None} response = ArticlesTipRanking(params, {}, dynamodb=self.dynamodb, elasticsearch=self.elasticsearch).main() self.assertEqual(response['statusCode'], 200) self.assertEqual(len(json.loads(response['body'])['Items']), 20)
def test_main_ok(self): params = {'queryStringParameters': {'limit': '2'}} response = ArticlesTipRanking(params, {}, dynamodb=self.dynamodb, elasticsearch=self.elasticsearch).main() expected_items = [{ 'article_id': 'testid000005', 'user_id': 'test_user_id_02', 'created_at': 1520150272, 'title': 'title04', 'overview': 'overview04', 'status': 'public', 'topic': 'crypto', 'sort_tip_value': 8000000000000000000000000, 'sort_key': 1520150272000003 }, { 'article_id': 'testid000006', 'user_id': 'test_user_id_03', 'created_at': 1520150272, 'title': 'title04', 'overview': 'overview04', 'status': 'public', 'topic': 'crypto', 'sort_tip_value': 7000000000000000000000000, 'sort_key': 1520150272000003 }] self.assertEqual(response['statusCode'], 200) self.assertEqual(json.loads(response['body'])['Items'], expected_items)
def test_main_with_no_index(self): TestsEsUtil.delete_alias(self.elasticsearch, settings.ARTICLE_TIP_RANKING_INDEX_NAME) params = {'queryStringParameters': {'limit': '2'}} response = ArticlesTipRanking(params, {}, dynamodb=self.dynamodb, elasticsearch=self.elasticsearch).main() self.assertEqual(response['statusCode'], 200) self.assertEqual(json.loads(response['body'])['Items'], [])
def test_call_validate_topic(self): params = {'queryStringParameters': {'topic': 'crypto'}} mock_lib = MagicMock() with patch('articles_tip_ranking.DBUtil', mock_lib): ArticlesTipRanking(params, {}, dynamodb=self.dynamodb, elasticsearch=self.elasticsearch).main() self.assertTrue(mock_lib.validate_topic.called) args, kwargs = mock_lib.validate_topic.call_args self.assertTrue(args[0]) self.assertEqual(args[1], 'crypto')
def test_main_ok_exists_blacklisted_user(self): # blacklist のユーザ追加 params = { 'article_type': 'write_blacklisted', 'users': ['test_user_id_02', 'test_user_id_03'] } screened_article_table = self.dynamodb.Table( os.environ['SCREENED_ARTICLE_TABLE_NAME']) screened_article_table.put_item(Item=params) params = {'queryStringParameters': {'limit': '2'}} response = ArticlesTipRanking(params, {}, dynamodb=self.dynamodb, elasticsearch=self.elasticsearch).main() expected_items = [{ 'article_id': 'testid000003', 'user_id': 'matsumatsu20', 'created_at': 1520150272, 'title': 'title04', 'overview': 'overview04', 'status': 'public', 'topic': 'crypto', 'sort_tip_value': 6000000000000000000000000, 'sort_key': 1520150272000003 }, { 'article_id': 'testid000004', 'user_id': 'matsumatsu20', 'created_at': 1520150272, 'title': 'title05', 'overview': 'overview05', 'status': 'public', 'topic': 'fashion', 'sort_tip_value': 18000000000000.1, 'sort_key': 1520150272000003 }] self.assertEqual(response['statusCode'], 200) self.assertEqual(json.loads(response['body'])['Items'], expected_items)
def lambda_handler(event, context): articles_tip_ranking = ArticlesTipRanking(event, context, dynamodb=dynamodb, elasticsearch=elasticsearch) return articles_tip_ranking.main()