Skip to content

Commit

Permalink
fix orderer (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsv1 authored Dec 4, 2022
1 parent e0e7f44 commit b4a9f80
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 23 deletions.
13 changes: 13 additions & 0 deletions tests/core/scenario_orderer/_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from pathlib import Path

from vedro import Scenario
from vedro.core import VirtualScenario

__all__ = ("make_vscenario",)


def make_vscenario(path: str) -> VirtualScenario:
class _Scenario(Scenario):
__file__ = Path(path).absolute()

return VirtualScenario(_Scenario, steps=[])
52 changes: 52 additions & 0 deletions tests/core/scenario_orderer/test_plain_scenario_orderer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import pytest
from baby_steps import given, then, when

from vedro.core.scenario_orderer import PlainScenarioOrderer

from ._utils import make_vscenario


@pytest.fixture()
def orderer() -> PlainScenarioOrderer:
return PlainScenarioOrderer()


@pytest.mark.asyncio
async def test_sort_no_scenarios(*, orderer: PlainScenarioOrderer):
with given:
scenarios = []

with when:
res = await orderer.sort(scenarios)

with then:
assert res == []
assert scenarios == []


@pytest.mark.asyncio
async def test_sort_scenarios(*, orderer: PlainScenarioOrderer):
with given:
orig_scenarios = {path: make_vscenario(path) for path in [
"scenarios/directory/scn.py",
"scenarios/dir1/scn1.py",
"scenarios/dir1/scn2.py",
"scenarios/dir2/scn2.py",
"scenarios/scn2.py",
"scenarios/scn10.py",
]}
scenarios = list(orig_scenarios.values())

with when:
res = await orderer.sort(scenarios)

with then:
assert res == [
orig_scenarios["scenarios/directory/scn.py"],
orig_scenarios["scenarios/dir1/scn1.py"],
orig_scenarios["scenarios/dir1/scn2.py"],
orig_scenarios["scenarios/dir2/scn2.py"],
orig_scenarios["scenarios/scn2.py"],
orig_scenarios["scenarios/scn10.py"],
]
assert scenarios == list(orig_scenarios.values())
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest
from baby_steps import given, then, when

from vedro.plugins.orderer import StableScenarioOrderer
from vedro.core.scenario_orderer import StableScenarioOrderer

from ._utils import make_vscenario

Expand Down
4 changes: 2 additions & 2 deletions vedro/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
ExtFilter,
HiddenFilter,
)
from vedro.core.scenario_orderer import PlainScenarioOrderer
from vedro.core.scenario_orderer import StableScenarioOrderer

__all__ = ("Config",)

Expand All @@ -54,7 +54,7 @@ class Registry(core.Config.Registry):

ScenarioLoader = Factory[ScenarioLoader](ScenarioFileLoader)

ScenarioOrderer = Factory[ScenarioOrderer](PlainScenarioOrderer)
ScenarioOrderer = Factory[ScenarioOrderer](StableScenarioOrderer)

ScenarioDiscoverer = Factory[ScenarioDiscoverer](lambda: MultiScenarioDiscoverer(
finder=Config.Registry.ScenarioFinder(),
Expand Down
3 changes: 2 additions & 1 deletion vedro/core/scenario_orderer/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from ._plain_scenario_orderer import PlainScenarioOrderer
from ._scenario_orderer import ScenarioOrderer
from ._stable_scenario_orderer import StableScenarioOrderer

__all__ = ("ScenarioOrderer", "PlainScenarioOrderer",)
__all__ = ("ScenarioOrderer", "PlainScenarioOrderer", "StableScenarioOrderer",)
1 change: 1 addition & 0 deletions vedro/core/scenario_orderer/_plain_scenario_orderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
__all__ = ("PlainScenarioOrderer",)


# deprecated
class PlainScenarioOrderer(ScenarioOrderer):
async def sort(self, scenarios: List[VirtualScenario]) -> List[VirtualScenario]:
return scenarios
16 changes: 16 additions & 0 deletions vedro/core/scenario_orderer/_stable_scenario_orderer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from pathlib import Path
from typing import Any, List, Tuple

from .._virtual_scenario import VirtualScenario
from ._scenario_orderer import ScenarioOrderer

__all__ = ("StableScenarioOrderer",)


class StableScenarioOrderer(ScenarioOrderer):
def _cmp(self, scn: VirtualScenario) -> Tuple[Any, ...]:
path = Path(scn.path)
return (len(path.parts),) + tuple((len(x), x) for x in path.parts)

async def sort(self, scenarios: List[VirtualScenario]) -> List[VirtualScenario]:
return list(sorted(scenarios, key=self._cmp))
12 changes: 8 additions & 4 deletions vedro/plugins/orderer/orderer_plugin.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from vedro.core import ConfigType, Dispatcher, Plugin, PluginConfig
from vedro.core.scenario_orderer import StableScenarioOrderer
from vedro.events import ArgParsedEvent, ArgParseEvent, ConfigLoadedEvent

from .random_orderer import RandomOrderer
from .reversed_orderer import ReversedOrderer
from .stable_orderer import StableScenarioOrderer

__all__ = ("Orderer", "OrdererPlugin")

Expand All @@ -29,11 +29,15 @@ def on_arg_parse(self, event: ArgParseEvent) -> None:
help="Set random scenario order")

def on_arg_parsed(self, event: ArgParsedEvent) -> None:
if event.args.order_random:
self._global_config.Registry.ScenarioOrderer.register(RandomOrderer, self)
return

if event.args.order_reversed:
self._global_config.Registry.ScenarioOrderer.register(ReversedOrderer, self)
elif event.args.order_random:
self._global_config.Registry.ScenarioOrderer.register(RandomOrderer, self)
else:
return

if event.args.order_stable:
self._global_config.Registry.ScenarioOrderer.register(StableScenarioOrderer, self)


Expand Down
3 changes: 1 addition & 2 deletions vedro/plugins/orderer/reversed_orderer.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from typing import List

from vedro.core import VirtualScenario

from .stable_orderer import StableScenarioOrderer
from vedro.core.scenario_orderer import StableScenarioOrderer

__all__ = ("ReversedOrderer",)

Expand Down
15 changes: 2 additions & 13 deletions vedro/plugins/orderer/stable_orderer.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
from pathlib import Path
from typing import Any, List, Tuple

from vedro.core import ScenarioOrderer, VirtualScenario
# backward compatibility
from vedro.core.scenario_orderer import StableScenarioOrderer

__all__ = ("StableScenarioOrderer",)


class StableScenarioOrderer(ScenarioOrderer):
def _cmp(self, scn: VirtualScenario) -> Tuple[Any, ...]:
path = Path(scn.path)
return (len(path.parts),) + tuple((len(x), x) for x in path.parts)

async def sort(self, scenarios: List[VirtualScenario]) -> List[VirtualScenario]:
return list(sorted(scenarios, key=self._cmp))

0 comments on commit b4a9f80

Please sign in to comment.