<commit_before>import os
import re
import markdown

from django.test import TestCase
from django.core.cache import cache

from mdx_twitter.extension import TWITTER_LINK_RE, get_cache_key

from httmock import urlmatch, HTTMock


class TestDjangoInterations(TestCase):

    def setUp(self):
        pass

    def test_django_caching(self):
        md = markdown.Markdown(extensions=['twitter'], safe_mode=True)
        key = get_cache_key('240192632003911681')
        html = md.convert('http://twitter.com/jasoncosta/statuses/240192632003911681')
        self.assertEqual(cache.get(key), html)

    def tearDown(self):
        [cache.delete(key) for key in cache._cache.keys()]


@urlmatch(netloc=r'(.*\.)?twitter\.com$')
def twitter_mock(url, request):
    return """{
        "html": "<blockquote class='twitter-tweet'><p>Need to plug in for a couple of hours? Here's the mix: 'NERO - Essential Mix (First broadcast Nov 2010)' - <a href='http://t.co/9MwHZCya' title='http://soundcloud.com/nerouk/nero-essential-mix-first?utm_source=soundcloud&amp;utm_campaign=share&amp;utm_medium=twitter&amp;utm_content=http://soundcloud.com/nerouk/nero-essential-mix-first'>soundcloud.com/nerouk/nero-es\\u2026</a></p>&mdash; <NAME> (@jasoncosta) <a href='https://twitter.com/jasoncosta/status/240192632003911681' data-datetime='2012-08-27T21:02:40+00:00'>August 27, 2012</a></blockquote>\\n<script src='//platform.twitter.com/widgets.js' charset='utf-8'></script>",
      "author_name": "<NAME>",
      "provider_url": "http://twitter.com",
      "url": "https://twitter.com/jasoncosta/status/240192632003911681",
      "provider_name": "Twitter",
      "version": "1.0",
      "type": "rich",
      "height": null,
      "cache_age": "31536000000",
      "author_url": "https://twitter.com/jasoncosta",
      "width": 550}"""


class TestMdxTwitter(TestCase):

    def setUp(self):
        pass

    def test_regex_1(self):
        m = re.match(TWITTER_LINK_RE, "https://twitter.com/jasoncosta/status/240192632003911681")
        self.assertEqual(m.group('tweet_id'), '240192632003911681')

    def test_regex_2(self):
        m = re.match(TWITTER_LINK_RE, "http://twitter.com/jasoncosta/statuses/240192632003911681")
        self.assertEqual(m.group('tweet_id'), '240192632003911681')

    def test_oembed(self):
        md = markdown.Markdown(extensions=['twitter'])

        with HTTMock(twitter_mock):<commit_msg>Fixed tests for Django caching to use httmock<commit_after>import os
import re
import markdown

from django.test import TestCase
from django.core.cache import cache

from mdx_twitter.extension import TWITTER_LINK_RE, get_cache_key

from httmock import urlmatch, HTTMock


@urlmatch(netloc=r'(.*\.)?twitter\.com$')
def twitter_mock(url, request):
    return """{
        "html": "<blockquote class='twitter-tweet'><p>Need to plug in for a couple of hours? Here's the mix: 'NERO - Essential Mix (First broadcast Nov 2010)' - <a href='http://t.co/9MwHZCya' title='http://soundcloud.com/nerouk/nero-essential-mix-first?utm_source=soundcloud&amp;utm_campaign=share&amp;utm_medium=twitter&amp;utm_content=http://soundcloud.com/nerouk/nero-essential-mix-first'>soundcloud.com/nerouk/nero-es\\u2026</a></p>&mdash; <NAME> (@jasoncosta) <a href='https://twitter.com/jasoncosta/status/240192632003911681' data-datetime='2012-08-27T21:02:40+00:00'>August 27, 2012</a></blockquote>\\n<script src='//platform.twitter.com/widgets.js' charset='utf-8'></script>",
      "author_name": "<NAME>",
      "provider_url": "http://twitter.com",
      "url": "https://twitter.com/jasoncosta/status/240192632003911681",
      "provider_name": "Twitter",
      "version": "1.0",
      "type": "rich",
      "height": null,
      "cache_age": "31536000000",
      "author_url": "https://twitter.com/jasoncosta",
      "width": 550}"""


class TestDjangoInterations(TestCase):

    def setUp(self):
        pass

    def test_django_caching(self):
        md = markdown.Markdown(extensions=['twitter'], safe_mode=True)
        key = get_cache_key('240192632003911681')
        with HTTMock(twitter_mock):
            html = md.convert('http://twitter.com/jasoncosta/statuses/240192632003911681')
            self.assertEqual(cache.get(key), html)

    def tearDown(self):
        cache.clear()


class TestMdxTwitter(TestCase):

    def setUp(self):
        pass

    def test_regex_1(self):
        m = re.match(TWITTER_LINK_RE, "https://twitter.com/jasoncosta/status/240192632003911681")
        self.assertEqual(m.group('tweet_id'), '240192632003911681')

    def test_regex_2(self):
        m = re.match(TWITTER_LINK_RE, "http://twitter.com/jasoncosta/statuses/240192632003911681")
        self.assertEqual(m.group('tweet_id'), '240192632003911681')

    def test_oembed(self):
        md = markdown.Markdown(extensions=['twitter'])

        with HTTMock(twitter_mock):
{'fraction_non_alphanumeric': 0.1552257266542981, 'fraction_numerical': 0.08080808080808081, 'mean_word_length': 4.0966386554621845, 'pattern_counts': {'":': 22, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 8, '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': '1723597', 'n_tokens_mistral': 1960, 'n_tokens_neox': 1666, 'n_words': 264}