1👍
✅
You need to @patch get_next_friday()
function and substitute it’s return value with the one you need:
date_in_the_future = date.today() + timedelta(50)
next_friday_in_the_future = get_next_friday(base_date=date_in_the_future)
with patch('module_under_test.get_next_friday') as mocked_function:
mocked_function.return_value = next_friday_in_the_future
# call refresh_settlement_date
4👍
An alternative approach would to be to patch the current date.
There is a relevant thread with multiple options:
My favorite option is to use a third-party module called freezegun
.
You would need only one line to add, very clean and readable:
@freeze_time("2014-10-14")
def test_refresh_settlement_date_in_the_future(self):
...
- [Django]-Django duplicate model definition/fields
- [Django]-Passing a Django database pk id to Dash app
- [Django]-Deleting files associated with model – django
1👍
I just tried this out, it seems to work:
first I need to copy the function:
old_get_next_friday = get_next_friday
then patch it:
with patch.object(get_next_friday) as mocked_func:
for i in range(8):
mocked_func.return_value = old_get_next_friday(date.today() + timedelta(days=i))
refresh_settlement_date()
- [Django]-Migrate existing live Django site from one server to another along with user records intact
- [Django]-Unable to connect to server when running docker django container
- [Django]-Remove keys from array in django rest framework serializer
Source:stackexchange.com