55from django .contrib .auth import get_user_model
66from django .core .management import call_command
77from django .test import TestCase
8+ from django .test .utils import override_settings
89from django .urls import reverse
10+ from django .utils import timezone
911from requests .exceptions import ConnectionError
1012from rest_framework .test import APIClient
1113
1214from dining .api_wrapper import APIError , DiningAPIWrapper
1315from dining .models import DiningMenu , Venue
16+ from dining .utils .menu_view_cache import get_menu_view_cache
1417
1518
1619User = get_user_model ()
@@ -35,7 +38,12 @@ def json(self):
3538 raise ValueError
3639
3740 with open (file_path ) as data :
38- return Mock (json .load (data ), 200 )
41+ if len (args ) > 0 and "menus" in args [0 ]:
42+ res = json .load (data )
43+ res ["menus" ]["days" ][0 ]["date" ] = str (timezone .now ().date ())
44+ return Mock (res , 200 )
45+ else :
46+ return Mock (json .load (data ), 200 )
3947
4048
4149def mock_request_raise_error (* args , ** kwargs ):
@@ -137,7 +145,7 @@ def test_get_default(self):
137145 self .try_structure (response .json ())
138146
139147 def test_get_date (self ):
140- response = self .client .get ("/dining/menus/2022-10-04 /" )
148+ response = self .client .get ("/dining/menus/" + str ( timezone . now (). date ()) + " /" )
141149 self .try_structure (response .json ())
142150
143151 @mock .patch ("requests.request" , mock_dining_requests )
@@ -149,6 +157,25 @@ def test_skip_venue(self):
149157 self .assertEqual (DiningMenu .objects .count (), 0 )
150158
151159
160+ @override_settings (
161+ CACHES = {
162+ "default" : {
163+ "BACKEND" : "django.core.cache.backends.locmem.LocMemCache" ,
164+ }
165+ }
166+ )
167+ class TestMenuViewCache (TestCase ):
168+ @mock .patch ("requests.post" , mock_dining_requests )
169+ @mock .patch ("requests.request" , mock_dining_requests )
170+ def test_cache_cleared_on_load_next_menu (self ):
171+ call_command ("load_next_menu" )
172+ response = self .client .get (reverse ("menus-with-date" , args = [str (timezone .now ().date ())]))
173+ self .assertEqual (response .status_code , 200 )
174+ self .assertIsNotNone (get_menu_view_cache (str (timezone .now ().date ())))
175+ call_command ("load_next_menu" )
176+ self .assertIsNone (get_menu_view_cache (str (timezone .now ().date ())))
177+
178+
152179class TestPreferences (TestCase ):
153180 def setUp (self ):
154181 call_command ("load_venues" )
0 commit comments