Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gettings settings and config from INI file for Pyramid functional testing

In a real Pyramid app it does not work per docs http://docs.pylonsproject.org/projects/pyramid//en/latest/narr/testing.html :

class FunctionalTests(unittest.TestCase):
    def setUp(self):
        from myapp import main
        app = main({})

Exception:

Traceback (most recent call last):
  File "C:\projects\myapp\tests\model\task_dispatcher_integration_test.py", line 35, in setUp
    app = main({})
  File "C:\projects\myapp\myapp\__init__.py", line 207, in main
    engine = engine_from_config(settings, 'sqlalchemy.')
  File "C:\projects\myapp\ve\lib\site-packages\sqlalchemy\engine\__init__.py", line 407, in engine_from_config
    url = options.pop('url')
KeyError: 'url'

The reason is trivial: an empty dictionary is passed to main, while it seems that while running real app (from __init__.py) it gets settings pre-filled with values from [app:main] section of development.ini / production.ini:

settings {'ldap_port': '4032', 'sqlalchemy.url': 'postgresql://.....}

Is there some way of reconstructing settings easily from an .ini file for functional testing?

like image 673
LetMeSOThat4U Avatar asked Sep 11 '15 08:09

LetMeSOThat4U


2 Answers

pyramid.paster.get_appsettings is the only thing you need:

from pyramid.paster import get_appsettings

settings = get_appsettings('test.ini', name='main')
app = main(settings)

That test.ini can include all the settings of another .ini file easily like this:

[app:main]
use = config:development.ini#main

and then you only need to override those keys that change (I guess you'd want to rather test against a separate DB):

[app:main]
use = config:development.ini#main
sqlalchemy.uri = postgresql://....

In case anyone else doesn't get @antti-haapala's answer right away:

Create a test.ini filled with:

[app:main]
use = config:development.ini#main

(Actually this step is not necessary. You could also keep your development.ini and use it instead of test.ini in the following code. A separate test.ini might however be useful if you want separate settings for testing)

In your tests.py add:

from pyramid.paster import get_appsettings
settings = get_appsettings('test.ini', name='main')

and replace

app = TestApp(main({}))

with

app = TestApp(main(global_config = None, **settings))

Relevant for this answer was the following comment: Pyramid fails to start when webtest and sqlalchemy are used together

like image 39
Christian Benke Avatar answered Oct 05 '22 23:10

Christian Benke