Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Two choices.</p> <ol> <li><p>Mock out datetime by providing your own. Since the local directory is searched before the standard library directories, you can put your tests in a directory with your own mock version of datetime. This is harder than it appears, because you don't know all the places datetime is secretly used.</p></li> <li><p>Use <strong>Strategy</strong>. Replace explicit references to <code>datetime.date.today()</code> and <code>datetime.date.now()</code> in your code with a <strong>Factory</strong> that generates these. The <strong>Factory</strong> must be configured with the module by the application (or the unittest). This configuration (called "Dependency Injection" by some) allows you to replace the normal run-time <strong>Factory</strong> with a special test factory. You gain a lot of flexibility with no special case handling of production. No "if testing do this differently" business.</p></li> </ol> <p>Here's the <strong>Strategy</strong> version.</p> <pre><code>class DateTimeFactory( object ): """Today and now, based on server's defined locale. A subclass may apply different rules for determining "today". For example, the broswer's time-zone could be used instead of the server's timezone. """ def getToday( self ): return datetime.date.today() def getNow( self ): return datetime.datetime.now() class Event( models.Model ): dateFactory= DateTimeFactory() # Definitions of "now" and "today". ... etc. ... def is_over( self ): return dateFactory.getToday() &gt; self.date_end class DateTimeMock( object ): def __init__( self, year, month, day, hour=0, minute=0, second=0, date=None ): if date: self.today= date self.now= datetime.datetime.combine(date,datetime.time(hour,minute,second)) else: self.today= datetime.date(year, month, day ) self.now= datetime.datetime( year, month, day, hour, minute, second ) def getToday( self ): return self.today def getNow( self ): return self.now </code></pre> <p>Now you can do this</p> <pre><code>class SomeTest( unittest.TestCase ): def setUp( self ): tomorrow = datetime.date.today() + datetime.timedelta(1) self.dateFactoryTomorrow= DateTimeMock( date=tomorrow ) yesterday = datetime.date.today() + datetime.timedelta(1) self.dateFactoryYesterday= DateTimeMock( date=yesterday ) def testThis( self ): x= Event( ... ) x.dateFactory= self.dateFactoryTomorrow self.assertFalse( x.is_over() ) x.dateFactory= self.dateFactoryYesterday self.asserTrue( x.is_over() ) </code></pre> <p>In the long run, you more-or-less <strong>must</strong> do this to account for browser locale separate from server locale. Using default <code>datetime.datetime.now()</code> uses the server's locale, which may piss off users who are in a different time zone.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload