def test_copy_story_to_new_medium_with_download_error():
    """Test copy_story_to_new_medium with an associated download error."""
    db = connect_to_db()

    topic = create_test_topic(db, 'copy foo')

    new_medium = create_test_medium(db, 'copy new')

    old_medium = create_test_medium(db, 'copy old')
    old_feed = create_test_feed(db=db, label='copy old', medium=old_medium)
    old_story = create_test_story(db=db, label='copy old', feed=old_feed)

    add_content_to_test_story(db, old_story, old_feed)

    db.query("update downloads set state = 'error' where stories_id = %(a)s", {'a': old_story['stories_id']})

    add_to_topic_stories(db, old_story, topic)

    new_story = copy_story_to_new_medium(db, topic, old_story, new_medium)

    assert db.find_by_id('stories', new_story['stories_id']) is not None

    new_download = db.query(
        "select * from downloads where stories_id = %(a)s",
        {'a': new_story['stories_id']}).hash()
    assert new_download is not None
    assert new_download['state'] == 'error'
def test_copy_story_to_new_medium():
    """Test copy_story_to_new_medium."""
    db = connect_to_db()

    topic = create_test_topic(db, 'copy foo')

    new_medium = create_test_medium(db, 'copy new')

    old_medium = create_test_medium(db, 'copy old')
    old_feed = create_test_feed(db=db, label='copy old', medium=old_medium)
    old_story = create_test_story(db=db, label='copy old', feed=old_feed)

    add_content_to_test_story(db, old_story, old_feed)

    add_to_topic_stories(db, old_story, topic)

    new_story = copy_story_to_new_medium(db, topic, old_story, new_medium)

    assert db.find_by_id('stories', new_story['stories_id']) is not None

    for field in 'title url guid publish_date'.split():
        assert old_story[field] == new_story[field]

    topic_story_exists = db.query("""
        SELECT *
        FROM topic_stories
        WHERE
            topics_id = %(topics_id)s AND
            stories_id = %(stories_id)s
    """, {
        'topics_id': topic['topics_id'],
        'stories_id': new_story['stories_id'],
    }).hash()
    assert topic_story_exists is not None

    new_download = db.query("""
        SELECT *
        FROM downloads
        WHERE stories_id = %(stories_id)s
    """, {
        'stories_id': new_story['stories_id'],
    }).hash()
    assert new_download is not None

    content = fetch_content(db, new_download)
    assert content is not None and len(content) > 0

    story_sentences = db.query("""
        SELECT *
        FROM story_sentences
        WHERE stories_id = %(stories_id)s
    """, {
        'stories_id': new_story['stories_id'],
    }).hashes()
    assert len(story_sentences) > 0
Example #3
0
    def setUp(self):
        """Add AP medium and some content so that we can find dup sentences."""
        super().setUp()

        ap_medium = create_test_medium(db=self.db(), label=get_ap_medium_name())
        feed = create_test_feed(db=self.db(), label='feed', medium=ap_medium)
        story = create_test_story(db=self.db(), label='story', feed=feed)

        story['content'] = "\n".join(self.__get_ap_sentences())

        add_content_to_test_story(db=self.db(), story=story, feed=feed)
Example #4
0
    def setUp(self):
        """Add AP medium and some content so that we can find dup sentences."""
        super().setUp()

        ap_medium = create_test_medium(db=self.db(),
                                       label=get_ap_medium_name())
        feed = create_test_feed(db=self.db(), label='feed', medium=ap_medium)
        story = create_test_story(db=self.db(), label='story', feed=feed)

        story['content'] = "\n".join(self.__get_ap_sentences())

        add_content_to_test_story(db=self.db(), story=story, feed=feed)
Example #5
0
def __is_syndicated(db: DatabaseHandler, content: str) -> bool:
    label = content[:64]

    medium = create_test_medium(db=db, label=label)
    feed = create_test_feed(db=db, label=label, medium=medium)
    story = create_test_story(db=db, label=label, feed=feed)

    story['content'] = content

    story = add_content_to_test_story(db=db, story=story, feed=feed)

    return is_syndicated(db=db, story_title=story['title'], story_text=content)
Example #6
0
    def __is_syndicated(self, content: str) -> bool:

        label = content[:64]

        medium = create_test_medium(db=self.db(), label=label)
        feed = create_test_feed(db=self.db(), label=label, medium=medium)
        story = create_test_story(db=self.db(), label=label, feed=feed)

        story['content'] = content

        story = add_content_to_test_story(db=self.db(), story=story, feed=feed)

        return is_syndicated(db=self.db(), story_title=story['title'], story_text=content)
Example #7
0
def test_ap_calls():
    db = connect_to_db()

    ap_medium = create_test_medium(db=db, label=get_ap_medium_name())
    feed = create_test_feed(db=db, label='feed', medium=ap_medium)
    story = create_test_story(db=db, label='story', feed=feed)

    story['content'] = "\n".join(AP_SENTENCES)

    add_content_to_test_story(db=db, story=story, feed=feed)

    ap_content_single_16_sentence = None
    ap_content_32_sentences = []

    for sentence in AP_SENTENCES:

        if ap_content_single_16_sentence is None and len(sentence) < 32:
            ap_content_single_16_sentence = sentence

        if len(sentence) > 32:
            ap_content_32_sentences.append(sentence)

    assert ap_content_single_16_sentence is not None
    assert len(ap_content_32_sentences) > 0

    ap_content_single_32_sentence = ap_content_32_sentences[0]

    assert __is_syndicated(db=db, content='foo') is False, "Simple unsyndicated story"
    assert __is_syndicated(db=db, content='(ap)') is True, "Simple ('ap') pattern"

    assert __is_syndicated(db=db, content="associated press") is False, "Only 'associated press'"
    assert __is_syndicated(db=db, content="'associated press'") is True, "Quoted 'associated press'"

    assert __is_syndicated(
        db=db,
        content="associated press.\n" + ap_content_single_32_sentence
    ) is True, "Associated press and AP sentence"
    assert __is_syndicated(
        db=db,
        content="associated press.\n" + ap_content_single_16_sentence
    ) is False, "Associated press and short AP sentence"

    assert __is_syndicated(
        db=db,
        content=ap_content_single_32_sentence
    ) is False, 'Single AP sentence'

    assert __is_syndicated(
        db=db,
        content="Boston (AP)\n" + ap_content_single_32_sentence
    ) is True, 'AP sentence and AP location'

    assert __is_syndicated(
        db=db,
        content=' '.join(AP_SENTENCES)
    ) is True, 'All AP sentences'

    assert is_syndicated(db=db, story_text='foo') is False, "No DB story: simple story"

    assert is_syndicated(db=db, story_text='(ap)') is True, "No DB story: ('ap') story"

    assert is_syndicated(
        db=db,
        story_text=' '.join(AP_SENTENCES),
    ) is True, "No DB story: AP sentences"