{"id":15170,"date":"2021-05-12T04:24:26","date_gmt":"2021-05-12T08:24:26","guid":{"rendered":"https:\/\/qxf2.com\/blog\/?p=15170"},"modified":"2021-05-12T04:24:26","modified_gmt":"2021-05-12T08:24:26","slug":"mocking-date-using-python-freezegun-library","status":"publish","type":"post","link":"https:\/\/qxf2.com\/blog\/mocking-date-using-python-freezegun-library\/","title":{"rendered":"Mocking date using Python freezegun library"},"content":{"rendered":"<p><span style=\"font-weight: 400;\"> Recently, I used <a href=\"https:\/\/pypi.org\/project\/freezegun\/\">freezegun<\/a> library to mock date for writing unit test for microservices endpoint. This is a fairly new library and I could not find many examples online. So, I wanted to blog about my experience of using this library. Freezegun library allows our Python tests to travel through time by mocking the datetime module. Once the decorator or context manager has been invoked, all calls to that particular method will return the time that has been frozen.<br \/>\n<\/span><\/p>\n<hr \/>\n<h3><b>Use case under test:<\/b><\/h3>\n<p>I wanted to test a microservice endpoint &#8216;comments_reviewer&#8217;, this endpoint returns a message containing the names of two comment reviewers if the given date is a Thursday. On the remaining days, the endpoint will publish a message as, &#8220;No comments reviewers for today&#8221;. Based on the use case, I have written the following two test cases. <\/p>\n<p>a. On a specific date, which falls on Thursday, the endpoint will display a message showing two comment reviewer&#8217;s names.<br \/>\nb. On other dates, a message &#8216;No comments reviewers&#8217; will be shown.<\/p>\n<p>I used freezegun Python library, which allowed me to freeze a particular date using freeze time decorator.<\/p>\n<hr \/>\n<h3><b>Test case:<\/b><\/h3>\n<pre lang=\"Python\">\r\n\"\"\"\r\nUnit tests for comment reviewer's endpoint\r\n\"\"\"\r\nimport main\r\nfrom unittest.mock import patch\r\nfrom messages import comments_reviewer\r\nfrom freezegun import freeze_time\r\n\r\n@freeze_time(\"2021-03-18\")\r\n@patch('messages.comments_reviewer.messages',{'2021-03-18':\"Today's comment reviewer's are A and B\"})\r\ndef test_get_comment_reviewers():\r\n    \"asserting message as per date\"\r\n    message = main.get_comment_reviewers()\r\n    assert message['msg'] == \"Today's comment reviewer's are A and B\"\r\n\r\n@freeze_time(\"2021-03-29\")\r\n@patch('messages.comments_reviewer.messages',{'2021-03-29':\"No comment reviewer's for today\"})\r\ndef test_no_comment_reviewers():\r\n    \"asserting message as per date\"\r\n    message = main.get_comment_reviewers()\r\n    assert message['msg']==\"No comment reviewer's for today\"\r\n    \r\n<\/pre>\n<p>After running test following output will be shown:<\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2021\/05\/freezegun_python.png\" data-rel=\"lightbox-image-0\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2021\/05\/freezegun_python.png\" alt=\"\"><\/a><\/a><\/p>\n<hr\/>\n<p>I hope this blog helped you to get acquainted and use this library quickly. Thanks for reading!<\/p>\n<hr\/>\n","protected":false},"excerpt":{"rendered":"<p>Recently, I used freezegun library to mock date for writing unit test for microservices endpoint. This is a fairly new library and I could not find many examples online. So, I wanted to blog about my experience of using this library. Freezegun library allows our Python tests to travel through time by mocking the datetime module. Once the decorator or [&hellip;]<\/p>\n","protected":false},"author":28,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[284,18],"tags":[],"class_list":["post-15170","post","type-post","status-publish","format-standard","hentry","category-freezegun","category-python"],"_links":{"self":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/15170","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/users\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/comments?post=15170"}],"version-history":[{"count":25,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/15170\/revisions"}],"predecessor-version":[{"id":15567,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/15170\/revisions\/15567"}],"wp:attachment":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/media?parent=15170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/categories?post=15170"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/tags?post=15170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}