Added tests for user service

This commit is contained in:
Mario de Frutos 2015-11-11 11:47:38 +01:00
parent f22807553f
commit fbd48135b2
5 changed files with 50 additions and 11 deletions

View File

@ -13,11 +13,13 @@ class QuotaService:
""" Check if the current user quota surpasses the current quota """
# TODO We need to add the hard/soft limit flag for the geocoder
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
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
def user_service(self):

View File

@ -20,26 +20,26 @@ class UserService:
self._redis_connection = self.__get_redis_connection(redis_connection=kwargs[self.REDIS_CONNECTION_KEY])
else:
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)
self._redis_connection = self.__get_redis_connection(redis_config = redis_config)
def user_quota(self):
# Check for exceptions or redis timeout
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 """
# Check for exceptions or redis timeout
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)
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
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
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
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()
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):
return "geocoder:{0}".format(self.user_id)

View File

@ -35,6 +35,6 @@ setup(
extras_require={
'dev': ['unittest'],
'test': ['unittest'],
'test': ['unittest', 'nose', 'mockredispy'],
}
)

View 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')