From 50272323357f5b7d46a1cb0e498330c2e9e5dc1b Mon Sep 17 00:00:00 2001 From: Bongjin Koo Date: Sat, 14 May 2022 14:42:47 -0700 Subject: [PATCH] Issue #74: Add a test_upload file and finish up datasets.py, test_datasets.py. --- ioSPI/datasets.py | 4 +- notebooks/download_and_upload_with_osf.ipynb | 61 ++++++++++++++++++-- tests/data/test_upload.txt | 1 + tests/test_datasets.py | 51 +++++++++------- 4 files changed, 89 insertions(+), 28 deletions(-) create mode 100644 tests/data/test_upload.txt diff --git a/ioSPI/datasets.py b/ioSPI/datasets.py index 427b87a0..2767ad5a 100644 --- a/ioSPI/datasets.py +++ b/ioSPI/datasets.py @@ -48,9 +48,9 @@ def __init__( with open(config_path, "w") as out_file: out_file.write("[osf]\n") out_file.write(f"username = {username}\n") - out_file.write(f"token = {token}\n") out_file.write(f"project = {project_id}\n") - out_file.write(f"storage = {storage}\n") + out_file.write(f"token = {token}\n") + # out_file.write(f"storage = {storage}\n") print("OSF config written to .osfcli.config!") def ls(self): diff --git a/notebooks/download_and_upload_with_osf.ipynb b/notebooks/download_and_upload_with_osf.ipynb index 107ac5c4..a81cbae3 100644 --- a/notebooks/download_and_upload_with_osf.ipynb +++ b/notebooks/download_and_upload_with_osf.ipynb @@ -55,7 +55,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 1, "id": "e74dd634", "metadata": { "pycharm": { @@ -118,7 +118,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 2, "id": "febfdedd", "metadata": { "pycharm": { @@ -159,7 +159,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 3, "id": "f4f5b054", "metadata": { "pycharm": { @@ -269,18 +269,67 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "id": "74c6125e", "metadata": { "pycharm": { "name": "#%%\n" } }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Uploading ../tests/data/test_upload.txt to osfstorage/test_upload.txt...\n", + "Done!\n" + ] + } + ], "source": [ - "cryoem_simulated_project.upload(\"osfstorage/new_4v6x_randomrot_copy0_defocus3.0_yes_noise.txt\", \"4v6x_randomrot_copy0_defocus3.0_yes_noise.txt\")\n" + "cryoem_simulated_project.upload(\"../tests/data/test_upload.txt\", \"test_upload.txt\")\n" ] }, + { + "cell_type": "code", + "execution_count": 4, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Removing osfstorage/test_upload.txt in the project...\n", + "Done!\n" + ] + } + ], + "source": [ + "\n", + "cryoem_simulated_project.remove(\"test_upload.txt\")" + ], + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%%\n" + } + } + }, + { + "cell_type": "code", + "execution_count": null, + "outputs": [], + "source": [ + "\n", + "\n", + "cryoem_simulated_project.upload(\"4v6x_randomrot_copy0_defocus3.0_yes_noise.txt\",\"osfstorage/new_4v6x_randomrot_copy0_defocus3.0_yes_noise.txt\")\n" + ], + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%%\n" + } + } + }, { "cell_type": "code", "execution_count": 6, diff --git a/tests/data/test_upload.txt b/tests/data/test_upload.txt new file mode 100644 index 00000000..980a0d5f --- /dev/null +++ b/tests/data/test_upload.txt @@ -0,0 +1 @@ +Hello World! diff --git a/tests/test_datasets.py b/tests/test_datasets.py index 54ecff5d..e3af2dca 100644 --- a/tests/test_datasets.py +++ b/tests/test_datasets.py @@ -20,18 +20,20 @@ def setup(): yield project -@pytest.fixture -def create_upload_file(): +@pytest.fixture(autouse=True, scope="session") +def set_file_path(): """Create a temporary text file for upload.""" - file_path = "test_upload.txt" - with open(file_path, "w") as f: - f.write("Hello World") - return file_path + file_path = "./tests/data/" + file_name = "test_upload.txt" + return file_path, file_name def test_constructor_valid(): """Test the constructor.""" - project = datasets.OSFProject(username="username", token="token") + project = datasets.OSFProject( + username="ninamio78@gmail.com", + token="HBGGBOJcLYQfadEKIOyXJiLTum3ydXK4nGP3KmbkYUeBuYkZma9LPBSYennQn92gjP2NHn", + ) assert project.username is not None assert project.token is not None assert project.project_id is not None @@ -50,13 +52,17 @@ def test_constructor_invalid_because_no_token(): datasets.OSFProject(username="username") -def test_upload_valid(setup, create_upload_file): +def test_upload_valid(setup, set_file_path): """Test the upload method.""" - setup.upload(create_upload_file, create_upload_file) - out = os.popen("osf ls").read() + setup.upload(set_file_path[0] + set_file_path[1], set_file_path[1]) file_exists = False - for file in out: - file_exists = create_upload_file == file.split("/")[1] + file_list = os.popen("osf ls") + line = file_list.readline() + while line: + file_exists = set_file_path[1] == line.split("/")[1].strip() + if file_exists: + break + line = file_list.readline() assert file_exists @@ -73,10 +79,11 @@ def test_upload_invalid_because_no_remote_path(setup): setup.upload(local_path="local_path") -def test_download_valid(setup, create_upload_file): +def test_download_valid(setup, set_file_path): """Test the download method.""" - setup.download(create_upload_file, create_upload_file) - assert os.path.exists(create_upload_file) + os.system(f"rm {set_file_path[0] + set_file_path[1]}") + setup.download(set_file_path[1], set_file_path[0] + set_file_path[1]) + assert os.path.exists(set_file_path[0] + set_file_path[1]) def test_download_invalid_because_no_remote_path(setup): @@ -91,13 +98,17 @@ def test_download_invalid_because_no_local_path(setup): setup.download(remote_path="remote_path") -def test_remove_valid(setup, create_upload_file): +def test_remove_valid(setup, set_file_path): """Test the remove method.""" - setup.remove(create_upload_file) - out = os.popen("osf ls").read() + setup.remove(set_file_path[1]) file_exists = False - for file in out: - file_exists = create_upload_file == file.split("/")[1] + file_list = os.popen("osf ls") + line = file_list.readline() + while line: + file_exists = set_file_path[1] == line.split("/")[1].strip() + if file_exists: + break + line = file_list.readline() assert not file_exists