Skip to content

Commit

Permalink
Issue #74: Testing test_datasets.py. Using subprocess instead of os.s…
Browse files Browse the repository at this point in the history
…ystem().
  • Loading branch information
bongjinkoo committed May 16, 2022
1 parent 4be65f0 commit 7f2d9ed
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
21 changes: 12 additions & 9 deletions ioSPI/datasets.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Module to house methods related to datasets (micrographs, meta-data, etc.)."""

import os
import subprocess


class OSFProject:
Expand Down Expand Up @@ -56,7 +57,7 @@ def ls(self):
"""List all files in the project."""
print(f"Listing files from OSF project: {self.project_id}...")
# os.system("osf ls")
os.system(f"/usr/share/miniconda/envs/ioSPI/bin/osf ls")
subprocess.run(["osf", "ls"], text=True, capture_output=True)

def download(self, remote_path: str, local_path: str):
"""Download a file from an OSF project and save it locally.
Expand All @@ -81,9 +82,10 @@ 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}")
os.system(
f"/usr/share/miniconda/envs/ioSPI/bin/osf fetch "
f"{full_remote_path} {local_path}"
subprocess.run(
["osf", "fetch", f"{full_remote_path}, f{local_path}"],
text=True,
capture_output=True,
)
print("Done!")

Expand Down Expand Up @@ -114,9 +116,10 @@ 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}")
os.system(
f"/usr/share/miniconda/envs/ioSPI/bin/osf upload "
f"{local_path} {full_remote_path}"
subprocess.run(
["osf", "upload", f"{local_path}", f"{full_remote_path}"],
text=True,
capture_output=True,
)
print("Done!")

Expand All @@ -138,7 +141,7 @@ 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}")
os.system(
f"/usr/share/miniconda/envs/ioSPI/bin/osf remove " f"{full_remote_path}"
subprocess.run(
["osf", "remove", f"{full_remote_path}"], text=True, capture_output=True
)
print("Done!")
16 changes: 11 additions & 5 deletions tests/test_datasets.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Unit tests for listing/uploading/downloading datasets on an OSF project."""

import io
import os
import subprocess

import pytest

Expand Down Expand Up @@ -57,9 +58,11 @@ def test_upload_valid(setup, set_file_path):
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 = os.popen("/usr/share/miniconda/envs/ioSPI/bin/osf ls")
file_list = subprocess.run(["osf", "ls"], text=True, capture_output=True).stdout
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
Expand All @@ -82,9 +85,11 @@ def test_upload_invalid_because_no_remote_path(setup):

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


def test_download_invalid_because_no_remote_path(setup):
Expand All @@ -104,7 +109,8 @@ def test_remove_valid(setup, set_file_path):
setup.remove(set_file_path[1])
file_exists = False
# file_list = os.popen("osf ls")
file_list = os.popen("/usr/share/miniconda/envs/ioSPI/bin/osf ls")
file_list = subprocess.run(["osf", "ls"], text=True, capture_output=True).stdout
file_list = io.StringIO(file_list)
line = file_list.readline()
while line:
file_exists = set_file_path[1] == line.split("/")[1].strip()
Expand Down

0 comments on commit 7f2d9ed

Please sign in to comment.