Skip to content

Commit

Permalink
Issue #74: Testing test_datasets.py, randomising the test file name a…
Browse files Browse the repository at this point in the history
…s difference test instances access the same file.
  • Loading branch information
bongjinkoo committed May 17, 2022
1 parent d550e4e commit 7d6356a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 41 deletions.
25 changes: 13 additions & 12 deletions ioSPI/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ class OSFProject:

def __init__(
self,
username: str,
token: str,
username: str = None,
token: str = None,
project_id: str = "xbr2m",
storage: str = "osfstorage",
osflient_path: str = None,
) -> None:
if username is None:
raise TypeError("username must be provided.")
Expand All @@ -45,6 +46,10 @@ def __init__(

self.project_id = project_id
self.storage = storage
self.osfclient_path = osflient_path
self.osfclient_command = "osf "
if osflient_path is not None:
self.osfclient_command = self.osfclient_path + self.osfclient_command

config_path = os.path.join(".osfcli.config")
with open(config_path, "w") as out_file:
Expand All @@ -57,14 +62,13 @@ def __init__(
def ls(self):
"""List all files in the project."""
print(f"Listing files from OSF project: {self.project_id}...")
# os.system("osf ls")
subprocess.run(
"$CONDA/bin/" + f"osf ls",
return subprocess.run(
self.osfclient_command + "ls",
shell=True,
text=True,
check=True,
stdout=subprocess.PIPE,
)
).stdout

def download(self, remote_path: str, local_path: str):
"""Download a file from an OSF project and save it locally.
Expand All @@ -88,9 +92,8 @@ def download(self, remote_path: str, local_path: str):

full_remote_path = self.storage + "/" + remote_path
print(f"Downloading {full_remote_path} to {local_path}...")
# os.system(f"osf fetch {full_remote_path} {local_path}")
subprocess.run(
"$CONDA/bin/" + f"osf fetch {full_remote_path} {local_path}",
self.osfclient_command + f"fetch {full_remote_path} {local_path}",
shell=True,
text=True,
check=True,
Expand Down Expand Up @@ -124,9 +127,8 @@ def upload(self, local_path: str, remote_path: str):

full_remote_path = self.storage + "/" + remote_path
print(f"Uploading {local_path} to {full_remote_path}...")
# os.system(f"osf upload {local_path} {full_remote_path}")
f = subprocess.run(
"$CONDA/bin/" + f"osf upload --force {local_path} " f"{full_remote_path}",
self.osfclient_command + f"upload {local_path} " f"{full_remote_path}",
shell=True,
text=True,
check=True,
Expand All @@ -152,9 +154,8 @@ def remove(self, remote_path: str):

full_remote_path = self.storage + "/" + remote_path
print(f"Removing {full_remote_path} in the project...")
# os.system(f"osf remove {full_remote_path}")
subprocess.run(
"$CONDA/bin/" + f"osf remove {full_remote_path}",
self.osfclient_command + f"remove {full_remote_path}",
shell=True,
text=True,
check=True,
Expand Down
1 change: 0 additions & 1 deletion tests/data/test_upload.txt

This file was deleted.

60 changes: 32 additions & 28 deletions tests/test_datasets.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
"""Unit tests for listing/uploading/downloading datasets on an OSF project.
These assume the tests are executed in a Conda environment.
These assume the tests are executed in a Conda environment and GitHub actions
when tested non-locally.
"""

import io
import os
import subprocess
import random
import string

import pytest

Expand All @@ -14,25 +16,43 @@

@pytest.fixture(autouse=True, scope="session")
def setup():
"""Test node creation and clean-up for tests."""
"""Set up for tests."""
print("Creating a test project for dataset")
project = datasets.OSFProject(
username="[email protected]",
token="HBGGBOJcLYQfadEKIOyXJiLTum3ydXK4nGP3KmbkYUeBuYkZma9LPBSYennQn92gjP2NHn",
project_id="xbr2m",
# osflient_path="$CONDA/bin/"
)

yield project


@pytest.fixture(autouse=True, scope="session")
def set_file_path():
"""Create a temporary text file for upload."""
file_path = "/home/runner/work/ioSPI/ioSPI/tests/data/"
file_name = "test_upload.txt"
"""Set up a temporary text file path for upload.
Set local_testing = True when testing locally, False if on GitHub.
"""
file_path = "tests/data/"
local_testing = True
if not local_testing:
file_path = "/home/runner/work/ioSPI/ioSPI/" + file_path

file_name = (
"test_upload-"
+ "".join(random.choice(string.ascii_letters) for i in range(5))
+ ".txt"
)
return file_path, file_name


@pytest.fixture(autouse=True, scope="session")
def create_test_file(set_file_path):
"""Create a temporary text file for upload."""
with open(set_file_path[0] + set_file_path[1], "w") as f:
f.write("Hello World!")


def test_constructor_valid():
"""Test the constructor."""
project = datasets.OSFProject(
Expand Down Expand Up @@ -61,24 +81,17 @@ def test_upload_valid(setup, set_file_path):
"""Test the upload method."""
setup.upload(set_file_path[0] + set_file_path[1], set_file_path[1])
file_exists = False
# file_list = os.popen("osf ls")
file_list = subprocess.run(
"$CONDA/bin/" + "osf ls",
shell=True,
text=True,
check=True,
stdout=subprocess.PIPE,
).stdout
file_list = setup.ls()
file_list = io.StringIO(file_list)
line = file_list.readline()
while line:
print(line)
file_exists = set_file_path[1] == line.split("/")[1].strip()
if file_exists:
break
line = file_list.readline()

assert file_exists
os.system(f"rm {set_file_path[0]}{set_file_path[1]}")


def test_upload_invalid_because_no_local_path(setup):
Expand All @@ -95,11 +108,9 @@ def test_upload_invalid_because_no_remote_path(setup):

def test_download_valid(setup, set_file_path):
"""Test the download method."""
setup.download(
set_file_path[1], set_file_path[0] + "downloaded_" + 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])
os.system(f"rm {set_file_path[0]}downloaded_{set_file_path[1]}")
os.system(f"rm {set_file_path[0]}{set_file_path[1]}")


def test_download_invalid_because_no_remote_path(setup):
Expand All @@ -118,14 +129,7 @@ def test_remove_valid(setup, set_file_path):
"""Test the remove method."""
setup.remove(set_file_path[1])
file_exists = False
# file_list = os.popen("osf ls")
file_list = subprocess.run(
"$CONDA/bin/" + "osf ls",
shell=True,
text=True,
check=True,
stdout=subprocess.PIPE,
).stdout
file_list = setup.ls()
file_list = io.StringIO(file_list)
line = file_list.readline()
while line:
Expand Down

0 comments on commit 7d6356a

Please sign in to comment.