def test_main_ok(self): params = { 'pathParameters': { 'user_id': 'TST' }, 'queryStringParameters': { 'limit': '2' } } response = UsersArticlesPopular(params, {}, self.dynamodb).main() expected_items = [ { 'article_id': 'testid000004', 'user_id': 'TST', 'status': 'public', 'sort_key': 1520150272000000, 'popular_sort_key': 5000 * 10 ** 18 }, { 'article_id': 'testid000003', 'user_id': 'TST', 'status': 'public', 'sort_key': 1520150272000000, 'popular_sort_key': 4000 * 10 ** 18 } ] self.assertEqual(response['statusCode'], 200) self.assertEqual(json.loads(response['body'])['Items'], expected_items)
def test_main_ok_not_exists_target_user(self): params = { 'pathParameters': { 'user_id': 'HOGE' } } response = UsersArticlesPopular(params, {}, self.dynamodb).main() self.assertEqual(response['statusCode'], 200) self.assertEqual(len(json.loads(response['body'])['Items']), 0)
def test_main_ok_without_limit(self): params = { 'pathParameters': { 'user_id': 'TST' } } response = UsersArticlesPopular(params, {}, self.dynamodb).main() expected_items = [ { 'article_id': 'testid000004', 'user_id': 'TST', 'status': 'public', 'sort_key': 1520150272000000, 'popular_sort_key': 5000 * 10 ** 18 }, { 'article_id': 'testid000003', 'user_id': 'TST', 'status': 'public', 'sort_key': 1520150272000000, 'popular_sort_key': 4000 * 10 ** 18 }, { 'article_id': 'testid000002', 'user_id': 'TST', 'status': 'public', 'sort_key': 1520150272000000, 'price': 200, 'popular_sort_key': 3000 * 10 ** 18 } ] self.assertEqual(response['statusCode'], 200) self.assertEqual(len(json.loads(response['body'])['Items']), settings.USERS_ARTICLE_POPULAR_INDEX_DEFAULT_LIMIT) self.assertEqual(json.loads(response['body'])['Items'], expected_items)
def lambda_handler(event, context): users_articles_popular = UsersArticlesPopular(event, context, dynamodb) return users_articles_popular.main()
def assert_bad_request(self, params): function = UsersArticlesPopular(params, {}, self.dynamodb) response = function.main() self.assertEqual(response['statusCode'], 400)