def assert_bad_request(self, params): function = ArticlesPopular(params, {}, dynamodb=self.dynamodb, elasticsearch=self.elasticsearch) response = function.main() self.assertEqual(response['statusCode'], 400)
def test_main_ok(self): params = { 'queryStringParameters': { 'limit': '3' } } response = ArticlesPopular(params, {}, self.dynamodb).main() expected_items = [ { 'article_id': 'testid000002', 'status': 'public', 'sort_key': 1520150272000002 }, { 'article_id': 'testid000001', 'status': 'public', 'sort_key': 1520150272000001 } ] expected_evaluated_key = { 'article_id': 'testid000001', 'score': 12, 'evaluated_at': 1520150272000000 } self.assertEqual(response['statusCode'], 200) self.assertEqual(json.loads(response['body'])['Items'], expected_items) self.assertEqual(json.loads(response['body'])['LastEvaluatedKey'], expected_evaluated_key)
def test_main_ok_with_no_limit(self): article_info_table = self.dynamodb.Table(os.environ['ARTICLE_INFO_TABLE_NAME']) article_score_table = self.dynamodb.Table(os.environ['ARTICLE_SCORE_TABLE_NAME']) for i in range(21): article_info_table.put_item(Item={ 'article_id': 'test_limit_number' + str(i), 'status': 'public', 'sort_key': 1520150273000000 + i } ) article_score_table.put_item(Item={ 'article_id': 'test_limit_number' + str(i), 'score': 30, 'evaluated_at': 1520150272000000 } ) params = { 'queryStringParameters': None } response = ArticlesPopular(params, {}, self.dynamodb).main() self.assertEqual(response['statusCode'], 200) self.assertEqual(len(json.loads(response['body'])['Items']), 20)
def test_main_ok_with_page(self): params = {'queryStringParameters': {'limit': '2', 'page': '2'}} response = ArticlesPopular(params, {}, dynamodb=self.dynamodb, elasticsearch=self.elasticsearch).main() expected_items = [{ 'article_id': 'testid000004', 'article_score': 10, 'created_at': 1520150272, 'overview': 'overview05', 'sort_key': 1520150272000003, 'status': 'public', 'title': 'title05', 'topic': 'game', 'user_id': 'keillera' }, { 'article_id': 'testid000003', 'user_id': 'matsumatsu20', 'created_at': 1520150272, 'title': 'title04', 'overview': 'overview04', 'status': 'public', 'topic': 'crypto', 'article_score': 6, 'sort_key': 1520150272000003 }] 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_SCORE_INDEX_NAME, doc_type="article_score", 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', 'article_score': 30, 'sort_key': 1520150273000000 + i }) self.elasticsearch.indices.refresh(settings.ARTICLE_SCORE_INDEX_NAME) params = {'queryStringParameters': None} response = ArticlesPopular(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 = ArticlesPopular(params, {}, dynamodb=self.dynamodb, elasticsearch=self.elasticsearch).main() expected_items = [{ 'article_id': 'testid000002', 'user_id': 'matsumatsu20', 'created_at': 1520150272, 'title': 'title03', 'overview': 'overview03', 'status': 'public', 'topic': 'fashion', 'article_score': 18, 'sort_key': 1520150272000002 }, { 'article_id': 'testid000001', 'user_id': 'matsumatsu20', 'created_at': 1520150272, 'title': 'title02', 'overview': 'overview02', 'status': 'public', 'topic': 'crypto', 'article_score': 12, 'sort_key': 1520150272000001, 'price': 100 }] self.assertEqual(response['statusCode'], 200) self.assertEqual(json.loads(response['body'])['Items'], expected_items)
def test_scenario_evaluated_at_changed_within_pagination(self): # 初回の記事一覧取得 params = { 'queryStringParameters': { 'limit': '2' } } response = ArticlesPopular(params, {}, self.dynamodb).main() evaluated_key = json.loads(response['body'])['LastEvaluatedKey'] self.assertEqual(response['statusCode'], 200) # 2回目の記事一覧取得、初回で得られたevaluated_keyを利用 # かつ処理の途中でバッチが実行されたことを想定して、ArticleEvaluatedManageを更新する article_evaluated_manage_table = self.dynamodb.Table(os.environ['ARTICLE_EVALUATED_MANAGE_TABLE_NAME']) article_evaluated_manage_table.put_item(Item={ 'type': 'article_score', 'active_evaluated_at': 1520150272000100 }) params = { 'queryStringParameters': { 'limit': '2', 'article_id': evaluated_key['article_id'], 'score': str(evaluated_key['score']), 'evaluated_at': str(evaluated_key['evaluated_at']) } } response = ArticlesPopular(params, {}, self.dynamodb).main() expected_items = [ { 'article_id': 'testid000001', 'status': 'public', 'sort_key': 1520150272000001 }, { 'article_id': 'testid000003', 'status': 'public', '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_SCORE_INDEX_NAME) params = {'queryStringParameters': {'limit': '2'}} response = ArticlesPopular(params, {}, dynamodb=self.dynamodb, elasticsearch=self.elasticsearch).main() self.assertEqual(response['statusCode'], 200) self.assertEqual(json.loads(response['body'])['Items'], [])
def test_main_no_evaluated_manager(self): article_evaluated_manage_table = self.dynamodb.Table( os.environ['ARTICLE_EVALUATED_MANAGE_TABLE_NAME']) article_evaluated_manage_table.delete_item( Key={'type': 'article_score'}) params = {'queryStringParameters': {'limit': '3'}} response = ArticlesPopular(params, {}, self.dynamodb).main() self.assertEqual(response['statusCode'], 200) self.assertEqual(json.loads(response['body']), [])
def test_call_validate_topic(self): params = {'queryStringParameters': {'topic': 'crypto'}} mock_lib = MagicMock() with patch('articles_popular.DBUtil', mock_lib): ArticlesPopular(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': ['keillera']} screened_article_table = self.dynamodb.Table( os.environ['SCREENED_ARTICLE_TABLE_NAME']) screened_article_table.put_item(Item=params) params = {'queryStringParameters': {'limit': '3'}} response = ArticlesPopular(params, {}, dynamodb=self.dynamodb, elasticsearch=self.elasticsearch).main() expected_items = [{ 'article_id': 'testid000002', 'user_id': 'matsumatsu20', 'created_at': 1520150272, 'title': 'title03', 'overview': 'overview03', 'status': 'public', 'topic': 'fashion', 'article_score': 18, 'sort_key': 1520150272000002 }, { 'article_id': 'testid000001', 'user_id': 'matsumatsu20', 'created_at': 1520150272, 'title': 'title02', 'overview': 'overview02', 'status': 'public', 'topic': 'crypto', 'article_score': 12, 'sort_key': 1520150272000001, 'price': 100 }, { 'article_id': 'testid000003', 'user_id': 'matsumatsu20', 'created_at': 1520150272, 'title': 'title04', 'overview': 'overview04', 'status': 'public', 'topic': 'crypto', 'article_score': 6, 'sort_key': 1520150272000003 }] self.assertEqual(response['statusCode'], 200) self.assertEqual(json.loads(response['body'])['Items'], expected_items)
def lambda_handler(event, context): articles_popular = ArticlesPopular(event, context, dynamodb) return articles_popular.main()
def lambda_handler(event, context): articles_popular = ArticlesPopular(event, context, dynamodb=dynamodb, elasticsearch=elasticsearch) return articles_popular.main()