Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8465529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:04:36+00:00 2026-06-10T15:04:36+00:00

I have a method in a django model that does a computation relative to

  • 0

I have a method in a django model that does a computation relative to the current time. Here is a snippet:

def next_date():
    now = datetime.now()
    trial_expires = max(self.date_status_changed + timedelta(self.trial_days), now)
    return timezone.datetime(trial_expires.year, trial_expires.month+1, 1, tzinfo=trial_expires.tzinfo)

What’s the proper way to test this in django/python using unittest? What I’d like to do is be able to hard code some values for “now” in the test so I can try the various edge cases. Ideally, I’d like to avoid relying on the current time and date in the test.

One approach would be to modify my method to accept an optional parameter that would override the ‘now’ value it uses. Does python have any functions to do something similar without having to modify my method signature?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T15:04:38+00:00Added an answer on June 10, 2026 at 3:04 pm

    You could extract datetime.now() as a parameter:

    def next_date(nowfunc=datetime.now):
        now = nowfunc()
        ...
    

    or as a class’ dependency:

    class X:
        def __init__(self, nowfunc=datetime.now):
            self._nowfunc = nowfunc
    
    def next_date(self):
        now = self._nowfunc()
        ...
    

    And pass a mock function with the required result from your tests.

    But if you don’t want to modify the signature, use patches:

    @patch.object(datetime, 'now')
    def test_next_date(self, nowfunc):
        nowfunc.return_value = ... # required result
        # the rest of the test
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a factory method that generates django form classes like so: def get_indicator_form(indicator,
I am newbie at Django. I have model with a custom method. In view
I have method that returns module path of given class name def findModulePath(path, className):
I have a custom property on my Django model that returns the full name
I am overriding the clean() method on a Django form. I want to have
I have method that returns Drawable , and if its Bitmap object is recycled
I have method that returned NSManagedObject and I don't know what kind of NSManagedObject
I have method in a class that I need to make sure is only
I've got some 3rd party beans that have method signatures that fit quite well
I have an Address model that contains two float fields, lat and lng .

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.