Hi Anand,
thanks for your patience :)
Anand Chitipothu wrote:
> I still didn't understand your intent. An example will help.
Ok, that means code :)
> Anyway, you can do something like this:
>
> def myprocessor(handler):
> if web.config.debug: # or if is_test_mode:
> return handler()
> else:
> initialize_my_db()
> return handler()
>
> app.add_processor(my_processor)
>
> Does it make sense?
It does, and it shows my problem. In the processor you can only do
global things, like your initialize_my_db(). It can only change the
global state. My handler will then likely do something like this:
class Handler(object):
def GET(self, id):
db = DB() #I don't want this
data = db.get_data(id)
return SimpleView(data).render()
When I want to unittest the Handler, I have to live with the fact that
it tries to access some database service, that just might not work or
even exist during testing. So currently I'm doing something like this:
class Handler(object):
def GET(self, id):
db = somemodule.get_db() #this is my workaround
...
Now at least I can influence the kind of db service my handler gets. But
this is a little unelegant, because changing the outcome of
somemodule.get_db during the tests is problematic when the tests run
simultaneously or in an undefined order.
So what I'd much rather like to do is this:
class Handler(object):
def __init__(self, db):
self.db = db
def GET(self, id):
data = self.db.get_data(id)
return SimpleView(data).render()
Now, during testing, I construct the handler with whatever fake db I
might consider suited, while in production it gets the real db service
from somewhere above.
I hope that helped clarify my thoughts.
Thanks again,
Ole.
--
Ole Trenner
<[email protected]>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"web.py" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/webpy?hl=en
-~----------~----~----~----~------~----~------~--~---