Added tests for user service
This commit is contained in:
parent
f22807553f
commit
fbd48135b2
@ -13,11 +13,13 @@ class QuotaService:
|
|||||||
""" Check if the current user quota surpasses the current quota """
|
""" Check if the current user quota surpasses the current quota """
|
||||||
# TODO We need to add the hard/soft limit flag for the geocoder
|
# TODO We need to add the hard/soft limit flag for the geocoder
|
||||||
user_quota = self.user_service.user_quota()
|
user_quota = self.user_service.user_quota()
|
||||||
current_used = self.user_service.used_quota_month()
|
today = date.today()
|
||||||
|
current_used = self.user_service.used_quota_month(today.year, today.month)
|
||||||
return True if (current_used + 1) < user_quota else False
|
return True if (current_used + 1) < user_quota else False
|
||||||
|
|
||||||
def increment_geocoder_use(self, amount=1):
|
def increment_geocoder_use(self, amount=1):
|
||||||
self.user_service.increment_geocoder_use(self.transaction_id)
|
today = date.today()
|
||||||
|
self.user_service.increment_geocoder_use(today.year, today.month, self.transaction_id)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def user_service(self):
|
def user_service(self):
|
||||||
|
@ -20,26 +20,26 @@ class UserService:
|
|||||||
self._redis_connection = self.__get_redis_connection(redis_connection=kwargs[self.REDIS_CONNECTION_KEY])
|
self._redis_connection = self.__get_redis_connection(redis_connection=kwargs[self.REDIS_CONNECTION_KEY])
|
||||||
else:
|
else:
|
||||||
if self.REDIS_CONNECTION_HOST not in kwargs:
|
if self.REDIS_CONNECTION_HOST not in kwargs:
|
||||||
raise "You have to provide redis configuration"
|
raise Exception("You have to provide redis configuration")
|
||||||
redis_config = self.__build_redis_config(kwargs)
|
redis_config = self.__build_redis_config(kwargs)
|
||||||
self._redis_connection = self.__get_redis_connection(redis_config = redis_config)
|
self._redis_connection = self.__get_redis_connection(redis_config = redis_config)
|
||||||
|
|
||||||
def user_quota(self):
|
def user_quota(self):
|
||||||
# Check for exceptions or redis timeout
|
# Check for exceptions or redis timeout
|
||||||
user_quota = self._redis_connection.hget(self.__get_user_redis_key(), self.GEOCODING_QUOTA_KEY)
|
user_quota = self._redis_connection.hget(self.__get_user_redis_key(), self.GEOCODING_QUOTA_KEY)
|
||||||
return int(user_quota) if user_quota else 0
|
return int(user_quota) if user_quota and int(user_quota) >= 0 else 0
|
||||||
|
|
||||||
def used_quota_month(self):
|
def used_quota_month(self, year, month):
|
||||||
""" Recover the used quota for the user in the current month """
|
""" Recover the used quota for the user in the current month """
|
||||||
# Check for exceptions or redis timeout
|
# Check for exceptions or redis timeout
|
||||||
current_used = 0
|
current_used = 0
|
||||||
for _, value in self._redis_connection.hscan_iter(self.__get_month_redis_key()):
|
for _, value in self._redis_connection.hscan_iter(self.__get_month_redis_key(year,month)):
|
||||||
current_used += int(value)
|
current_used += int(value)
|
||||||
return current_used
|
return current_used
|
||||||
|
|
||||||
def increment_geocoder_use(self, key, amount=1):
|
def increment_geocoder_use(self, year, month, key, amount=1):
|
||||||
# TODO Manage exceptions or timeout
|
# TODO Manage exceptions or timeout
|
||||||
self._redis_connection.hincrby(self.__get_month_redis_key(),key,amount)
|
self._redis_connection.hincrby(self.__get_month_redis_key(year, month),key,amount)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def redis_connection(self):
|
def redis_connection(self):
|
||||||
@ -64,9 +64,9 @@ class UserService:
|
|||||||
redis_db = config[self.REDIS_CONNECTION_DB] if self.REDIS_CONNECTION_DB in config else self.REDIS_DEFAULT_USER_DB
|
redis_db = config[self.REDIS_CONNECTION_DB] if self.REDIS_CONNECTION_DB in config else self.REDIS_DEFAULT_USER_DB
|
||||||
return {'host': redis_host, 'port': redis_port, 'db': redis_db}
|
return {'host': redis_host, 'port': redis_port, 'db': redis_db}
|
||||||
|
|
||||||
def __get_month_redis_key(self):
|
def __get_month_redis_key(self, year, month):
|
||||||
today = date.today()
|
today = date.today()
|
||||||
return "geocoder:{0}:{1}".format(self.user_id, today.strftime("%Y%m"))
|
return "geocoder:{0}:{1}{2}".format(self.user_id, year, month)
|
||||||
|
|
||||||
def __get_user_redis_key(self):
|
def __get_user_redis_key(self):
|
||||||
return "geocoder:{0}".format(self.user_id)
|
return "geocoder:{0}".format(self.user_id)
|
@ -35,6 +35,6 @@ setup(
|
|||||||
|
|
||||||
extras_require={
|
extras_require={
|
||||||
'dev': ['unittest'],
|
'dev': ['unittest'],
|
||||||
'test': ['unittest'],
|
'test': ['unittest', 'nose', 'mockredispy'],
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
37
server/lib/python/cartodb_geocoder/test/test_user_service.py
Normal file
37
server/lib/python/cartodb_geocoder/test/test_user_service.py
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
from mockredis import MockRedis
|
||||||
|
from cartodb_geocoder import user_service
|
||||||
|
from unittest import TestCase
|
||||||
|
from nose.tools import assert_raises
|
||||||
|
|
||||||
|
|
||||||
|
class TestUserService(TestCase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
self.fake_redis_connection = MockRedis()
|
||||||
|
self.us = user_service.UserService('user_id', redis_connection = self.fake_redis_connection)
|
||||||
|
|
||||||
|
def test_user_quota_should_be_10(self):
|
||||||
|
self.fake_redis_connection.hset('geocoder:user_id','geocoding_quota', 10)
|
||||||
|
assert self.us.user_quota() == 10
|
||||||
|
|
||||||
|
def test_should_return_0_if_negative_quota(self):
|
||||||
|
self.fake_redis_connection.hset('geocoder:user_id','geocoding_quota', -10)
|
||||||
|
assert self.us.user_quota() == 0
|
||||||
|
|
||||||
|
def test_should_return_0_if_not_user(self):
|
||||||
|
assert self.us.user_quota() == 0
|
||||||
|
|
||||||
|
def test_user_used_quota_for_a_month(self):
|
||||||
|
self.fake_redis_connection.hset('geocoder:user_id:201511','tx_id', 10)
|
||||||
|
self.fake_redis_connection.hset('geocoder:user_id:201511','tx_id_2', 10)
|
||||||
|
assert self.us.used_quota_month(2015, 11) == 20
|
||||||
|
|
||||||
|
def test_user_not_amount_in_used_quota_for_month_should_be_0(self):
|
||||||
|
assert self.us.used_quota_month(2015, 11) == 0
|
||||||
|
|
||||||
|
def test_increment_used_quota(self):
|
||||||
|
self.us.increment_geocoder_use(2015, 11, 'tx_id', 1)
|
||||||
|
assert self.us.used_quota_month(2015, 11) == 1
|
||||||
|
|
||||||
|
def test_exception_if_not_redis_config(self):
|
||||||
|
assert_raises(Exception, user_service.UserService, 'user_id')
|
Loading…
Reference in New Issue
Block a user