<commit_before># -*- coding: utf-8 -*-

from __future__ import absolute_import

import mock

from celery.task import Task
from sentry.models import Event
from sentry.tasks.fetch_source import fetch_javascript_source
from sentry.testutils import TestCase


class StoreEventTest(TestCase):
    def test_is_task(self):
        self.assertTrue(isinstance(fetch_javascript_source, Task))

    @mock.patch('sentry.models.Event.save')
    @mock.patch('urllib2.build_opener')
    def test_calls_from_kwargs(self, build_opener, save):
        event = Event(data={
            'sentry.interfaces.Stacktrace': {
                'frames': [
                    {
                        'abs_path': 'http://example.com/foo.js',
                        'filename': 'foo.js',
                        'lineno': 4,
                        'colno': 0,
                    },
                    {
                        'abs_path': 'http://example.com/foo.js',
                        'filename': 'foo.js',
                        'lineno': 1,
                        'colno': 0,
                    },
                ],
            },
        })
        build_opener.return_value.open.return_value.read.return_value = '\n'.join('hello world')

        fetch_javascript_source(event)

        build_opener.assert_called_once_with()
        build_opener.return_value.open.assert_called_once_with('http://example.com/foo.js')
        build_opener.return_value.open.return_value.read.assert_called_once_with()
        save.assert_called_once_with()

        frame_list = event.data['sentry.interfaces.Stacktrace']['frames']
        frame = frame_list[0]
        assert frame['pre_context'] == ['h', 'e', 'l']
        assert frame['context_line'] == 'l'
        assert frame['post_context'] == ['o', ' ', 'w', 'o', 'r']

        frame = frame_list[1]
        assert frame['pre_context'] == []
        assert frame['context_line'] == 'h'
        assert frame['post_context'] == ['e', 'l', 'l', 'o', ' ']<commit_msg>Correct test for fetch_javascript_source<commit_after># -*- coding: utf-8 -*-

from __future__ import absolute_import

import mock

from celery.task import Task
from sentry.models import Event
from sentry.tasks.fetch_source import fetch_javascript_source
from sentry.testutils import TestCase


class StoreEventTest(TestCase):
    def test_is_task(self):
        self.assertTrue(isinstance(fetch_javascript_source, Task))

    @mock.patch('sentry.models.Event.update')
    @mock.patch('urllib2.build_opener')
    def test_calls_from_kwargs(self, build_opener, update):
        event = Event(data={
            'sentry.interfaces.Stacktrace': {
                'frames': [
                    {
                        'abs_path': 'http://example.com/foo.js',
                        'filename': 'foo.js',
                        'lineno': 4,
                        'colno': 0,
                    },
                    {
                        'abs_path': 'http://example.com/foo.js',
                        'filename': 'foo.js',
                        'lineno': 1,
                        'colno': 0,
                    },
                ],
            },
        })
        build_opener.return_value.open.return_value.read.return_value = '\n'.join('hello world')

        fetch_javascript_source(event)

        build_opener.assert_called_once_with()
        build_opener.return_value.open.assert_called_once_with('http://example.com/foo.js')
        build_opener.return_value.open.return_value.read.assert_called_once_with()
        update.assert_called_once_with(data=event.data)

        frame_list = event.data['sentry.interfaces.Stacktrace']['frames']
        frame = frame_list[0]
        assert frame['pre_context'] == ['h', 'e', 'l']
        assert frame['context_line'] == 'l'
        assert frame['post_context'] == ['o', ' ', 'w', 'o', 'r']

        frame = frame_list[1]
        assert frame['pre_context'] == []
        assert frame['context_line'] == 'h'
        assert frame['post_context'] == ['e', 'l', 'l', 'o', ' ']
{'fraction_non_alphanumeric': 0.13620604259534422, 'fraction_numerical': 0.00396235760277365, 'mean_word_length': 1.976418570375829, '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': '1355740', 'n_tokens_mistral': 1204, 'n_tokens_neox': 1154, 'n_words': 195}