11import os
2+ from unittest .mock import MagicMock , patch
23
34import pytest
45from tests .conftest import REDIS_DB , REDIS_PORT
56from piccione .upload .cache_manager import CacheManager
6- from piccione .upload .on_triplestore import save_failed_query_file , upload_sparql_updates
7+ from piccione .upload .on_triplestore import remove_stop_file , save_failed_query_file , upload_sparql_updates
78from sparqlite import SPARQLClient
89
910SPARQL_ENDPOINT = "http://localhost:28890/sparql"
@@ -24,8 +25,6 @@ def test_add_and_contains(self, clean_redis):
2425 test_file = "test.sparql"
2526 cache_manager .add (test_file )
2627
27- assert test_file in cache_manager .processed_files
28- assert cache_manager ._redis .sismember (CacheManager .REDIS_KEY , test_file )
2928 assert test_file in cache_manager
3029
3130 def test_persistence (self , clean_redis ):
@@ -153,3 +152,81 @@ def test_data_loaded_to_triplestore(self, temp_dir, clean_redis, clean_virtuoso)
153152 bindings = result ["results" ]["bindings" ]
154153 assert len (bindings ) == 1
155154 assert bindings [0 ]["o" ]["value" ] == "test value"
155+
156+ def test_nonexistent_folder_returns_early (self , temp_dir , clean_redis ):
157+ cache_manager = CacheManager (redis_port = REDIS_PORT , redis_db = REDIS_DB )
158+ upload_sparql_updates (
159+ SPARQL_ENDPOINT ,
160+ os .path .join (temp_dir , "nonexistent" ),
161+ cache_manager = cache_manager ,
162+ )
163+ assert cache_manager .get_all () == set ()
164+
165+ def test_empty_folder_returns_early (self , temp_dir , clean_redis , clean_virtuoso ):
166+ sparql_dir = os .path .join (temp_dir , "empty_sparql" )
167+ os .makedirs (sparql_dir )
168+
169+ cache_manager = CacheManager (redis_port = REDIS_PORT , redis_db = REDIS_DB )
170+ upload_sparql_updates (
171+ SPARQL_ENDPOINT ,
172+ sparql_dir ,
173+ cache_manager = cache_manager ,
174+ )
175+ assert cache_manager .get_all () == set ()
176+
177+ def test_empty_query_file_is_skipped (self , temp_dir , clean_redis , clean_virtuoso ):
178+ sparql_dir = os .path .join (temp_dir , "sparql_files" )
179+ os .makedirs (sparql_dir )
180+
181+ with open (os .path .join (sparql_dir , "empty.sparql" ), "w" ) as f :
182+ f .write (" \n " )
183+
184+ cache_manager = CacheManager (redis_port = REDIS_PORT , redis_db = REDIS_DB )
185+ upload_sparql_updates (
186+ SPARQL_ENDPOINT ,
187+ sparql_dir ,
188+ cache_manager = cache_manager ,
189+ show_progress = False ,
190+ )
191+ assert "empty.sparql" in cache_manager
192+
193+ def test_remove_stop_file_when_exists (self , temp_dir ):
194+ stop_file = os .path .join (temp_dir , ".stop_upload" )
195+ with open (stop_file , "w" ) as f :
196+ f .write ("" )
197+
198+ assert os .path .exists (stop_file )
199+ remove_stop_file (stop_file )
200+ assert not os .path .exists (stop_file )
201+
202+ def test_remove_stop_file_when_not_exists (self , temp_dir ):
203+ stop_file = os .path .join (temp_dir , ".stop_upload" )
204+ assert not os .path .exists (stop_file )
205+ remove_stop_file (stop_file )
206+
207+ def test_creates_cache_manager_when_none (self , temp_dir , clean_virtuoso ):
208+ sparql_dir = os .path .join (temp_dir , "sparql_files" )
209+ os .makedirs (sparql_dir )
210+
211+ query = """
212+ INSERT DATA {
213+ GRAPH <http://test.graph> {
214+ <http://test.subject> <http://test.predicate> "value" .
215+ }
216+ }
217+ """
218+ with open (os .path .join (sparql_dir , "test.sparql" ), "w" ) as f :
219+ f .write (query )
220+
221+ mock_cache = MagicMock ()
222+ mock_cache .__contains__ = MagicMock (return_value = False )
223+
224+ with patch ("piccione.upload.on_triplestore.CacheManager" , return_value = mock_cache ):
225+ upload_sparql_updates (
226+ SPARQL_ENDPOINT ,
227+ sparql_dir ,
228+ cache_manager = None ,
229+ show_progress = False ,
230+ )
231+
232+ mock_cache .add .assert_called_once_with ("test.sparql" )
0 commit comments