<commit_before>from tests import feedev
from feedplatform.db import Item
from feedplatform.lib import guid_by_content

ADDINS = [guid_by_content(prefix='cnt::', fields=('title', 'link'),
                          allow_empty=False)]

class TestFeed(feedev.Feed):
    content = """
        <rss><channel>
            <item>
                <title>Foo</title>
                {% 2 %}<description>The foo item.</description>{% end %}
            </item>

            <item>
                <title>Bar</title>
                {% <3 %}<link>http://example.org/posts/bar</link>{% end %}
                {% 3 %}<link>http://example.org/posts/bar-fixed</link>{% end %}
            </item>

            <item>
                {% <4 %}<title>Good item with an id</title>{% end %}
                {% 4 %}<title>Good item with a guid</title>{% end %}
                <guid>123456</guid>
            </item>

            {% 5 %}
            <item></item>
            {% end %}
        </channel></rss>
    """

    def pass1(feed):
        # found three items
        assert feed.items.count() == 3

        # the guids generated by our addin should have the requested prefix
        assert any([item.guid.startswith('cnt::') for item in feed.items])

    def pass2(feed):
        # the change in "foo" was detected correctly
        assert feed.items.count() == 3

    def pass3(feed):
        # the change in "bar" was not, since <link> is in the guid.
        # thus, we pick up a duplicate.
        assert feed.items.count() == 4

    def pass4(feed):
        # the third item is using a guid; thus, the change is detected
        assert feed.items.count() == 4

        # we also want to make sure that the <guid> element is indeed
        # use, i.e. preferred over the content hash.
        assert feed.items.find(Item.guid == u'123456').count() == 1

    def pass5(feed):
        # guids are not generated for empty content bases,
        # so no change in item count
        assert feed.items.count() == 4

        # now allow it
        ADDINS[0].allow_empty = True

    def pass6(feed):
        # now the last item is picked up, because a guid is generated
        assert feed.items.count() == 5

def test():
    feedev.testmod()<commit_msg>try to avoid keeping a direct reference to models, in case they are generated (we don't have a good solution for that yet); in this particular instance it's unlikely to make a difference, though<commit_after>from tests import feedev
from feedplatform import db
from feedplatform.lib import guid_by_content

ADDINS = [guid_by_content(prefix='cnt::', fields=('title', 'link'),
                          allow_empty=False)]

class TestFeed(feedev.Feed):
    content = """
        <rss><channel>
            <item>
                <title>Foo</title>
                {% 2 %}<description>The foo item.</description>{% end %}
            </item>

            <item>
                <title>Bar</title>
                {% <3 %}<link>http://example.org/posts/bar</link>{% end %}
                {% 3 %}<link>http://example.org/posts/bar-fixed</link>{% end %}
            </item>

            <item>
                {% <4 %}<title>Good item with an id</title>{% end %}
                {% 4 %}<title>Good item with a guid</title>{% end %}
                <guid>123456</guid>
            </item>

            {% 5 %}
            <item></item>
            {% end %}
        </channel></rss>
    """

    def pass1(feed):
        # found three items
        assert feed.items.count() == 3

        # the guids generated by our addin should have the requested prefix
        assert any([item.guid.startswith('cnt::') for item in feed.items])

    def pass2(feed):
        # the change in "foo" was detected correctly
        assert feed.items.count() == 3

    def pass3(feed):
        # the change in "bar" was not, since <link> is in the guid.
        # thus, we pick up a duplicate.
        assert feed.items.count() == 4

    def pass4(feed):
        # the third item is using a guid; thus, the change is detected
        assert feed.items.count() == 4

        # we also want to make sure that the <guid> element is indeed
        # use, i.e. preferred over the content hash.
        # TODO: fails, but also move somehwere else?
        assert feed.items.find(db.Item.guid == u'123456').count() == 1


    def pass5(feed):
        # guids are not generated for empty content bases,
        # so no change in item count
        assert feed.items.count() == 4

        # now allow it
        ADDINS[0].allow_empty = True

    def pass6(feed):
        # now the last item is picked up, because a guid is generated
        assert feed.items.count() == 5

def test():
    feedev.testmod()
{'fraction_non_alphanumeric': 0.12714922521757588, 'fraction_numerical': 0.013585226066652516, 'mean_word_length': 1.8784361637141112, 'pattern_counts': {'":': 0, '<': 67, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1205841', 'n_tokens_mistral': 1422, 'n_tokens_neox': 1300, 'n_words': 445}