Add basic primaite session e2e tests
This commit is contained in:
@@ -9,8 +9,10 @@ from unittest.mock import patch
|
||||
|
||||
import nodeenv
|
||||
import pytest
|
||||
import yaml
|
||||
|
||||
from primaite import getLogger
|
||||
from primaite.game.session import PrimaiteSession
|
||||
|
||||
# from primaite.environment.primaite_env import Primaite
|
||||
# from primaite.primaite_session import PrimaiteSession
|
||||
@@ -71,102 +73,32 @@ def file_system() -> FileSystem:
|
||||
|
||||
|
||||
# PrimAITE v2 stuff
|
||||
@pytest.mark.skip("Deprecated") # TODO: implement a similar test for primaite v3
|
||||
class TempPrimaiteSession: # PrimaiteSession):
|
||||
class TempPrimaiteSession(PrimaiteSession):
|
||||
"""
|
||||
A temporary PrimaiteSession class.
|
||||
|
||||
Uses context manager for deletion of files upon exit.
|
||||
"""
|
||||
|
||||
# def __init__(
|
||||
# self,
|
||||
# training_config_path: Union[str, Path],
|
||||
# lay_down_config_path: Union[str, Path],
|
||||
# ):
|
||||
# super().__init__(training_config_path, lay_down_config_path)
|
||||
# self.setup()
|
||||
@classmethod
|
||||
def from_config(cls, config_path: Union[str, Path]) -> "TempPrimaiteSession":
|
||||
"""Create a temporary PrimaiteSession object from a config file."""
|
||||
config_path = Path(config_path)
|
||||
with open(config_path, "r") as f:
|
||||
config = yaml.safe_load(f)
|
||||
|
||||
# @property
|
||||
# def env(self) -> Primaite:
|
||||
# """Direct access to the env for ease of testing."""
|
||||
# return self._agent_session._env # noqa
|
||||
return super().from_config(cfg=config)
|
||||
|
||||
# def __enter__(self):
|
||||
# return self
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
# def __exit__(self, type, value, tb):
|
||||
# shutil.rmtree(self.session_path)
|
||||
# _LOGGER.debug(f"Deleted temp session directory: {self.session_path}")
|
||||
def __exit__(self, type, value, tb):
|
||||
pass
|
||||
|
||||
|
||||
@pytest.mark.skip("Deprecated") # TODO: implement a similar test for primaite v3
|
||||
@pytest.fixture
|
||||
def temp_primaite_session(request):
|
||||
"""
|
||||
Provides a temporary PrimaiteSession instance.
|
||||
def temp_primaite_session(request) -> TempPrimaiteSession:
|
||||
"""Create a temporary PrimaiteSession object."""
|
||||
|
||||
It's temporary as it uses a temporary directory as the session path.
|
||||
|
||||
To use this fixture you need to:
|
||||
|
||||
- parametrize your test function with:
|
||||
|
||||
- "temp_primaite_session"
|
||||
- [[path to training config, path to lay down config]]
|
||||
- Include the temp_primaite_session fixture as a param in your test
|
||||
function.
|
||||
- use the temp_primaite_session as a context manager assigning is the
|
||||
name 'session'.
|
||||
|
||||
.. code:: python
|
||||
|
||||
from primaite.config.lay_down_config import dos_very_basic_config_path
|
||||
from primaite.config.training_config import main_training_config_path
|
||||
@pytest.mark.parametrize(
|
||||
"temp_primaite_session",
|
||||
[
|
||||
[main_training_config_path(), dos_very_basic_config_path()]
|
||||
],
|
||||
indirect=True
|
||||
)
|
||||
def test_primaite_session(temp_primaite_session):
|
||||
with temp_primaite_session as session:
|
||||
# Learning outputs are saved in session.learning_path
|
||||
session.learn()
|
||||
|
||||
# Evaluation outputs are saved in session.evaluation_path
|
||||
session.evaluate()
|
||||
|
||||
# To ensure that all files are written, you must call .close()
|
||||
session.close()
|
||||
|
||||
# If you need to inspect any session outputs, it must be done
|
||||
# inside the context manager
|
||||
|
||||
# Now that we've exited the context manager, the
|
||||
# session.session_path directory and its contents are deleted
|
||||
"""
|
||||
training_config_path = request.param[0]
|
||||
lay_down_config_path = request.param[1]
|
||||
with patch("primaite.agents.agent_abc.get_session_path", get_temp_session_path) as mck:
|
||||
mck.session_timestamp = datetime.now()
|
||||
|
||||
return TempPrimaiteSession(training_config_path, lay_down_config_path)
|
||||
|
||||
|
||||
@pytest.mark.skip("Deprecated") # TODO: implement a similar test for primaite v3
|
||||
@pytest.fixture
|
||||
def temp_session_path() -> Path:
|
||||
"""
|
||||
Get a temp directory session path the test session will output to.
|
||||
|
||||
:return: The session directory path.
|
||||
"""
|
||||
session_timestamp = datetime.now()
|
||||
date_dir = session_timestamp.strftime("%Y-%m-%d")
|
||||
session_path = session_timestamp.strftime("%Y-%m-%d_%H-%M-%S")
|
||||
session_path = Path(tempfile.gettempdir()) / "_primaite" / date_dir / session_path
|
||||
session_path.mkdir(exist_ok=True, parents=True)
|
||||
|
||||
return session_path
|
||||
config_path = request.param[0]
|
||||
return TempPrimaiteSession.from_config(config_path=config_path)
|
||||
|
||||
Reference in New Issue
Block a user