Skip to content

Commit 2add243

Browse files
MrButtCodecanihavesomecoffee
authored andcommitted
refactor(ci): isolate test cache using mock patch and addCleanup
1 parent 498207c commit 2add243

2 files changed

Lines changed: 13 additions & 7 deletions

File tree

tests/base.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import os
44
import warnings
5+
import utility
56
from collections import namedtuple
67
from contextlib import contextmanager
78
from unittest import mock
@@ -278,6 +279,14 @@ def create_app(self, mock_config, mock_storage_client):
278279

279280
def setUp(self):
280281
"""Set up all entities."""
282+
# Patch the cache before parent setup runs to prevent HTTP calls
283+
self.cache_patcher = mock.patch.object(
284+
utility, 'cache_has_expired', return_value=False
285+
)
286+
self.cache_patcher.start()
287+
self.addCleanup(self.cache_patcher.stop)
288+
289+
super().setUp()
281290
self.app.preprocess_request()
282291
g.db = create_session(
283292
self.app.config['DATABASE_URI'], drop_tables=True)
@@ -396,6 +405,10 @@ def setUp(self):
396405
g.db.add_all(forbidden_ext)
397406
g.db.commit()
398407

408+
def tearDown(self):
409+
"""Clean up after every test."""
410+
super().tearDown()
411+
399412
@staticmethod
400413
def create_login_form_data(email, password) -> dict:
401414
"""

utility.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,6 @@ def cache_has_expired() -> bool:
8989
:rtype: bool
9090
"""
9191
global cached_load_time
92-
import sys
93-
94-
# Foolproof bypass: if a test framework is running in the Python
95-
# interpreter, bypass the cache to prevent mock pollution.
96-
if 'nose' in sys.modules or 'unittest' in sys.modules:
97-
return True
98-
9992
from datetime import datetime, timedelta
10093
return cached_load_time + timedelta(hours=1) < datetime.now()
10194

0 commit comments

Comments
 (0)