120 lines
4.0 KiB
Python
120 lines
4.0 KiB
Python
from core.builtin_concepts import ConceptAlreadyInSet, BuiltinConcepts
|
|
from core.concept import Concept
|
|
|
|
from tests.TestUsingFileBasedSheerka import TestUsingFileBasedSheerka
|
|
|
|
|
|
class TestSheerkaSetsManager(TestUsingFileBasedSheerka):
|
|
|
|
def init(self, use_dict, *concepts):
|
|
sheerka = self.get_sheerka(use_dict, True)
|
|
for c in concepts:
|
|
sheerka.set_id_if_needed(c, False)
|
|
sheerka.add_in_cache(c)
|
|
|
|
context = self.get_context(sheerka)
|
|
return sheerka, context
|
|
|
|
def test_i_can_add_concept_to_set(self):
|
|
foo = Concept("foo")
|
|
all_foos = Concept("all_foos")
|
|
sheerka, context = self.init(False, foo, all_foos)
|
|
|
|
res = sheerka.add_concept_to_set(context, foo, all_foos)
|
|
|
|
assert res.status
|
|
assert sheerka.isinstance(res.body, BuiltinConcepts.SUCCESS)
|
|
|
|
all_entries = self.get_sheerka(False, True).sdp.get("All_" + all_foos.id, None, False)
|
|
assert len(all_entries) == 1
|
|
assert foo.id in all_entries
|
|
|
|
def test_i_can_add_several_concepts_to_set(self):
|
|
foo1 = Concept("foo1")
|
|
foo2 = Concept("foo2")
|
|
all_foos = Concept("all_foos")
|
|
sheerka, context = self.init(False, foo1, foo2, all_foos)
|
|
|
|
res = sheerka.sets_handler.add_concepts_to_set(context, (foo1, foo2), all_foos)
|
|
|
|
assert res.status
|
|
assert sheerka.isinstance(res.body, BuiltinConcepts.SUCCESS)
|
|
|
|
all_entries = self.get_sheerka(False, False).sdp.get("All_" + all_foos.id, None, False)
|
|
assert len(all_entries) == 2
|
|
assert foo1.id in all_entries
|
|
assert foo2.id in all_entries
|
|
|
|
# I can add another elements
|
|
foo3 = Concept("foo3")
|
|
foo4 = Concept("foo4")
|
|
for c in [foo3, foo4]:
|
|
sheerka.set_id_if_needed(c, False)
|
|
sheerka.add_in_cache(c)
|
|
|
|
res = sheerka.sets_handler.add_concepts_to_set(context, (foo3, foo4), all_foos)
|
|
|
|
assert res.status
|
|
assert sheerka.isinstance(res.body, BuiltinConcepts.SUCCESS)
|
|
|
|
all_entries = self.get_sheerka(False, False).sdp.get("All_" + all_foos.id, None, False)
|
|
assert len(all_entries) == 4
|
|
assert foo1.id in all_entries
|
|
assert foo2.id in all_entries
|
|
assert foo3.id in all_entries
|
|
assert foo4.id in all_entries
|
|
|
|
def test_i_cannot_add_the_same_concept_twice_in_a_set(self):
|
|
foo = Concept("foo")
|
|
all_foos = Concept("all_foos")
|
|
sheerka, context = self.init(True, foo, all_foos)
|
|
|
|
sheerka.add_concept_to_set(context, foo, all_foos)
|
|
res = sheerka.add_concept_to_set(context, foo, all_foos)
|
|
|
|
assert not res.status
|
|
assert res.body == ConceptAlreadyInSet(foo, all_foos)
|
|
|
|
all_entries = sheerka.sdp.get("All_" + all_foos.id, None, False)
|
|
assert len(all_entries) == 1
|
|
assert foo.id in all_entries
|
|
|
|
def test_i_get_elements_from_a_set(self):
|
|
one = Concept("one")
|
|
two = Concept("two")
|
|
three = Concept("three")
|
|
number = Concept("number")
|
|
sheerka, context = self.init(True, one, two, three, number)
|
|
|
|
for c in [one, two, three]:
|
|
sheerka.add_concept_to_set(context, c, number)
|
|
|
|
elements = sheerka.get_set_elements(number)
|
|
|
|
assert set(elements) == {one, two, three}
|
|
|
|
def test_i_cannot_get_elements_if_not_a_set(self):
|
|
one = Concept("one")
|
|
sheerka, context = self.init(True, one)
|
|
|
|
error = sheerka.get_set_elements(one)
|
|
|
|
assert sheerka.isinstance(error, BuiltinConcepts.NOT_A_SET)
|
|
assert error.body == one
|
|
|
|
def test_isa_and_isa_group(self):
|
|
group = Concept("group")
|
|
foo = Concept("foo")
|
|
sheerka, context = self.init(True, group, foo)
|
|
|
|
assert not sheerka.isaset(group)
|
|
assert not sheerka.isa(foo, group)
|
|
|
|
context = self.get_context(sheerka)
|
|
sheerka.add_concept_to_set(context, foo, group)
|
|
assert sheerka.isaset(group)
|
|
assert sheerka.isa(foo, group)
|
|
|
|
def test_i_can_a_multiples_concepts(self):
|
|
pass
|