105 lines
4.3 KiB
Python
105 lines
4.3 KiB
Python
import pytest
|
|
from core.builtin_concepts import BuiltinConcepts
|
|
from core.concept import Concept, ConceptParts
|
|
from core.sheerka.Sheerka import Sheerka
|
|
|
|
from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
|
|
|
|
|
|
class TestSheerkaModifyConcept(TestUsingMemoryBasedSheerka):
|
|
def test_i_can_modify_a_concept(self):
|
|
sheerka, context, foo, bar = self.init_concepts("foo", "bar", create_new=True)
|
|
|
|
foo_instance = sheerka.new("foo")
|
|
foo_instance.metadata.body = "value"
|
|
foo_instance.set_prop(BuiltinConcepts.ISA, bar)
|
|
foo_instance.set_metadata_value(ConceptParts.BODY, "body value")
|
|
res = sheerka.modify_concept(context, foo_instance)
|
|
|
|
assert res.status
|
|
assert sheerka.isinstance(res.body, BuiltinConcepts.NEW_CONCEPT)
|
|
assert res.body.body.metadata.body == "value"
|
|
assert res.body.body.get_prop(BuiltinConcepts.ISA) == bar
|
|
assert res.body.body.body == "body value"
|
|
|
|
# test that object
|
|
sheerka.reset_cache()
|
|
foo_from_sheerka = sheerka.new("foo")
|
|
assert foo_from_sheerka.metadata.body == "value"
|
|
assert foo_from_sheerka.get_prop(BuiltinConcepts.ISA) == bar
|
|
assert foo_from_sheerka.body == "body value"
|
|
|
|
# test that ref by id is updated
|
|
sheerka.reset_cache()
|
|
foo_from_sheerka = sheerka.get_by_id(foo.id)
|
|
assert foo_from_sheerka.metadata.body == "value"
|
|
assert foo_from_sheerka.get_prop(BuiltinConcepts.ISA) == bar
|
|
assert foo_from_sheerka.body == "body value"
|
|
|
|
# test that ref by hash is updated
|
|
foo_from_sdp = sheerka.sdp.get(Sheerka.CONCEPTS_BY_HASH_ENTRY, foo_instance.get_definition_hash())
|
|
assert foo_from_sdp.metadata.body == "value"
|
|
assert foo_from_sdp.get_prop(BuiltinConcepts.ISA) == bar
|
|
assert foo_from_sdp.body == "body value"
|
|
|
|
# previous ref by hash is removed (since that definition hash has changed)
|
|
with pytest.raises(IndexError):
|
|
sheerka.sdp.get(Sheerka.CONCEPTS_BY_HASH_ENTRY, foo_instance.get_original_definition_hash())
|
|
|
|
def test_i_can_modify_concept_modifying_only_properties_and_body(self):
|
|
sheerka, context, foo, bar = self.init_concepts("foo", "bar", create_new=True)
|
|
|
|
foo_instance = sheerka.new("foo")
|
|
foo_instance.set_prop(BuiltinConcepts.ISA, bar)
|
|
foo_instance.set_metadata_value(ConceptParts.BODY, "body value")
|
|
res = sheerka.modify_concept(context, foo_instance)
|
|
|
|
assert res.status
|
|
|
|
foo_from_sheerka = sheerka.new("foo")
|
|
assert foo_from_sheerka.get_prop(BuiltinConcepts.ISA) == bar
|
|
assert foo_from_sheerka.body == "body value"
|
|
|
|
def test_cache_is_updated_when_a_concept_is_modified(self):
|
|
sheerka, context, foo = self.init_concepts("foo", create_new=True)
|
|
|
|
foo_instance = sheerka.new("foo")
|
|
foo_instance.metadata.body = "value"
|
|
res = sheerka.modify_concept(context, foo_instance)
|
|
assert res.status
|
|
|
|
foo_from_sheerka = sheerka.get("foo")
|
|
assert foo_from_sheerka.metadata.body == "value"
|
|
|
|
foo_by_id_from_sheerka = sheerka.get_by_id(foo.id)
|
|
assert foo_by_id_from_sheerka.metadata.body == "value"
|
|
|
|
def test_i_cannot_modify_a_concept_that_does_not_exists(self):
|
|
sheerka, context, foo = self.init_concepts("foo", create_new=False)
|
|
|
|
foo_instance = sheerka.new("foo")
|
|
foo_instance.metadata.body = "value"
|
|
res = sheerka.modify_concept(context, foo_instance)
|
|
|
|
assert not res.status
|
|
assert sheerka.isinstance(res.body, BuiltinConcepts.UNKNOWN_CONCEPT)
|
|
assert res.body.body.key == foo.key
|
|
|
|
def test_i_can_modify_a_concept_that_is_in_a_list(self):
|
|
sheerka, context, foo1, foo2 = self.init_concepts(
|
|
Concept("foo", body="1"),
|
|
Concept("foo", body="2"), create_new=True)
|
|
|
|
foo2_instance = sheerka.new("foo")[1]
|
|
foo2_instance.metadata.body = "value"
|
|
|
|
res = sheerka.modify_concept(context, foo2_instance)
|
|
assert res.status
|
|
assert sheerka.isinstance(res.body, BuiltinConcepts.NEW_CONCEPT)
|
|
assert res.body.body.metadata.body == "value"
|
|
|
|
sheerka.reset_cache()
|
|
foo_from_sheerka = sheerka.new("foo")
|
|
assert foo_from_sheerka[0].metadata.body == "1"
|
|
assert foo_from_sheerka[1].metadata.body == "value"
|