예제 #1
0
def test_get_top_stories_with_comments():
    """Retrieves the top stories with all its direct kids/comments"""

    def get_top_stories(*args):
        return [{'id': 1, 'kids': [3]}, {'id': 2, 'kids': [4, 5]}]

    def get_kids(*args):
        if args[0] == {'id': 1, 'kids': [3]}:
            return [{'id': 3}]
        elif args[0] == {'id': 2, 'kids': [4, 5]}:
            return [{'id': 4}, {'id': 5}]

    api_client = APIClient()
    api_client.get_top_stories = Mock(side_effect=get_top_stories)
    api_client.get_kids = Mock(side_effect=get_kids)
    result = api_client.get_top_stories_with_comments(2) # get two top stories
    stories = result['stories']
    comments = result['comments']
    stories_ids = [story['id'] for story in stories]
    stories_ids.sort()
    comments_ids = [comment['id'] for comment in comments]
    comments_ids.sort()

    assert set(['comments', 'stories']) == set(result.keys())
    assert stories_ids == [1, 2]
    assert comments_ids == [3, 4, 5]
예제 #2
0
def get_top_stories_with_comments():
    api_client = APIClient()
    db_wrapper = DatabaseWrapper()
    json_response = api_client.get_top_stories_with_comments()
    db_wrapper.upsert_stories(json_response['stories'])
    db_wrapper.upsert_comments(json_response['comments'])