<commit_before>from StringIO import StringIO
import json
from django.test import TestCase
from mock import patch


class ApiCallTest(TestCase):

    def setUp(self):
        import urllib
        self._urllib_mock = patch('MentionGraphs.firehose.crawl.urllib')
        mock_urllib = self._urllib_mock.start()
        self.mock_urlopen = mock_urllib.urlopen
        mock_urllib.urlencode = urllib.urlencode

    def tearDown(self):
        self._urllib_mock.stop()

    def set_api_response(self, mentions):
        data = {
            'count': len(mentions),
            'total': len(mentions),
            'results': mentions,
        }
        self.mock_urlopen.return_value = StringIO(json.dumps(data))

    def test_api_call(self):
        from crawl import do_api_call
        item1 = {
            'language': 'english',
            'generator': 'twitter',
            'sentiment': 'neutral',
            'published': 1315051701,
        }
        self.set_api_response([item1])

        mentions = list(do_api_call('python', 'twitter'))

        self.assertEqual(mentions, [item1])<commit_msg>factor out helper "_mention" constructor<commit_after>from StringIO import StringIO
import json
from django.test import TestCase
from mock import patch


def _mention(language, generator, sentiment, published):
    return {
        'language': language,
        'generator': generator,
        'sentiment': sentiment,
        'published': published,
    }

class ApiCallTest(TestCase):

    def setUp(self):
        import urllib
        self._urllib_mock = patch('MentionGraphs.firehose.crawl.urllib')
        mock_urllib = self._urllib_mock.start()
        self.mock_urlopen = mock_urllib.urlopen
        mock_urllib.urlencode = urllib.urlencode

    def tearDown(self):
        self._urllib_mock.stop()

    def set_api_response(self, mentions):
        data = {
            'count': len(mentions),
            'total': len(mentions),
            'results': mentions,
        }
        self.mock_urlopen.return_value = StringIO(json.dumps(data))

    def test_api_call(self):
        from crawl import do_api_call
        item1 = _mention('english', 'twitter', 'neutral', 1315051701)
        self.set_api_response([item1])

        mentions = list(do_api_call('python', 'twitter'))

        self.assertEqual(mentions, [item1])
{'fraction_non_alphanumeric': 0.10600858369098712, 'fraction_numerical': 0.011158798283261802, 'mean_word_length': 2.630841121495327, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4914822', 'n_tokens_mistral': 743, 'n_tokens_neox': 710, 'n_words': 141}