1

My application provides a status depending on the date and time of day that is provided as input to a custom URL. Normally it will use the current time (datetime.now()), but I'd like to create unit tests to verify future times. However, I'm having trouble using a timestamp as the variable to test. Ideas? Here is the code so far:

urls.py:

urlpatterns = patterns('',
   (r'^/(\w+)/status/$', 'app.views.fetch_status'),
   # Normally:
   #(r'^/(\w+)/status/$', 'app.views.fetch_status', {'timestamp': datetime.now() }),
   ...

views.py:

def fetch_status(request, slug, timestamp):
    ...

tests.py:

class TimeTests(TestCase):
    fixtures = ['hours.json']

    def testSchedule(self):
        timestamp = datetime(2012, 6, 19, 5, 0)
        client = Client()
        response = client.get('/service/status/', {'timestamp': timestamp})
        self.assertEqual(response.context['status'], 'closed')

Result:

TypeError: fetch_status() takes exactly 2 arguments (1 given)

1 Answers1

1

Why did you change the pattern to not include the timestamp parameter? From the looks of it, and based on your error, the function is expecting one more argument, which you are not really providing because you changed the pattern.

Jordan
  • 4,928
  • 4
  • 26
  • 39
  • I'd like to override the timestamp in my unit test. If I leave that line uncommented, it uses datetime.now() instead of the override. –  Jun 25 '12 at 20:45
  • 1
    why not create an empty datetime object that you can then pass in? or change the `fetch_status` function to take fewer arguments and just treat the test datetime as part of the payload – Jordan Jun 25 '12 at 20:47
  • I created an optional parameter that accepts a unix timestamp via the URL. Thanks! –  Jun 25 '12 at 21:21