2016-11-04 19:14:48 +08:00
|
|
|
from test_helper import *
|
2015-11-11 18:47:38 +08:00
|
|
|
from mockredis import MockRedis
|
2016-01-29 18:54:50 +08:00
|
|
|
from cartodb_services.metrics import UserMetricsService
|
|
|
|
from cartodb_services.metrics import GeocoderConfig
|
2016-01-22 01:07:27 +08:00
|
|
|
from datetime import datetime, date
|
2015-11-11 18:47:38 +08:00
|
|
|
from unittest import TestCase
|
2016-03-08 19:24:27 +08:00
|
|
|
from mock import Mock
|
2015-11-11 18:47:38 +08:00
|
|
|
from nose.tools import assert_raises
|
2016-01-22 01:07:27 +08:00
|
|
|
from datetime import timedelta
|
2016-06-13 18:57:00 +08:00
|
|
|
from freezegun import freeze_time
|
2015-11-11 18:47:38 +08:00
|
|
|
|
|
|
|
|
|
|
|
class TestUserService(TestCase):
|
|
|
|
|
2016-01-22 01:07:27 +08:00
|
|
|
NOKIA_GEOCODER = 'geocoder_here'
|
2015-11-18 01:02:21 +08:00
|
|
|
|
2015-11-11 18:47:38 +08:00
|
|
|
def setUp(self):
|
2016-01-22 01:07:27 +08:00
|
|
|
self.redis_conn = MockRedis()
|
2015-11-11 18:47:38 +08:00
|
|
|
|
2016-01-22 01:07:27 +08:00
|
|
|
def test_user_used_quota_for_a_day(self):
|
|
|
|
us = self.__build_user_service('test_user')
|
2016-11-04 19:14:48 +08:00
|
|
|
increment_service_uses(self.redis_conn, 'test_user',
|
2016-04-22 22:43:28 +08:00
|
|
|
amount=400)
|
2016-01-22 01:07:27 +08:00
|
|
|
assert us.used_quota(self.NOKIA_GEOCODER, date.today()) == 400
|
2015-11-11 18:47:38 +08:00
|
|
|
|
2016-01-22 01:07:27 +08:00
|
|
|
def test_org_used_quota_for_a_day(self):
|
|
|
|
us = self.__build_user_service('test_user', orgname='test_org')
|
2016-11-04 19:14:48 +08:00
|
|
|
increment_service_uses(self.redis_conn, 'test_user',
|
2016-04-22 22:43:28 +08:00
|
|
|
orgname='test_org',
|
|
|
|
amount=400)
|
2016-01-22 01:07:27 +08:00
|
|
|
assert us.used_quota(self.NOKIA_GEOCODER, date.today()) == 400
|
2015-11-11 18:47:38 +08:00
|
|
|
|
2015-11-18 01:02:21 +08:00
|
|
|
def test_user_not_amount_in_used_quota_for_month_should_be_0(self):
|
2016-01-22 01:07:27 +08:00
|
|
|
us = self.__build_user_service('test_user')
|
|
|
|
assert us.used_quota(self.NOKIA_GEOCODER, date.today()) == 0
|
2015-11-18 01:02:21 +08:00
|
|
|
|
|
|
|
def test_org_not_amount_in_used_quota_for_month_should_be_0(self):
|
2016-01-22 01:07:27 +08:00
|
|
|
us = self.__build_user_service('test_user', orgname='test_org')
|
|
|
|
assert us.used_quota(self.NOKIA_GEOCODER, date.today()) == 0
|
2015-11-18 01:02:21 +08:00
|
|
|
|
2016-01-22 01:07:27 +08:00
|
|
|
def test_should_increment_user_used_quota_for_one_date(self):
|
|
|
|
us = self.__build_user_service('test_user')
|
|
|
|
us.increment_service_use(self.NOKIA_GEOCODER, 'success_responses')
|
|
|
|
assert us.used_quota(self.NOKIA_GEOCODER, date.today()) == 1
|
|
|
|
us.increment_service_use(self.NOKIA_GEOCODER, 'empty_responses')
|
|
|
|
assert us.used_quota(self.NOKIA_GEOCODER, date.today()) == 2
|
|
|
|
us.increment_service_use(self.NOKIA_GEOCODER, 'fail_responses')
|
|
|
|
assert us.used_quota(self.NOKIA_GEOCODER, date.today()) == 2
|
2015-11-18 01:02:21 +08:00
|
|
|
|
|
|
|
def test_should_increment_org_used_quota(self):
|
2016-01-22 01:07:27 +08:00
|
|
|
us = self.__build_user_service('test_user', orgname='test_org')
|
|
|
|
us.increment_service_use(self.NOKIA_GEOCODER, 'success_responses')
|
|
|
|
assert us.used_quota(self.NOKIA_GEOCODER, date.today()) == 1
|
|
|
|
us.increment_service_use(self.NOKIA_GEOCODER, 'empty_responses')
|
|
|
|
assert us.used_quota(self.NOKIA_GEOCODER, date.today()) == 2
|
|
|
|
us.increment_service_use(self.NOKIA_GEOCODER, 'fail_responses')
|
|
|
|
assert us.used_quota(self.NOKIA_GEOCODER, date.today()) == 2
|
|
|
|
|
|
|
|
def test_should_increment_user_used_quota_in_for_multiples_dates(self):
|
|
|
|
two_days_ago = datetime.today() - timedelta(days=2)
|
|
|
|
one_day_ago = datetime.today() - timedelta(days=1)
|
|
|
|
one_day_after = datetime.today() + timedelta(days=1)
|
|
|
|
us = self.__build_user_service('test_user', end_date=one_day_ago)
|
|
|
|
us.increment_service_use(self.NOKIA_GEOCODER, 'success_responses',
|
|
|
|
date=date.today())
|
|
|
|
assert us.used_quota(self.NOKIA_GEOCODER, date.today()) == 1
|
|
|
|
us.increment_service_use(self.NOKIA_GEOCODER, 'empty_responses',
|
|
|
|
date=one_day_ago)
|
|
|
|
assert us.used_quota(self.NOKIA_GEOCODER, date.today()) == 2
|
|
|
|
us.increment_service_use(self.NOKIA_GEOCODER, 'empty_responses',
|
|
|
|
date=two_days_ago)
|
|
|
|
assert us.used_quota(self.NOKIA_GEOCODER, date.today()) == 2
|
|
|
|
us.increment_service_use(self.NOKIA_GEOCODER, 'empty_responses',
|
|
|
|
date=one_day_after)
|
|
|
|
assert us.used_quota(self.NOKIA_GEOCODER, date.today()) == 2
|
|
|
|
us.increment_service_use(self.NOKIA_GEOCODER, 'fail_responses')
|
|
|
|
assert us.used_quota(self.NOKIA_GEOCODER, date.today()) == 2
|
|
|
|
|
2016-06-13 19:07:21 +08:00
|
|
|
@freeze_time("2015-06-01")
|
2016-06-13 17:33:11 +08:00
|
|
|
def test_should_account_for_zero_paddded_keys(self):
|
2016-06-13 19:07:21 +08:00
|
|
|
us = self.__build_user_service('test_user')
|
|
|
|
self.redis_conn.zincrby('user:test_user:geocoder_here:success_responses:201506', '01', 400)
|
|
|
|
assert us.used_quota(self.NOKIA_GEOCODER, date(2015, 6,1)) == 400
|
2016-06-13 17:33:11 +08:00
|
|
|
|
2016-06-13 18:57:00 +08:00
|
|
|
@freeze_time("2015-06-01")
|
2016-06-13 17:33:11 +08:00
|
|
|
def test_should_account_for_wrongly_stored_non_padded_keys(self):
|
2016-06-13 18:57:00 +08:00
|
|
|
us = self.__build_user_service('test_user')
|
|
|
|
self.redis_conn.zincrby('user:test_user:geocoder_here:success_responses:201506', '1', 400)
|
|
|
|
assert us.used_quota(self.NOKIA_GEOCODER, date(2015, 6,1)) == 400
|
2016-06-13 17:33:11 +08:00
|
|
|
|
2016-06-13 19:10:07 +08:00
|
|
|
@freeze_time("2015-06-01")
|
2016-06-13 17:33:11 +08:00
|
|
|
def test_should_sum_amounts_from_both_key_formats(self):
|
2016-06-13 19:10:07 +08:00
|
|
|
us = self.__build_user_service('test_user')
|
|
|
|
self.redis_conn.zincrby('user:test_user:geocoder_here:success_responses:201506', '1', 400)
|
|
|
|
self.redis_conn.zincrby('user:test_user:geocoder_here:success_responses:201506', '01', 300)
|
|
|
|
assert us.used_quota(self.NOKIA_GEOCODER, date(2015, 6,1)) == 700
|
2016-06-13 17:33:11 +08:00
|
|
|
|
2016-06-13 21:45:56 +08:00
|
|
|
@freeze_time("2015-06-15")
|
2016-06-13 17:33:11 +08:00
|
|
|
def test_should_not_request_redis_twice_when_unneeded(self):
|
2016-06-13 21:45:56 +08:00
|
|
|
class MockRedisWithCounter(MockRedis):
|
|
|
|
def __init__(self):
|
|
|
|
super(MockRedisWithCounter, self).__init__()
|
|
|
|
self._zscore_counter = 0
|
|
|
|
def zscore(self, *args):
|
|
|
|
print args
|
|
|
|
self._zscore_counter += 1
|
|
|
|
return super(MockRedisWithCounter, self).zscore(*args)
|
|
|
|
def zscore_counter(self):
|
|
|
|
return self._zscore_counter
|
|
|
|
self.redis_conn = MockRedisWithCounter()
|
|
|
|
us = self.__build_user_service('test_user', end_date=date.today())
|
|
|
|
us.used_quota(self.NOKIA_GEOCODER, date(2015, 6, 15))
|
|
|
|
|
|
|
|
#('user:test_user:geocoder_here:success_responses:201506', 15)
|
|
|
|
#('user:test_user:geocoder_here:empty_responses:201506', 15)
|
|
|
|
#('user:test_user:geocoder_cache:success_responses:201506', 15)
|
|
|
|
assert self.redis_conn.zscore_counter() == 3
|
2016-06-13 17:33:11 +08:00
|
|
|
|
2016-06-14 16:04:17 +08:00
|
|
|
def test_should_write_zero_padded_dates(self):
|
|
|
|
us = self.__build_user_service('test_user')
|
|
|
|
us.increment_service_use(self.NOKIA_GEOCODER, 'success_responses',
|
|
|
|
date=date(2015,6,1))
|
|
|
|
assert self.redis_conn.zscore('user:test_user:geocoder_here:success_responses:201506', '01') == 1
|
|
|
|
assert self.redis_conn.zscore('user:test_user:geocoder_here:success_responses:201506', '1') == None
|
|
|
|
|
2016-06-14 16:28:35 +08:00
|
|
|
def test_orgs_should_write_zero_padded_dates(self):
|
|
|
|
us = self.__build_user_service('test_user', orgname='test_org')
|
|
|
|
us.increment_service_use(self.NOKIA_GEOCODER, 'success_responses',
|
|
|
|
amount=400,
|
|
|
|
date=date(2015,6,1))
|
|
|
|
assert self.redis_conn.zscore('org:test_org:geocoder_here:success_responses:201506', '01') == 400
|
|
|
|
assert self.redis_conn.zscore('org:test_org:geocoder_here:success_responses:201506', '1') == None
|
|
|
|
|
2016-06-13 17:33:11 +08:00
|
|
|
|
2016-01-22 01:07:27 +08:00
|
|
|
def __build_user_service(self, username, quota=100, service='heremaps',
|
|
|
|
orgname=None, soft_limit=False,
|
2016-06-13 21:45:56 +08:00
|
|
|
end_date=date.today()):
|
2016-11-04 19:14:48 +08:00
|
|
|
build_redis_user_config(self.redis_conn, username,
|
2016-01-22 01:07:27 +08:00
|
|
|
quota=quota, service=service,
|
|
|
|
soft_limit=soft_limit,
|
|
|
|
end_date=end_date)
|
|
|
|
if orgname:
|
2016-11-04 19:14:48 +08:00
|
|
|
build_redis_org_config(self.redis_conn, orgname,
|
2016-01-22 01:07:27 +08:00
|
|
|
quota=quota, end_date=end_date)
|
2016-03-08 19:24:27 +08:00
|
|
|
geocoder_config = GeocoderConfig(self.redis_conn, plpy_mock,
|
2016-03-23 02:33:55 +08:00
|
|
|
username, orgname)
|
2016-01-29 18:54:50 +08:00
|
|
|
return UserMetricsService(geocoder_config, self.redis_conn)
|