pecan/pecan — Issue #38: Issues with the generated config.py, import order, and ming
Author: sechastain
Created: 2012-02-29 17:31:56+00:00
State: open

So here's a backstory summary and the work around.

Backstory:
- Created a pecan project to work with Ming
- I did some early prototyping with pecan shell, everything seemed okay
- Unit Testing of developed models failed
- When I finally included my model in controllers, running the server failed

Concerning Ming:
As far as I can tell, there's only one pattern for Ming's use.
1. Module import order is very important
2. A session has to exist by the time that any Ming extensions are imported
3. It's best to house session initialization and variables in its own module (e.g. /model/session.py)
4. All model modules will import session.py to obtain session information for __mongometa__
5. Pattern /model/__init__.py as follows:

import .session

from .model_module import ModelClass1, ModelClass2

# pecan functions defined here: init_model, start, start_readonly (The preceding are useless), commit, clear, close

from ming.orm import Mapper

Mapper.compile_all()

Why the unit tests failed:
- The model classes that extended Ming's MappedClass were being imported at the top of the test module
- WebTest wasn't creating the app until the tests setUp are ran
- Because the app wasn't created, pecan.conf wasn't appropriately initialized
- Because pecan.conf wasn't appropriately initialized, setting up the session failed

Workaround:
- import the model classes inside each test instead of at the top of the test module

Why the server was failing:
- The config.py module was being imported
- Which in turn imported the Root controller and subsequently all child controllers
- As part of the controller modules being imported, the model modules were being imported
- The model modules imported the model.session module to gain session information
- the session module failed on import because it tried to import pecan.conf, which was in the process of being imported and thus not ready.

Workaround:
- config.py does not specify the Root Controller and does not include related imports
- the /app.py file imports the following modules in order: pecan.conf, .model, and .controllers.root
- the make_app call is directly passed a new RootController