示例#1
0
    def test_upload_dir_with_last_time(self, client):
        dirname1 = tempfile.mkdtemp()
        fpath1 = dirname1 + "/test1.txt"
        with open(fpath1, "w") as f:
            f.write("data1")

        fpath2 = dirname1 + "/test2.txt"
        with open(fpath2, "w") as f:
            f.write("data2")

        last_time = to_datetime(os.stat(fpath2).st_mtime)
        time.sleep(0.1)

        dirname2 = tempfile.mkdtemp(prefix=dirname1 + "/")
        fpath3 = dirname2 + "/test3.txt"
        with open(fpath3, "w") as f:
            f.write("data3")

        store = AzureBlobStoreService()
        store.set_connection(connection=client)

        blob_path = "path/to/"
        azure_url = self.wasbs_base + blob_path
        rel_path1 = dirname1.split("/")[-1]
        rel_path2 = dirname2.split("/")[-1]

        # Test without basename
        store.upload_dir(dirname=dirname1,
                         blob=azure_url,
                         use_basename=False,
                         last_time=last_time)
        call_args_list = client.get_container_client(
        ).upload_blob.call_args_list
        for call_args in call_args_list:
            call_arg1, call_arg2 = call_args[0]
            if call_arg1 == "{}{}/test3.txt".format(blob_path, rel_path2):
                assert call_arg2.name == fpath3
            else:
                assert False

        # Test with basename
        store.upload_dir(dirname=dirname1,
                         blob=azure_url,
                         use_basename=True,
                         last_time=last_time)
        call_args_list = client.get_container_client(
        ).upload_blob.call_args_list[1:]
        for call_args in call_args_list:
            call_arg1, call_arg2 = call_args[0]
            if call_arg1 == "{}{}/{}/test3.txt".format(blob_path, rel_path1,
                                                       rel_path2):
                assert call_arg2.name == fpath3
            else:
                assert False
示例#2
0
    def test_upload_dir(self, client):
        dirname1 = tempfile.mkdtemp()
        fpath1 = dirname1 + "/test1.txt"
        with open(fpath1, "w") as f:
            f.write("data1")

        fpath2 = dirname1 + "/test2.txt"
        with open(fpath2, "w") as f:
            f.write("data2")

        dirname2 = tempfile.mkdtemp(prefix=dirname1 + "/")
        fpath3 = dirname2 + "/test3.txt"
        with open(fpath3, "w") as f:
            f.write("data3")

        store = AzureBlobStoreService()

        blob_path = "path/to/"
        azure_url = self.wasbs_base + blob_path
        rel_path1 = dirname1.split("/")[-1]
        rel_path2 = dirname2.split("/")[-1]

        # Test without basename
        store.upload_dir(dirname=dirname1, blob=azure_url, use_basename=False)
        client.return_value.create_blob_from_path.assert_has_calls(
            [
                mock.call("container", "{}test1.txt".format(blob_path), fpath1),
                mock.call("container", "{}test2.txt".format(blob_path), fpath2),
                mock.call(
                    "container", "{}{}/test3.txt".format(blob_path, rel_path2), fpath3
                ),
            ],
            any_order=True,
        )

        # Test with basename
        store.upload_dir(dirname=dirname1, blob=azure_url, use_basename=True)
        client.return_value.create_blob_from_path.assert_has_calls(
            [
                mock.call(
                    "container", "{}{}/test1.txt".format(blob_path, rel_path1), fpath1
                ),
                mock.call(
                    "container", "{}{}/test2.txt".format(blob_path, rel_path1), fpath2
                ),
                mock.call(
                    "container",
                    "{}{}/{}/test3.txt".format(blob_path, rel_path1, rel_path2),
                    fpath3,
                ),
            ],
            any_order=True,
        )