<commit_before># pylint: disable=missing-docstring,maybe-no-member

from datetime import datetime

from mock import patch
from mock import sentinel
from pytz import UTC

from django.test import TestCase
from django.test.client import RequestFactory

from track import views


class TestTrackViews(TestCase):

    def setUp(self):
        self.request_factory = RequestFactory()

        patcher = patch('track.views.tracker')
        self.mock_tracker = patcher.start()
        self.addCleanup(patcher.stop)

        self._expected_timestamp = datetime.now(UTC)
        self._datetime_patcher = patch('track.views.datetime')
        self.addCleanup(self._datetime_patcher.stop)
        mock_datetime_mod = self._datetime_patcher.start()
        mock_datetime_mod.datetime.now.return_value = self._expected_timestamp  # pylint: disable=maybe-no-member

        self.path_with_course = '/courses/foo/bar/baz/xmod/'
        self.url_with_course = 'http://www.edx.org' + self.path_with_course

        self.event = {
            sentinel.key: sentinel.value
        }

    def test_user_track(self):
        request = self.request_factory.get('/event', {
            'page': self.url_with_course,
            'event_type': sentinel.event_type,
            'event': {}
        })
        views.user_track(request)

        expected_event = {
            'username': 'anonymous',
            'session': '',
            'ip': '127.0.0.1',
            'event_source': 'browser',
            'event_type': str(sentinel.event_type),
            'event': '{}',
            'agent': '',
            'page': self.url_with_course,
            'time': self._expected_timestamp,
            'host': 'testserver',
            'context': {
                'course_id': 'foo/bar/baz',
                'org_id': 'foo',
            },
        }
        self.mock_tracker.send.assert_called_once_with(expected_event)

    def test_server_track(self):
        request = self.request_factory.get(self.path_with_course)
        views.server_track(request, str(sentinel.event_type), '{}')

        expected_event = {
            'username': 'anonymous',
            'ip': '127.0.0.1',
            'event_source': 'server',
            'event_type': str(sentinel.event_type),
            'event': '{}',
            'agent': '',
            'page': None,
            'time': self._expected_timestamp,
            'host': 'testserver',
            'context': {},
        }
        self.mock_tracker.send.assert_called_once_with(expected_event)

    def test_server_track_with_no_request(self):
        request = None
        views.server_track(request, str(sentinel.event_type), '{}')

        expected_event = {
            'username': 'anonymous',
            'ip': '',
            'event_source': 'server',
            'event_type': str(sentinel.event_type),
            'event': '{}',
            'agent': '',
            'page': None,
            'time': self._expected_timestamp,
            'host': '',
            'context': {},
        }
        self.mock_tracker.send.assert_called_once_with(expected_event)

    def test_task_track(self):
        request_info = {
            'username': 'anonymous',
            'ip': '127.0.0.1',
            'agent': 'agent',
            'host': 'testserver',
        }

        task_info = {
            sentinel.task_key: sentinel.task_value
        }
        expected_event_data = dict(task_info)
        expected_event_data.update(self.event)

        views.task_track(request_info, task_info, str(sentinel.event_type), self.event)

        expected_event = {
            'username': 'anonymous',
            'ip': '127.0.0.1',
            'event_source': 'task',
            'event_type': str(sentinel.event_type),
            'event': expected_event_data,
            'agent': 'agent',
            'page': None,
            'time': self._expected_timestamp,
            'host': 'testserver',
            'context': {
                'course_id': '',
                'org_id': ''
            },
        }
        self.mock_tracker.send.assert_called_once_with(expected_event)
<commit_msg>fix tests<commit_after># pylint: disable=missing-docstring,maybe-no-member

from track import views
from mock import patch, sentinel
from freezegun import freeze_time

from django.test import TestCase
from django.test.client import RequestFactory

from datetime import datetime
expected_time = datetime(2013, 10, 3, 8, 24, 55)


class TestTrackViews(TestCase):

    def setUp(self):
        self.request_factory = RequestFactory()

        patcher = patch('track.views.tracker')
        self.mock_tracker = patcher.start()
        self.addCleanup(patcher.stop)

        self.path_with_course = '/courses/foo/bar/baz/xmod/'
        self.url_with_course = 'http://www.edx.org' + self.path_with_course

        self.event = {
            sentinel.key: sentinel.value
        }

    @freeze_time(expected_time)
    def test_user_track(self):
        request = self.request_factory.get('/event', {
            'page': self.url_with_course,
            'event_type': sentinel.event_type,
            'event': {}
        })
        views.user_track(request)

        expected_event = {
            'username': 'anonymous',
            'session': '',
            'ip': '127.0.0.1',
            'event_source': 'browser',
            'event_type': str(sentinel.event_type),
            'event': '{}',
            'agent': '',
            'page': self.url_with_course,
            'time': expected_time,
            'host': 'testserver',
            'context': {
                'course_id': 'foo/bar/baz',
                'org_id': 'foo',
            },
        }
        self.mock_tracker.send.assert_called_once_with(expected_event)

    @freeze_time(expected_time)
    def test_server_track(self):
        request = self.request_factory.get(self.path_with_course)
        views.server_track(request, str(sentinel.event_type), '{}')

        expected_event = {
            'username': 'anonymous',
            'ip': '127.0.0.1',
            'event_source': 'server',
            'event_type': str(sentinel.event_type),
            'event': '{}',
            'agent': '',
            'page': None,
            'time': expected_time,
            'host': 'testserver',
            'context': {},
        }
        self.mock_tracker.send.assert_called_once_with(expected_event)

    @freeze_time(expected_time)
    def test_server_track_with_no_request(self):
        request = None
        views.server_track(request, str(sentinel.event_type), '{}')

        expected_event = {
            'username': 'anonymous',
            'ip': '',
            'event_source': 'server',
            'event_type': str(sentinel.event_type),
            'event': '{}',
            'agent': '',
            'page': None,
            'time': expected_time,
            'host': '',
            'context': {},
        }
        self.mock_tracker.send.assert_called_once_with(expected_event)

    @freeze_time(expected_time)
    def test_task_track(self):
        request_info = {
            'username': 'anonymous',
            'ip': '127.0.0.1',
            'agent': 'agent',
            'host': 'testserver',
        }

        task_info = {
            sentinel.task_key: sentinel.task_value
        }
        expected_event_data = dict(task_info)
        expected_event_data.update(self.event)

        views.task_track(request_info, task_info, str(sentinel.event_type), self.event)

        expected_event = {
            'username': 'anonymous',
            'ip': '127.0.0.1',
            'event_source': 'task',
            'event_type': str(sentinel.event_type),
            'event': expected_event_data,
            'agent': 'agent',
            'page': None,
            'time': expected_time,
            'host': 'testserver',
            'context': {
                'course_id': '',
                'org_id': ''
            },
        }
        self.mock_tracker.send.assert_called_once_with(expected_event)

{'fraction_non_alphanumeric': 0.13143428285857073, 'fraction_numerical': 0.0074962518740629685, 'mean_word_length': 1.940852314474651, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 8, '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': '122344', 'n_tokens_mistral': 2449, 'n_tokens_neox': 2342, 'n_words': 372}