|
17 | 17 | import pathlib
|
18 | 18 | import random
|
19 | 19 | import string
|
| 20 | +import tempfile |
20 | 21 | import unittest
|
21 | 22 | import urllib.parse
|
22 | 23 | import uuid
|
@@ -702,6 +703,37 @@ def test_transfer(self) -> None:
|
702 | 703 | with self.assertRaises(FileNotFoundError):
|
703 | 704 | dest.transfer_from(src, "auto")
|
704 | 705 |
|
| 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 | + |
705 | 737 | def test_local_transfer(self) -> None:
|
706 | 738 | """Test we can transfer to and from local file."""
|
707 | 739 | remote_src = self.tmpdir.join("src.json")
|
@@ -762,6 +794,35 @@ def test_local(self) -> None:
|
762 | 794 | with self.root_uri.as_local() as local_uri:
|
763 | 795 | pass
|
764 | 796 |
|
| 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 | + |
765 | 826 | def test_walk(self) -> None:
|
766 | 827 | """Walk a directory hierarchy."""
|
767 | 828 | root = self.tmpdir.join("walk/")
|
|
0 commit comments