Skip to content

Commit

Permalink
Refactor test_runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
sveinugu committed Dec 9, 2024
1 parent b565666 commit 02d91d3
Show file tree
Hide file tree
Showing 7 changed files with 296 additions and 382 deletions.
8 changes: 8 additions & 0 deletions src/omnipy/api/protocols/private/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,11 @@ def set_config(self, config: IsEngineConfig) -> None:

def set_registry(self, registry: IsRunStateRegistry | None) -> None:
...

@property
def config(self) -> IsEngineConfig:
...

@property
def registry(self) -> IsRunStateRegistry | None:
...
42 changes: 20 additions & 22 deletions src/omnipy/api/protocols/public/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ class IsRootLogObjects(Protocol):
def set_config(self, config: IsRootLogConfig) -> None:
...

@property
def config(self) -> IsRootLogConfig:
...


class IsRuntimeConfig(Protocol):
""""""
Expand All @@ -35,19 +39,16 @@ class IsRuntimeConfig(Protocol):
prefect: IsPrefectEngineConfig
root_log: IsRootLogConfig

def __init__(
self,
job: IsJobConfig | None = None, # noqa
data: IsDataConfig | None = None, # noqa
engine: EngineChoice = EngineChoice.LOCAL, # noqa
local: IsLocalRunnerConfig | None = None, # noqa
prefect: IsPrefectEngineConfig | None = None, # noqa
root_log: IsRootLogConfig | None = None, # noqa
*args: object,
**kwargs: object) -> None:
def __init__(self,
job: IsJobConfig,
data: IsDataConfig,
engine: EngineChoice,
local: IsLocalRunnerConfig,
prefect: IsPrefectEngineConfig,
root_log: IsRootLogConfig) -> None:
...

def reset_to_defaults(self):
def reset_to_defaults(self) -> None:
...


Expand All @@ -62,17 +63,14 @@ class IsRuntimeObjects(Protocol):
serializers: IsSerializerRegistry
root_log: IsRootLogObjects

def __init__(
self,
job_creator: IsJobConfigHolder | None = None, # noqa
data_class_creator: IsDataClassCreator | None = None, # noqa
local: IsEngine | None = None, # noqa
prefect: IsEngine | None = None, # noqa
registry: IsRunStateRegistry | None = None, # noqa
serializers: IsSerializerRegistry | None = None, # noqa
root_log: IsRootLogObjects | None = None, # noqa
*args: object,
**kwargs: object) -> None:
def __init__(self,
job_creator: IsJobConfigHolder,
data_class_creator: IsDataClassCreator,
local: IsEngine,
prefect: IsEngine,
registry: IsRunStateRegistry,
serializers: IsSerializerRegistry,
root_log: IsRootLogObjects) -> None:
...


Expand Down
8 changes: 8 additions & 0 deletions src/omnipy/engine/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,11 @@ def set_config(self, config: IsEngineConfig) -> None:

def set_registry(self, registry: IsRunStateRegistry | None) -> None:
self._registry = registry

@property
def config(self) -> IsEngineConfig:
return self._config

@property
def registry(self) -> IsRunStateRegistry | None:
return self._registry
4 changes: 4 additions & 0 deletions src/omnipy/hub/log/root_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ def set_config(self, config: IsRootLogConfig):
self._config = config
self._configure_all_objects()

@property
def config(self) -> IsRootLogConfig:
return self._config

def _configure_all_objects(self):
self._remove_all_handlers_from_root_logger()

Expand Down
4 changes: 2 additions & 2 deletions src/omnipy/modules/prefect/engine/prefect.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ def _update_from_config(self) -> None:

@classmethod
def get_config_cls(cls) -> Type[IsPrefectEngineConfig]:
return PrefectEngineConfig
return PrefectEngineConfigEntryPublisher

# TaskRunnerEngine

