[Twisted-Python] Testing with trial, adbapi questions

Brendon Colby brendoncolby at gmail.com
Fri Jul 20 11:40:45 EDT 2007


On 6/26/07, Christian Simms <christian.simms at gmail.com> wrote:
>
> Looks like I mis-assumed your problem, sorry.  In the setUp method in your
> tests, you should return a Deferred which fires when all your data is
> loaded.  This can be a pain if you already organized your code into classes
> inheriting from twisted.application.service.Service which have
> startService/stopService calls. It's a pain because trial doesn't create an
> application object like running inside twistd does.  So, in the case of
> writing unit tests for applications, you can abstract out your
> initialization code into a method, and then call that method directly in
> your setUp method.


I've spent some time working on this, and I see the logic in doing so
(firing a deferred when my data is loaded) but I'm stuck at the point of how
to do this. In my factory, I call this method in __init__ to load data:

  def loadDevelopers(self):
    def cb(results):
      for result in results:
        self.addDeveloper(Developer(result['auth_host'],result['auth_url'],
          result['devid'],self,result['key'],result['post_host'],
          result['post_url']))
    return self.dbPool.runQuery("select * from developers where active=1").\
      addCallback(cb).addErrback(printError)

I created a tac file and am using twistd to run this and it works great. But
in my unit tests, what happens is my tests run before my data is loaded (and
fail). I haven't been able to figure out how to tell my tests to run after
this data is loaded. Here's my setUp method:

    def setUp(self):
        self.file = StringIOWithoutClosing()
        self.transport = FileWrapperThatWorks(self.file)
        self.protocol = sserver.SServerProtocol()
        self.protocol.factory = sserver.SServerFactory(self.protocol)
        self.protocol.factory.dbPool.start()
        self.protocol.makeConnection(self.transport)

    def testSendDeveloperID(self):
        print self.protocol.factory.developers # This dictionary prints as
empty - it shouldn't!

I guess I'm not making the connection here. How do I code the deferred in
setUp to fire when my data is loaded? Also, if I abstract out this logic to
an initialization method, how would I have it fire properly when I run under
twistd? Here's my tac file:

factory = SServerFactory(protocol=SServerProtocol)
application = service.Application("SServer")
sserverService = internet.TCPServer(4000,factory)
sserverService.setServiceParent(application)

I appreciate the help!

Brendon
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20070720/9fe68afc/attachment.htm 


More information about the Twisted-Python mailing list