Skip to content

Commit

Permalink
add boolean logic tag matcher (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsv1 authored Jul 31, 2023
1 parent ba91ca2 commit eca89fe
Show file tree
Hide file tree
Showing 12 changed files with 484 additions and 36 deletions.
5 changes: 3 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
dessert>=1.4,<2.0
rich>=10.0,<14.0
cabina>=0.7,<1.0
dessert>=1.4,<2.0
filelock>=3.5,<4.0
niltype>=0.3,<2.0
pyparsing>=3.0,<4.0
rich>=11.0,<14.0
11 changes: 9 additions & 2 deletions tests/plugins/tagger/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import pytest

from vedro import Scenario
from vedro.core import Dispatcher, VirtualScenario
from vedro.events import ArgParsedEvent, ArgParseEvent
from vedro.core import Dispatcher
from vedro.core import MonotonicScenarioScheduler as Scheduler
from vedro.core import VirtualScenario
from vedro.events import ArgParsedEvent, ArgParseEvent, StartupEvent
from vedro.plugins.tagger import Tagger, TaggerPlugin


Expand Down Expand Up @@ -43,3 +45,8 @@ async def fire_arg_parsed_event(dispatcher: Dispatcher, *, tags: Optional[str] =

arg_parsed_event = ArgParsedEvent(Namespace(tags=tags))
await dispatcher.fire(arg_parsed_event)


async def fire_startup_event(dispatcher: Dispatcher, scheduler: Scheduler) -> None:
startup_event = StartupEvent(scheduler)
await dispatcher.fire(startup_event)
139 changes: 139 additions & 0 deletions tests/plugins/tagger/test_logic_ops.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
from typing import Set

import pytest
from baby_steps import given, then, when
from pytest import raises

from vedro.plugins.tagger.logic_tag_matcher._logic_ops import And, Expr, Not, Operand, Operator, Or


def test_tag_expr():
with when, raises(BaseException) as exc:
Expr()

with then:
assert exc.type is TypeError


def test_tag_operator():
with when, raises(BaseException) as exc:
Operator()

with then:
assert exc.type is TypeError


def test_tag_operand():
with when:
operand = Operand("API")

with then:
assert isinstance(operand, Expr)
assert repr(operand) == "Tag(API)"


@pytest.mark.parametrize(("tags", "result"), [
({"API"}, True),
({"P0"}, False),
])
def test_tag_operand_expr(tags: Set[str], result: bool):
with given:
operand = Operand("API")

with when:
res = operand(tags)

with then:
assert res is result


def test_not_operator():
with given:
operand = Operand("API")

with when:
operator = Not(operand)

with then:
assert isinstance(operator, Expr)
assert repr(operator) == "Not(Tag(API))"


@pytest.mark.parametrize(("tags", "result"), [
({"API"}, False),
({"P0"}, True),
])
def test_not_operator_expr(tags: Set[str], result: bool):
with given:
operand = Operand("API")
operator = Not(operand)

with when:
res = operator(tags)

with then:
assert res is result


def test_and_operator():
with given:
left_operand = Operand("API")
right_operand = Operand("P0")

with when:
operator = And(left_operand, right_operand)

with then:
assert isinstance(operator, Expr)
assert repr(operator) == "And(Tag(API), Tag(P0))"


@pytest.mark.parametrize(("tags", "result"), [
({"API", "P0"}, True),
({"P0"}, False),
({"API"}, False),
({"CLI"}, False),
])
def test_and_operator_expr(tags: Set[str], result: bool):
with given:
left_operand = Operand("API")
right_operand = Operand("P0")
operator = And(left_operand, right_operand)

with when:
res = operator(tags)

with then:
assert res is result


def test_or_operator():
with given:
left_operand = Operand("API")
right_operand = Operand("P0")

with when:
operator = Or(left_operand, right_operand)

with then:
assert isinstance(operator, Expr)
assert repr(operator) == "Or(Tag(API), Tag(P0))"


@pytest.mark.parametrize(("tags", "result"), [
({"API", "P0"}, True),
({"P0"}, True),
({"API"}, True),
({"CLI"}, False),
])
def test_or_operator_expr(tags: Set[str], result: bool):
with given:
left_operand = Operand("API")
right_operand = Operand("P0")
operator = Or(left_operand, right_operand)

with when:
res = operator(tags)

with then:
assert res is result
50 changes: 50 additions & 0 deletions tests/plugins/tagger/test_logic_tag_matcher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import pytest
from baby_steps import given, then, when
from pytest import raises

from vedro.plugins.tagger.logic_tag_matcher import LogicTagMatcher


def test_match():
with given:
matcher = LogicTagMatcher("P0")

with when:
res = matcher.match({"P0"})

with then:
assert res is True


def test_not_match():
with given:
matcher = LogicTagMatcher("P0")

with when:
res = matcher.match({"API"})

with then:
assert res is False


def test_invalid_expr():
with when, raises(ValueError) as exc:
LogicTagMatcher("P0 and")

with then:
assert exc.type is ValueError


@pytest.mark.parametrize("expr", [
"1TAG",
"-TAG",
"and",
"or",
"not",
])
def test_invalid_tag(expr: str):
with when, raises(ValueError) as exc:
LogicTagMatcher(expr)

with then:
assert exc.type is ValueError
12 changes: 12 additions & 0 deletions tests/plugins/tagger/test_tag_matcher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from baby_steps import then, when
from pytest import raises

from vedro.plugins.tagger import TagMatcher


def test_tag_matcher():
with when, raises(BaseException) as exc:
TagMatcher("expr")

with then:
assert exc.type is TypeError
Loading

0 comments on commit eca89fe

Please sign in to comment.