예제 #1
0
    def assert_bad_request(self, params):
        function = ArticlesRecent(params, {},
                                  dynamodb=self.dynamodb,
                                  elasticsearch=self.elasticsearch)
        response = function.main()

        self.assertEqual(response['statusCode'], 400)
예제 #2
0
    def test_main_ok_exists_blacklisted_users(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': '1'}}

        response = ArticlesRecent(params, {},
                                  dynamodb=self.dynamodb,
                                  elasticsearch=self.elasticsearch).main()

        expected_items = [{
            'article_id': 'testid000003',
            'status': 'public',
            'sort_key': 1565579800000003,
            'topic': 'fashion',
            'price': 300,
            'tip_value': 500,
            'user_id': 'test_user_id',
            'title': 'test_title',
            'overview': 'test_overview',
            'eye_catch_url': 'test_eye_catch_url',
            'created_at': 1565579800,
            'published_at': 1565579900,
            'tags': ['test1', 'test2'],
            'version': 2,
        }]

        self.assertEqual(response['statusCode'], 200)
        self.assertEqual(expected_items, json.loads(response['body'])['Items'])
예제 #3
0
    def test_main_ok(self):
        params = {'queryStringParameters': {'limit': '1'}}

        response = ArticlesRecent(params, {},
                                  dynamodb=self.dynamodb,
                                  elasticsearch=self.elasticsearch).main()

        expected_items = [{
            'article_id': 'testid000003',
            'status': 'public',
            'sort_key': 1565579800000003,
            'topic': 'fashion',
            'price': 300,
            'tip_value': 500,
            'user_id': 'test_user_id',
            'title': 'test_title',
            'overview': 'test_overview',
            'eye_catch_url': 'test_eye_catch_url',
            'created_at': 1565579800,
            'published_at': 1565579900,
            'tags': ['test1', 'test2'],
            'version': 2,
        }]

        self.assertEqual(response['statusCode'], 200)
        self.assertEqual(expected_items, json.loads(response['body'])['Items'])
예제 #4
0
    def test_main_ok_no_page(self):
        params = {'queryStringParameters': {'limit': '20', 'topic': 'food'}}
        response = ArticlesRecent(params, {},
                                  dynamodb=self.dynamodb,
                                  elasticsearch=self.elasticsearch).main()

        self.assertEqual(response['statusCode'], 200)
        self.assertEqual(len(json.loads(response['body'])['Items']), 20)
예제 #5
0
    def test_main_ok_with_no_limit(self):
        params = {'queryStringParameters': None}
        response = ArticlesRecent(params, {},
                                  dynamodb=self.dynamodb,
                                  elasticsearch=self.elasticsearch).main()

        self.assertEqual(response['statusCode'], 200)
        self.assertEqual(len(json.loads(response['body'])['Items']), 20)
예제 #6
0
    def test_call_validate_topic(self):
        params = {'queryStringParameters': {'topic': 'crypto'}}

        mock_lib = MagicMock()
        with patch('articles_recent.DBUtil', mock_lib):
            ArticlesRecent(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(self):
        params = {'queryStringParameters': {'limit': '1'}}

        response = ArticlesRecent(params, {}, self.dynamodb).main()

        expected_items = [{
            '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_ok_with_no_limit(self):
        table = TestArticlesRecent.dynamodb.Table(
            os.environ['ARTICLE_INFO_TABLE_NAME'])

        for i in range(21):
            table.put_item(
                Item={
                    'article_id': 'test_limit_number' + str(i),
                    'status': 'public',
                    'sort_key': 1520150273000000 + i
                })

        params = {'queryStringParameters': None}
        response = ArticlesRecent(params, {}, self.dynamodb).main()

        self.assertEqual(response['statusCode'], 200)
        self.assertEqual(len(json.loads(response['body'])['Items']), 20)
    def test_main_ok_search_by_topic(self):
        params = {'queryStringParameters': {'limit': '10', 'topic': 'crypto'}}

        response = ArticlesRecent(params, {},
                                  dynamodb=self.dynamodb,
                                  elasticsearch=self.elasticsearch).main()

        expected_items = [{
            'article_id': 'testid000002',
            'status': 'public',
            'sort_key': 1520150272000002,
            'topic': 'crypto'
        }, {
            'article_id': 'testid000001',
            'status': 'public',
            'sort_key': 1520150272000001,
            'topic': 'crypto'
        }]

        self.assertEqual(response['statusCode'], 200)
        self.assertEqual(json.loads(response['body'])['Items'], expected_items)
예제 #10
0
def lambda_handler(event, context):
    articles_recent = ArticlesRecent(event, context, dynamodb=dynamodb, elasticsearch=elasticsearch)
    return articles_recent.main()
예제 #11
0
def lambda_handler(event, context):
    articles_recent = ArticlesRecent(event, context, dynamodb)
    return articles_recent.main()