Skip to content

Commit 4de167b

Browse files
[#253]: Test SQLite cache requests document loader
1 parent 2ee81df commit 4de167b

1 file changed

Lines changed: 117 additions & 0 deletions

File tree

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
"""Tests for SqliteCacheRequestsDocumentLoader and HTTP cache behavior."""
2+
3+
import json
4+
import threading
5+
from http.server import BaseHTTPRequestHandler, HTTPServer
6+
from pathlib import Path
7+
8+
import pytest
9+
10+
from pyld import (
11+
DocumentLoader,
12+
RequestsDocumentLoader,
13+
SqliteCacheRequestsDocumentLoader,
14+
)
15+
16+
requests_cache = pytest.importorskip('requests_cache')
17+
CachedSession = requests_cache.CachedSession
18+
19+
20+
class _ContextHandler(BaseHTTPRequestHandler):
21+
request_count = 0
22+
23+
def do_GET(self):
24+
type(self).request_count += 1
25+
body = json.dumps({
26+
'@context': {'name': 'http://example.org/name'},
27+
}).encode()
28+
self.send_response(200)
29+
self.send_header('Content-Type', 'application/ld+json')
30+
self.send_header('Cache-Control', 'max-age=3600')
31+
self.end_headers()
32+
self.wfile.write(body)
33+
34+
def log_message(self, format, *args):
35+
pass
36+
37+
38+
@pytest.fixture
39+
def context_url():
40+
"""Local HTTP server returning JSON-LD with Cache-Control: max-age=3600."""
41+
_ContextHandler.request_count = 0
42+
server = HTTPServer(('127.0.0.1', 0), _ContextHandler)
43+
thread = threading.Thread(target=server.serve_forever, daemon=True)
44+
thread.start()
45+
port = server.server_address[1]
46+
url = f'http://127.0.0.1:{port}/context.jsonld'
47+
yield url
48+
server.shutdown()
49+
50+
51+
def test_requests_document_loader_accepts_custom_session():
52+
"""RequestsDocumentLoader accepts a CachedSession via session=."""
53+
loader = RequestsDocumentLoader(
54+
session=CachedSession(backend='memory', cache_control=True))
55+
assert isinstance(loader, DocumentLoader)
56+
assert callable(loader)
57+
loader.session.close()
58+
59+
60+
def test_sqlite_cache_requests_document_loader_is_document_loader():
61+
"""Sqlite loader is a DocumentLoader composing RequestsDocumentLoader."""
62+
loader = SqliteCacheRequestsDocumentLoader()
63+
assert isinstance(loader, DocumentLoader)
64+
assert isinstance(loader._loader, RequestsDocumentLoader)
65+
assert callable(loader)
66+
loader.session.close()
67+
68+
69+
def test_sqlite_cache_requests_document_loader_rejects_relative_sqlite_file_path():
70+
"""Relative sqlite_file_path is rejected."""
71+
with pytest.raises(ValueError, match='absolute path'):
72+
SqliteCacheRequestsDocumentLoader(
73+
sqlite_file_path=Path('relative.sqlite'))
74+
75+
76+
def test_http_cache_headers_serve_from_cache_with_cache_control(context_url):
77+
"""With cache_control=True, Cache-Control max-age avoids a second HTTP hit."""
78+
loader = RequestsDocumentLoader(
79+
session=CachedSession(
80+
'test_memory_cache_control',
81+
backend='memory',
82+
cache_control=True,
83+
))
84+
loader(context_url)
85+
loader(context_url)
86+
assert _ContextHandler.request_count == 1
87+
loader.session.close()
88+
89+
90+
def test_http_cache_headers_without_cache_control_hits_server_twice(context_url):
91+
"""With cache_control=False, response Cache-Control headers are ignored."""
92+
loader = RequestsDocumentLoader(
93+
session=CachedSession(
94+
'test_memory_no_cache_control',
95+
backend='memory',
96+
cache_control=False,
97+
expire_after=0,
98+
))
99+
loader(context_url)
100+
loader(context_url)
101+
assert _ContextHandler.request_count == 2
102+
loader.session.close()
103+
104+
105+
def test_sqlite_cache_requests_document_loader_persists(context_url, tmp_path):
106+
"""Second loader instance reuses the on-disk SQLite cache."""
107+
cache_path = tmp_path / 'contexts.sqlite'
108+
loader = SqliteCacheRequestsDocumentLoader(sqlite_file_path=cache_path)
109+
loader(context_url)
110+
assert _ContextHandler.request_count == 1
111+
loader.session.close()
112+
113+
_ContextHandler.request_count = 0
114+
loader = SqliteCacheRequestsDocumentLoader(sqlite_file_path=cache_path)
115+
loader(context_url)
116+
assert _ContextHandler.request_count == 0
117+
loader.session.close()

0 commit comments

Comments
 (0)