Skip to content

Commit

Permalink
test: delete state functionality and enable state change earlier star…
Browse files Browse the repository at this point in the history
…ting in execute state
  • Loading branch information
Jon Hagg committed Feb 4, 2022
1 parent 76b7cc5 commit 4a69606
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 13 deletions.
1 change: 0 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
FROM python:3.8.3

RUN apt-get update
RUN apt-get install gawk
RUN ln -s /mnt/bes/pcm $HOME/ScenarioData

COPY powersimdata/utility/templates /mnt/bes/pcm/
Expand Down
5 changes: 4 additions & 1 deletion powersimdata/data_access/data_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,17 @@ def remove(self, pattern, confirm=True):
:param str pattern: glob specifying files to remove
:param bool confirm: prompt before executing command
:return: (*bool*) -- True if the operation is completed
"""
if confirm:
confirmed = input(f"Delete '{pattern}'? [y/n] (default is 'n')")
if confirmed.lower() != "y":
print("Operation cancelled.")
return
return False
self.fs.glob(pattern).remove()
self.local_fs.glob(pattern).remove()
print("--> Done!")
return True

def _check_file_exists(self, path, should_exist=True):
"""Check that file exists (or not) at the given path
Expand Down
4 changes: 2 additions & 2 deletions powersimdata/scenario/analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Analyze(Ready):
"""

name = "analyze"
allowed = []
allowed = ["delete"]
exported_methods = {
"get_averaged_cong",
"get_congl",
Expand Down Expand Up @@ -53,7 +53,7 @@ def refresh(self, scenario):
def _set_allowed_state(self):
"""Sets allowed state."""
if self._scenario_status == "extracted":
self.allowed = ["delete", "move"]
self.allowed.append("move")

def _set_ct_and_grid(self):
"""Sets change table and grid."""
Expand Down
11 changes: 3 additions & 8 deletions powersimdata/scenario/delete.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from powersimdata.data_access.data_access import LocalDataAccess
from powersimdata.scenario.ready import Ready
from powersimdata.utility import server_setup

Expand All @@ -15,10 +14,7 @@ def delete_scenario(self, confirm=True):
:param bool confirm: prompt before each batch
"""
# Delete entry in scenario list
scenario_id = self._scenario_info["id"]
self._scenario_list_manager.delete_entry(scenario_id)
self._execute_list_manager.delete_entry(scenario_id)

print("--> Deleting scenario input data")
target = self._data_access.join(*server_setup.INPUT_DIR, f"{scenario_id}_*")
Expand All @@ -33,10 +29,9 @@ def delete_scenario(self, confirm=True):
tmp_dir = self._data_access.tmp_folder(scenario_id)
self._data_access.remove(f"{tmp_dir}/**", confirm=confirm)

print("--> Deleting input and output data on local machine")
local_data_access = LocalDataAccess()
target = local_data_access.join("data", "**", f"{scenario_id}_*")
local_data_access.remove(target, confirm=confirm)
print("--> Deleting entries in scenario and execute list")
self._scenario_list_manager.delete_entry(scenario_id)
self._execute_list_manager.delete_entry(scenario_id)

# Delete attributes
self._clean()
Expand Down
2 changes: 1 addition & 1 deletion powersimdata/scenario/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Execute(Ready):
"""

name = "execute"
allowed = []
allowed = ["delete"]
exported_methods = {
"check_progress",
"extract_simulation_output",
Expand Down
24 changes: 24 additions & 0 deletions powersimdata/scenario/tests/test_scenario.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest

from powersimdata.data_access.context import Context
from powersimdata.scenario.delete import Delete
from powersimdata.scenario.scenario import Scenario
from powersimdata.tests.mock_context import MockContext

Expand Down Expand Up @@ -45,9 +46,32 @@ def test_scenario_workflow(monkeypatch):
s.get_grid()
s.get_ct()

rfs = s.data_access.fs
lfs = s.data_access.local_fs
tmp_dir = s.data_access.tmp_folder(1)

s.print_scenario_info()
s.create_scenario()

scenario_list = s.get_scenario_table()
assert 1 == scenario_list.shape[0]

for fs in (lfs, rfs):
assert fs.exists("data/input/1_ct.pkl")

# hack to use truncated profiles so the test runs quickly
s.info["grid_model"] = "test_usa_tamu"
s.prepare_simulation_input()

tmp_files = rfs.listdir(tmp_dir)
assert len(tmp_files) > 0

s.change(Delete)
s.delete_scenario(confirm=False)

for fs in (rfs, lfs):
assert not fs.exists(tmp_dir)
assert len(fs.listdir("data/input")) == 0

scenario_list = Scenario().get_scenario_table()
assert 0 == scenario_list.shape[0]

0 comments on commit 4a69606

Please sign in to comment.