addons-server/lib/metrics/test.py

66 строки
1.9 KiB
Python
Исходник Обычный вид История

# -*- coding: utf8 -*-
import json
import urlparse
2012-04-12 02:21:09 +04:00
from django.conf import settings
import mock
from nose.tools import eq_
2012-04-12 02:21:09 +04:00
import amo.tests
from lib.metrics import metrics, send, send_request
@mock.patch('lib.metrics.urllib2.urlopen')
@mock.patch.object(settings, 'METRICS_SERVER', 'http://localhost')
2012-04-12 02:21:09 +04:00
class TestMetrics(amo.tests.TestCase):
def test_called(self, urlopen):
send('install', {})
eq_(urlopen.call_args[0][0].data, '{}')
def test_called_data(self, urlopen):
data = {'foo': 'bar'}
send('install', data)
eq_(urlopen.call_args[0][0].data, json.dumps(data))
def test_called_url(self, urlopen):
send('install', {})
url = urlopen.call_args[0][0].get_full_url()
eq_(urlparse.urlparse(url)[:2], ('http', 'localhost'))
def test_some_unicode(self, urlopen):
send('install', {'name': u'Вагиф Сәмәдоғлу'})
def test_send_request(self, urlopen):
request = mock.Mock()
2012-04-13 20:34:21 +04:00
request.GET = {'src': 'foo'}
2012-04-12 03:53:14 +04:00
request.LANG = 'en'
request.META = {'HTTP_USER_AGENT': 'py'}
send_request('install', request, {})
2012-04-12 03:53:14 +04:00
data = json.loads(urlopen.call_args[0][0].data)
eq_(data['user-agent'], 'py')
eq_(data['locale'], 'en')
2012-04-13 20:34:21 +04:00
eq_(data['src'], 'foo')
def get_response(self, code):
response = mock.Mock()
response.status_code = code
return response
def test_error(self, urlopen):
urlopen.return_value = self.get_response(403)
eq_(metrics('x', 'install', {}), 403)
def test_good(self, urlopen):
urlopen.return_value = self.get_response(200)
eq_(metrics('x', 'install', {}), 200)
def test_other(self, urlopen):
urlopen.return_value = self.get_response(206)
eq_(metrics('x', 'install', {}), 206)
def test_uid(self, urlopen):
metrics('x', 'install', {})
assert urlopen.call_args[0][0].get_full_url().endswith('x')