def test_with_self():
    class WithSelfSet(RuleFunctionBase):
        def execute(self, arg1, arg2, arg3):

            self.payload["arg1"] = arg1
            self.payload["arg2"] = arg2
            self.payload["arg3"] = arg3

    RuleFactory.create(
        "test-with-self",
        subscribe_to="test-argprocessors-self",
        data={
            processing: [
                WithSelfSet(lambda self: self.payload["value_from"],
                            arg2=lambda self: self.subject.get("value_from"),
                            arg3=lambda p: "I'll never be called")
            ]
        })

    payload = {"value_from": 1}
    subject = subject_factory("test-1")

    subject.set("value_from", 2)

    event_router_factory().route("test-argprocessors-self", subject, payload)
    proc_events_rx_factory.subscribe(lambda x: x[
        rulename] == "test-with-self" and _assert(x[processing][0]["args"][
            0] == 1 and x[processing][0]["kwargs"]["arg2"] == 2 and isinstance(
                x[processing][0]["kwargs"]["arg3"], str)))

    assert payload["arg1"] == 1
    assert payload["arg2"] == 2
    assert inspect.isfunction(payload["arg3"])
def test_simple_callable():

    class SimpleSet(RuleFunctionBase):

        def execute(self, arg1, arg2, arg3, arg4, arg5):

            self.payload["arg1"] = arg1
            self.payload["arg2"] = arg2
            self.payload["arg3"] = arg3
            self.payload["arg4"] = arg4
            self.payload["arg5"] = arg5

    RuleFactory.create(
        "test-simple-callable",
        subscribe_to="test-argprocessors-callables",
        data={
            processing: [
                SimpleSet(lambda: 1, 2, arg3=lambda: 3, arg4=4, arg5=lambda p: "I'll never be called")
            ]
        }
    )

    payload = {}
    event_router_factory().route("test-argprocessors-callables", "test-0", payload)

    proc_events_rx_factory().subscribe(
        lambda x: x[rulename] == "test-simple-callable" and _assert(
            x[processing][0]["args"][0] == 1
            and x[processing][0]["kwargs"]["arg3"] == 3
            and x[processing][0]["kwargs"]["arg4"] == 4
            and isinstance(x[processing][0]["kwargs"]["arg5"], str)
        )
    )

    assert payload["arg1"] == 1
    assert payload["arg2"] == 2
    assert payload["arg3"] == 3
    assert payload["arg4"] == 4
    assert inspect.isfunction(payload["arg5"])
def test_with_payload_and_subject():
    class WithPayloadSet(RuleFunctionBase):
        def execute(self, arg1, arg2, arg3):

            self.payload["arg1"] = arg1
            self.payload["arg2"] = arg2
            self.payload["arg3"] = arg3

    RuleFactory.create(
        "test-with-payload-and-subject",
        subscribe_to="test-argprocessors-payload-and-subject",
        data={
            processing: [
                WithPayloadSet(lambda payload: payload["value_from"],
                               arg2=lambda subject: subject.get("value_from"),
                               arg3=lambda p: "I'll never be called")
            ]
        })

    _payload = {"value_from": 1}
    _subject = subject_factory("test-1")

    _subject.set("value_from", 2)

    event_router_factory().route("test-argprocessors-payload-and-subject",
                                 _subject, _payload)

    proc_events_rx_factory().subscribe(
        lambda x: x[rulename] == "test-with-payload-and-subject" and _assert(x[
            processing][0]["args"][0] == 1 and x[processing][0][
                "kwargs"]["arg2"] == 2 and hasattr(
                    x[processing][0]["kwargs"]["arg3"], "__call__")))

    assert _payload["arg1"] == 1
    assert _payload["arg2"] == 2
    assert inspect.isfunction(_payload["arg3"])