Skip to content

Commit b52a09f

Browse files
committed
Add mtransfer tests
1 parent 0d2b2be commit b52a09f

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

python/lsst/resources/tests.py

+61
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import pathlib
1818
import random
1919
import string
20+
import tempfile
2021
import unittest
2122
import urllib.parse
2223
import uuid
@@ -702,6 +703,37 @@ def test_transfer(self) -> None:
702703
with self.assertRaises(FileNotFoundError):
703704
dest.transfer_from(src, "auto")
704705

706+
def test_mtransfer(self) -> None:
707+
n_files = 10
708+
sources = [self.tmpdir.join(f"test{n}.txt") for n in range(n_files)]
709+
destinations = [self.tmpdir.join(f"dest_test{n}.txt") for n in range(n_files)]
710+
711+
for i, src in enumerate(sources):
712+
content = f"{i}\nContent is some content\nwith something to say\n\n"
713+
src.write(content.encode())
714+
715+
results = ResourcePath.mtransfer("copy", zip(sources, destinations, strict=True))
716+
self.assertTrue(all(res.success for res in results.values()))
717+
self.assertTrue(all(dest.exists() for dest in results))
718+
719+
for i, dest in enumerate(destinations):
720+
new_content = dest.read().decode()
721+
self.assertTrue(new_content.startswith(f"{i}\n"))
722+
723+
# Overwrite should work.
724+
results = ResourcePath.mtransfer("copy", zip(sources, destinations, strict=True), overwrite=True)
725+
726+
# Overwrite failure.
727+
results = ResourcePath.mtransfer(
728+
"copy", zip(sources, destinations, strict=True), overwrite=False, do_raise=False
729+
)
730+
self.assertFalse(all(res.success for res in results.values()))
731+
732+
with self.assertRaises(ExceptionGroup):
733+
results = ResourcePath.mtransfer(
734+
"copy", zip(sources, destinations, strict=True), overwrite=False, do_raise=True
735+
)
736+
705737
def test_local_transfer(self) -> None:
706738
"""Test we can transfer to and from local file."""
707739
remote_src = self.tmpdir.join("src.json")
@@ -762,6 +794,35 @@ def test_local(self) -> None:
762794
with self.root_uri.as_local() as local_uri:
763795
pass
764796

797+
def test_local_mtransfer(self) -> None:
798+
"""Check that bulk transfer to/from local works."""
799+
# Create remote resources
800+
n_files = 10
801+
sources = [self.tmpdir.join(f"test{n}.txt") for n in range(n_files)]
802+
803+
for i, src in enumerate(sources):
804+
content = f"{i}\nContent is some content\nwith something to say\n\n"
805+
src.write(content.encode())
806+
807+
# Potentially remote to local.
808+
with tempfile.TemporaryDirectory() as tmpdir:
809+
temp_dir = ResourcePath(tmpdir, forceDirectory=True)
810+
destinations = [temp_dir.join(f"dest_test{n}.txt") for n in range(n_files)]
811+
812+
results = ResourcePath.mtransfer("copy", zip(sources, destinations, strict=True))
813+
self.assertTrue(all(res.success for res in results.values()))
814+
self.assertTrue(all(dest.exists() for dest in results))
815+
816+
# Overwrite should work.
817+
results = ResourcePath.mtransfer("copy", zip(sources, destinations, strict=True), overwrite=True)
818+
819+
# Now reverse so local to potentially remote.
820+
for src in sources:
821+
src.remove()
822+
results = ResourcePath.mtransfer("copy", zip(destinations, sources, strict=True), overwrite=False)
823+
self.assertTrue(all(res.success for res in results.values()))
824+
self.assertTrue(all(dest.exists() for dest in results))
825+
765826
def test_walk(self) -> None:
766827
"""Walk a directory hierarchy."""
767828
root = self.tmpdir.join("walk/")

0 commit comments

Comments
 (0)