2022-11-15 20:27:34 +00:00
|
|
|
import pytest
|
|
|
|
|
2022-11-16 15:56:36 +00:00
|
|
|
import nucypher
|
|
|
|
from nucypher.blockchain.eth.constants import NULL_ADDRESS
|
|
|
|
from nucypher.policy.conditions.context import USER_ADDRESS_CONTEXT
|
2022-11-18 18:42:25 +00:00
|
|
|
from nucypher.policy.conditions.exceptions import InvalidConditionLingo
|
2022-09-22 11:42:48 +00:00
|
|
|
from nucypher.policy.conditions.lingo import ConditionLingo
|
2023-06-07 20:36:07 +00:00
|
|
|
from tests.constants import TESTERCHAIN_CHAIN_ID
|
2022-09-22 11:42:48 +00:00
|
|
|
|
2022-11-17 11:20:53 +00:00
|
|
|
|
|
|
|
@pytest.fixture(scope='module')
|
|
|
|
def lingo():
|
|
|
|
return [
|
2023-06-07 20:36:07 +00:00
|
|
|
{
|
|
|
|
"returnValueTest": {"value": 0, "comparator": ">"},
|
2023-06-08 14:11:30 +00:00
|
|
|
"method": "blocktime",
|
2023-06-07 20:36:07 +00:00
|
|
|
"chain": TESTERCHAIN_CHAIN_ID,
|
|
|
|
},
|
2022-10-03 15:52:06 +00:00
|
|
|
{"operator": "and"},
|
2023-06-07 20:36:07 +00:00
|
|
|
{
|
|
|
|
"returnValueTest": {"value": 99999999999999999, "comparator": "<"},
|
2023-06-08 14:11:30 +00:00
|
|
|
"method": "blocktime",
|
2023-06-07 20:36:07 +00:00
|
|
|
"chain": TESTERCHAIN_CHAIN_ID,
|
|
|
|
},
|
2022-09-22 11:42:48 +00:00
|
|
|
]
|
|
|
|
|
2022-11-10 14:16:36 +00:00
|
|
|
|
2022-11-15 20:27:34 +00:00
|
|
|
def test_invalid_condition():
|
2022-11-18 18:42:25 +00:00
|
|
|
with pytest.raises(InvalidConditionLingo):
|
2022-11-15 20:27:34 +00:00
|
|
|
ConditionLingo.from_list([{}])
|
|
|
|
|
2022-11-18 18:42:25 +00:00
|
|
|
with pytest.raises(InvalidConditionLingo):
|
2022-11-15 20:27:34 +00:00
|
|
|
ConditionLingo.from_list([{"dont_mind_me": "nothing_to_see_here"}])
|
|
|
|
|
2022-11-18 19:52:49 +00:00
|
|
|
# operator in incorrect spot
|
|
|
|
invalid_operator_position_lingo = [
|
|
|
|
{"operator": "and"},
|
2023-06-07 20:36:07 +00:00
|
|
|
{
|
|
|
|
"returnValueTest": {"value": 0, "comparator": ">"},
|
2023-06-08 14:11:30 +00:00
|
|
|
"method": "blocktime",
|
2023-06-07 20:36:07 +00:00
|
|
|
"chain": TESTERCHAIN_CHAIN_ID,
|
|
|
|
},
|
2022-11-18 19:52:49 +00:00
|
|
|
]
|
|
|
|
with pytest.raises(InvalidConditionLingo):
|
|
|
|
ConditionLingo.from_list(invalid_operator_position_lingo)
|
|
|
|
|
2022-11-15 20:27:34 +00:00
|
|
|
|
2022-11-17 11:20:53 +00:00
|
|
|
def test_condition_lingo_to_from_list(lingo):
|
|
|
|
clingo = ConditionLingo.from_list(lingo)
|
2022-11-16 01:57:08 +00:00
|
|
|
clingo_list = clingo.to_list()
|
2022-11-17 11:20:53 +00:00
|
|
|
assert clingo_list == lingo
|
2022-11-10 14:16:36 +00:00
|
|
|
|
|
|
|
|
2022-11-17 11:20:53 +00:00
|
|
|
def test_condition_lingo_repr(lingo):
|
|
|
|
clingo = ConditionLingo.from_list(lingo)
|
2022-11-10 14:16:36 +00:00
|
|
|
clingo_string = f"{clingo}"
|
|
|
|
assert f"{clingo.__class__.__name__}" in clingo_string
|
|
|
|
assert f"id={clingo.id}" in clingo_string
|
|
|
|
assert f"size={len(bytes(clingo))}" in clingo_string
|
2022-11-16 15:56:36 +00:00
|
|
|
|
|
|
|
|
2022-11-17 15:27:24 +00:00
|
|
|
def test_lingo_parameter_int_type_preservation(custom_abi_with_multiple_parameters, mocker):
|
2022-11-16 15:56:36 +00:00
|
|
|
mocker.patch.dict(
|
|
|
|
nucypher.policy.conditions.context._DIRECTIVES,
|
|
|
|
{USER_ADDRESS_CONTEXT: lambda: NULL_ADDRESS},
|
|
|
|
)
|
2022-11-17 11:20:53 +00:00
|
|
|
clingo = ConditionLingo.from_list([custom_abi_with_multiple_parameters])
|
2022-11-16 15:56:36 +00:00
|
|
|
conditions = clingo.to_list()
|
|
|
|
assert conditions[0]["parameters"][2] == 4
|