def test_double_underscore_raise_on_declare(): ke = KnowledgeEngine() ke.reset() ke.declare(Fact(__startwithdoubleunderscore__=True)) with pytest.raises(KeyError): ke.declare(Fact(key__with__double__underscores=True))
def test_fact_setitem_do_raise_after_declare(): f = Fact() ke = KnowledgeEngine() ke.reset() ke.declare(f) with pytest.raises(RuntimeError): f[0] = 1
def test_KnowledgeEngine_declare_define_fact(): from experta.engine import KnowledgeEngine from experta import Fact from unittest.mock import patch ke = KnowledgeEngine() with patch('experta.factlist.FactList') as mock: ke.facts = mock ke.declare(Fact()) assert mock.declare.called
def test_duplicate_declare(): from experta import KnowledgeEngine, Fact ke = KnowledgeEngine() f1 = ke.declare(Fact(color='red')) assert len(ke.facts) == 1 f2 = ke.duplicate(f1, color='yellow', blink=True) assert len(ke.facts) == 2 assert f2 is not f1 assert f2['color'] == 'yellow' assert f2['blink']
def test_modify_retracts_and_declare(): from experta import KnowledgeEngine, Fact ke = KnowledgeEngine() f1 = ke.declare(Fact()) assert len(ke.facts) == 1 f2 = ke.modify(f1, _0='test_pos', key='test_key') assert f1 != f2 assert len(ke.facts) == 1 assert f2.__factid__ in ke.facts assert f2[0] == 'test_pos' assert f2['key'] == 'test_key'
def test_declare_raises_typeerror_if_conditionalelement_found(): from experta import KnowledgeEngine, L, W, P, Fact ke = KnowledgeEngine() with pytest.raises(TypeError): ke.declare(Fact(L(1))) with pytest.raises(TypeError): ke.declare(Fact(W())) with pytest.raises(TypeError): ke.declare(Fact(P(lambda _: True))) with pytest.raises(TypeError): ke.declare(Fact(~L(1))) with pytest.raises(TypeError): ke.declare(Fact(L(1) | L(2))) with pytest.raises(TypeError): ke.declare(Fact(L(1) & L(2)))