import os import shutil from os import path import pytest from core.builtin_concepts import ConceptAlreadyInSet, BuiltinConcepts from core.concept import Concept from tests.TestUsingFileBasedSheerka import TestUsingFileBasedSheerka class TestSheerkaSetsManager(TestUsingFileBasedSheerka): def test_i_can_add_concept_to_set(self): sheerka = self.get_sheerka(False, False) foo = Concept("foo") sheerka.set_id_if_needed(foo, False) all_foos = Concept("all_foos") sheerka.set_id_if_needed(all_foos, False) context = self.get_context(sheerka) 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, False).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): sheerka = self.get_sheerka(False, False) foo1 = Concept("foo1") sheerka.set_id_if_needed(foo1, False) foo2 = Concept("foo1") sheerka.set_id_if_needed(foo2, False) all_foos = Concept("all_foos") sheerka.set_id_if_needed(all_foos, False) context = self.get_context(sheerka) sheerka.add_concept_to_set(context, foo1, all_foos) res = sheerka.add_concept_to_set(context, 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 def test_i_cannot_add_the_same_concept_twice_in_a_set(self): sheerka = self.get_sheerka() foo = Concept("foo") sheerka.set_id_if_needed(foo, False) all_foos = Concept("all_foos") sheerka.set_id_if_needed(all_foos, False) context = self.get_context(sheerka) 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): sheerka = self.get_sheerka() one = Concept("one") two = Concept("two") three = Concept("three") number = Concept("number") for c in [one, two, three, number]: sheerka.set_id_if_needed(c, False) sheerka.add_in_cache(c) context = self.get_context(sheerka) 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): sheerka = self.get_sheerka() one = Concept("one") sheerka.set_id_if_needed(one, False) sheerka.add_in_cache(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): sheerka = self.get_sheerka() group = Concept("group").init_key() group.metadata.id = "1001" assert not sheerka.isaset(group) foo = Concept("foo").init_key() foo.metadata.id = "1002" 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)