Skip to content

Commit

Permalink
refactor(collector.py): move method to get fixture manager into colle…
Browse files Browse the repository at this point in the history
…ctor
  • Loading branch information
matosys committed Oct 12, 2024
1 parent 58005bd commit 197f559
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
6 changes: 4 additions & 2 deletions src/_balder/balder_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,10 @@ def solve(self):
"""
This method resolves all classes and executes different checks, that can be done before the test session starts.
"""
self.solver = Solver(setups=self.all_collected_setups, scenarios=self.all_collected_scenarios,
connections=self.all_collected_connections, raw_fixtures=self.collector._raw_fixtures)
self.solver = Solver(setups=self.all_collected_setups,
scenarios=self.all_collected_scenarios,
connections=self.all_collected_connections,
fixture_manager=self.collector.get_fixture_manager())
self.solver.resolve(plugin_manager=self.plugin_manager)

def create_executor_tree(self):
Expand Down
21 changes: 20 additions & 1 deletion src/_balder/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
import pathlib
import functools
import importlib.util
from _balder.utils import get_class_that_defines_method
from _balder.utils import get_class_that_defines_method, inspect_method
from _balder.setup import Setup
from _balder.device import Device
from _balder.feature import Feature
from _balder.vdevice import VDevice
from _balder.scenario import Scenario
from _balder.connection import Connection
from _balder.fixture_manager import FixtureManager
from _balder.fixture_execution_level import FixtureExecutionLevel
from _balder.controllers import ScenarioController, SetupController, DeviceController, VDeviceController, \
FeatureController, NormalScenarioSetupController
from _balder.exceptions import DuplicateForVDeviceError, UnknownVDeviceException
Expand Down Expand Up @@ -124,6 +126,23 @@ def all_connections(self) -> List[Type[Connection]]:
raise AttributeError("please call the `collect()` method before omitting this value")
return self._all_connections

def get_fixture_manager(self) -> FixtureManager:
"""
Resolves all fixtures and returns the fixture manager for this session
:return: the fixture manager that is valid for this session
"""
resolved_dict = {}
for cur_level_as_str, cur_module_fixture_dict in self._raw_fixtures.items():
cur_level = FixtureExecutionLevel(cur_level_as_str)
resolved_dict[cur_level] = {}
for cur_fn in cur_module_fixture_dict:
cls, func_type = inspect_method(cur_fn)
# mechanism also works for balderglob fixtures (`func_type` is 'function' and `cls` is None)
if cls not in resolved_dict[cur_level].keys():
resolved_dict[cur_level][cls] = []
resolved_dict[cur_level][cls].append((func_type, cur_fn))
return FixtureManager(resolved_dict)

def load_balderglob_py_file(self) -> Union[types.ModuleType, None]:
"""
This method loads the global balderglob.py file and returns the module or None if the file does not exist.
Expand Down
27 changes: 3 additions & 24 deletions src/_balder/solver.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from __future__ import annotations
from typing import List, Dict, Tuple, Type, Union, Callable, TYPE_CHECKING
from typing import List, Dict, Tuple, Type, Union, TYPE_CHECKING

import itertools
from _balder.utils import inspect_method
from _balder.fixture_manager import FixtureManager
from _balder.fixture_execution_level import FixtureExecutionLevel
from _balder.executor.executor_tree import ExecutorTree
from _balder.executor.setup_executor import SetupExecutor
from _balder.executor.scenario_executor import ScenarioExecutor
Expand All @@ -28,7 +26,7 @@ class Solver:
"""

def __init__(self, setups: List[Type[Setup]], scenarios: List[Type[Scenario]], connections: List[Type[Connection]],
raw_fixtures: Dict[str, List[Callable]]):
fixture_manager: Union[FixtureManager, None]):
#: contains all available setup classes
self._all_existing_setups = setups
#: contains all available scenario classes
Expand All @@ -41,8 +39,7 @@ def __init__(self, setups: List[Type[Setup]], scenarios: List[Type[Scenario]], c
self._mapping: List[Tuple[Type[Setup], Type[Scenario], Dict[Type[Device], Type[Device]]]] = []
self._resolving_was_executed = False

self._raw_fixtures = raw_fixtures
self._fixture_manager: Union[FixtureManager, None] = None
self._fixture_manager = fixture_manager

# ---------------------------------- STATIC METHODS ----------------------------------------------------------------

Expand Down Expand Up @@ -118,23 +115,6 @@ def _get_all_unfiltered_mappings(self) -> List[Tuple[Type[Setup], Type[Scenario]

# ---------------------------------- METHODS -----------------------------------------------------------------------

def get_fixture_manager(self) -> FixtureManager:
"""
Resolves all fixtures and returns the fixture manager for this session
:return: the fixture manager that is valid for this session
"""
resolved_dict = {}
for cur_level_as_str, cur_module_fixture_dict in self._raw_fixtures.items():
cur_level = FixtureExecutionLevel(cur_level_as_str)
resolved_dict[cur_level] = {}
for cur_fn in cur_module_fixture_dict:
cls, func_type = inspect_method(cur_fn)
# mechanism also works for balderglob fixtures (`func_type` is 'function' and `cls` is None)
if cls not in resolved_dict[cur_level].keys():
resolved_dict[cur_level][cls] = []
resolved_dict[cur_level][cls].append((func_type, cur_fn))
return FixtureManager(resolved_dict)

def get_initial_mapping(self) -> List[Tuple[Type[Setup], Type[Scenario], Dict[Device, Device]]]:
"""
This method creates the initial amount of data for `self._mapping`. Only those elements are returned where the
Expand Down Expand Up @@ -165,7 +145,6 @@ def resolve(self, plugin_manager: PluginManager) -> None: # pylint: disable=unu
This method carries out the entire resolve process and saves the end result in the object property
`self._mapping`.
"""
self._fixture_manager = self.get_fixture_manager()
# reset mapping list
self._mapping = []
initial_mapping = self.get_initial_mapping()
Expand Down

0 comments on commit 197f559

Please sign in to comment.