Improve tests and add more unit tests for the quota functionality

This commit is contained in:
Mario de Frutos 2016-12-19 17:52:38 +01:00
parent 6c71d73498
commit 0672f2752b
5 changed files with 493 additions and 142 deletions

View File

@ -1,7 +1,282 @@
from unittest import TestCase
from mockredis import MockRedis
from datetime import datetime, timedelta
from ..test_helper import *
from cartodb_services.metrics.config import RoutingConfig, ServicesRedisConfig
from cartodb_services.metrics.config import *
class TestGeocoderUserConfig(TestCase):
GEOCODER_PROVIDERS = ['heremaps', 'mapzen', 'google']
def setUp(self):
self.redis_conn = MockRedis()
plpy_mock_config()
def test_should_return_geocoder_config_for_user(self):
for geocoder_provider in self.GEOCODER_PROVIDERS:
build_redis_user_config(self.redis_conn, 'test_user', 'geocoding',
provider=geocoder_provider, quota=100)
geocoder_config = GeocoderConfig(self.redis_conn, plpy_mock,
'test_user', None)
if geocoder_provider == 'heremaps':
assert geocoder_config.heremaps_geocoder is True
assert geocoder_config.geocoding_quota == 100
elif geocoder_provider == 'mapzen':
assert geocoder_config.mapzen_geocoder is True
assert geocoder_config.geocoding_quota == 100
elif geocoder_provider == 'google':
assert geocoder_config.google_geocoder is True
assert geocoder_config.geocoding_quota is None
assert geocoder_config.soft_geocoding_limit is False
def test_should_return_quota_0_when_is_0_in_redis(self):
for geocoder_provider in self.GEOCODER_PROVIDERS:
build_redis_user_config(self.redis_conn, 'test_user', 'geocoding',
quota=0, provider=geocoder_provider)
geocoder_config = GeocoderConfig(self.redis_conn, plpy_mock,
'test_user', None)
if geocoder_provider is not 'google':
assert geocoder_config.geocoding_quota == 0
def test_should_return_quota_0_if_quota_is_empty(self):
for geocoder_provider in self.GEOCODER_PROVIDERS:
build_redis_user_config(self.redis_conn, 'test_user', 'geocoding',
quota='', provider=geocoder_provider)
geocoder_config = GeocoderConfig(self.redis_conn, plpy_mock,
'test_user', None)
if geocoder_provider is not 'google':
assert geocoder_config.geocoding_quota == 0
def test_should_return_quota_None_when_is_provider_is_google(self):
for geocoder_provider in self.GEOCODER_PROVIDERS:
build_redis_user_config(self.redis_conn, 'test_user', 'geocoding',
quota=0, provider=geocoder_provider)
geocoder_config = GeocoderConfig(self.redis_conn, plpy_mock,
'test_user', None)
if geocoder_provider is 'google':
assert geocoder_config.geocoding_quota == None
def test_should_return_true_if_soft_limit_is_true(self):
for geocoder_provider in self.GEOCODER_PROVIDERS:
build_redis_user_config(self.redis_conn, 'test_user', 'geocoding',
quota=0, soft_limit=True,
provider=geocoder_provider)
geocoder_config = GeocoderConfig(self.redis_conn, plpy_mock,
'test_user', None)
assert geocoder_config.soft_geocoding_limit == True
def test_should_return_false_if_soft_limit_is_empty_string(self):
for geocoder_provider in self.GEOCODER_PROVIDERS:
build_redis_user_config(self.redis_conn, 'test_user', 'geocoding',
quota=0, soft_limit='',
provider=geocoder_provider)
geocoder_config = GeocoderConfig(self.redis_conn, plpy_mock,
'test_user', None)
assert geocoder_config.soft_geocoding_limit == False
class TestGeocoderOrgConfig(TestCase):
GEOCODER_PROVIDERS = ['heremaps', 'mapzen', 'google']
def setUp(self):
self.redis_conn = MockRedis()
plpy_mock_config()
def test_should_return_org_config(self):
for geocoder_provider in self.GEOCODER_PROVIDERS:
yesterday = datetime.today() - timedelta(days=1)
build_redis_user_config(self.redis_conn, 'test_user', 'geocoding',
provider=geocoder_provider)
build_redis_org_config(self.redis_conn, 'test_org', 'geocoding',
quota=200, end_date=yesterday,
provider=geocoder_provider)
geocoder_config = GeocoderConfig(self.redis_conn, plpy_mock,
'test_user', 'test_org')
if geocoder_provider == 'heremaps':
assert geocoder_config.heremaps_geocoder is True
assert geocoder_config.geocoding_quota == 200
elif geocoder_provider == 'mapzen':
assert geocoder_config.mapzen_geocoder is True
assert geocoder_config.geocoding_quota == 200
elif geocoder_provider == 'google':
assert geocoder_config.google_geocoder is True
assert geocoder_config.geocoding_quota is None
assert geocoder_config.soft_geocoding_limit is False
assert geocoder_config.period_end_date.date() == yesterday.date()
def test_should_return_0_quota_if_has_0_in_redis_config(self):
for geocoder_provider in self.GEOCODER_PROVIDERS:
yesterday = datetime.today() - timedelta(days=1)
build_redis_user_config(self.redis_conn, 'test_user', 'geocoding',
provider=geocoder_provider)
build_redis_org_config(self.redis_conn, 'test_org', 'geocoding',
quota=0, end_date=yesterday,
provider=geocoder_provider)
geocoder_config = GeocoderConfig(self.redis_conn, plpy_mock,
'test_user', 'test_org')
if geocoder_provider is not 'google':
assert geocoder_config.geocoding_quota == 0
def test_should_return_0_if_quota_is_empty_for_org_in_redis(self):
for geocoder_provider in self.GEOCODER_PROVIDERS:
yesterday = datetime.today() - timedelta(days=1)
build_redis_user_config(self.redis_conn, 'test_user', 'geocoding',
provider=geocoder_provider)
build_redis_org_config(self.redis_conn, 'test_org', 'geocoding',
quota='', end_date=yesterday,
provider=geocoder_provider)
geocoder_config = GeocoderConfig(self.redis_conn, plpy_mock,
'test_user', 'test_org')
if geocoder_provider is not 'google':
assert geocoder_config.geocoding_quota == 0
def test_should_return_None_if_provider_is_google(self):
for geocoder_provider in self.GEOCODER_PROVIDERS:
yesterday = datetime.today() - timedelta(days=1)
build_redis_user_config(self.redis_conn, 'test_user', 'geocoding',
provider=geocoder_provider)
build_redis_org_config(self.redis_conn, 'test_org', 'geocoding',
quota='', end_date=yesterday,
provider=geocoder_provider)
geocoder_config = GeocoderConfig(self.redis_conn, plpy_mock,
'test_user', 'test_org')
if geocoder_provider is 'google':
assert geocoder_config.geocoding_quota == None
def test_should_return_user_quota_if_is_not_defined_for_org(self):
for geocoder_provider in self.GEOCODER_PROVIDERS:
yesterday = datetime.today() - timedelta(days=1)
build_redis_user_config(self.redis_conn, 'test_user', 'geocoding',
quota=100, provider=geocoder_provider)
build_redis_org_config(self.redis_conn, 'test_org', 'geocoding',
quota=None, end_date=yesterday,
provider=geocoder_provider)
geocoder_config = GeocoderConfig(self.redis_conn, plpy_mock,
'test_user', 'test_org')
if geocoder_provider is not 'google':
assert geocoder_config.geocoding_quota == 100
class TestIsolinesUserConfig(TestCase):
ISOLINES_PROVIDERS = ['heremaps', 'mapzen']
def setUp(self):
self.redis_conn = MockRedis()
plpy_mock_config()
def test_should_return_user_config_for_isolines(self):
for isolines_provider in self.ISOLINES_PROVIDERS:
build_redis_user_config(self.redis_conn, 'test_user', 'isolines',
quota=100, provider=isolines_provider)
isolines_config = IsolinesRoutingConfig(self.redis_conn, plpy_mock,
'test_user')
if isolines_provider is 'mapzen':
assert isolines_config.service_type is 'mapzen_isolines'
else:
assert isolines_config.service_type is 'here_isolines'
assert isolines_config.isolines_quota == 100
assert isolines_config.soft_isolines_limit is False
def test_should_return_0_quota_for_0_value(self):
for isolines_provider in self.ISOLINES_PROVIDERS:
build_redis_user_config(self.redis_conn, 'test_user', 'isolines',
provider=isolines_provider, quota=0,
soft_limit=True)
isolines_config = IsolinesRoutingConfig(self.redis_conn, plpy_mock,
'test_user')
assert isolines_config.isolines_quota == 0
def test_should_return_0_quota_for_empty_quota_value(self):
for isolines_provider in self.ISOLINES_PROVIDERS:
build_redis_user_config(self.redis_conn, 'test_user', 'isolines',
provider=isolines_provider, quota='')
isolines_config = IsolinesRoutingConfig(self.redis_conn, plpy_mock,
'test_user')
assert isolines_config.isolines_quota == 0
def test_should_return_true_soft_limit(self):
for isolines_provider in self.ISOLINES_PROVIDERS:
build_redis_user_config(self.redis_conn, 'test_user', 'isolines',
provider=isolines_provider, quota=0,
soft_limit=True)
isolines_config = IsolinesRoutingConfig(self.redis_conn, plpy_mock,
'test_user')
assert isolines_config.soft_isolines_limit is True
def test_should_return_false_soft_limit_with_empty_string(self):
for isolines_provider in self.ISOLINES_PROVIDERS:
build_redis_user_config(self.redis_conn, 'test_user', 'isolines',
provider=isolines_provider, quota=0,
soft_limit='')
isolines_config = IsolinesRoutingConfig(self.redis_conn, plpy_mock,
'test_user')
assert isolines_config.soft_isolines_limit is False
class TestIsolinesOrgConfig(TestCase):
ISOLINES_PROVIDERS = ['heremaps', 'mapzen']
def setUp(self):
self.redis_conn = MockRedis()
plpy_mock_config()
def test_should_return_org_config_for_isolines(self):
yesterday = datetime.today() - timedelta(days=1)
for isolines_provider in self.ISOLINES_PROVIDERS:
build_redis_user_config(self.redis_conn, 'test_user', 'isolines',
provider=isolines_provider)
build_redis_org_config(self.redis_conn, 'test_org', 'isolines',
quota=200, end_date=yesterday,
provider=isolines_provider)
isolines_config = IsolinesRoutingConfig(self.redis_conn, plpy_mock,
'test_user', 'test_org')
assert isolines_config.isolines_quota == 200
assert isolines_config.soft_isolines_limit is False
assert isolines_config.period_end_date.date() == yesterday.date()
def test_should_return_quota_0_for_0_redis_quota(self):
yesterday = datetime.today() - timedelta(days=1)
for isolines_provider in self.ISOLINES_PROVIDERS:
build_redis_user_config(self.redis_conn, 'test_user', 'isolines',
provider=isolines_provider,
soft_limit=True)
build_redis_org_config(self.redis_conn, 'test_org', 'isolines',
quota=0, end_date=yesterday,
provider=isolines_provider)
isolines_config = IsolinesRoutingConfig(self.redis_conn, plpy_mock,
'test_user', 'test_org')
assert isolines_config.isolines_quota == 0
def test_should_return_quota_0_for_empty_string_quota_in_org_config(self):
yesterday = datetime.today() - timedelta(days=1)
for isolines_provider in self.ISOLINES_PROVIDERS:
build_redis_user_config(self.redis_conn, 'test_user', 'isolines',
provider=isolines_provider)
build_redis_org_config(self.redis_conn, 'test_org', 'isolines',
quota='', end_date=yesterday,
provider=isolines_provider)
isolines_config = IsolinesRoutingConfig(self.redis_conn, plpy_mock,
'test_user', 'test_org')
assert isolines_config.isolines_quota == 0
def test_should_return_user_quota_for_non_existent_org_quota(self):
yesterday = datetime.today() - timedelta(days=1)
for isolines_provider in self.ISOLINES_PROVIDERS:
build_redis_user_config(self.redis_conn, 'test_user', 'isolines',
provider=isolines_provider, quota=100)
build_redis_org_config(self.redis_conn, 'test_org', 'isolines',
quota=None, end_date=yesterday,
provider=isolines_provider)
isolines_config = IsolinesRoutingConfig(self.redis_conn, plpy_mock,
'test_user', 'test_org')
assert isolines_config.isolines_quota == 100
class TestRoutingConfig(TestCase):
@ -13,11 +288,6 @@ class TestRoutingConfig(TestCase):
self._user_key = "rails:users:{0}".format(self._username)
self._redis_conn.hset(self._user_key, 'period_end_date', '2016-10-10')
def test_should_pick_quota_from_server_by_default(self):
orgname = None
config = RoutingConfig(self._redis_conn, self._db_conn, self._username, orgname)
assert config.monthly_quota == 1500000
def test_should_pick_quota_from_redis_if_present(self):
self._redis_conn.hset(self._user_key, 'mapzen_routing_quota', 1000)
orgname = None
@ -38,7 +308,6 @@ class TestRoutingConfig(TestCase):
config = RoutingConfig(self._redis_conn, self._db_conn, self._username, orgname)
assert config.monthly_quota == 5000
def test_should_have_soft_limit_false_by_default(self):
orgname = None
config = RoutingConfig(self._redis_conn, self._db_conn, self._username, orgname)
@ -51,6 +320,136 @@ class TestRoutingConfig(TestCase):
assert config.soft_limit == True
class TestDataObservatoryUserConfig(TestCase):
def setUp(self):
self.redis_conn = MockRedis()
plpy_mock_config()
def test_should_return_config_for_obs_snapshot(self):
yesterday = datetime.today() - timedelta(days=1)
build_redis_user_config(self.redis_conn, 'test_user', 'data_observatory',
quota=100, end_date=yesterday)
do_config = ObservatorySnapshotConfig(self.redis_conn, plpy_mock,
'test_user')
assert do_config.monthly_quota == 100
assert do_config.soft_limit is False
assert do_config.period_end_date.date() == yesterday.date()
def test_should_return_true_if_soft_limit_is_true_in_redis(self):
yesterday = datetime.today() - timedelta(days=1)
build_redis_user_config(self.redis_conn, 'test_user', 'data_observatory',
quota=0, soft_limit=True, end_date=yesterday)
do_config = ObservatorySnapshotConfig(self.redis_conn, plpy_mock,
'test_user')
assert do_config.soft_limit is True
def test_should_return_0_if_quota_is_0_in_redis(self):
yesterday = datetime.today() - timedelta(days=1)
build_redis_user_config(self.redis_conn, 'test_user', 'data_observatory',
quota=0, end_date=yesterday)
do_config = ObservatorySnapshotConfig(self.redis_conn, plpy_mock,
'test_user')
assert do_config.monthly_quota == 0
def test_should_return_0_if_quota_is_empty_in_redis(self):
yesterday = datetime.today() - timedelta(days=1)
build_redis_user_config(self.redis_conn, 'test_user', 'data_observatory',
quota='', end_date=yesterday)
do_config = ObservatorySnapshotConfig(self.redis_conn, plpy_mock,
'test_user')
assert do_config.monthly_quota == 0
def test_should_return_config_for_obs_snapshot(self):
yesterday = datetime.today() - timedelta(days=1)
build_redis_user_config(self.redis_conn, 'test_user', 'data_observatory',
quota=100, end_date=yesterday)
do_config = ObservatoryConfig(self.redis_conn, plpy_mock,
'test_user')
assert do_config.monthly_quota == 100
assert do_config.soft_limit is False
assert do_config.period_end_date.date() == yesterday.date()
def test_should_return_0_if_quota_is_0_in_redis(self):
yesterday = datetime.today() - timedelta(days=1)
build_redis_user_config(self.redis_conn, 'test_user', 'data_observatory',
quota=0, end_date=yesterday)
do_config = ObservatoryConfig(self.redis_conn, plpy_mock,
'test_user')
assert do_config.monthly_quota == 0
def test_should_return_0_if_quota_is_empty_in_redis(self):
yesterday = datetime.today() - timedelta(days=1)
build_redis_user_config(self.redis_conn, 'test_user', 'data_observatory',
quota='', end_date=yesterday)
do_config = ObservatoryConfig(self.redis_conn, plpy_mock,
'test_user')
assert do_config.monthly_quota == 0
def test_should_return_true_if_soft_limit_is_true_in_redis(self):
yesterday = datetime.today() - timedelta(days=1)
build_redis_user_config(self.redis_conn, 'test_user', 'data_observatory',
quota=0, soft_limit=True, end_date=yesterday)
do_config = ObservatoryConfig(self.redis_conn, plpy_mock,
'test_user')
assert do_config.soft_limit is True
def test_should_return_true_if_soft_limit_is_empty_string_in_redis(self):
yesterday = datetime.today() - timedelta(days=1)
build_redis_user_config(self.redis_conn, 'test_user', 'data_observatory',
quota=0, soft_limit='', end_date=yesterday)
do_config = ObservatoryConfig(self.redis_conn, plpy_mock,
'test_user')
assert do_config.soft_limit is False
class TestDataObservatoryOrgConfig(TestCase):
def setUp(self):
self.redis_conn = MockRedis()
plpy_mock_config()
def test_should_return_organization_config(self):
yesterday = datetime.today() - timedelta(days=1)
build_redis_user_config(self.redis_conn, 'test_user', 'data_observatory',
quota=100, end_date=yesterday)
build_redis_org_config(self.redis_conn, 'test_org', 'data_observatory',
quota=200, end_date=yesterday)
do_config = ObservatoryConfig(self.redis_conn, plpy_mock,
'test_user', 'test_org')
assert do_config.monthly_quota == 200
assert do_config.period_end_date.date() == yesterday.date()
def test_should_return_quota_0_for_0_in_org_quota_config(self):
yesterday = datetime.today() - timedelta(days=1)
build_redis_user_config(self.redis_conn, 'test_user', 'data_observatory',
quota=100)
build_redis_org_config(self.redis_conn, 'test_org', 'data_observatory',
quota=0, end_date=yesterday)
do_config = ObservatoryConfig(self.redis_conn, plpy_mock,
'test_user', 'test_org')
assert do_config.monthly_quota == 0
def test_should_return_quota_0_for_empty_in_org_quota_config(self):
yesterday = datetime.today() - timedelta(days=1)
build_redis_user_config(self.redis_conn, 'test_user', 'data_observatory',
quota=100)
build_redis_org_config(self.redis_conn, 'test_org', 'data_observatory',
quota='', end_date=yesterday)
do_config = ObservatoryConfig(self.redis_conn, plpy_mock,
'test_user', 'test_org')
assert do_config.monthly_quota == 0
def test_should_return_user_config_when_org_quota_is_not_defined(self):
yesterday = datetime.today() - timedelta(days=1)
build_redis_user_config(self.redis_conn, 'test_user', 'data_observatory',
quota=100)
build_redis_org_config(self.redis_conn, 'test_org', 'data_observatory',
quota=None, end_date=yesterday)
do_config = ObservatoryConfig(self.redis_conn, plpy_mock,
'test_user', 'test_org')
assert do_config.monthly_quota == 100
class TestServicesRedisConfig(TestCase):
def test_it_picks_mapzen_routing_quota_from_redis(self):
redis_conn = MockRedis()

