33# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
44
55import textwrap
6+ from datetime import datetime
67
8+ from dateutil .relativedelta import relativedelta
79from freezegun import freeze_time
810
911from odoo import fields
1618class TestEndpoint (CommonEndpoint ):
1719 @classmethod
1820 def _setup_records (cls ):
21+ # pylint: disable=missing-return
1922 super ()._setup_records ()
2023 cls .endpoint1 = cls .env .ref ("endpoint.endpoint_demo_1" )
2124 cls .cron = cls .env .ref ("endpoint_cache_preheat.cron_endpoint_cache_preheat" )
@@ -27,7 +30,7 @@ def _run_cron(self):
2730 def test_cron_preheat_cache (self ):
2831 self .assertFalse (self .endpoint1 .cache_preheat )
2932 self .assertFalse (self .endpoint1 .cache_preheat_ts )
30- now = fields . Datetime . from_string ( "2025-02-19 23:00:00" )
33+ now = datetime . now (). replace ( microsecond = 0 )
3134 ep_daily = self .endpoint1 .copy ({"route" : "/daily" })
3235 ep_daily .cache_preheat = True
3336 ep_daily .cache_preheat_ts = now
@@ -46,14 +49,15 @@ def test_cron_preheat_cache(self):
4649 )
4750 ep_weekly = ep_daily .copy ({"route" : "/weekly" , "cache_policy" : "week" })
4851 ep_monthly = ep_daily .copy ({"route" : "/monthly" , "cache_policy" : "month" })
52+
4953 # 1 day later
50- future_date = fields . Datetime . from_string ( "2025-02-20 20:00:00" )
51- with trap_jobs () as trap , freeze_time (future_date ), MockRequest (self .env ):
54+ future_date_1 = now + relativedelta ( days = 1 )
55+ with trap_jobs () as trap , freeze_time (future_date_1 ), MockRequest (self .env ):
5256 self ._run_cron ()
5357 trap .assert_jobs_count (1 )
5458 trap .assert_enqueued_job (ep_daily ._cron_endpoint_cache_preheat )
5559 trap .perform_enqueued_jobs ()
56- self .assertEqual (ep_daily .cache_preheat_ts , future_date )
60+ self .assertEqual (ep_daily .cache_preheat_ts , future_date_1 )
5761 self .assertEqual (ep_weekly .cache_preheat_ts , now )
5862 self .assertEqual (ep_monthly .cache_preheat_ts , now )
5963
@@ -75,22 +79,23 @@ def test_cron_preheat_cache(self):
7579 ),
7680 0 ,
7781 )
82+
7883 # 2 days later
79- future_date = fields . Datetime . from_string ( "2025-02-21 21:30:00" )
80- with trap_jobs () as trap , freeze_time (future_date ), MockRequest (self .env ):
84+ future_date_2 = now + relativedelta ( days = 2 )
85+ with trap_jobs () as trap , freeze_time (future_date_2 ), MockRequest (self .env ):
8186 self ._run_cron ()
8287 trap .assert_jobs_count (1 )
8388 trap .assert_enqueued_job (ep_daily ._cron_endpoint_cache_preheat )
8489 trap .perform_enqueued_jobs ()
85- self .assertEqual (ep_daily .cache_preheat_ts , future_date )
90+ self .assertEqual (ep_daily .cache_preheat_ts , future_date_2 )
8691 self .assertEqual (ep_weekly .cache_preheat_ts , now )
8792 self .assertEqual (ep_monthly .cache_preheat_ts , now )
8893
8994 self .assertEqual (
9095 self .env ["ir.attachment" ].search_count (
9196 ep_daily ._endpoint_view_cache_domain ()
9297 ),
93- 1 ,
98+ 2 ,
9499 )
95100 self .assertEqual (
96101 self .env ["ir.attachment" ].search_count (
@@ -104,22 +109,23 @@ def test_cron_preheat_cache(self):
104109 ),
105110 0 ,
106111 )
112+
107113 # 1 week later
108- future_date = fields . Datetime . from_string ( "2025-02-26 19:45:00" )
109- with trap_jobs () as trap , freeze_time (future_date ), MockRequest (self .env ):
114+ future_date_3 = now + relativedelta ( weeks = 1 )
115+ with trap_jobs () as trap , freeze_time (future_date_3 ), MockRequest (self .env ):
110116 self ._run_cron ()
111117 trap .assert_jobs_count (2 )
112118 trap .assert_enqueued_job (ep_daily ._cron_endpoint_cache_preheat )
113119 trap .perform_enqueued_jobs ()
114- self .assertEqual (ep_daily .cache_preheat_ts , future_date )
115- self .assertEqual (ep_weekly .cache_preheat_ts , future_date )
120+ self .assertEqual (ep_daily .cache_preheat_ts , future_date_3 )
121+ self .assertEqual (ep_weekly .cache_preheat_ts , future_date_3 )
116122 self .assertEqual (ep_monthly .cache_preheat_ts , now )
117123
118124 self .assertEqual (
119125 self .env ["ir.attachment" ].search_count (
120126 ep_daily ._endpoint_view_cache_domain ()
121127 ),
122- 2 ,
128+ 3 ,
123129 )
124130 self .assertEqual (
125131 self .env ["ir.attachment" ].search_count (
@@ -133,22 +139,23 @@ def test_cron_preheat_cache(self):
133139 ),
134140 0 ,
135141 )
142+
136143 # 1 month later
137- future_date = fields . Datetime . from_string ( "2025-03-19 23:45:00" )
138- with trap_jobs () as trap , freeze_time (future_date ), MockRequest (self .env ):
144+ future_date_4 = now + relativedelta ( months = 1 )
145+ with trap_jobs () as trap , freeze_time (future_date_4 ), MockRequest (self .env ):
139146 self ._run_cron ()
140147 trap .assert_jobs_count (3 )
141148 trap .assert_enqueued_job (ep_daily ._cron_endpoint_cache_preheat )
142149 trap .perform_enqueued_jobs ()
143- self .assertEqual (ep_daily .cache_preheat_ts , future_date )
144- self .assertEqual (ep_weekly .cache_preheat_ts , future_date )
145- self .assertEqual (ep_monthly .cache_preheat_ts , future_date )
150+ self .assertEqual (ep_daily .cache_preheat_ts , future_date_4 )
151+ self .assertEqual (ep_weekly .cache_preheat_ts , future_date_4 )
152+ self .assertEqual (ep_monthly .cache_preheat_ts , future_date_4 )
146153
147154 self .assertEqual (
148155 self .env ["ir.attachment" ].search_count (
149156 ep_daily ._endpoint_view_cache_domain ()
150157 ),
151- 3 ,
158+ 4 ,
152159 )
153160 self .assertEqual (
154161 self .env ["ir.attachment" ].search_count (
0 commit comments