extending abs_path in db and test coverage
This commit is contained in:
53
tests/test_cached_route.py
Normal file
53
tests/test_cached_route.py
Normal file
@@ -0,0 +1,53 @@
|
||||
import os
|
||||
import tempfile
|
||||
from web.app import app
|
||||
|
||||
|
||||
def test_cached_route_serves_file(monkeypatch):
|
||||
# Create a temporary file in the configured cache dir
|
||||
cache_dir = os.path.abspath(os.path.join(
|
||||
os.path.dirname(__file__), '..', 'cache'))
|
||||
os.makedirs(cache_dir, exist_ok=True)
|
||||
fd, tmp_path = tempfile.mkstemp(
|
||||
prefix='test_cached_', suffix='.html', dir=cache_dir)
|
||||
os.close(fd)
|
||||
with open(tmp_path, 'w', encoding='utf-8') as f:
|
||||
f.write('<html><body>cached</body></html>')
|
||||
|
||||
# Fake job record returned by get_job_by_id
|
||||
fake_job = {
|
||||
'id': 'fake123',
|
||||
'job_id': 'fake123',
|
||||
'file_path': os.path.relpath(tmp_path, cache_dir),
|
||||
'file_path_abs': tmp_path,
|
||||
}
|
||||
|
||||
def fake_get_job_by_id(jid):
|
||||
if str(jid) in ('fake123',):
|
||||
return fake_job
|
||||
return {}
|
||||
|
||||
# Patch the symbol imported into web.app
|
||||
monkeypatch.setattr('web.app.get_job_by_id', fake_get_job_by_id)
|
||||
|
||||
# Request route
|
||||
client = app.test_client()
|
||||
res = client.get('/cached/fake123')
|
||||
assert res.status_code == 200
|
||||
assert b'cached' in res.data
|
||||
|
||||
# Cleanup
|
||||
try:
|
||||
os.remove(tmp_path)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def test_cached_route_missing(monkeypatch):
|
||||
def fake_get_job_by_id(jid):
|
||||
return {}
|
||||
|
||||
monkeypatch.setattr('web.app.get_job_by_id', fake_get_job_by_id)
|
||||
client = app.test_client()
|
||||
res = client.get('/cached/nope')
|
||||
assert res.status_code == 404
|
||||
27
tests/test_cachedpage_abs_path.py
Normal file
27
tests/test_cachedpage_abs_path.py
Normal file
@@ -0,0 +1,27 @@
|
||||
import os
|
||||
from web.db import CachedPage
|
||||
from web.utils import get_cache_dir
|
||||
|
||||
|
||||
def test_cachedpage_abs_path(tmp_path, monkeypatch):
|
||||
# Create a fake cache dir and monkeypatch get_cache_dir
|
||||
fake_cache = tmp_path / 'cache'
|
||||
fake_cache.mkdir()
|
||||
monkeypatch.setenv('PYTHONIOENCODING', 'utf-8')
|
||||
|
||||
# Patch the symbol used by CachedPage.abs_path (imported into web.db)
|
||||
monkeypatch.setattr('web.db.get_cache_dir', lambda: str(fake_cache))
|
||||
|
||||
# Create a CachedPage instance and set file_path attribute
|
||||
cp = CachedPage()
|
||||
setattr(cp, 'file_path', 'subdir/test.html')
|
||||
|
||||
# Ensure the computed absolute path joins the fake cache dir
|
||||
expected = os.path.join(os.path.abspath(
|
||||
str(fake_cache)), 'subdir/test.html')
|
||||
assert cp.abs_path == expected
|
||||
|
||||
# When file_path is falsy, abs_path should be None
|
||||
cp2 = CachedPage()
|
||||
setattr(cp2, 'file_path', None)
|
||||
assert cp2.abs_path is None
|
||||
Reference in New Issue
Block a user