<commit_before>from django.test import TestCase
from hello.twitter_api import (
    TwitterCli,
    get_twitter_comments,
    json_into_table,
    save_tweets
)
import io
import json

try:
    from urllib.request import urlopen, Request
    from urllib.error import HTTPError
except ImportError:
    from urllib2 import urlopen, Request, HTTPError


with io.open("hello/tests/example_twit.json") as samplejson:
    SAMPLE_JSON = json.loads(samplejson.read())

EXPECTED_TWITTER = {
    "symbols": [],
    "author": "<NAME>",
    "hashtags": [
        "MSFT",
        "Build2016",
        "mountaindew",
        "bldwin",
        "BeverageFail",
        "HadToDrinkPepsi"
    ],
    "source": "twitter",
    "focus": "MSFT",
    "author_image": "http://pbs.twimg.com/profile_images/558825363619344386/gUN09sSf_normal.jpeg",
    "urls": [],
    "created_time": "2009-01-29T21:01:24Z",
    "popularity": 0,
    "content": "Hey #MSFT, I thought #Build2016 was a developer conference. Where's the #mountaindew? #bldwin #BeverageFail #HadToDrinkPepsi",
    "social_id": 19734130
}


class TwitterCase(TestCase):
    """Test Twitter API."""

    def test_json_to_table(self):
        """Test the corrected format from json to db."""
        jsonified = json_into_table(SAMPLE_JSON, "MSFT")
        self.assertEqual(EXPECTED_TWITTER, jsonified)

    def test_invalid_json(self):
        """Test passing in message with invalid ticker."""
        with self.assertRaises(ValueError):
            so_true = False
            json_into_table(SAMPLE_JSON, so_true)

    def test_saving_tweetdict(self):
        """Test if dict of tweets are saved."""
        self.assertTrue(save_tweets(EXPECTED_TWITTER))
        self.assertFalse(save_tweets(EXPECTED_TWITTER))
            
    def test_dne_ticker(self):
        """Test access without auth."""
        with self.assertRaises(HTTPError):
            get_twitter_comments("opwuirehe")

    def test_no_access(self):
        """Test acess without proper auth."""
        with self.assertRaises(HTTPError):
            client = TwitterCli("merp", "1333322")
            resp = client.request("https://api.twitter.com/1.1/search/tweets.json?q=%23MSFT")
            return resp
<commit_msg>modified tests to run with new fields, cleaned up. url and message must stay as one consistant string(cannot break to fit 80limit)<commit_after>from django.test import TestCase
from hello.twitter_api import (
    TwitterCli,
    get_twitter_comments,
    json_into_table,
    save_tweets
)
import io
import json

try:
    from urllib.error import HTTPError
except ImportError:
    from urllib2 import HTTPError


with io.open("hello/tests/example_twit.json") as samplejson:
    SAMPLE_JSON = json.loads(samplejson.read())

EXPECTED_TWITTER = {
    "symbols": [],
    "author": "<NAME>",
    "hashtags": [
        "MSFT",
        "Build2016",
        "mountaindew",
        "bldwin",
        "BeverageFail",
        "HadToDrinkPepsi"
    ],
    "source": "twitter",
    "focus": "MSFT",
    "author_image": "http://pbs.twimg.com/profile_images/558825363619344386/gUN09sSf_normal.jpeg",
    "urls": [],
    "url": "https://www.twitter.com/duanenewman/status/715241582597812224",
    "created_time": "2009-01-29T21:01:24Z",
    "popularity": 0,
    "content": "Hey #MSFT, I thought #Build2016 was a developer conference. Where's the #mountaindew? #bldwin #BeverageFail #HadToDrinkPepsi",
    "social_id": 19734130
}


class TwitterCase(TestCase):
    """Test Twitter API."""

    def test_json_to_table(self):
        """Test the corrected format from json to db."""
        jsonified = json_into_table(SAMPLE_JSON, "MSFT")
        self.assertEqual(EXPECTED_TWITTER, jsonified)

    def test_invalid_json(self):
        """Test passing in message with invalid ticker."""
        with self.assertRaises(ValueError):
            so_bad = {'potato': 'fries'}
            json_into_table(SAMPLE_JSON, so_bad)

    def test_saving_tweetdict(self):
        """Test if dict of tweets are saved."""
        self.assertTrue(save_tweets(EXPECTED_TWITTER))

    def test_dne_ticker(self):
        """Test invalid ticker return."""
        wrong = get_twitter_comments("opwuirehe")
        self.assertEqual(wrong, [])

    def test_get_access(self):
        """Test access to Twitter with correct auth."""
        access = get_twitter_comments("aapl")
        self.assertNotEqual(access, {})

    def test_no_access(self):
        """Test acess without proper auth."""
        with self.assertRaises(HTTPError):
            client = TwitterCli("merp", "1333322")
            resp = client.request("https://api.twitter.com/1.1/search/tweets.json?q=%23MSFT")
            return resp

{'fraction_non_alphanumeric': 0.1220136518771331, 'fraction_numerical': 0.03114334470989761, 'mean_word_length': 3.0916230366492146, 'pattern_counts': {'":': 23, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 3, 'lorem ipsum': 0, 'www.': 1, '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': '5257258', 'n_tokens_mistral': 1638, 'n_tokens_neox': 1529, 'n_words': 328}