Implement pgwui.routes config settings
authorKarl O. Pinc <kop@meme.com>
Tue, 13 Nov 2018 23:47:04 +0000 (17:47 -0600)
committerKarl O. Pinc <kop@meme.com>
Tue, 13 Nov 2018 23:47:04 +0000 (17:47 -0600)
src/pgwui_server/__init__.py
tests/test___init__.py

index dde886e652b9428f075101e8558e2c4bfa14c5b5..567130409b8a1dcc6887ef4701657b46d49f3956 100644 (file)
 from pyramid.config import Configurator
 
 
+def parse_assignments(lines):
+    '''Return a list of key/value tuples from the lines of a setting
+    '''
+    result = []
+    for line in lines.split('\n'):
+        if '=' in line:
+            key, val = line.split('=')
+            result.append((key.rstrip(), val.lstrip()))
+    return result
+
+
+def add_routes(config, settings):
+    '''Add routes found in pgwui.routes setting
+    '''
+    if 'pgwui.routes' in settings:
+        routes = parse_assignments(settings['pgwui.routes'])
+        for route_entry in routes:
+            name, route = route_entry
+            config.add_route(name, route)
+
+
 def pgwui_server_config(settings):
+    '''Configure pyramid
+    '''
     rp = settings.get('pgwui.route_prefix')
     with Configurator(settings=settings, route_prefix=rp) as config:
         config.include('pgwui_common')
+        add_routes(config, settings)
     return config
 
 
index 13d92a7713a71ccab3e922ec1014ccba14271b45..f84248bf5c6236f82b5009b88f0be41e11d4708f 100644 (file)
@@ -40,14 +40,57 @@ class MockConfigurator():
         pass
 
 
+class MockConfig():
+    def __init__(self):
+        self.add_route_called = 0
+
+    def add_route(self, *args):
+        self.add_route_called += 1
+
+
 # Unit tests
 
+# parse_assignments()
+
+def test_parse_assignments():
+    '''Returns key/value tuples and ignores lines without an "="'''
+    lines = ('key1 = value1\n'  # whitespace around = is ignored
+             '\n'
+             'ignored\n'
+             'key2=value2\n'    # missing whitespace is fine
+             )
+    result = pgwui_server_init.parse_assignments(lines)
+    assert result == [('key1', 'value1'),
+                      ('key2', 'value2')]
+
+
+# add_routes()
+
+def test_add_routes_empty():
+    '''When there is no pgwui.routes setting nothing gets added'''
+    config = MockConfig()
+    pgwui_server_init.add_routes(config, {})
+    assert config.add_route_called == 0
+
+
+def test_add_routes_notempty(monkeypatch):
+    '''When there is a pgwui.routes setting config.add_route() is called
+    for each route'''
+    config = MockConfig()
+    monkeypatch.setattr(pgwui_server_init, 'parse_assignments',
+                        lambda *args: [('name1', 'route1'),
+                                       ('name2', 'route2')])
+    pgwui_server_init.add_routes(config, {'pgwui.routes': ''})
+    assert config.add_route_called == 2
+
 
 # pgwui_server_config()
 
 def test_pgwui_server_config(monkeypatch):
     '''Returns a configuration'''
     monkeypatch.setattr(pgwui_server_init, 'Configurator', MockConfigurator)
+    monkeypatch.setattr(pgwui_server_init, 'add_routes',
+                        lambda *args: None)
 
     result = pgwui_server_init.pgwui_server_config({})
     assert isinstance(result, MockConfigurator)