Added SheerkaComparisonManager
This commit is contained in:
@@ -0,0 +1,149 @@
|
||||
import pytest
|
||||
from core.builtin_concepts import BuiltinConcepts
|
||||
from core.sheerka.services.SheerkaComparisonManager import SheerkaComparisonManager, ComparisonObj
|
||||
|
||||
from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
|
||||
|
||||
|
||||
class TestSheerkaGreaterThanManager(TestUsingMemoryBasedSheerka):
|
||||
def test_i_can_add_a_is_greater_than(self):
|
||||
sheerka, context, one, two = self.init_concepts("one", "two", cache_only=False)
|
||||
service = sheerka.services[SheerkaComparisonManager.NAME]
|
||||
|
||||
res = service.is_greater_than(context, "prop_name", two, one)
|
||||
assert res.status
|
||||
assert sheerka.isinstance(res.body, BuiltinConcepts.SUCCESS)
|
||||
|
||||
in_cache = sheerka.cache_manager.get(SheerkaComparisonManager.COMPARISON_ENTRY, "prop_name|#")
|
||||
assert in_cache == [ComparisonObj(context.event.get_digest(), "prop_name", two.id, one.id, ">", "#")]
|
||||
|
||||
weighted = sheerka.cache_manager.get(SheerkaComparisonManager.RESOLVED_COMPARISON_ENTRY, "prop_name|#")
|
||||
assert weighted == {"1001": 1, "1002": 2}
|
||||
|
||||
# I can commit
|
||||
sheerka.cache_manager.commit(context)
|
||||
in_db = sheerka.sdp.get(SheerkaComparisonManager.COMPARISON_ENTRY, "prop_name|#")
|
||||
assert in_db == [ComparisonObj(context.event.get_digest(), "prop_name", two.id, one.id, ">", "#")]
|
||||
|
||||
def test_i_can_add_a_is_less_than(self):
|
||||
sheerka, context, one, two = self.init_concepts("one", "two", cache_only=False)
|
||||
service = sheerka.services[SheerkaComparisonManager.NAME]
|
||||
|
||||
res = service.is_less_than(context, "prop_name", one, two)
|
||||
assert res.status
|
||||
assert sheerka.isinstance(res.body, BuiltinConcepts.SUCCESS)
|
||||
|
||||
in_cache = sheerka.cache_manager.get(SheerkaComparisonManager.COMPARISON_ENTRY, "prop_name|#")
|
||||
assert in_cache == [ComparisonObj(context.event.get_digest(), "prop_name", one.id, two.id, "<", "#")]
|
||||
|
||||
weighted = sheerka.cache_manager.get(SheerkaComparisonManager.RESOLVED_COMPARISON_ENTRY, "prop_name|#")
|
||||
assert weighted == {"1001": 1, "1002": 2}
|
||||
|
||||
# I can commit
|
||||
sheerka.cache_manager.commit(context)
|
||||
in_db = sheerka.sdp.get(SheerkaComparisonManager.COMPARISON_ENTRY, "prop_name|#")
|
||||
assert in_db == [ComparisonObj(context.event.get_digest(), "prop_name", one.id, two.id, "<", "#")]
|
||||
|
||||
def test_i_can_add_multiples_constraints(self):
|
||||
sheerka, context, one, two, three, four = self.init_concepts("one", "two", "three", "four", cache_only=False)
|
||||
service = sheerka.services[SheerkaComparisonManager.NAME]
|
||||
|
||||
service.is_greater_than(context, "prop_name", two, one)
|
||||
service.is_greater_than(context, "prop_name", three, two)
|
||||
|
||||
in_cache = sheerka.cache_manager.get(SheerkaComparisonManager.COMPARISON_ENTRY, "prop_name|#")
|
||||
assert in_cache == [
|
||||
ComparisonObj(context.event.get_digest(), "prop_name", two.id, one.id, ">", "#"),
|
||||
ComparisonObj(context.event.get_digest(), "prop_name", three.id, two.id, ">", "#")
|
||||
]
|
||||
|
||||
# I can commit
|
||||
sheerka.cache_manager.commit(context)
|
||||
in_db = sheerka.sdp.get(SheerkaComparisonManager.COMPARISON_ENTRY, "prop_name|#")
|
||||
assert in_db == [
|
||||
ComparisonObj(context.event.get_digest(), "prop_name", two.id, one.id, ">", "#"),
|
||||
ComparisonObj(context.event.get_digest(), "prop_name", three.id, two.id, ">", "#")
|
||||
]
|
||||
|
||||
sheerka.cache_manager.clear(SheerkaComparisonManager.COMPARISON_ENTRY) # reset the cache
|
||||
|
||||
service.is_greater_than(context, "prop_name", four, three)
|
||||
in_cache = sheerka.cache_manager.get(SheerkaComparisonManager.COMPARISON_ENTRY, "prop_name|#")
|
||||
assert in_cache == [
|
||||
ComparisonObj(context.event.get_digest(), "prop_name", two.id, one.id, ">", "#"),
|
||||
ComparisonObj(context.event.get_digest(), "prop_name", three.id, two.id, ">", "#"),
|
||||
ComparisonObj(context.event.get_digest(), "prop_name", four.id, three.id, ">", "#"),
|
||||
]
|
||||
|
||||
weighted = sheerka.cache_manager.get(SheerkaComparisonManager.RESOLVED_COMPARISON_ENTRY, "prop_name|#")
|
||||
assert weighted == {"1001": 1, "1002": 2, "1003": 3, "1004": 4}
|
||||
|
||||
@pytest.mark.parametrize("entries, expected", [
|
||||
(["two > one"], {'1001': 1, '1002': 2}),
|
||||
(["one < two"], {'1001': 1, '1002': 2}),
|
||||
(["three > two", "one < two"], {'1001': 1, '1002': 2, '1003': 3}),
|
||||
(["three > one", "one < two"], {'1001': 1, '1002': 2, '1003': 2}),
|
||||
])
|
||||
def test_i_can_get_concept_weight(self, entries, expected):
|
||||
sheerka, context, *concepts = self.init_concepts("one", "two", "three")
|
||||
service = sheerka.services[SheerkaComparisonManager.NAME]
|
||||
concepts_map = dict(zip(["one", "two", "three", "four", "five", "six"], concepts))
|
||||
|
||||
for entry in entries:
|
||||
if ">" in entry:
|
||||
a, b = [concepts_map[e.strip()] for e in entry.split(">")]
|
||||
service.is_greater_than(context, "prop_name", a, b)
|
||||
else:
|
||||
a, b = [concepts_map[e.strip()] for e in entry.split("<")]
|
||||
service.is_less_than(context, "prop_name", a, b)
|
||||
|
||||
assert service.get_concepts_weights("prop_name") == expected
|
||||
|
||||
def test_i_can_get_partition(self):
|
||||
sheerka, context, one, two, three = self.init_concepts("one", "two", "three")
|
||||
service = sheerka.services[SheerkaComparisonManager.NAME]
|
||||
|
||||
service.is_greater_than(context, "prop_name", two, one)
|
||||
service.is_less_than(context, "prop_name", two, three)
|
||||
|
||||
res = service.get_partition("prop_name")
|
||||
|
||||
assert res == {
|
||||
1: ["1001"],
|
||||
2: ["1002"],
|
||||
3: ["1003"],
|
||||
}
|
||||
|
||||
def test_i_can_detect_chicken_and_egg(self):
|
||||
sheerka, context, one, two = self.init_concepts("one", "two")
|
||||
service = sheerka.services[SheerkaComparisonManager.NAME]
|
||||
|
||||
service.is_greater_than(context, "prop_name", two, one)
|
||||
res = service.is_greater_than(context, "prop_name", one, two)
|
||||
|
||||
assert not res.status
|
||||
assert sheerka.isinstance(res.body, BuiltinConcepts.CHICKEN_AND_EGG)
|
||||
assert set(res.body.body) == {one, two}
|
||||
|
||||
def test_i_can_detect_more_complex_chicken_and_egg(self):
|
||||
sheerka, context, one, two, three, four, five = self.init_concepts("one", "two", "three", "four", "five")
|
||||
service = sheerka.services[SheerkaComparisonManager.NAME]
|
||||
|
||||
service.is_greater_than(context, "prop_name", two, one)
|
||||
service.is_greater_than(context, "prop_name", five, four)
|
||||
service.is_greater_than(context, "prop_name", four, three)
|
||||
service.is_greater_than(context, "prop_name", five, two)
|
||||
|
||||
res = service.is_greater_than(context, "prop_name", two, one)
|
||||
assert res.status
|
||||
|
||||
res = service.is_greater_than(context, "prop_name", one, five)
|
||||
|
||||
assert not res.status
|
||||
assert sheerka.isinstance(res.body, BuiltinConcepts.CHICKEN_AND_EGG)
|
||||
assert set(res.body.body) == {one, two, five}
|
||||
|
||||
def test_methods_are_correctly_bound(self):
|
||||
sheerka, context, one, two = self.init_concepts("one", "two")
|
||||
res = sheerka.is_greater_than(context, "prop_name", two, one)
|
||||
assert res.status
|
||||
Reference in New Issue
Block a user