Skip to content

Commit

Permalink
Issue #74: Add a test_upload file and finish up datasets.py, test_dat…
Browse files Browse the repository at this point in the history
…asets.py.
  • Loading branch information
bongjinkoo committed May 14, 2022
1 parent f7e9e8f commit 5027232
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 28 deletions.
4 changes: 2 additions & 2 deletions ioSPI/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
61 changes: 55 additions & 6 deletions notebooks/download_and_upload_with_osf.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 1,
"id": "e74dd634",
"metadata": {
"pycharm": {
Expand Down Expand Up @@ -118,7 +118,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 2,
"id": "febfdedd",
"metadata": {
"pycharm": {
Expand Down Expand Up @@ -159,7 +159,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 3,
"id": "f4f5b054",
"metadata": {
"pycharm": {
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions tests/data/test_upload.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World!
51 changes: 31 additions & 20 deletions tests/test_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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="[email protected]",
token="HBGGBOJcLYQfadEKIOyXJiLTum3ydXK4nGP3KmbkYUeBuYkZma9LPBSYennQn92gjP2NHn",
)
assert project.username is not None
assert project.token is not None
assert project.project_id is not None
Expand All @@ -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

Expand All @@ -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):
Expand All @@ -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

Expand Down

0 comments on commit 5027232

Please sign in to comment.