<commit_before>"""Test functions for deliver_cute project."""
from __future__ import unicode_literals, absolute_import

import random
from string import ascii_letters, digits
from itertools import product, chain
from django.test import TestCase
from django.core import mail
from django.core.urlresolvers import reverse
from nose_parameterized import parameterized

from on_schedule import fix_image_links, htmlize_posts
from constants import SUBREDDIT_NAMES, LIMIT, EMAIL, UNICODE
from tests.classes import (
    FakePost,
    SubscriberFactory,
    SubRedditFactory,
    BATCH_SIZE,
    BATCH_PARAMS,
    # SUBR_BATCH_SIZE,
    # SUBR_PARAMS,
    SUBR_NAME_PARAMS,
)

# TODO
# test unicode status of incoming PRAW post objects
# test encoding of outgoing html
#   test fix_urls
#       make some bad urls
#   test sort_urls
#   test subscribers_for_now
#   test email outbox

# edge cases:
#   unsorted
#   already sorted
#   no subscribers
#   no subreddits
#   no posts
#   bad links
#   limit 0
#   very high limit


CUTE_POSTS = []
FIXED_LINKS = list(fix_image_links(CUTE_POSTS))
SUBJECT = 'DEBUG'
TEXT = 'Plain text message.'
BODY = '''
<html>
<h1>Test Header</h1>
<p>can't not apostrophe</p>
<img src="http://i.imgur.com/URw6C0c.jpg">
</html>
'''

PROTO = ('http://', 'https://')
DOMAIN = ('i.imgur.com/', )
HASH = ''.join(random.sample(ascii_letters + digits, 8))
EXT = ('.jpg', '.gifv', '.gif', '.png', )

BAD_DOMAIN = ('www.imgur.com/', 'reddit.com/', 'gfycat.com/')
BAD_EXT = ('', )

GOOD_URLS = product(PROTO, DOMAIN, HASH, EXT)
BAD_URLS = product(PROTO, BAD_DOMAIN, HASH, BAD_EXT)


class EmailCase(TestCase):
    """Testing sending of email."""

    def setUp(self):
        """Send an email."""
        mail.send_mail(
            SUBJECT, TEXT, EMAIL, [EMAIL],
            html_message=BODY,
            fail_silently=False,
        )

    def test_outbox(self):
        """Test sending an email."""
        self.assertEqual(len(mail.outbox), 1)

    def test_subject(self):
        """Test sending an email."""
        email = mail.outbox[0]
        self.assertEqual(SUBJECT, email.subject)


class RedditAPICase(TestCase):
    """Test retrieval of posts from reddit API."""

    def setUp(self):
        """Get post data to test."""
        from on_schedule import create_post_map
        # test with different limits
        self.no_posts = create_post_map([], LIMIT)
        self.all_posts = create_post_map(SUBREDDIT_NAMES, LIMIT)

    def test_no_subreddits(self):
        """Confirm that zero subreddits returns an empty dictionary."""
        self.assertEqual(self.no_posts, {})

    def test_dictionary(self):
        """Confirm that given subreddits returns a dictionary."""
        self.assertIsInstance(self.all_posts, dict)

    # @parameterized.expand(SUBR_NAME_PARAMS)
    # def test_cute_posts(self, name):
    #     """Test that gathered posts have links."""
    #     for post in self.all_posts[name]:
    #         self.assertTrue(post.url.startswith('http'))

    # @parameterized.expand(SUBR_NAME_PARAMS)
    # def test_cute_posts_count(self, name):
    #     """Test that number of links is at or under the limit per subreddit."""
    #     self.assertLessEqual(len(self.all_posts[name]), LIMIT)

FAKE_POSTS = FakePost.create_batch(BATCH_SIZE)
# FAKE_POST_ARGS = ((p, ) for p in FAKE_POSTS)
FAKE_POST_ATTRS = ((attr, ) for attr in chain(
    *((p.title, p.url, p.permalink, p.subreddit.display_name)
      for p in FAKE_POSTS)))
# HTMLIZED_POSTS = list(htmlize_posts(FAKE_POSTS))
# FAKE_HTMLIZED_POSTS = zip(FAKE_POSTS, HTMLIZED_POSTS)


