Skip to content

Commit

Permalink
test: wip testing scenario lifecycle with temp fs
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Hagg committed Jan 31, 2022
1 parent e574619 commit 6efaba5
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 7 deletions.
36 changes: 29 additions & 7 deletions powersimdata/data_access/data_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,20 +307,28 @@ def push(self, file_name, checksum, rename):
raise IOError("Failed to push file - most likely a conflict was detected.")


class MemoryDataAccess(SSHDataAccess):
"""Mimic a client server architecture using in memory filesystems"""
class _DataAccessTemplate(SSHDataAccess):
"""Template for data access object using temp or in memory filesystems"""

def __init__(self):
self.local_fs = fs.open_fs("mem://")
self._fs = self._get_fs()
def __init__(self, fs_url):
self.local_fs = fs.open_fs(fs_url)
self._fs = self._get_fs(fs_url)
self.root = "foo"
self.join = fs.path.join

def _get_fs(self):
def _get_fs(self, fs_url):
mfs = MultiFS()
mfs.add_fs("in_memory", fs.open_fs("mem://"), write=True)
mfs.add_fs("remotefs", fs.open_fs(fs_url), write=True)
return mfs

def checksum(self, relative_path):
"""Return the checksum of the file path
:param str relative_path: path relative to root
:return: (*str*) -- the checksum of the file
"""
return self.fs.hash(relative_path, "sha256")

def push(self, file_name, checksum, rename):
"""Push file from local to remote filesystem, bypassing checksum since this is
in memory.
Expand All @@ -330,3 +338,17 @@ def push(self, file_name, checksum, rename):
:param str rename: the new filename
"""
fs.move.move_file(self.local_fs, file_name, self.fs, rename)


class TempDataAccess(_DataAccessTemplate):
"""Mimic a client server architecture using temp filesystems"""

def __init__(self):
super().__init__("temp://")


class MemoryDataAccess(_DataAccessTemplate):
"""Mimic a client server architecture using in memory filesystems"""

def __init__(self):
super().__init__("mem://")
50 changes: 50 additions & 0 deletions powersimdata/scenario/tests/test_scenario.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import os

import pytest

from powersimdata.data_access.context import Context
from powersimdata.data_access.data_access import TempDataAccess
from powersimdata.scenario.scenario import Scenario
from powersimdata.utility import templates


@pytest.mark.integration
Expand All @@ -9,3 +14,48 @@ def test_bad_scenario_name():
# This test will fail if we do add a scenario with this name
with pytest.raises(ValueError):
Scenario("this_scenario_does_not_exist")


def _mock_return(x=None):
tda = TempDataAccess()
for path in ("ExecuteList.csv", "ScenarioList.csv"):
orig = os.path.join(templates.__path__[0], path)
with open(orig, "rb") as f:
tda.fs.upload(path, f)
return tda


def test_scenario_workflow(monkeypatch):
monkeypatch.setattr(Context, "get_data_access", _mock_return)

s = Scenario()
print(s.state.name)

s.set_grid(interconnect="Texas")

s.set_name("test", "dummy")
s.set_time("2016-01-01 00:00:00", "2016-01-01 03:00:00", "1H")

s.set_base_profile("demand", "vJan2021")
s.set_base_profile("hydro", "vJan2021")
s.set_base_profile("solar", "vJan2021")
s.set_base_profile("wind", "vJan2021")
s.change_table.ct = {
"wind": {
"zone_id": {
301: 1.1293320940114195,
302: 2.2996731828360466,
303: 1.1460693669609412,
304: 1.5378918905751389,
305: 1.6606575751914816,
},
"plant_id": {12912: 0},
}
}

s.get_grid()
s.get_ct()

s.print_scenario_info()
# s.create_scenario()
# s.prepare_simulation_input()

0 comments on commit 6efaba5

Please sign in to comment.