On 6/26/07, <b class="gmail_sendername">Christian Simms</b> &lt;<a href="mailto:christian.simms@gmail.com">christian.simms@gmail.com</a>&gt; wrote:<div><span class="gmail_quote"></span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<span class="q"></span>Looks like I mis-assumed your problem, sorry.&nbsp; In the setUp method in your tests, you should return a Deferred which fires when all your data is loaded.&nbsp; 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&#39;s a pain because trial doesn&#39;t create an application object like running inside twistd does.&nbsp; 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.
</blockquote><div><br>I&#39;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&#39;m stuck at the point of how to do this. In my factory, I call this method in __init__ to load data:
<br><br>&nbsp; def loadDevelopers(self):<br>&nbsp;&nbsp;&nbsp; def cb(results):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for result in results:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.addDeveloper(Developer(result[&#39;auth_host&#39;],result[&#39;auth_url&#39;],<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; result[&#39;devid&#39;],self,result[&#39;key&#39;],result[&#39;post_host&#39;],
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; result[&#39;post_url&#39;]))<br>&nbsp;&nbsp;&nbsp; return self.dbPool.runQuery(&quot;select * from developers where active=1&quot;).\<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; addCallback(cb).addErrback(printError)<br><br>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&#39;t been able to figure out how to tell my tests to run after this data is loaded. Here&#39;s my setUp method:
<br><br>&nbsp;&nbsp;&nbsp; def setUp(self):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.file = StringIOWithoutClosing()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.transport = FileWrapperThatWorks(self.file)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.protocol = sserver.SServerProtocol()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.protocol.factory
 = sserver.SServerFactory(self.protocol)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.protocol.factory.dbPool.start()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.protocol.makeConnection(self.transport)<br><br>&nbsp;&nbsp;&nbsp; def testSendDeveloperID(self):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print self.protocol.factory.developers
 # This dictionary prints as empty - it shouldn&#39;t!<br><br>I guess I&#39;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&#39;s my tac file:
<br><br>factory = SServerFactory(protocol=SServerProtocol)<br>application = service.Application(&quot;SServer&quot;)<br>sserverService = internet.TCPServer(4000,factory)<br>sserverService.setServiceParent(application)<br>
<br>I appreciate the help!<br><br>Brendon<br></div></div>