Commit 5be58fc1 by David Baumgold

Merge pull request #2883 from edx/db/fix-import

Fix undefined references to pytz, and update tests
parents 5e4c25dd f25a7870
# pylint: disable=missing-docstring,maybe-no-member
from datetime import datetime
from mock import patch
from mock import sentinel
from pytz import UTC
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 track import views
from datetime import datetime
expected_time = datetime(2013, 10, 3, 8, 24, 55)
class TestTrackViews(TestCase):
......@@ -21,12 +20,6 @@ class TestTrackViews(TestCase):
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
......@@ -34,6 +27,7 @@ class TestTrackViews(TestCase):
sentinel.key: sentinel.value
}
@freeze_time(expected_time)
def test_user_track(self):
request = self.request_factory.get('/event', {
'page': self.url_with_course,
......@@ -51,7 +45,7 @@ class TestTrackViews(TestCase):
'event': '{}',
'agent': '',
'page': self.url_with_course,
'time': self._expected_timestamp,
'time': expected_time,
'host': 'testserver',
'context': {
'course_id': 'foo/bar/baz',
......@@ -60,6 +54,7 @@ class TestTrackViews(TestCase):
}
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), '{}')
......@@ -72,12 +67,13 @@ class TestTrackViews(TestCase):
'event': '{}',
'agent': '',
'page': None,
'time': self._expected_timestamp,
'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), '{}')
......@@ -90,12 +86,13 @@ class TestTrackViews(TestCase):
'event': '{}',
'agent': '',
'page': None,
'time': self._expected_timestamp,
'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',
......@@ -120,7 +117,7 @@ class TestTrackViews(TestCase):
'event': expected_event_data,
'agent': 'agent',
'page': None,
'time': self._expected_timestamp,
'time': expected_time,
'host': 'testserver',
'context': {
'course_id': '',
......
import datetime
from pytz import UTC
import pytz
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
......@@ -59,7 +59,7 @@ def user_track(request):
"event": request.REQUEST['event'],
"agent": _get_request_header(request, 'HTTP_USER_AGENT'),
"page": page,
"time": datetime.datetime.now(UTC),
"time": datetime.datetime.utcnow(),
"host": _get_request_header(request, 'SERVER_NAME'),
"context": eventtracker.get_tracker().resolve_context(),
}
......@@ -92,7 +92,7 @@ def server_track(request, event_type, event, page=None):
"event": event,
"agent": _get_request_header(request, 'HTTP_USER_AGENT'),
"page": page,
"time": datetime.datetime.now(UTC),
"time": datetime.datetime.utcnow(),
"host": _get_request_header(request, 'SERVER_NAME'),
"context": eventtracker.get_tracker().resolve_context(),
}
......@@ -136,7 +136,7 @@ def task_track(request_info, task_info, event_type, event, page=None):
"event": full_event,
"agent": request_info.get('agent', 'unknown'),
"page": page,
"time": datetime.datetime.now(UTC),
"time": datetime.datetime.utcnow(),
"host": request_info.get('host', 'unknown'),
"context": eventtracker.get_tracker().resolve_context(),
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment