Added tests for user service
This commit is contained in:
@@ -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):
|
||||
|
||||
@@ -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)
|
||||
Reference in New Issue
Block a user