def testTransfer(self): src = ButlerURI(os.path.join(self.tmpdir, "test.txt")) content = "Content is some content\nwith something to say\n\n" src.write(content.encode()) for mode in ("copy", "link", "hardlink", "symlink", "relsymlink"): dest = ButlerURI(os.path.join(self.tmpdir, f"dest_{mode}.txt")) dest.transfer_from(src, transfer=mode) self.assertTrue(dest.exists(), f"Check that {dest} exists (transfer={mode})") with open(dest.ospath, "r") as fh: new_content = fh.read() self.assertEqual(new_content, content) if mode in ("symlink", "relsymlink"): self.assertTrue(os.path.islink(dest.ospath), f"Check that {dest} is symlink") with self.assertRaises(FileExistsError): dest.transfer_from(src, transfer=mode) dest.transfer_from(src, transfer=mode, overwrite=True) os.remove(dest.ospath) b = src.read() self.assertEqual(b.decode(), new_content) nbytes = 10 subset = src.read(size=nbytes) self.assertEqual(len(subset), nbytes) self.assertEqual(subset.decode(), content[:nbytes]) with self.assertRaises(ValueError): src.transfer_from(src, transfer="unknown")
class WebdavURITestCase(unittest.TestCase): def setUp(self): serverRoot = "www.not-exists.orgx" existingFolderName = "existingFolder" existingFileName = "existingFile" notExistingFileName = "notExistingFile" self.baseURL = ButlerURI( f"https://{serverRoot}", forceDirectory=True) self.existingFileButlerURI = ButlerURI( f"https://{serverRoot}/{existingFolderName}/{existingFileName}") self.notExistingFileButlerURI = ButlerURI( f"https://{serverRoot}/{existingFolderName}/{notExistingFileName}") self.existingFolderButlerURI = ButlerURI( f"https://{serverRoot}/{existingFolderName}", forceDirectory=True) self.notExistingFolderButlerURI = ButlerURI( f"https://{serverRoot}/{notExistingFileName}", forceDirectory=True) # Need to declare the options responses.add(responses.OPTIONS, self.baseURL.geturl(), status=200, headers={"DAV": "1,2,3"}) # Used by ButlerHttpURI.exists() responses.add(responses.HEAD, self.existingFileButlerURI.geturl(), status=200, headers={'Content-Length': '1024'}) responses.add(responses.HEAD, self.notExistingFileButlerURI.geturl(), status=404) # Used by ButlerHttpURI.read() responses.add(responses.GET, self.existingFileButlerURI.geturl(), status=200, body=str.encode("It works!")) responses.add(responses.GET, self.notExistingFileButlerURI.geturl(), status=404) # Used by ButlerHttpURI.write() responses.add(responses.PUT, self.existingFileButlerURI.geturl(), status=201) # Used by ButlerHttpURI.transfer_from() responses.add(responses.Response(url=self.existingFileButlerURI.geturl(), method="COPY", headers={"Destination": self.existingFileButlerURI.geturl()}, status=201)) responses.add(responses.Response(url=self.existingFileButlerURI.geturl(), method="COPY", headers={"Destination": self.notExistingFileButlerURI.geturl()}, status=201)) responses.add(responses.Response(url=self.existingFileButlerURI.geturl(), method="MOVE", headers={"Destination": self.notExistingFileButlerURI.geturl()}, status=201)) # Used by ButlerHttpURI.remove() responses.add(responses.DELETE, self.existingFileButlerURI.geturl(), status=200) responses.add(responses.DELETE, self.notExistingFileButlerURI.geturl(), status=404) # Used by ButlerHttpURI.mkdir() responses.add(responses.HEAD, self.existingFolderButlerURI.geturl(), status=200, headers={'Content-Length': '1024'}) responses.add(responses.HEAD, self.baseURL.geturl(), status=200, headers={'Content-Length': '1024'}) responses.add(responses.HEAD, self.notExistingFolderButlerURI.geturl(), status=404) responses.add(responses.Response(url=self.notExistingFolderButlerURI.geturl(), method="MKCOL", status=201)) responses.add(responses.Response(url=self.existingFolderButlerURI.geturl(), method="MKCOL", status=403)) @responses.activate def testExists(self): self.assertTrue(self.existingFileButlerURI.exists()) self.assertFalse(self.notExistingFileButlerURI.exists()) @responses.activate def testRemove(self): self.assertIsNone(self.existingFileButlerURI.remove()) with self.assertRaises(FileNotFoundError): self.notExistingFileButlerURI.remove() @responses.activate def testMkdir(self): # The mock means that we can't check this now exists self.notExistingFolderButlerURI.mkdir() # This should do nothing self.existingFolderButlerURI.mkdir() with self.assertRaises(ValueError): self.notExistingFileButlerURI.mkdir() @responses.activate def testRead(self): self.assertEqual(self.existingFileButlerURI.read().decode(), "It works!") self.assertNotEqual(self.existingFileButlerURI.read().decode(), "Nope.") with self.assertRaises(FileNotFoundError): self.notExistingFileButlerURI.read() @responses.activate def testWrite(self): self.assertIsNone(self.existingFileButlerURI.write(data=str.encode("Some content."))) with self.assertRaises(FileExistsError): self.existingFileButlerURI.write(data=str.encode("Some content."), overwrite=False) @responses.activate def testTransfer(self): self.assertIsNone(self.notExistingFileButlerURI.transfer_from( src=self.existingFileButlerURI)) self.assertIsNone(self.notExistingFileButlerURI.transfer_from( src=self.existingFileButlerURI, transfer="move")) with self.assertRaises(FileExistsError): self.existingFileButlerURI.transfer_from(src=self.existingFileButlerURI) with self.assertRaises(ValueError): self.notExistingFileButlerURI.transfer_from( src=self.existingFileButlerURI, transfer="unsupported") def testParent(self): self.assertEqual(self.existingFolderButlerURI.geturl(), self.notExistingFileButlerURI.parent().geturl()) self.assertEqual(self.baseURL.geturl(), self.baseURL.parent().geturl()) self.assertEqual(self.existingFileButlerURI.parent().geturl(), self.existingFileButlerURI.dirname().geturl())
def testTransfer(self): src = ButlerURI(os.path.join(self.tmpdir, "test.txt")) content = "Content is some content\nwith something to say\n\n" src.write(content.encode()) dest = ButlerURI(self.makeS3Uri("test.txt")) self.assertFalse(dest.exists()) dest.transfer_from(src, transfer="copy") self.assertTrue(dest.exists()) dest2 = ButlerURI(self.makeS3Uri("copied.txt")) dest2.transfer_from(dest, transfer="copy") self.assertTrue(dest2.exists()) local = ButlerURI(os.path.join(self.tmpdir, "copied.txt")) local.transfer_from(dest2, transfer="copy") with open(local.ospath, "r") as fd: new_content = fd.read() self.assertEqual(new_content, content) with self.assertRaises(ValueError): dest2.transfer_from(local, transfer="symlink") b = dest.read() self.assertEqual(b.decode(), new_content) nbytes = 10 subset = dest.read(size=nbytes) self.assertEqual(len(subset), nbytes) # Extra byte comes back self.assertEqual(subset.decode(), content[:nbytes]) with self.assertRaises(FileExistsError): dest.transfer_from(src, transfer="copy") dest.transfer_from(src, transfer="copy", overwrite=True)