class FakePostsCase(TestCase):
    """Using fake posts to test unicode and html escaping."""

    def __init__(self, *args, **kwargs):
        """Initialize data groups."""
        super(FakePostsCase, self).__init__(*args, **kwargs)
        self.fake_posts = FakePost.create_batch(BATCH_SIZE)
        self.htmlized_posts = list(htmlize_posts(self.fake_posts))

    # def setUp(self):
    #     """Set up fake_posts."""
    #     from on_schedule import get_email_body
    #     self.subscriber = SubscriberFactory.create(email=EMAIL)
    #     self.email_body = get_email_body(self.subscriber, HTMLIZED_POSTS)
    #     # self.duplicates = FakePost.create_batch_with_dupes(BATCH_SIZE)

    @parameterized.expand(BATCH_PARAMS)
    def test_doubletest(self, idx):
        """Check if setting stuff up in __init__ works."""
        post = self.fake_posts[idx]
        self.assertIsInstance(post.title, UNICODE)

    # @parameterized.expand(FAKE_POST_ATTRS)
    # def test_unicode_attrs(self, attr):
    #     """Ensure that all FakePost attributes are unicode."""
    #     self.assertIsInstance(attr, UNICODE)

    @parameterized.expand(BATCH_PARAMS)
    def test_htmlize_unicode(self, idx):
        """Test that htmlize runs without breaking."""
        htmlized_post = self.htmlized_posts[idx]
        self.assertIsInstance(htmlized_post, UNICODE)

    @parameterized.expand(BATCH_PARAMS)
    def test_html_post_contains_title(self, idx):
        """Ensure that all FakePost attributes in htmlized version."""
        post = self.fake_posts[idx]
        htmlized_post = self.htmlized_posts[idx]
        self.assertIn(post.title, htmlized_post)

    @parameterized.expand(BATCH_PARAMS)
    def test_html_post_contains_url(self, idx):
        """Ensure that all FakePost attributes in htmlized version."""
        post = self.fake_posts[idx]
        htmlized_post = self.htmlized_posts[idx]
        self.assertIn(post.url, htmlized_post)

    @parameterized.expand(BATCH_PARAMS)
    def test_html_post_contains_permalink(self, idx):
        """Ensure that all FakePost attributes in htmlized version."""
        post = self.fake_posts[idx]
        htmlized_post = self.htmlized_posts[idx]
        self.assertIn(post.permalink, htmlized_post)

    @parameterized.expand(BATCH_PARAMS)
    def test_html_post_contains_subreddit(self, idx):
        """Ensure that all FakePost attributes in htmlized version."""
        post = self.fake_posts[idx]
        htmlized_post = self.htmlized_posts[idx]
        self.assertIn(post.subreddit.display_name, htmlized_post)

    # @parameterized.expand(FAKE_HTMLIZED_POSTS)
    # def test_html_body_contains(self, post, htmlized_post):
    #     """Ensure that htmlized posts are contained by email body."""
    #     self.assertIn(htmlized_post, self.email_body)

    # def test_html_body_unsub_link(self):
    #     """Ensure that all FakePost attributes are unicode."""
    #     unsub_url = reverse(
    #         'unsubscribe',
    #         args=(self.subscriber.unsubscribe_hash, )
    #     )
    #     self.assertIn(unsub_url, self.email_body)

    # def test_dedupe_posts(self):
    #     """Test that urls of deduped posts is equal to set of those urls."""
    #     from on_schedule import dedupe_posts
    #     deduped_urls = [post.url for post in dedupe_posts(self.duplicates)]
    #     self.assertEqual(
    #         list(sorted(deduped_urls)),
    #         list(sorted(set(deduped_urls)))
    #     )

    # test html escaping by checking for &quot, \u2018 etc.


class CheckURLCase(TestCase):
    """Test URL matching regexes."""

    def setUp(self):<commit_msg>Passing tests for parameterizing testcase attrs used in __init__()<commit_after>"""Test functions for deliver_cute project."""
from __future__ import unicode_literals, absolute_import

