55import shutil
66import os
77
8+
89# Fixture to create a temporary directory for each test
910@pytest .fixture
1011def temp_dir (tmp_path ):
1112 """Provides a temporary directory path for tests."""
1213 yield tmp_path
1314 # Cleanup happens automatically via pytest's tmp_path fixture
1415
16+
1517# Fixture to create a temporary directory with a pre-initialized OntoEnv
1618@pytest .fixture
1719def existing_env_dir (tmp_path ):
@@ -23,11 +25,12 @@ def existing_env_dir(tmp_path):
2325 env = OntoEnv (config = cfg , path = env_path , recreate = True )
2426 # Add a dummy file to ensure the env is not empty if needed later
2527 # For now, just initializing is enough to create the .ontoenv structure
26- env .flush () # Ensure data is written if not temporary
28+ env .flush () # Ensure data is written if not temporary
2729 del env
2830 yield env_path
2931 # Cleanup happens automatically via pytest's tmp_path fixture
3032
33+
3134def test_init_with_config_new_dir (temp_dir ):
3235 """Test initializing OntoEnv with a Config in a new directory."""
3336 env_path = temp_dir / "new_env"
@@ -36,7 +39,10 @@ def test_init_with_config_new_dir(temp_dir):
3639 cfg = Config (root = str (env_path ), temporary = False )
3740 env = OntoEnv (config = cfg , path = env_path , recreate = True )
3841 assert (env_path / ".ontoenv" ).is_dir ()
39- assert env .store_path () is not None # Assuming store_path handles non-temporary envs
42+ assert (
43+ env .store_path () is not None
44+ ) # Assuming store_path handles non-temporary envs
45+
4046
4147def test_init_with_config_existing_empty_dir (temp_dir ):
4248 """Test initializing OntoEnv with a Config in an existing empty directory."""
@@ -48,6 +54,7 @@ def test_init_with_config_existing_empty_dir(temp_dir):
4854 assert (env_path / ".ontoenv" ).is_dir ()
4955 assert env .store_path () is not None
5056
57+
5158def test_init_load_from_existing_dir (existing_env_dir ):
5259 """Test initializing OntoEnv by loading from an existing directory."""
5360 assert (existing_env_dir / ".ontoenv" ).is_dir ()
@@ -57,6 +64,7 @@ def test_init_load_from_existing_dir(existing_env_dir):
5764 assert env .store_path () == str (existing_env_dir / ".ontoenv" / "store.db" )
5865 # Add more checks if the fixture pre-populates data
5966
67+
6068def test_init_recreate_existing_dir (existing_env_dir ):
6169 """Test initializing OntoEnv with recreate=True on an existing directory."""
6270 assert (existing_env_dir / ".ontoenv" ).is_dir ()
@@ -73,6 +81,7 @@ def test_init_recreate_existing_dir(existing_env_dir):
7381 assert not (existing_env_dir / ".ontoenv" / "dummy.txt" ).exists ()
7482 assert len (env .get_ontology_names ()) == 0
7583
84+
7685# Note: This test assumes add() raises an error for read-only mode.
7786# The Rust ReadOnlyPersistentGraphIO::add returns Err, which should map to PyErr.
7887def test_init_read_only (existing_env_dir ):
@@ -82,39 +91,43 @@ def test_init_read_only(existing_env_dir):
8291
8392 # Attempting to modify should fail
8493 with pytest .raises (ValueError , match = "Cannot add to read-only store" ):
85- # Use a dummy file path or URL
94+ # Use a dummy file path or URL
8695 env .add ("file:///dummy.ttl" )
8796
97+
8898def test_init_no_config_no_path_error ():
8999 """Test initializing OntoEnv without config or valid path fails."""
90100 # Assuming current dir '.' does not contain a valid .ontoenv
91101 # Clean up potential leftover .ontoenv in cwd just in case
92102 if os .path .exists (".ontoenv" ):
93- if os .path .isfile (".ontoenv" ):
94- os .remove (".ontoenv" )
95- else :
96- shutil .rmtree (".ontoenv" )
103+ if os .path .isfile (".ontoenv" ):
104+ os .remove (".ontoenv" )
105+ else :
106+ shutil .rmtree (".ontoenv" )
97107
98108 # Expecting failure because '.' likely doesn't contain a valid .ontoenv
99- with pytest .raises (ValueError , match = "OntoEnv directory not found at ." ):
100- OntoEnv () # No args
109+ with pytest .raises (ValueError , match = "OntoEnv directory not found at: \" ./.ontoenv\" " ):
110+ OntoEnv () # No args
111+
101112
102113def test_init_path_no_env_error (temp_dir ):
103114 """Test initializing OntoEnv with a path to a dir without .ontoenv fails."""
104115 env_path = temp_dir / "no_env_here"
105116 env_path .mkdir ()
106117 assert not (env_path / ".ontoenv" ).exists ()
107118 # Expecting failure because the specified path doesn't contain a .ontoenv dir
108- with pytest .raises (ValueError , match = f"OntoEnv directory not found at { env_path } " ):
119+ absolute_path = (env_path / ".ontoenv" ).resolve ()
120+ with pytest .raises (ValueError , match = f"OntoEnv directory not found at: \" { absolute_path } \" " ):
109121 # This fails because load_from_directory expects .ontoenv unless recreate=True
110- OntoEnv (path = env_path )
122+ OntoEnv (path = env_path )
123+
111124
112125def test_init_temporary (temp_dir ):
113126 """Test initializing OntoEnv with temporary=True."""
114127 env_path = temp_dir / "temp_env_root"
115128 # temporary envs don't persist to disk relative to root
116129 cfg = Config (root = str (env_path ), temporary = True , strict = False )
117- env = OntoEnv (config = cfg ) # Path shouldn't matter for temporary
130+ env = OntoEnv (config = cfg ) # Path shouldn't matter for temporary
118131
119132 # .ontoenv directory should NOT be created at the root
120133 assert not (env_path / ".ontoenv" ).exists ()
@@ -137,4 +150,5 @@ def test_init_temporary(temp_dir):
137150 # Catch other potential errors (like network) during add
138151 pass
139152
153+
140154# TODO: Add tests for offline mode, different resolution policies, includes/excludes etc.
0 commit comments