|
|
|
@@ -1,233 +0,0 @@
|
|
|
|
|
from dataclasses import dataclass
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
from core.builtin_concepts import BuiltinConcepts
|
|
|
|
|
from core.sheerka.services.SheerkaFilter import Pipe, SheerkaFilter
|
|
|
|
|
from printer.FormatInstructions import FormatInstructions, FormatDetailDesc, FormatDetailType
|
|
|
|
|
|
|
|
|
|
from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
|
class Obj:
|
|
|
|
|
prop1: str
|
|
|
|
|
prop2: str
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
|
class ObjWithAsBag:
|
|
|
|
|
prop1: str
|
|
|
|
|
prop2: object
|
|
|
|
|
|
|
|
|
|
def as_bag(self):
|
|
|
|
|
return {
|
|
|
|
|
"first_prop": self.prop1,
|
|
|
|
|
"second_prop": self.prop2,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestSheerkaFilter(TestUsingMemoryBasedSheerka):
|
|
|
|
|
|
|
|
|
|
def test_i_can_pipe_using_decorator(self):
|
|
|
|
|
@Pipe
|
|
|
|
|
def is_ok_with_decorator(iterable):
|
|
|
|
|
for item in iterable:
|
|
|
|
|
yield item + " ok"
|
|
|
|
|
|
|
|
|
|
def exclamation(iterable):
|
|
|
|
|
for item in iterable:
|
|
|
|
|
yield item + "!"
|
|
|
|
|
|
|
|
|
|
res = ["one", "two", "three"] | is_ok_with_decorator | Pipe(exclamation)
|
|
|
|
|
assert list(res) == ["one ok!", "two ok!", "three ok!"]
|
|
|
|
|
|
|
|
|
|
def test_i_can_pipe_function_with_context_as_first_parameter(self):
|
|
|
|
|
def func_with_context(context, iterable, var_name):
|
|
|
|
|
for item in iterable:
|
|
|
|
|
yield f"{context.desc}: {var_name}={item}"
|
|
|
|
|
|
|
|
|
|
sheerka, context = self.init_concepts()
|
|
|
|
|
context.desc = "desc"
|
|
|
|
|
pipeable = Pipe(func_with_context, context)
|
|
|
|
|
|
|
|
|
|
assert pipeable.need_context
|
|
|
|
|
assert list(["one", "two", "three"] | pipeable("var")) == ['desc: var=one', 'desc: var=two', 'desc: var=three']
|
|
|
|
|
|
|
|
|
|
def test_i_can_pipe_function_with_context_as_only_parameter(self):
|
|
|
|
|
# This time, func_with_context does not have other parameter than context and iterable
|
|
|
|
|
def func_with_context(context, iterable):
|
|
|
|
|
for item in iterable:
|
|
|
|
|
yield f"{context.desc}: var={item}"
|
|
|
|
|
|
|
|
|
|
sheerka, context = self.init_concepts()
|
|
|
|
|
context.desc = "desc"
|
|
|
|
|
pipeable = Pipe(func_with_context, context)
|
|
|
|
|
|
|
|
|
|
assert pipeable.need_context
|
|
|
|
|
assert list(["one", "two", "three"] | pipeable) == ['desc: var=one', 'desc: var=two', 'desc: var=three']
|
|
|
|
|
|
|
|
|
|
def test_i_can_pipe_explanation_concept(self):
|
|
|
|
|
sheerka, context = self.init_concepts()
|
|
|
|
|
execution_contexts = [context.push(BuiltinConcepts.NOP, None, desc=f"desc_{i}") for i in range(4)]
|
|
|
|
|
explanation_node = sheerka.new(BuiltinConcepts.EXPLANATION, body=execution_contexts)
|
|
|
|
|
|
|
|
|
|
@Pipe
|
|
|
|
|
def get_desc(iterable):
|
|
|
|
|
for item in iterable:
|
|
|
|
|
yield item.desc
|
|
|
|
|
|
|
|
|
|
res = explanation_node | get_desc
|
|
|
|
|
|
|
|
|
|
assert sheerka.isinstance(res, BuiltinConcepts.EXPLANATION)
|
|
|
|
|
assert list(res.body) == ["desc_0", "desc_1", "desc_2", "desc_3"] # body is modified
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("predicate, expected", [
|
|
|
|
|
("True", ["one", "two", "three"]),
|
|
|
|
|
("self == 'two'", ["two"])
|
|
|
|
|
])
|
|
|
|
|
def test_i_can_filter(self, predicate, expected):
|
|
|
|
|
filter_service = SheerkaFilter(None)
|
|
|
|
|
|
|
|
|
|
res = ["one", "two", "three"] | Pipe(filter_service.pipe_filter)(predicate)
|
|
|
|
|
|
|
|
|
|
assert list(res) == expected
|
|
|
|
|
|
|
|
|
|
def test_i_can_filter_obj(self):
|
|
|
|
|
filter_service = SheerkaFilter(None)
|
|
|
|
|
|
|
|
|
|
lst = [Obj("a", "b"), Obj("c", "d")]
|
|
|
|
|
predicate = "prop2 == 'd'"
|
|
|
|
|
res = lst | Pipe(filter_service.pipe_filter)(predicate)
|
|
|
|
|
|
|
|
|
|
assert list(res) == [Obj("c", "d")]
|
|
|
|
|
|
|
|
|
|
def test_i_can_filter_obj_implementing_as_bag(self):
|
|
|
|
|
filter_service = SheerkaFilter(None)
|
|
|
|
|
|
|
|
|
|
lst = [ObjWithAsBag("a", "b"), ObjWithAsBag("c", "d")]
|
|
|
|
|
predicate = "second_prop == 'd'"
|
|
|
|
|
res = lst | Pipe(filter_service.pipe_filter)(predicate)
|
|
|
|
|
|
|
|
|
|
assert list(res) == [ObjWithAsBag("c", "d")]
|
|
|
|
|
|
|
|
|
|
def test_i_can_manage_name_error(self):
|
|
|
|
|
filter_service = SheerkaFilter(None)
|
|
|
|
|
|
|
|
|
|
lst = [Obj("a", "b"), Obj("c", "d"), ObjWithAsBag("a", "b"), ObjWithAsBag("c", "d")]
|
|
|
|
|
predicate = "second_prop == 'd'" # 'second_prop' does not exist in Obj
|
|
|
|
|
res = lst | Pipe(filter_service.pipe_filter)(predicate)
|
|
|
|
|
|
|
|
|
|
assert list(res) == [ObjWithAsBag("c", "d")]
|
|
|
|
|
|
|
|
|
|
def test_i_cannot_filter_if_the_predicate_is_incorrect(self):
|
|
|
|
|
filter_service = SheerkaFilter(None)
|
|
|
|
|
|
|
|
|
|
lst = [Obj("a", "b"), Obj("c", "d")]
|
|
|
|
|
predicate = "prop2 =="
|
|
|
|
|
|
|
|
|
|
with pytest.raises(SyntaxError):
|
|
|
|
|
res = lst | Pipe(filter_service.pipe_filter)(predicate)
|
|
|
|
|
list(res)
|
|
|
|
|
|
|
|
|
|
def test_i_can_format_l(self):
|
|
|
|
|
sheerka, context, foo, bar = self.init_concepts("foo", "bar")
|
|
|
|
|
lst = [foo, bar]
|
|
|
|
|
|
|
|
|
|
res = lst | Pipe(SheerkaFilter.pipe_format_l)("my_format")
|
|
|
|
|
res = list(res)
|
|
|
|
|
|
|
|
|
|
assert len(res) == 2
|
|
|
|
|
format_instructions = res[0].get_prop(BuiltinConcepts.FORMAT_INSTRUCTIONS)
|
|
|
|
|
assert isinstance(format_instructions, FormatInstructions)
|
|
|
|
|
assert format_instructions.format_l[f"c:{foo.id}:"] == "my_format"
|
|
|
|
|
|
|
|
|
|
format_instructions = res[1].get_prop(BuiltinConcepts.FORMAT_INSTRUCTIONS)
|
|
|
|
|
assert isinstance(format_instructions, FormatInstructions)
|
|
|
|
|
assert format_instructions.format_l[f"c:{bar.id}:"] == "my_format"
|
|
|
|
|
|
|
|
|
|
def test_i_can_format_d(self):
|
|
|
|
|
sheerka, context, foo, bar = self.init_concepts("foo", "bar")
|
|
|
|
|
lst = [foo, bar]
|
|
|
|
|
|
|
|
|
|
res = lst | Pipe(SheerkaFilter.pipe_format_d)("id", "name", "body", id="%red%{id}%reset%")
|
|
|
|
|
res = list(res)
|
|
|
|
|
|
|
|
|
|
expected_props = {
|
|
|
|
|
"id": "%red%{id}%reset%",
|
|
|
|
|
"name": "{name}",
|
|
|
|
|
"body": "{body}"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert len(res) == 2
|
|
|
|
|
format_instructions = res[0].get_prop(BuiltinConcepts.FORMAT_INSTRUCTIONS)
|
|
|
|
|
assert isinstance(format_instructions, FormatInstructions)
|
|
|
|
|
assert format_instructions.format_d[f"c:{foo.id}:"] == FormatDetailDesc(FormatDetailType.Props_In_Line, expected_props)
|
|
|
|
|
|
|
|
|
|
format_instructions = res[1].get_prop(BuiltinConcepts.FORMAT_INSTRUCTIONS)
|
|
|
|
|
assert isinstance(format_instructions, FormatInstructions)
|
|
|
|
|
assert format_instructions.format_d[f"c:{bar.id}:"] == FormatDetailDesc(FormatDetailType.Props_In_Line, expected_props)
|
|
|
|
|
|
|
|
|
|
def test_i_can_format_d_all_properties(self):
|
|
|
|
|
sheerka, context, foo, bar = self.init_concepts("foo", "bar")
|
|
|
|
|
lst = [foo, bar]
|
|
|
|
|
|
|
|
|
|
res = lst | Pipe(SheerkaFilter.pipe_format_d)()
|
|
|
|
|
res = list(res)
|
|
|
|
|
|
|
|
|
|
expected_props = {
|
|
|
|
|
'id': '{id}',
|
|
|
|
|
'name': '{name}',
|
|
|
|
|
'key': '{key}',
|
|
|
|
|
'body': '{body}',
|
|
|
|
|
'self': '{self}'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert len(res) == 2
|
|
|
|
|
format_instructions = res[0].get_prop(BuiltinConcepts.FORMAT_INSTRUCTIONS)
|
|
|
|
|
assert isinstance(format_instructions, FormatInstructions)
|
|
|
|
|
assert format_instructions.format_d[f"c:{foo.id}:"] == FormatDetailDesc(FormatDetailType.Props_In_Line, expected_props)
|
|
|
|
|
|
|
|
|
|
def test_i_can_set_recurse(self):
|
|
|
|
|
sheerka, context, foo, bar = self.init_concepts("foo", "bar")
|
|
|
|
|
lst = [foo, bar]
|
|
|
|
|
|
|
|
|
|
res = lst | Pipe(SheerkaFilter.pipe_recurse)(10)
|
|
|
|
|
res = list(res)
|
|
|
|
|
|
|
|
|
|
assert len(res) == 2
|
|
|
|
|
format_instructions = res[0].get_prop(BuiltinConcepts.FORMAT_INSTRUCTIONS)
|
|
|
|
|
assert isinstance(format_instructions, FormatInstructions)
|
|
|
|
|
assert format_instructions.recursive_props["_children"] == 10
|
|
|
|
|
|
|
|
|
|
format_instructions = res[1].get_prop(BuiltinConcepts.FORMAT_INSTRUCTIONS)
|
|
|
|
|
assert isinstance(format_instructions, FormatInstructions)
|
|
|
|
|
assert format_instructions.recursive_props["_children"] == 10
|
|
|
|
|
|
|
|
|
|
res = lst | Pipe(SheerkaFilter.pipe_recurse)(15, "other_prop")
|
|
|
|
|
res = list(res)
|
|
|
|
|
|
|
|
|
|
assert len(res) == 2
|
|
|
|
|
format_instructions = res[0].get_prop(BuiltinConcepts.FORMAT_INSTRUCTIONS)
|
|
|
|
|
assert isinstance(format_instructions, FormatInstructions)
|
|
|
|
|
assert format_instructions.recursive_props["_children"] == 10
|
|
|
|
|
assert format_instructions.recursive_props["other_prop"] == 15
|
|
|
|
|
|
|
|
|
|
def test_i_can_inspect_obj(self):
|
|
|
|
|
filter_service = SheerkaFilter(None)
|
|
|
|
|
|
|
|
|
|
lst = [Obj("a", "b"), Obj("c", "d")]
|
|
|
|
|
res = lst | Pipe(filter_service.pipe_inspect)("prop2")
|
|
|
|
|
|
|
|
|
|
assert list(res) == ["b", "d"]
|
|
|
|
|
|
|
|
|
|
def test_i_can_inspect_obj_with_bag(self):
|
|
|
|
|
filter_service = SheerkaFilter(None)
|
|
|
|
|
|
|
|
|
|
lst = [ObjWithAsBag("a", "b"), ObjWithAsBag("c", "d")]
|
|
|
|
|
res = lst | Pipe(filter_service.pipe_inspect)("second_prop")
|
|
|
|
|
|
|
|
|
|
assert list(res) == ["b", "d"]
|
|
|
|
|
|
|
|
|
|
lst = [ObjWithAsBag("a", ObjWithAsBag("b", ObjWithAsBag("c", "d")))]
|
|
|
|
|
res = lst | Pipe(filter_service.pipe_inspect)("second_prop.second_prop.second_prop")
|
|
|
|
|
assert list(res) == ["d"]
|