View File

@ -1,60 +0,0 @@
from test_helper import *
from unittest import TestCase
from nose.tools import assert_raises
from mockredis import MockRedis
from datetime import datetime, timedelta
from cartodb_services.metrics import GeocoderConfig, ObservatorySnapshotConfig, ConfigException
class TestConfig(TestCase):
def setUp(self):
self.redis_conn = MockRedis()
plpy_mock_config()
def test_should_return_list_of_nokia_geocoder_config_if_its_ok(self):
build_redis_user_config(self.redis_conn, 'test_user')
geocoder_config = GeocoderConfig(self.redis_conn, plpy_mock,
'test_user', None)
assert geocoder_config.heremaps_geocoder is True
assert geocoder_config.geocoding_quota == 100
assert geocoder_config.soft_geocoding_limit is False
def test_should_return_list_of_nokia_geocoder_config_ok_for_org(self):
yesterday = datetime.today() - timedelta(days=1)
build_redis_user_config(self.redis_conn, 'test_user')
build_redis_org_config(self.redis_conn, 'test_org',
quota=200, end_date=yesterday)
geocoder_config = GeocoderConfig(self.redis_conn, plpy_mock,
'test_user', 'test_org')
assert geocoder_config.heremaps_geocoder is True
assert geocoder_config.geocoding_quota == 200
assert geocoder_config.soft_geocoding_limit is False
assert geocoder_config.period_end_date.date() == yesterday.date()
def test_should_return_config_for_obs_snapshot(self):
yesterday = datetime.today() - timedelta(days=1)
build_redis_user_config(self.redis_conn, 'test_user',
do_quota=100, soft_do_limit=True,
end_date=yesterday)
do_config = ObservatorySnapshotConfig(self.redis_conn, plpy_mock,
'test_user')
assert do_config.monthly_quota == 100
assert do_config.soft_limit is True
assert do_config.period_end_date.date() == yesterday.date()
def test_should_return_db_quota_if_not_redis_quota_config_obs_snapshot(self):
yesterday = datetime.today() - timedelta(days=1)
build_redis_user_config(self.redis_conn, 'test_user',
end_date=yesterday)
do_config = ObservatorySnapshotConfig(self.redis_conn, plpy_mock,
'test_user')
assert do_config.monthly_quota == 0
assert do_config.soft_limit is False
assert do_config.period_end_date.date() == yesterday.date()
def test_should_raise_exception_when_missing_parameters(self):
plpy_mock._reset()
build_redis_user_config(self.redis_conn, 'test_user')
assert_raises(ConfigException, GeocoderConfig, self.redis_conn,
plpy_mock, 'test_user', None)

