Add server config storage classes

This commit is contained in:
Rafa de la Torre
2016-09-28 17:32:26 +02:00
parent 18f05fbd4f
commit fd2cc21942
6 changed files with 114 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
# NOTE: This init function must be called from plpythonu entry points to
# initialize cartodb_services module properly. E.g:
#
# CREATE OR REPLACE FUNCTION cdb_dataservices_server.cdb_isochrone(...)
# RETURNS SETOF cdb_dataservices_server.isoline AS $$
#
# import cartodb_services
# cartodb_services.init(plpy, GD)
#
# # rest of the code here
# cartodb_services.GD[key] = val
# cartodb_services.plpy.execute('SELECT * FROM ...')
#
# $$ LANGUAGE plpythonu;
plpy = None
GD = None
def init(_plpy, _GD):
global plpy
global GD
if plpy is None:
plpy = _plpy
if GD is None:
GD = _GD
def _reset():
# NOTE: just for testing
global plpy
global GD
plpy = None
GD = None

View File

@@ -0,0 +1,11 @@
import abc
class ConfigStorageInterface(object):
"""This is an interface that all config storages must abide to"""
__metaclass__ = abc.ABCMeta
@abc.abstractmethod
def get(self, key):
"""Return a value based on the key supplied from some storage"""
pass

View File

@@ -0,0 +1,26 @@
import json
import cartodb_services
from interfaces import ConfigStorageInterface
class InDbServerConfigStorage(ConfigStorageInterface):
def get(self, key):
sql = "SELECT cdb_dataservices_server.cdb_conf_getconf('{0}') as conf".format(key)
rows = cartodb_services.plpy.execute(sql, 1)
json_output = rows[0]['conf']
if json_output:
return json.loads(json_output)
else:
return None
class InMemoryConfigStorage(ConfigStorageInterface):
def __init__(self, config_hash={}):
self._config_hash = config_hash
def get(self, key):
try:
return self._config_hash[key]
except KeyError:
return None

View File

@@ -0,0 +1,42 @@
from unittest import TestCase
from mock import Mock, MagicMock
from nose.tools import raises
from cartodb_services.refactor.storage.server_config import *
import cartodb_services
class TestInDbServerConfigStorage(TestCase):
def setUp(self):
self.plpy_mock = Mock()
cartodb_services.init(self.plpy_mock, _GD={})
def tearDown(self):
cartodb_services._reset()
def test_gets_configs_from_db(self):
self.plpy_mock.execute = MagicMock(return_value=[{'conf': '"any_value"'}])
server_config = InDbServerConfigStorage()
assert server_config.get('any_config') == 'any_value'
self.plpy_mock.execute.assert_called_once_with("SELECT cdb_dataservices_server.cdb_conf_getconf('any_config') as conf", 1)
def test_gets_none_if_cannot_retrieve_key(self):
self.plpy_mock.execute = MagicMock(return_value=[{'conf': None}])
server_config = InDbServerConfigStorage()
assert server_config.get('any_non_existing_key') is None
def test_deserializes_from_db_to_plain_dict(self):
self.plpy_mock.execute = MagicMock(return_value=[{'conf': '{"environment": "testing"}'}])
server_config = InDbServerConfigStorage()
assert server_config.get('server_conf') == {'environment': 'testing'}
self.plpy_mock.execute.assert_called_once_with("SELECT cdb_dataservices_server.cdb_conf_getconf('server_conf') as conf", 1)
class TestInMemoryConfigStorage(TestCase):
def test_can_provide_values_from_hash(self):
server_config = InMemoryConfigStorage({'any_key': 'any_value'})
assert server_config.get('any_key') == 'any_value'
def test_gets_none_if_cannot_retrieve_key(self):
server_config = InMemoryConfigStorage()
assert server_config.get('any_non_existing_key') == None