def test_main_ok(self): params = { 'pathParameters': { 'user_id': 'TST' }, 'queryStringParameters': { 'limit': '2' } } response = UsersArticlesPublic(params, {}, self.dynamodb).main() expected_items = [{ 'article_id': 'testid000004', 'user_id': 'TST', 'status': 'public', 'sort_key': 1520150272000004 }, { 'article_id': 'testid000002', 'user_id': 'TST', 'status': 'public', 'sort_key': 1520150272000002, 'price': 200 }] self.assertEqual(response['statusCode'], 200) self.assertEqual(json.loads(response['body'])['Items'], expected_items)
def test_main_ok_with_only_sort_key(self): table = self.dynamodb.Table('ArticleInfo') for i in range(11): table.put_item( Item={ 'user_id': 'test-only-sort-key', 'article_id': 'test_limit_number' + str(i), 'status': 'public', 'sort_key': 1520150273000000 + i }) params = { 'pathParameters': { 'user_id': 'test-only-sort-key' }, 'queryStringParameters': { 'sort_key': '1520150272000002' } } response = UsersArticlesPublic(params, {}, self.dynamodb).main() self.assertEqual(response['statusCode'], 200) self.assertEqual(len(json.loads(response['body'])['Items']), 10)
def test_main_with_no_recource(self): params = { 'pathParameters': { 'user_id': 'A' * 30 }, 'queryStringParameters': { 'limit': '3' } } response = UsersArticlesPublic(params, {}, self.dynamodb).main() self.assertEqual(response['statusCode'], 200) self.assertEqual(json.loads(response['body'])['Items'], [])
def test_main_ok_with_draft_articles(self): table = self.dynamodb.Table('ArticleInfo') for i in range(10): # public,draft,draft,public,draft,draft,public,public,public,public # の順でステータスが検索されるようにテストデータを生成する status = 'public' if i % 3 == 0 or i < 4 else 'draft' table.put_item( Item={ 'user_id': 'public-test-user', 'article_id': 'test_limit_' + str(i), 'status': status, 'sort_key': 1520150273000000 + i }) params = { 'pathParameters': { 'user_id': 'public-test-user' }, 'queryStringParameters': { 'limit': '3' } } response = UsersArticlesPublic(params, {}, self.dynamodb).main() print(response) self.assertEqual(response['statusCode'], 200) self.assertEqual(len(json.loads(response['body'])['Items']), 3) expected_evaluated_key = { 'user_id': 'public-test-user', 'article_id': 'test_limit_3', 'sort_key': 1520150273000003 } self.assertEqual( json.loads(response['body'])['LastEvaluatedKey'], expected_evaluated_key)
def lambda_handler(event, context): users_articles_public = UsersArticlesPublic(event, context, dynamodb) return users_articles_public.main()
def assert_bad_request(self, params): function = UsersArticlesPublic(params, {}, self.dynamodb) response = function.main() self.assertEqual(response['statusCode'], 400)
def test_validation_with_no_query_params(self): params = {'pathParameters': {'user_id': 'TST'}} response = UsersArticlesPublic(params, {}, self.dynamodb).main() self.assertEqual(response['statusCode'], 200)