import random
from string import ascii_letters, digits
from itertools import product, chain
from django.test import TestCase
from django.core import mail
from django.core.urlresolvers import reverse
from nose_parameterized import parameterized

from on_schedule import fix_image_links
from constants import SUBREDDIT_NAMES, LIMIT, EMAIL, UNICODE
from tests.classes import (
    FakePost,
    SubscriberFactory,
    SubRedditFactory,
    BATCH_SIZE,
    BATCH_PARAMS,
    # SUBR_BATCH_SIZE,
    # SUBR_PARAMS,
    SUBR_NAME_PARAMS,
)

# TODO
# test unicode status of incoming PRAW post objects
# test encoding of outgoing html
#   test fix_urls
#       make some bad urls
#   test sort_urls
#   test subscribers_for_now
#   test email outbox

# edge cases:
#   unsorted
#   already sorted
#   no subscribers
#   no subreddits
#   no posts
#   bad links
#   limit 0
#   very high limit


CUTE_POSTS = []
FIXED_LINKS = list(fix_image_links(CUTE_POSTS))
SUBJECT = 'DEBUG'
TEXT = 'Plain text message.'
BODY = '''
<html>
<h1>Test Header</h1>
<p>can't not apostrophe</p>
<img src="http://i.imgur.com/URw6C0c.jpg">
</html>
'''

PROTO = ('http://', 'https://')
DOMAIN = ('i.imgur.com/', )
HASH = ''.join(random.sample(ascii_letters + digits, 8))
EXT = ('.jpg', '.gifv', '.gif', '.png', )

BAD_DOMAIN = ('www.imgur.com/', 'reddit.com/', 'gfycat.com/')
BAD_EXT = ('', )

GOOD_URLS = product(PROTO, DOMAIN, HASH, EXT)
BAD_URLS = product(PROTO, BAD_DOMAIN, HASH, BAD_EXT)


class EmailCase(TestCase):
    """Testing sending of email."""

    def setUp(self):
        """Send an email."""
        mail.send_mail(
            SUBJECT, TEXT, EMAIL, [EMAIL],
            html_message=BODY,
            fail_silently=False,
        )

    def test_outbox(self):
        """Test sending an email."""
        self.assertEqual(len(mail.outbox), 1)

    def test_subject(self):
        """Test sending an email."""
        email = mail.outbox[0]
        self.assertEqual(SUBJECT, email.subject)


class RedditAPICase(TestCase):
    """Test retrieval of posts from reddit API."""

    def setUp(self):
        """Get post data to test."""
        from on_schedule import create_post_map
        # test with different limits
        self.no_posts = create_post_map([], LIMIT)
        self.all_posts = create_post_map(SUBREDDIT_NAMES, LIMIT)

    def test_no_subreddits(self):
        """Confirm that zero subreddits returns an empty dictionary."""
        self.assertEqual(self.no_posts, {})

    def test_dictionary(self):
        """Confirm that given subreddits returns a dictionary."""
        self.assertIsInstance(self.all_posts, dict)

    # @parameterized.expand(SUBR_NAME_PARAMS)
    # def test_cute_posts(self, name):
    #     """Test that gathered posts have links."""
    #     for post in self.all_posts[name]:
    #         self.assertTrue(post.url.startswith('http'))

    # @parameterized.expand(SUBR_NAME_PARAMS)
    # def test_cute_posts_count(self, name):
    #     """Test that number of links is at or under the limit per subreddit."""
    #     self.assertLessEqual(len(self.all_posts[name]), LIMIT)

FAKE_POSTS = FakePost.create_batch(BATCH_SIZE)
# FAKE_POST_ARGS = ((p, ) for p in FAKE_POSTS)
FAKE_POST_ATTRS = ((attr, ) for attr in chain(
    *((p.title, p.url, p.permalink, p.subreddit.display_name)
      for p in FAKE_POSTS)))
# HTMLIZED_POSTS = list(htmlize_posts(FAKE_POSTS))
# FAKE_HTMLIZED_POSTS = zip(FAKE_POSTS, HTMLIZED_POSTS)


