<commit_before>import datetime

import django.utils.timezone

from grouprise.core import tests
from grouprise.core.tests import temporary_settings_override
from grouprise.features.associations import models as associations
from grouprise.features.memberships.test_mixins import MemberMixin, OtherMemberMixin
from ..management.commands.import_feeds import (
    import_from_feed, parse_feed_url_from_website_content)


FEED_LINK_EXAMPLES = (
    ('<link rel="alternate" type="application/rss+xml" title="<NAME> &raquo; Feed" '
     'href="https://peterweisshaus.de/feed/" />"', 'https://peterweisshaus.de/feed/'),
)

FEED_CONTENT_TEMPLATE = (
    '<?xml version="1.0" encoding="UTF-8"?><rss version="2.0">'
    '<channel><title>Channel Title</title><link>https://example.org</link>'
    '<item><title>{title}</title><link>{url}</link><pubDate>{date}</pubDate>'
    '<description>{description}</description><content>Some Content</content>'
    '</item></channel></rss>')


class DetectFeedURL(tests.Test):

    def test_references(self):
        for snippet, feed_url in FEED_LINK_EXAMPLES:
            with self.subTest(feed_url=feed_url):
                self.assertEqual(parse_feed_url_from_website_content(snippet), feed_url)


class ImportFeedItems(MemberMixin, OtherMemberMixin, tests.Test):

    FEED_DEFAULTS = {
        "title": "First Title", "url": "https://example.org/1/2",
        "date": "Tue, 05 Jun 2018 07:55:15 +0000", "description": "Some Description",
        "content": "Some Content",
    }

    def _get_now(self):
        tz = django.utils.timezone.get_current_timezone()
        return datetime.datetime.now(tz=tz)

    def _get_feed_content(self, **kwargs):
        data = dict(self.FEED_DEFAULTS)
        data.update(kwargs)
        if "date" not in data:
            data["date"] = self._get_now()
        if isinstance(data["date"], datetime.datetime):
            data["date"] = data["date"].strftime("%a, %d %b %Y %H:%M:%S %z").strip()
        return FEED_CONTENT_TEMPLATE.format(**data)

    def test_content(self):
        now = self._get_now()
        import_from_feed(self._get_feed_content(date=now), self.gestalt, self.group)
        # TODO: check timezone correctness ("hour" is omitted below)
        self.assertExists(
            associations.Association,
            content__title="First Title",
            content__versions__time_created__year=now.year,
            content__versions__time_created__month=now.month,
            content__versions__time_created__day=now.day,
            content__versions__time_created__minute=now.minute,
            content__versions__time_created__second=now.second,
        )

    def test_ignore_duplicates(self):
        import_from_feed(self._get_feed_content(), self.gestalt, self.group)
        self.assertEqual(associations.Association.objects.count(), 1)
        import_from_feed(self._get_feed_content(), self.gestalt, self.group)
        self.assertEqual(associations.Association.objects.count(), 1)

    def test_notifications_all(self):
        with temporary_settings_override("FEED_IMPORTER_GESTALT_ID", self.other_gestalt.id):
            self.assertEqual(associations.Association.objects.count(), 0)
            # send a "new" (recent date) feed entry: otherwise its notification is skipped
            import_from_feed(self._get_feed_content(date=self._get_now()),
                             self.gestalt, self.group)
            self.assertEqual(associations.Association.objects.count(), 1)
            self.assertNotificationRecipient(self.gestalt)
            # the "other_gestalt" receives no notifications (see FEED_IMPORTER_GESTALT_ID)<commit_msg>tests: fix date and time comparisons in feed tests<commit_after>import datetime

import django.utils.timezone

from grouprise.core import tests
from grouprise.core.tests import temporary_settings_override
from grouprise.features.associations import models as associations
from grouprise.features.memberships.test_mixins import MemberMixin, OtherMemberMixin
from ..management.commands.import_feeds import (
    import_from_feed, parse_feed_url_from_website_content)


FEED_LINK_EXAMPLES = (
    ('<link rel="alternate" type="application/rss+xml" title="<NAME> &raquo; Feed" '
     'href="https://peterweisshaus.de/feed/" />"', 'https://peterweisshaus.de/feed/'),
)

FEED_CONTENT_TEMPLATE = (
    '<?xml version="1.0" encoding="UTF-8"?><rss version="2.0">'
    '<channel><title>Channel Title</title><link>https://example.org</link>'
    '<item><title>{title}</title><link>{url}</link><pubDate>{date}</pubDate>'
    '<description>{description}</description><content>Some Content</content>'
    '</item></channel></rss>')


class DetectFeedURL(tests.Test):

    def test_references(self):
        for snippet, feed_url in FEED_LINK_EXAMPLES:
            with self.subTest(feed_url=feed_url):
                self.assertEqual(parse_feed_url_from_website_content(snippet), feed_url)


class ImportFeedItems(MemberMixin, OtherMemberMixin, tests.Test):

    FEED_DEFAULTS = {
        "title": "First Title", "url": "https://example.org/1/2",
        "date": "Tue, 05 Jun 2018 07:55:15 +0000", "description": "Some Description",
        "content": "Some Content",
    }

    def _get_now(self):
        tz = django.utils.timezone.get_current_timezone()
        # remove microseconds, since sqlite uses stores only seconds
        return datetime.datetime.now(tz=tz).replace(microsecond=0)

    def _get_feed_content(self, **kwargs):
        data = dict(self.FEED_DEFAULTS)
        data.update(kwargs)
        if "date" not in data:
            data["date"] = self._get_now()
        if isinstance(data["date"], datetime.datetime):
            data["date"] = data["date"].strftime("%a, %d %b %Y %H:%M:%S %z").strip()
        return FEED_CONTENT_TEMPLATE.format(**data)

    def test_content(self):
        now = self._get_now()
        import_from_feed(self._get_feed_content(date=now), self.gestalt, self.group)
        # compare date and time (timezone aware) separately in order to ignore different timezones
        now_utc = now.astimezone(datetime.timezone.utc)
        self.assertExists(
            associations.Association,
            content__title="First Title",
            content__versions__time_created__date=now_utc.date(),
            content__versions__time_created__time=now_utc.time(),
        )

    def test_ignore_duplicates(self):
        import_from_feed(self._get_feed_content(), self.gestalt, self.group)
        self.assertEqual(associations.Association.objects.count(), 1)
        import_from_feed(self._get_feed_content(), self.gestalt, self.group)
        self.assertEqual(associations.Association.objects.count(), 1)

    def test_notifications_all(self):
        with temporary_settings_override("FEED_IMPORTER_GESTALT_ID", self.other_gestalt.id):
            self.assertEqual(associations.Association.objects.count(), 0)
            # send a "new" (recent date) feed entry: otherwise its notification is skipped
            import_from_feed(self._get_feed_content(date=self._get_now()),
                             self.gestalt, self.group)
            self.assertEqual(associations.Association.objects.count(), 1)
            self.assertNotificationRecipient(self.gestalt)
            # the "other_gestalt" receives no notifications (see FEED_IMPORTER_GESTALT_ID)
{'fraction_non_alphanumeric': 0.12355264950279253, 'fraction_numerical': 0.007492167279662171, 'mean_word_length': 3.623425692695214, 'pattern_counts': {'":': 10, '<': 49, '<?xml version=': 2, '>': 49, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '6951213', 'n_tokens_mistral': 2277, 'n_tokens_neox': 2180, 'n_words': 385}