-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cache.py
More file actions
99 lines (82 loc) · 3.07 KB
/
Copy pathtest_cache.py
File metadata and controls
99 lines (82 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import pytest
import tempfile
from gh_similarity_detector.infrastructure.cache.fingerprint_cache import FingerprintCache
from gh_similarity_detector.models.entities import Module
from gh_similarity_detector.models.enums import ModuleType
from gh_similarity_detector.models.entities import FingerprintSet
@pytest.fixture
def cache_dir():
with tempfile.TemporaryDirectory() as d:
yield d
@pytest.fixture
def cache(cache_dir):
return FingerprintCache(cache_dir)
@pytest.fixture
def sample_module():
return Module(
name="test_func",
file_path="test.py",
module_type=ModuleType.FUNCTION,
source_code="def foo(x, y): return x + y",
start_line=1,
end_line=1,
language="python",
token_count=10,
)
class TestFingerprintCache:
def test_put_and_get(self, cache, sample_module):
fp_set = FingerprintSet(
module_id=sample_module.id,
winnowing_fingerprints={100, 200, 300},
ast_fingerprints={400, 500},
token_count=10,
)
cache.put(sample_module, fp_set)
result = cache.get(sample_module)
assert result is not None
assert result.winnowing_fingerprints == {100, 200, 300}
assert result.ast_fingerprints == {400, 500}
def test_cache_miss(self, cache, sample_module):
result = cache.get(sample_module)
assert result is None
def test_content_change_invalidates(self, cache, sample_module):
fp_set = FingerprintSet(
module_id=sample_module.id, winnowing_fingerprints={100, 200}, token_count=5
)
cache.put(sample_module, fp_set)
modified = Module(
name="test_func",
file_path="test.py",
module_type=ModuleType.FUNCTION,
source_code="def foo(x, y): return x * y",
start_line=1,
end_line=1,
language="python",
token_count=10,
)
result = cache.get(modified)
assert result is None
def test_flush_persists(self, cache_dir, sample_module):
cache1 = FingerprintCache(cache_dir)
fp_set = FingerprintSet(
module_id=sample_module.id, winnowing_fingerprints={100}, token_count=5
)
cache1.put(sample_module, fp_set)
cache1.flush()
cache2 = FingerprintCache(cache_dir)
result = cache2.get(sample_module)
assert result is not None
assert result.winnowing_fingerprints == {100}
def test_invalidate(self, cache, sample_module):
fp_set = FingerprintSet(
module_id=sample_module.id, winnowing_fingerprints={100}, token_count=5
)
cache.put(sample_module, fp_set)
cache.invalidate(sample_module.id)
assert cache.get(sample_module) is None
def test_content_hash_deterministic(self):
h1 = FingerprintCache.compute_content_hash("def foo(): pass")
h2 = FingerprintCache.compute_content_hash("def foo(): pass")
assert h1 == h2
h3 = FingerprintCache.compute_content_hash("def bar(): pass")
assert h1 != h3