class FakePostsCase(TestCase):
    """Using fake posts to test unicode and html escaping."""

    def __init__(self, *args, **kwargs):
        """Initialize data groups."""
        from on_schedule import htmlize_posts
        super(FakePostsCase, self).__init__(*args, **kwargs)
        self.fake_posts = FakePost.create_batch(BATCH_SIZE)
        self.htmlized_posts = list(htmlize_posts(self.fake_posts))

    def setUp(self):
        """Set up fake_posts."""
        from on_schedule import get_email_body
        self.subscriber = SubscriberFactory.create(email=EMAIL)
        self.email_body = get_email_body(self.subscriber, self.htmlized_posts)
    #     self.email_body = get_email_body(self.subscriber, HTMLIZED_POSTS)
    #     # self.duplicates = FakePost.create_batch_with_dupes(BATCH_SIZE)

    @parameterized.expand(BATCH_PARAMS)
    def test_doubletest(self, idx):
        """Check if setting stuff up in __init__ works."""
        post = self.fake_posts[idx]
        self.assertIsInstance(post.title, UNICODE)

    # @parameterized.expand(FAKE_POST_ATTRS)
    # def test_unicode_attrs(self, attr):
    #     """Ensure that all FakePost attributes are unicode."""
    #     self.assertIsInstance(attr, UNICODE)

    @parameterized.expand(BATCH_PARAMS)
    def test_htmlize_unicode(self, idx):
        """Test that htmlize runs without breaking."""
        htmlized_post = self.htmlized_posts[idx]
        self.assertIsInstance(htmlized_post, UNICODE)

    @parameterized.expand(BATCH_PARAMS)
    def test_html_post_contains_title(self, idx):
        """Ensure that all FakePost attributes in htmlized version."""
        post = self.fake_posts[idx]
        htmlized_post = self.htmlized_posts[idx]
        self.assertIn(post.title, htmlized_post)

    @parameterized.expand(BATCH_PARAMS)
    def test_html_post_contains_url(self, idx):
        """Ensure that all FakePost attributes in htmlized version."""
        post = self.fake_posts[idx]
        htmlized_post = self.htmlized_posts[idx]
        self.assertIn(post.url, htmlized_post)

    @parameterized.expand(BATCH_PARAMS)
    def test_html_post_contains_permalink(self, idx):
        """Ensure that all FakePost attributes in htmlized version."""
        post = self.fake_posts[idx]
        htmlized_post = self.htmlized_posts[idx]
        self.assertIn(post.permalink, htmlized_post)

    @parameterized.expand(BATCH_PARAMS)
    def test_html_post_contains_subreddit(self, idx):
        """Ensure that all FakePost attributes in htmlized version."""
        post = self.fake_posts[idx]
        htmlized_post = self.htmlized_posts[idx]
        self.assertIn(post.subreddit.display_name, htmlized_post)

    @parameterized.expand(BATCH_PARAMS)
    def test_html_body_contains(self, idx):
        """Ensure that htmlized posts are contained by email body."""
        htmlized_post = self.htmlized_posts[idx]
        self.assertIn(htmlized_post, self.email_body)

    def test_html_body_unsub_link(self):
        """Ensure that all FakePost attributes are unicode."""
        hash_ = self.subscriber.unsubscribe_hash
        unsub_url = reverse('unsubscribe', args=(hash_, ))
        self.assertIn(unsub_url, self.email_body)

    # def test_dedupe_posts(self):
    #     """Test that urls of deduped posts is equal to set of those urls."""
    #     from on_schedule import dedupe_posts
    #     deduped_urls = [post.url for post in dedupe_posts(self.duplicates)]
    #     self.assertEqual(
    #         list(sorted(deduped_urls)),
    #         list(sorted(set(deduped_urls)))
    #     )

    # test html escaping by checking for &quot, \u2018 etc.


class CheckURLCase(TestCase):
    """Test URL matching regexes."""

    def setUp(self):
{'fraction_non_alphanumeric': 0.1111111111111111, 'fraction_numerical': 0.001651186790505676, 'mean_word_length': 3.3599280143971204, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '1641728', 'n_tokens_mistral': 4810, 'n_tokens_neox': 4722, 'n_words': 1085}