From fbd48135b2d05d4053d95d230bac275a64436172 Mon Sep 17 00:00:00 2001 From: Mario de Frutos Date: Wed, 11 Nov 2015 11:47:38 +0100 Subject: [PATCH] Added tests for user service --- .../cartodb_geocoder/quota_service.py | 6 ++- .../cartodb_geocoder/user_service.py | 16 ++++---- server/lib/python/cartodb_geocoder/setup.py | 2 +- .../test/test_quota_service.py | 0 .../test/test_user_service.py | 37 +++++++++++++++++++ 5 files changed, 50 insertions(+), 11 deletions(-) create mode 100644 server/lib/python/cartodb_geocoder/test/test_quota_service.py create mode 100644 server/lib/python/cartodb_geocoder/test/test_user_service.py diff --git a/server/lib/python/cartodb_geocoder/cartodb_geocoder/quota_service.py b/server/lib/python/cartodb_geocoder/cartodb_geocoder/quota_service.py index bf49548..490c0b9 100644 --- a/server/lib/python/cartodb_geocoder/cartodb_geocoder/quota_service.py +++ b/server/lib/python/cartodb_geocoder/cartodb_geocoder/quota_service.py @@ -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): diff --git a/server/lib/python/cartodb_geocoder/cartodb_geocoder/user_service.py b/server/lib/python/cartodb_geocoder/cartodb_geocoder/user_service.py index 145dc06..b804f14 100644 --- a/server/lib/python/cartodb_geocoder/cartodb_geocoder/user_service.py +++ b/server/lib/python/cartodb_geocoder/cartodb_geocoder/user_service.py @@ -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) \ No newline at end of file diff --git a/server/lib/python/cartodb_geocoder/setup.py b/server/lib/python/cartodb_geocoder/setup.py index edcd74b..bc71c8f 100644 --- a/server/lib/python/cartodb_geocoder/setup.py +++ b/server/lib/python/cartodb_geocoder/setup.py @@ -35,6 +35,6 @@ setup( extras_require={ 'dev': ['unittest'], - 'test': ['unittest'], + 'test': ['unittest', 'nose', 'mockredispy'], } ) diff --git a/server/lib/python/cartodb_geocoder/test/test_quota_service.py b/server/lib/python/cartodb_geocoder/test/test_quota_service.py new file mode 100644 index 0000000..e69de29 diff --git a/server/lib/python/cartodb_geocoder/test/test_user_service.py b/server/lib/python/cartodb_geocoder/test/test_user_service.py new file mode 100644 index 0000000..491b123 --- /dev/null +++ b/server/lib/python/cartodb_geocoder/test/test_user_service.py @@ -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') \ No newline at end of file