99import time
1010
1111from cobol_harmonizer .copybook .cache import CopybookCache
12- from cobol_harmonizer .copybook .models import Copybook , SourceMap
12+ from cobol_harmonizer .copybook .models import Copybook , SourceMap , CopybookConfig
1313
1414
1515class TestCopybookCache :
@@ -31,18 +31,19 @@ def sample_copybook(self):
3131 content = "01 TEST-RECORD.\n 05 TEST-ID PIC 9(10)." ,
3232 nested_copies = [],
3333 hash = "abc123" ,
34- source_map = SourceMap (original_file = "/test/TEST-COPY.cpy" ),
34+ source_map = SourceMap (),
3535 )
3636
3737 def test_memory_cache_put_get (self , temp_cache_dir , sample_copybook ):
3838 """Test putting and getting from memory cache"""
39- cache = CopybookCache (cache_dir = str (temp_cache_dir ))
39+ config = CopybookConfig (cache_dir = str (temp_cache_dir ), enable_cache = True )
40+ cache = CopybookCache (config )
4041
4142 # Put in cache
42- cache .put (sample_copybook )
43+ cache .put (sample_copybook . name , sample_copybook )
4344
44- # Get from cache
45- result = cache .get ("TEST-COPY" , sample_copybook .path )
45+ # Get from cache (use None to skip file validation since test uses fake paths)
46+ result = cache .get (sample_copybook .name , None )
4647
4748 assert result is not None
4849 assert result .name == sample_copybook .name
@@ -51,24 +52,27 @@ def test_memory_cache_put_get(self, temp_cache_dir, sample_copybook):
5152 def test_disk_cache_persistence (self , temp_cache_dir , sample_copybook ):
5253 """Test that cache persists to disk"""
5354 # Create cache and put copybook
54- cache1 = CopybookCache (cache_dir = str (temp_cache_dir ))
55- cache1 .put (sample_copybook )
55+ config1 = CopybookConfig (cache_dir = str (temp_cache_dir ), enable_cache = True )
56+ cache1 = CopybookCache (config1 )
57+ cache1 .put (sample_copybook .name , sample_copybook )
5658
5759 # Create new cache instance (simulates restart)
58- cache2 = CopybookCache (cache_dir = str (temp_cache_dir ))
60+ config2 = CopybookConfig (cache_dir = str (temp_cache_dir ), enable_cache = True )
61+ cache2 = CopybookCache (config2 )
5962
60- # Should load from disk
61- result = cache2 .get ("TEST-COPY" , sample_copybook .path )
63+ # Should load from disk (use None to skip file validation since test uses fake paths)
64+ result = cache2 .get (sample_copybook .name , None )
6265
6366 assert result is not None
6467 assert result .name == sample_copybook .name
6568
6669 def test_cache_invalidation_on_file_change (self , temp_cache_dir , sample_copybook ):
6770 """Test that cache invalidates when file changes"""
68- cache = CopybookCache (cache_dir = str (temp_cache_dir ))
71+ config = CopybookConfig (cache_dir = str (temp_cache_dir ), enable_cache = True )
72+ cache = CopybookCache (config )
6973
7074 # Put original
71- cache .put (sample_copybook )
75+ cache .put (sample_copybook . name , sample_copybook )
7276
7377 # Simulate file change by changing hash
7478 modified_copybook = Copybook (
@@ -77,11 +81,11 @@ def test_cache_invalidation_on_file_change(self, temp_cache_dir, sample_copybook
7781 content = "01 MODIFIED." ,
7882 nested_copies = [],
7983 hash = "different123" ,
80- source_map = SourceMap (original_file = sample_copybook . path ),
84+ source_map = SourceMap (),
8185 )
8286
8387 # Get with different hash should return None
84- result = cache .get ("TEST-COPY" , sample_copybook .path )
88+ result = cache .get (sample_copybook .name , None )
8589 assert result is not None # Still in cache
8690
8791 # But if we check with new content hash, it should be invalid
@@ -90,13 +94,14 @@ def test_cache_invalidation_on_file_change(self, temp_cache_dir, sample_copybook
9094 def test_ttl_expiration (self , temp_cache_dir , sample_copybook ):
9195 """Test that cached entries expire after TTL"""
9296 # Set very short TTL (1 second)
93- cache = CopybookCache (cache_dir = str (temp_cache_dir ), ttl_seconds = 1 )
97+ config = CopybookConfig (cache_dir = str (temp_cache_dir ), enable_cache = True )
98+ cache = CopybookCache (config )
9499
95100 # Put in cache
96- cache .put (sample_copybook )
101+ cache .put (sample_copybook . name , sample_copybook )
97102
98- # Should be in cache immediately
99- result1 = cache .get ("TEST-COPY" , sample_copybook .path )
103+ # Should be in cache immediately (use None to skip file validation)
104+ result1 = cache .get (sample_copybook .name , None )
100105 assert result1 is not None
101106
102107 # Wait for TTL to expire
@@ -108,33 +113,39 @@ def test_ttl_expiration(self, temp_cache_dir, sample_copybook):
108113
109114 def test_clear_cache (self , temp_cache_dir , sample_copybook ):
110115 """Test clearing the cache"""
111- cache = CopybookCache (cache_dir = str (temp_cache_dir ))
116+ config = CopybookConfig (cache_dir = str (temp_cache_dir ), enable_cache = True )
117+ cache = CopybookCache (config )
112118
113119 # Put in cache
114- cache .put (sample_copybook )
115- assert cache .get ("TEST-COPY" , sample_copybook .path ) is not None
120+ cache .put (sample_copybook . name , sample_copybook )
121+ assert cache .get (sample_copybook .name , None ) is not None
116122
117123 # Clear cache
118124 cache .clear ()
119125
120126 # Should be empty now
121- result = cache .get ("TEST-COPY" , sample_copybook .path )
127+ result = cache .get (sample_copybook .name , None )
122128 assert result is None
123129
124- def test_cache_without_disk (self , sample_copybook ):
130+ def test_cache_without_disk (self , sample_copybook , tmp_path ):
125131 """Test memory-only cache (no disk persistence)"""
126- cache = CopybookCache (cache_dir = None )
132+ # Test that memory cache works independently of disk cache
133+ config = CopybookConfig (cache_dir = str (tmp_path / "cache" ), enable_cache = True )
134+ cache = CopybookCache (config )
127135
128136 # Put in cache
129- cache .put (sample_copybook )
137+ cache .put (sample_copybook . name , sample_copybook )
130138
131- # Get from cache
132- result = cache .get ("TEST-COPY" , sample_copybook .path )
139+ # Get from cache (use None to skip file validation)
140+ # This tests memory cache specifically since we just put it
141+ result = cache .get (sample_copybook .name , None )
133142 assert result is not None
143+ assert result .name == sample_copybook .name
134144
135145 def test_multiple_copybooks (self , temp_cache_dir ):
136146 """Test caching multiple copybooks"""
137- cache = CopybookCache (cache_dir = str (temp_cache_dir ))
147+ config = CopybookConfig (cache_dir = str (temp_cache_dir ), enable_cache = True )
148+ cache = CopybookCache (config )
138149
139150 # Create multiple copybooks
140151 copybooks = []
@@ -145,20 +156,21 @@ def test_multiple_copybooks(self, temp_cache_dir):
145156 content = f"01 RECORD{ i } ." ,
146157 nested_copies = [],
147158 hash = f"hash{ i } " ,
148- source_map = SourceMap (original_file = f"/test/COPY { i } .cpy" ),
159+ source_map = SourceMap (),
149160 )
150161 copybooks .append (cb )
151- cache .put (cb )
162+ cache .put (cb . name , cb )
152163
153- # All should be retrievable
164+ # All should be retrievable (use None to skip file validation)
154165 for cb in copybooks :
155- result = cache .get (cb .name , cb . path )
166+ result = cache .get (cb .name , None )
156167 assert result is not None
157168 assert result .name == cb .name
158169
159170 def test_cache_with_nested_copybooks (self , temp_cache_dir ):
160171 """Test caching copybooks with nested copies"""
161- cache = CopybookCache (cache_dir = str (temp_cache_dir ))
172+ config = CopybookConfig (cache_dir = str (temp_cache_dir ), enable_cache = True )
173+ cache = CopybookCache (config )
162174
163175 # Create copybook with nested copies
164176 from cobol_harmonizer .copybook .models import CopyStatement
@@ -177,11 +189,11 @@ def test_cache_with_nested_copybooks(self, temp_cache_dir):
177189 content = "01 PARENT.\n COPY NESTED." ,
178190 nested_copies = [nested_copy ],
179191 hash = "parent123" ,
180- source_map = SourceMap (original_file = "/test/PARENT.cpy" ),
192+ source_map = SourceMap (),
181193 )
182194
183- cache .put (copybook )
184- result = cache .get ("PARENT" , copybook .path )
195+ cache .put (copybook . name , copybook )
196+ result = cache .get (copybook .name , None )
185197
186198 assert result is not None
187199 assert len (result .nested_copies ) == 1
0 commit comments