Skip to content

Commit

Permalink
test: factor out commands and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Hagg committed Nov 24, 2020
1 parent 3061062 commit ea91048
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 22 deletions.
6 changes: 3 additions & 3 deletions powersimdata/scenario/scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ def err_message(table, text):
:param pandas.DataFrame table: scenario table.
:param str text: message to print.
"""
print("------------------")
print(text)
print("------------------")
print(
table.to_string(
index=False,
Expand All @@ -77,6 +74,9 @@ def err_message(table, text):
],
)
)
print("------------------")
print(text)
print("------------------")

try:
int(descriptor)
Expand Down
19 changes: 19 additions & 0 deletions powersimdata/utility/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,25 @@
import sys


class CommandBuilder:
@staticmethod
def copy(src, dest, recursive=False, update=False):
r_flag = "R" if recursive else ""
u_flag = "u" if update else ""
p_flag = "p"
flags = f"-{r_flag}{u_flag}{p_flag}"
return fr"\cp {flags} {src} {dest}"

@staticmethod
def remove(target, recursive=False, force=False):
r_flag = "r" if recursive else ""
f_flag = "f" if force else ""
if recursive or force:
flags = f"-{r_flag}{f_flag}"
return f"rm {flags} {target}"
return f"rm {target}"


class MemoryCache:
"""Wrapper around a dict object that exposes a cache interface. Users should
create a separate instance for each distinct use case.
Expand Down
39 changes: 38 additions & 1 deletion powersimdata/utility/tests/test_helpers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import pytest

from powersimdata.utility.helpers import MemoryCache, PrintManager, cache_key
from powersimdata.utility.helpers import (
CommandBuilder,
MemoryCache,
PrintManager,
cache_key,
)


def test_print_is_disabled(capsys):
Expand Down Expand Up @@ -61,3 +66,35 @@ def test_mem_cache_get_returns_copy():
obj = {"key1": 42}
cache.put(key, obj)
assert id(cache.get(key)) != id(obj)


def test_copy_command():
expected = r"\cp -p source dest"
command = CommandBuilder.copy("source", "dest")
assert expected == command

expected = r"\cp -Rp source dest"
command = CommandBuilder.copy("source", "dest", recursive=True)
assert expected == command

expected = r"\cp -up source dest"
command = CommandBuilder.copy("source", "dest", update=True)
assert expected == command

expected = r"\cp -Rup source dest"
command = CommandBuilder.copy("source", "dest", recursive=True, update=True)
assert expected == command


def test_remove_command():
expected = "rm target"
command = CommandBuilder.remove("target")
assert expected == command

expected = "rm -r target"
command = CommandBuilder.remove("target", recursive=True)
assert expected == command

expected = "rm -rf target"
command = CommandBuilder.remove("target", recursive=True, force=True)
assert expected == command
24 changes: 6 additions & 18 deletions powersimdata/utility/transfer_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from tqdm import tqdm

from powersimdata.utility import server_setup
from powersimdata.utility.helpers import CommandBuilder


class DataAccess:
Expand Down Expand Up @@ -80,7 +81,7 @@ class SSHDataAccess(DataAccess):

_last_attempt = 0

def __init__(self, root):
def __init__(self, root=None):
"""Constructor"""
self._ssh = None
self._retry_after = 5
Expand Down Expand Up @@ -141,12 +142,9 @@ def copy_from(self, file_name, from_dir=None):

print(f"Transferring {file_name} from server")
to_path = os.path.join(to_dir, file_name)
sftp = self.ssh.open_sftp()
try:
with self.ssh.open_sftp() as sftp:
cbk, bar = progress_bar(ascii=True, unit="b", unit_scale=True)
sftp.get(from_path, to_path, callback=cbk)
finally:
sftp.close()
bar.close()

def copy_to(self, file_name, to_dir, change_name_to=None):
Expand All @@ -170,28 +168,18 @@ def copy_to(self, file_name, to_dir, change_name_to=None):
raise IOError(f"{file_name} already exists in {to_dir} on server")

print(f"Transferring {from_path} to server")
sftp = self.ssh.open_sftp()
try:
with self.ssh.open_sftp() as sftp:
sftp.put(from_path, to_path)
finally:
sftp.close()

print(f"--> Deleting {from_path} on local machine")
os.remove(from_path)

def copy(self, src, dest, recursive=False, update=False):
r_flag = "R" if recursive else ""
u_flag = "u" if update else ""
p_flag = "p"
flags = f"-{r_flag}{u_flag}{p_flag}"
command = f"\cp {flags} {src} {dest}"
command = CommandBuilder.copy(src, dest, recursive, update)
return self.execute_command(command)

def remove(self, target, recursive=False, force=False):
r_flag = "r" if recursive else ""
f_flag = "f" if force else ""
flags = f"-{r_flag}{f_flag}"
command = f"rm {flags} {target}"
command = CommandBuilder.remove(target, recursive, force)
return self.execute_command(command)

def execute_command(self, command):
Expand Down

0 comments on commit ea91048

Please sign in to comment.