<br><br><div class="gmail_quote">On Thu, May 15, 2008 at 12:21 PM, Marcelo de Moraes Serpa <<a href="mailto:celoserpa@gmail.com">celoserpa@gmail.com</a>> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hello,<br><br>So, I have this particular method, generate_chat_dir_string, which should generate a string in the following format:<br> "md5hexstring-day-month-year-hour-minute"<br><br>This string will be used to create a directory in the filesystem.<br>
<br>I'm trying to adopt the TDD approach, so, I'm starting by testing all the methods of the Chat class, including this one. However, an issue just popped up, this is the test code:<br><br><ol><li><div>
def test_generate_chat_dir_string<span>(</span>self<span>)</span>:</div></li><li><div> import md5</div></li><li><div> import random</div>
</li><li><div> import datetime</div></li><li><div> md5_handler = md5.new<span>(</span><span>)</span></div></li><li><div>
users = <span>[</span><span>{</span>"user":"pedro","atendente":False<span>}</span>,<span>{</span>"user":"joćo","atendente":True<span>}</span><span>]</span></div>
</li><li><div> #salt = random.random<span>(</span><span>)</span>.to_s<span>(</span><span>)</span></div></li><li><div> #md5_handler.update<span>(</span>salt<span>)</span></div>
</li><li><div> now = datetime.datetime.now<span>(</span><span>)</span></div></li><li><div> ts = now.strftime<span>(</span>"%d-%m-%Y-%H-%M"<span>)</span></div>
</li><li><div> for user in users:</div></li><li><div> md5_handler.update<span>(</span>user<span>[</span>"user"<span>]</span><span>)</span></div>
</li><li><div> </div></li><li><div> </div></li><li><div> seed = md5_handler.hexdigest<span>(</span><span>)</span></div>
</li><li><div> </div></li><li><div> final_string = seed + "-" + ts</div></li><li><div> </div></li><li><div>
method_generated_string = self.chat.generated_chat_dir_string<span>(</span>users,seed<span>)</span></div></li><li><div> </div></li><li><div>
self.assertEquals<span>(</span>final_string,method_generated_string<span>)</span></div></li></ol>As you can see, it generates a specific set of data and assembles a final_string. It then feeds the same data to generate_chat_dir_string and compare to see if the final_string is equal to the string generated by the tested method. <br>
<br>However, they might not be equal becouse of the temporal data. In this case I'm not using seconds, but in a bizarre situation a minute could have passed and the test would faile becouse the two strings would have a different minute portion. Something like this:<br>
<br>"b2ef9c7b10eb0985365f913420ccb84a-30-10-2008-10-31"<br>"b2ef9c7b10eb0985365f913420ccb84a-30-10-2008-10-32"<br><br>How could I handle this issue?<br><br>Thanks in advance,</blockquote><div><br>Add another parameter, with a default, to the method's interface:<br>
<br>def test_generate_chat_dir_string<span>(</span>self<span>, now=None)</span>:<br> if not now:<br> import datetime<br> now = datetime.datetime.now()<br> .....<br><br><br>That way you can feed it values when testing to validate the calculations but leave it up to the datetime module to fill in the used value in production.<br>
<br><br></div></div><br>-- <br>Erik Jones<br><a href="mailto:mage2k@gmail.com">mage2k@gmail.com</a><br>