Skip to content

Commit 7c11ebf

Browse files
committed
fix tests
1 parent 39679f5 commit 7c11ebf

File tree

6 files changed

+48
-50
lines changed

6 files changed

+48
-50
lines changed

disk_objectstore/backup_utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ def call_rsync( # pylint: disable=too-many-arguments,too-many-branches
221221

222222
def get_existing_backup_folders(self):
223223
"""Get all folders matching the backup folder name pattern."""
224+
224225
success, stdout = self.run_cmd(
225226
[
226227
"find",

disk_objectstore/cli.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -244,17 +244,13 @@ def backup(
244244
logger.setLevel(logging.DEBUG)
245245

246246
with dostore.container as container:
247-
try:
248-
backup_manager = backup_utils.BackupManager(
249-
dest,
250-
keep=keep,
251-
rsync_exe=rsync_exe,
252-
)
253-
backup_manager.backup_auto_folders(
254-
lambda path, prev: backup_utils.backup_container(
255-
backup_manager, container, path, prev
256-
)
247+
backup_manager = backup_utils.BackupManager(
248+
dest,
249+
keep=keep,
250+
rsync_exe=rsync_exe,
251+
)
252+
backup_manager.backup_auto_folders(
253+
lambda path, prev: backup_utils.backup_container(
254+
backup_manager, container, path, prev
257255
)
258-
except (ValueError, backup_utils.BackupError) as e:
259-
click.echo(f"Error: {e}")
260-
sys.exit(1)
256+
)

disk_objectstore/container.py

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ def __init__(self, folder: str | Path) -> None:
121121
self._folder = Path(folder).resolve()
122122
# Will be populated by the _get_session function
123123
self._session: Session | None = None
124+
self._keep_open_session: Session | None = None
124125

125126
# These act as caches and will be populated by the corresponding properties
126127
# IMPORANT! IF YOU ADD MORE, REMEMBER TO CLEAR THEM IN `init_container()`!
@@ -139,6 +140,12 @@ def close(self) -> None:
139140
engine.dispose()
140141
self._session = None
141142

143+
if self._keep_open_session is not None:
144+
engine = self._keep_open_session.bind
145+
self._keep_open_session.close()
146+
engine.dispose()
147+
self._keep_open_session = None
148+
142149
def __enter__(self) -> Container:
143150
"""Return a context manager that will close the session when exiting the context."""
144151
return self
@@ -182,32 +189,21 @@ def _get_config_file(self) -> Path:
182189
"""Return the path to the container config file."""
183190
return self._folder / "config.json"
184191

185-
@overload
186-
def _get_session(
187-
self, create: bool = False, raise_if_missing: Literal[True] = True
192+
def _create_init_session(
193+
self
188194
) -> Session:
189-
...
190-
191-
@overload
192-
def _get_session(
193-
self, create: bool = False, raise_if_missing: Literal[False] = False
194-
) -> Session | None:
195-
...
196-
197-
def _get_session(
198-
self, create: bool = False, raise_if_missing: bool = False
199-
) -> Session | None:
200195
"""Return a new session to connect to the pack-index SQLite DB.
201196
202197
:param create: if True, creates the sqlite file and schema.
203198
:param raise_if_missing: ignored if create==True. If create==False, and the index file
204199
is missing, either raise an exception (FileNotFoundError) if this flag is True, or return None
205200
"""
206-
return get_session(
207-
self._get_pack_index_path(),
208-
create=create,
209-
raise_if_missing=raise_if_missing,
210-
)
201+
if self._keep_open_session is None:
202+
self._keep_open_session = get_session(
203+
self._get_pack_index_path(),
204+
create=True,
205+
)
206+
return self._keep_open_session
211207

212208
def _get_cached_session(self) -> Session:
213209
"""Return the SQLAlchemy session to access the SQLite file,
@@ -216,7 +212,10 @@ def _get_cached_session(self) -> Session:
216212
# the latter means that in the previous run the pack file was missing
217213
# but maybe by now it has been created!
218214
if self._session is None:
219-
self._session = self._get_session(create=False, raise_if_missing=True)
215+
self._session = get_session(
216+
self._get_pack_index_path(),
217+
create=False,
218+
)
220219
return self._session
221220

222221
def _get_loose_path_from_hashkey(self, hashkey: str) -> Path:
@@ -334,6 +333,7 @@ def init_container(
334333
raise ValueError(f'Unknown hash type "{hash_type}"')
335334

336335
if clear:
336+
self.close()
337337
if self._folder.exists():
338338
shutil.rmtree(self._folder)
339339

@@ -393,7 +393,7 @@ def init_container(
393393
]:
394394
os.makedirs(folder)
395395

396-
self._get_session(create=True)
396+
self._create_init_session()
397397

398398
def _get_repository_config(self) -> dict[str, int | str]:
399399
"""Return the repository config."""
@@ -1143,7 +1143,7 @@ def get_total_size(self) -> TotalSize:
11431143

11441144
retval["total_size_packindexes_on_disk"] = (
11451145
self._get_pack_index_path().stat().st_size
1146-
)
1146+
)
11471147

11481148
total_size_loose = 0
11491149
for loose_hashkey in self._list_loose():
@@ -1918,6 +1918,9 @@ def add_objects_to_pack( # pylint: disable=too-many-arguments
19181918
19191919
:return: a list of object hash keys
19201920
"""
1921+
# TODO should be custom error but not sure what
1922+
if not self.is_initialised:
1923+
raise ValueError("Invalid use of function, please first initialise the container.")
19211924
stream_list: list[StreamSeekBytesType] = [
19221925
io.BytesIO(content) for content in content_list
19231926
]

disk_objectstore/database.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class Obj(Base): # pylint: disable=too-few-public-methods
3232

3333

3434
def get_session(
35-
path: Path, create: bool = False, raise_if_missing: bool = False
35+
path: Path, create: bool = False
3636
) -> Optional[Session]:
3737
"""Return a new session to connect to the pack-index SQLite DB.
3838
@@ -41,9 +41,7 @@ def get_session(
4141
is missing, either raise an exception (FileNotFoundError) if this flag is True, or return None
4242
"""
4343
if not create and not path.exists():
44-
if raise_if_missing:
45-
raise FileNotFoundError("Pack index does not exist")
46-
return None
44+
raise FileNotFoundError("Pack index does not exist")
4745

4846
engine = create_engine(f"sqlite:///{path}", future=True)
4947

tests/test_cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ def test_backup(temp_container, temp_dir, remote, verbosity):
223223
if verbosity:
224224
args += [f"--verbosity={verbosity}"]
225225

226-
result = CliRunner().invoke(cli.backup, args, obj=obj)
226+
result = CliRunner().invoke(cli.backup, args, obj=obj, catch_exceptions=False)
227227

228228
assert result.exit_code == 0
229229
assert path.exists()

tests/test_container.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -677,15 +677,16 @@ def test_initialisation(temp_dir):
677677

678678
# Check that the session cannot be obtained before initialising
679679
with pytest.raises(FileNotFoundError):
680-
container._get_session(create=False, raise_if_missing=True)
681-
assert container._get_session(create=False, raise_if_missing=False) is None
680+
container._get_cached_session()
682681

683682
container.init_container()
684683
assert container.is_initialised
684+
container.close()
685685

686686
# This call should go through
687687
container.init_container(clear=True)
688688
assert container.is_initialised
689+
container.close()
689690

690691
with pytest.raises(FileExistsError) as excinfo:
691692
container.init_container()
@@ -717,31 +718,32 @@ def test_initialisation(temp_dir):
717718

718719
@pytest.mark.parametrize("hash_type", ["sha256", "sha1"])
719720
@pytest.mark.parametrize("compress", [True, False])
720-
def test_check_hash_computation(temp_container, hash_type, compress):
721+
def test_check_hash_computation(temp_dir, hash_type, compress):
721722
"""Check that the hashes are correctly computed, when storing loose,
722723
directly to packs, and while repacking all loose.
723724
724725
Check both compressed and uncompressed packed objects.
725726
"""
726727
# Re-init the container with the correct hash type
727-
temp_container.init_container(hash_type=hash_type, clear=True)
728+
container = Container(temp_dir)
729+
container.init_container(hash_type=hash_type, clear=True)
728730
content1 = b"1"
729731
content2 = b"222"
730732
content3 = b"n2fwd"
731733

732734
expected_hasher = getattr(hashlib, hash_type)
733735

734-
hashkey1 = temp_container.add_object(content1)
736+
hashkey1 = container.add_object(content1)
735737
assert hashkey1 == expected_hasher(content1).hexdigest()
736738

737-
hashkey2, hashkey3 = temp_container.add_objects_to_pack(
739+
hashkey2, hashkey3 = container.add_objects_to_pack(
738740
[content2, content3], compress=compress
739741
)
740742
assert hashkey2 == expected_hasher(content2).hexdigest()
741743
assert hashkey3 == expected_hasher(content3).hexdigest()
742744

743745
# No exceptions should be aised
744-
temp_container.pack_all_loose(compress=compress, validate_objects=True)
746+
container.pack_all_loose(compress=compress, validate_objects=True)
745747

746748

747749
@pytest.mark.parametrize("validate_objects", [True, False])
@@ -1064,9 +1066,7 @@ def test_sizes(
10641066
temp_container, generate_random_data, compress_packs, compression_algorithm
10651067
):
10661068
"""Check that the information on size is reliable."""
1067-
temp_container.init_container(
1068-
clear=True, compression_algorithm=compression_algorithm
1069-
)
1069+
temp_container.init_container( clear=True, compression_algorithm=compression_algorithm)
10701070
size_info = temp_container.get_total_size()
10711071
assert size_info["total_size_packed"] == 0
10721072
assert size_info["total_size_packed_on_disk"] == 0

0 commit comments

Comments
 (0)