Improve tests and add more unit tests for the quota functionality

This commit is contained in:
Mario de Frutos
2016-12-20 16:17:41 +01:00
parent 6c71d73498
commit 0672f2752b
5 changed files with 493 additions and 142 deletions
@@ -35,26 +35,26 @@ class TestQuotaService(TestCase):
qs = self.__build_geocoder_quota_service('test_user',
orgname='test_org')
increment_service_uses(self.redis_conn, 'test_user',
orgname='test_org')
orgname='test_org')
assert qs.check_user_quota() is True
def test_should_return_false_if_user_quota_is_surpassed(self):
qs = self.__build_geocoder_quota_service('test_user')
increment_service_uses(self.redis_conn, 'test_user',
amount=300)
amount=300)
assert qs.check_user_quota() is False
def test_should_return_false_if_org_quota_is_surpassed(self):
qs = self.__build_geocoder_quota_service('test_user',
orgname='test_org')
increment_service_uses(self.redis_conn, 'test_user',
orgname='test_org', amount=400)
orgname='test_org', amount=400)
assert qs.check_user_quota() is False
def test_should_return_true_if_user_quota_is_surpassed_but_soft_limit_is_enabled(self):
qs = self.__build_geocoder_quota_service('test_user', soft_limit=True)
increment_service_uses(self.redis_conn, 'test_user',
amount=300)
amount=300)
assert qs.check_user_quota() is True
def test_should_return_true_if_org_quota_is_surpassed_but_soft_limit_is_enabled(self):
@@ -62,7 +62,7 @@ class TestQuotaService(TestCase):
orgname='test_org',
soft_limit=True)
increment_service_uses(self.redis_conn, 'test_user',
orgname='test_org', amount=400)
orgname='test_org', amount=400)
assert qs.check_user_quota() is True
def test_should_check_user_increment_and_quota_check_correctly(self):
@@ -81,7 +81,7 @@ class TestQuotaService(TestCase):
assert qs.check_user_quota() is False
def test_should_check_user_mapzen_geocoder_quota_correctly(self):
qs = self.__build_geocoder_quota_service('test_user', service='mapzen')
qs = self.__build_geocoder_quota_service('test_user', provider='mapzen')
qs.increment_success_service_use()
assert qs.check_user_quota() is True
qs.increment_success_service_use(amount=1500000)
@@ -89,7 +89,7 @@ class TestQuotaService(TestCase):
def test_should_check_org_mapzen_geocoder_quota_correctly(self):
qs = self.__build_geocoder_quota_service('test_user', orgname='testorg',
service='mapzen')
provider='mapzen')
qs.increment_success_service_use()
assert qs.check_user_quota() is True
qs.increment_success_service_use(amount=1500000)
@@ -138,55 +138,51 @@ class TestQuotaService(TestCase):
qs.increment_success_service_use(amount=100000)
assert qs.check_user_quota() is False
def __prepare_quota_service(self, username, quota, service, orgname,
soft_limit, do_quota, soft_do_limit, end_date):
build_redis_user_config(self.redis_conn, username,
quota=quota, service=service,
soft_limit=soft_limit,
soft_do_limit=soft_do_limit,
do_quota=do_quota,
end_date=end_date)
def __prepare_quota_service(self, username, service, quota, provider,
orgname, soft_limit, end_date):
build_redis_user_config(self.redis_conn, username, service,
quota=quota, provider=provider,
soft_limit=soft_limit, end_date=end_date)
if orgname:
build_redis_org_config(self.redis_conn, orgname,
quota=quota, service=service,
do_quota=do_quota,
end_date=end_date)
build_redis_org_config(self.redis_conn, orgname, service,
quota=quota, provider=provider,
end_date=end_date)
def __build_geocoder_quota_service(self, username, quota=100,
service='heremaps', orgname=None,
provider='heremaps', orgname=None,
soft_limit=False,
end_date=datetime.today()):
self.__prepare_quota_service(username, quota, service, orgname,
soft_limit, 0, False, end_date)
self.__prepare_quota_service(username, 'geocoding', quota,
provider, orgname, soft_limit, end_date)
geocoder_config = GeocoderConfig(self.redis_conn, plpy_mock,
username, orgname)
return QuotaService(geocoder_config, redis_connection=self.redis_conn)
def __build_routing_quota_service(self, username, service='mapzen',
def __build_routing_quota_service(self, username, provider='mapzen',
orgname=None, soft_limit=False,
quota=100, end_date=datetime.today()):
self.__prepare_quota_service(username, quota, service, orgname,
soft_limit, 0, False, end_date)
self.__prepare_quota_service(username, 'routing', quota, provider,
orgname, soft_limit, end_date)
routing_config = RoutingConfig(self.redis_conn, plpy_mock,
username, orgname)
return QuotaService(routing_config, redis_connection=self.redis_conn)
def __build_isolines_quota_service(self, username, service='mapzen',
def __build_isolines_quota_service(self, username, provider='mapzen',
orgname=None, soft_limit=False,
quota=100, end_date=datetime.today()):
self.__prepare_quota_service(username, quota, service, orgname,
soft_limit, 0, False, end_date)
self.__prepare_quota_service(username, 'isolines', quota, provider,
orgname, soft_limit, end_date)
isolines_config = IsolinesRoutingConfig(self.redis_conn, plpy_mock,
username, orgname)
return QuotaService(isolines_config, redis_connection=self.redis_conn)
def __build_obs_snapshot_quota_service(self, username, quota=100,
service='obs_snapshot',
orgname=None,
soft_limit=False,
end_date=datetime.today()):
self.__prepare_quota_service(username, 0, service, orgname, False,
quota, soft_limit, end_date)
provider='obs_snapshot',
orgname=None,
soft_limit=False,
end_date=datetime.today()):
self.__prepare_quota_service(username, 'data_observatory', quota,
None, orgname, soft_limit, end_date)
do_config = ObservatorySnapshotConfig(self.redis_conn, plpy_mock,
username, orgname)
return QuotaService(do_config, redis_connection=self.redis_conn)