View File

@ -1,4 +1,5 @@
from datetime import datetime, date
from dateutil.tz import tzlocal
from mock import Mock, MagicMock
import random
import sys
@ -8,46 +9,60 @@ plpy_mock = MockPlPy()
sys.modules['plpy'] = plpy_mock
def build_redis_user_config(redis_conn, username, quota=100, soft_limit=False,
service="heremaps", isolines_quota=0,
do_quota=None, soft_do_limit=None,
do_general_quota=None, soft_do_general_limit=None,
def build_redis_user_config(redis_conn, username, service, quota=100,
soft_limit=False, provider="heremaps",
end_date=datetime.today()):
end_date_tz = end_date.replace(tzinfo=tzlocal())
user_redis_name = "rails:users:{0}".format(username)
redis_conn.hset(user_redis_name, 'soft_geocoding_limit', soft_limit)
redis_conn.hset(user_redis_name, 'geocoding_quota', quota)
redis_conn.hset(user_redis_name, 'here_isolines_quota', isolines_quota)
redis_conn.hset(user_redis_name, 'geocoder_provider', service)
redis_conn.hset(user_redis_name, 'isolines_provider', service)
redis_conn.hset(user_redis_name, 'routing_provider', service)
redis_conn.hset(user_redis_name, 'period_end_date', end_date)
if do_quota:
redis_conn.hset(user_redis_name, 'obs_snapshot_quota', do_quota)
if soft_do_limit:
redis_conn.hset(user_redis_name, 'soft_obs_snapshot_limit',
soft_do_limit)
if do_general_quota:
redis_conn.hset(user_redis_name, 'obs_general_quota', do_general_quota)
if soft_do_general_limit:
redis_conn.hset(user_redis_name, 'soft_obs_general_limit',
soft_do_general_limit)
if service is 'geocoding':
redis_conn.hset(user_redis_name, 'geocoder_provider', provider)
redis_conn.hset(user_redis_name, 'geocoding_quota', str(quota))
redis_conn.hset(user_redis_name, 'soft_geocoding_limit', str(soft_limit).lower())
elif service is 'isolines':
redis_conn.hset(user_redis_name, 'isolines_provider', provider)
redis_conn.hset(user_redis_name, 'here_isolines_quota', str(quota))
redis_conn.hset(user_redis_name, 'soft_here_isolines_limit', str(soft_limit).lower())
elif service is 'routing':
redis_conn.hset(user_redis_name, 'routing_provider', provider)
redis_conn.hset(user_redis_name, 'mapzen_routing_quota', str(quota))
redis_conn.hset(user_redis_name, 'soft_mapzen_routing_limit', str(soft_limit).lower())
elif service is 'data_observatory':
redis_conn.hset(user_redis_name, 'obs_snapshot_quota', str(quota))
redis_conn.hset(user_redis_name, 'obs_general_quota', str(quota))
redis_conn.hset(user_redis_name, 'soft_obs_snapshot_limit', str(soft_limit).lower())
redis_conn.hset(user_redis_name, 'soft_obs_general_limit', str(soft_limit).lower())
redis_conn.hset(user_redis_name, 'google_maps_client_id', '')
redis_conn.hset(user_redis_name, 'google_maps_api_key', '')
redis_conn.hset(user_redis_name, 'period_end_date', end_date_tz.strftime("%Y-%m-%d %H:%M:%S %z"))
def build_redis_org_config(redis_conn, orgname, quota=100, service="heremaps",
isolines_quota=0, do_quota=None,
do_general_quota=None, end_date=datetime.today()):
def build_redis_org_config(redis_conn, orgname, service, quota=100,
provider="heremaps", end_date=datetime.now(tzlocal())):
org_redis_name = "rails:orgs:{0}".format(orgname)
redis_conn.hset(org_redis_name, 'geocoding_quota', quota)
redis_conn.hset(org_redis_name, 'here_isolines_quota', isolines_quota)
if do_quota:
redis_conn.hset(org_redis_name, 'obs_snapshot_quota', do_quota)
if do_general_quota:
redis_conn.hset(org_redis_name, 'obs_snapshot_quota', do_quota)
redis_conn.hset(org_redis_name, 'period_end_date', end_date)
end_date_tz = end_date.replace(tzinfo=tzlocal())
if service is 'geocoding':
redis_conn.hset(org_redis_name, 'geocoder_provider', provider)
if quota is not None:
redis_conn.hset(org_redis_name, 'geocoding_quota', str(quota))
elif service is 'isolines':
redis_conn.hset(org_redis_name, 'isolines_provider', provider)
if quota is not None:
redis_conn.hset(org_redis_name, 'here_isolines_quota', str(quota))
elif service is 'routing':
redis_conn.hset(org_redis_name, 'routing_provider', provider)
if quota is not None:
redis_conn.hset(org_redis_name, 'mapzen_routing_quota', str(quota))
elif service is 'data_observatory':
if quota is not None:
redis_conn.hset(org_redis_name, 'obs_snapshot_quota', str(quota))
redis_conn.hset(org_redis_name, 'obs_general_quota', str(quota))
redis_conn.hset(org_redis_name, 'google_maps_client_id', '')
redis_conn.hset(org_redis_name, 'google_maps_api_key', '')
redis_conn.hset(org_redis_name, 'period_end_date', end_date_tz.strftime("%Y-%m-%d %H:%M:%S %z"))
def increment_service_uses(redis_conn, username, orgname=None,

View File

@ -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)

View File

@ -108,7 +108,7 @@ class TestUserService(TestCase):
def zscore_counter(self):
return self._zscore_counter
self.redis_conn = MockRedisWithCounter()
us = self.__build_user_service('test_user', end_date=date.today())
us = self.__build_user_service('test_user', end_date=datetime.today())
us.used_quota(self.NOKIA_GEOCODER, date(2015, 6, 15))
#('user:test_user:geocoder_here:success_responses:201506', 15)
@ -132,16 +132,17 @@ class TestUserService(TestCase):
assert self.redis_conn.zscore('org:test_org:geocoder_here:success_responses:201506', '1') == None
def __build_user_service(self, username, quota=100, service='heremaps',
orgname=None, soft_limit=False,
end_date=date.today()):
build_redis_user_config(self.redis_conn, username,
quota=quota, service=service,
soft_limit=soft_limit,
end_date=end_date)
def __build_user_service(self, username, service='geocoding', quota=100,
provider='heremaps', orgname=None,
soft_limit=False, end_date=datetime.today()):
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, end_date=end_date)
build_redis_org_config(self.redis_conn, orgname, service,
provider=provider, quota=quota,
end_date=end_date)
geocoder_config = GeocoderConfig(self.redis_conn, plpy_mock,
username, orgname)
return UserMetricsService(geocoder_config, self.redis_conn)