def _init_task(self, task: IsTask, call_func: Callable) -> PrefectTask:
assert isinstance(self._config, PrefectEngineConfig)
assert isinstance(self._config, PrefectEngineConfigEntryPublisher)
task_kwargs = dict(
name=task.name,
cache_key_fn=task_input_hash if self._config.use_cached_results else None,
Expand Down
138 changes: 21 additions & 117 deletions tests/hub/runtime/helpers/mocks.py
Original file line number Diff line number Diff line change
@@ -1,75 +1,24 @@
from abc import ABC, abstractmethod
from dataclasses import dataclass
from datetime import datetime
from typing import Protocol, Type
from typing import cast, Generic, Protocol

from omnipy.api.enums import RunState
from omnipy.api.protocols.private.log import IsRunStateRegistry
from omnipy.api.protocols.public.compute import IsTask
from omnipy.api.protocols.public.config import (IsEngineConfig,
IsLocalRunnerConfig,
IsPrefectEngineConfig,
IsRootLogConfig)
from omnipy.api.protocols.public.engine import IsTaskRunnerEngine


class IsMockJobConfig(Protocol):
persist_outputs: bool = True
restore_outputs: bool = False


@dataclass
class MockJobConfig:
persist_outputs: bool = True
restore_outputs: bool = False


class MockJobCreator:
def __init__(self) -> None:
self.engine: IsTaskRunnerEngine | None = None
self.config: IsMockJobConfig = MockJobConfig()

def set_engine(self, engine: IsTaskRunnerEngine) -> None:
self.engine = engine

def set_config(self, config: IsMockJobConfig) -> None:
self.config = config


class MockJobCreator2(MockJobCreator):
...


class IsMockDataConfig(Protocol):
interactive_mode: bool = True


@dataclass
class MockDataConfig:
interactive_mode: bool = True


class MockDataClassCreator:
def __init__(self) -> None:
self.config: IsMockDataConfig = MockDataConfig()

def set_config(self, config: IsMockDataConfig) -> None:
self.config = config
from typing_extensions import TypeVar

from omnipy.api.protocols.private.log import IsRunStateRegistry
from omnipy.api.protocols.public.config import IsEngineConfig

class MockDataClassCreator2(MockDataClassCreator):
...
EngineConfigT = TypeVar('EngineConfigT', bound=IsEngineConfig)


class MockEngine(ABC):
class MockEngine(ABC, Generic[EngineConfigT]):
def __init__(self) -> None:
config_cls = self.get_config_cls()
self._config: IsEngineConfig = config_cls()
self.registry: IsRunStateRegistry | None = None

@classmethod
@abstractmethod
def get_config_cls(cls) -> Type[IsEngineConfig]:
def get_config_cls(cls) -> type[EngineConfigT]:
...

def set_config(self, config: IsEngineConfig) -> None:
Expand All @@ -84,81 +33,36 @@ class MockLocalRunnerConfig:
backend_verbose: bool = True


class MockLocalRunnerConfig2(MockLocalRunnerConfig):
...
class IsMockLocalRunnerConfig(IsEngineConfig, Protocol):
backend_verbose: bool


class MockLocalRunner(MockEngine):
class MockLocalRunner(MockEngine[IsMockLocalRunnerConfig]):
@classmethod
def get_config_cls(cls) -> Type[IsEngineConfig]:
def get_config_cls(cls) -> type[IsMockLocalRunnerConfig]:
return MockLocalRunnerConfig

@property
def config(self) -> IsLocalRunnerConfig:
return self._config


class MockLocalRunner2(MockLocalRunner):
@classmethod
def get_config_cls(cls) -> Type[IsEngineConfig]:
return MockLocalRunnerConfig2
def config(self) -> IsMockLocalRunnerConfig:
return cast(IsMockLocalRunnerConfig, self._config)


@dataclass
class MockPrefectEngineConfig:
server_url: str = ''
use_cached_results: bool = False


class MockPrefectEngineConfig2(MockPrefectEngineConfig):
...
class IsMockPrefectEngineConfig(IsEngineConfig, Protocol):
server_url: str = ''
use_cached_results: bool


class MockPrefectEngine(MockEngine):
class MockPrefectEngine(MockEngine[IsMockPrefectEngineConfig]):
@classmethod
def get_config_cls(cls) -> Type[IsEngineConfig]:
def get_config_cls(cls) -> type[IsMockPrefectEngineConfig]:
return MockPrefectEngineConfig

@property
def config(self) -> IsPrefectEngineConfig:
return self._config


class MockPrefectEngine2(MockPrefectEngine):
@classmethod
def get_config_cls(cls) -> Type[IsEngineConfig]:
return MockPrefectEngineConfig2


class MockRootLogObjects:
def __init__(self):
self.config: IsRootLogConfig | None = None

def set_config(self, config: IsRootLogConfig):
self.config = config


class MockRootLogObjects2(MockRootLogObjects):
...


@dataclass
class MockRootLogConfig:
log_to_stderr: bool = True


class MockRunStateRegistry:
def get_task_state(self, task: IsTask) -> RunState:
...

def get_task_state_datetime(self, task: IsTask, state: RunState) -> datetime:
...

def all_tasks(self, state: RunState | None = None) -> tuple[IsTask, ...]: # noqa
...

def set_task_state(self, task: IsTask, state: RunState) -> None:
...


class MockRunStateRegistry2(MockRunStateRegistry):
...
def config(self) -> IsMockPrefectEngineConfig:
return cast(IsMockPrefectEngineConfig, self._config)
Loading

0 comments on commit 02d91d3

Please sign in to comment.