Files

338 lines
11 KiB
Python

import pytest
from core.builtin_concepts import BuiltinConcepts
from core.concept import Concept, ConceptParts, DEFINITION_TYPE_DEF, ALL_ATTRIBUTES, get_concept_attrs, \
freeze_concept_attrs
from core.global_symbols import NotInit
@pytest.mark.parametrize("name, variables, expected", [
("foo", [], "foo"),
("foo(bar)", [], "foo ( bar )"),
("foo a", ["a"], "foo __var__0"),
("a foo b", ["a", "b"], "__var__0 foo __var__1"),
("a foo b", ["b", "a"], "__var__1 foo __var__0"),
("foo", ["foo"], "foo"),
("foo a", ["foo"], "__var__0 a"),
("foo a b", ["a"], "foo __var__0 b"),
("'foo'", [], "'foo'"),
("my name is a", ["a"], "my name is __var__0"),
("a b c d", ["b", "c"], "a __var__0 __var__1 d"),
("a 'b c' d", ["b", "c"], "a 'b c' d"),
("a | b", ["a", "b"], "__var__0 | __var__1"),
("a b a c", ["a", "b"], "__var__0 __var__1 __var__0 c"),
("a b a c", ["b", "a"], "__var__1 __var__0 __var__1 c"),
("def concept a", ["a"], "def concept __var__0"),
])
def test_i_can_compute_the_key(name, variables, expected):
concept = Concept(name)
for var_name in variables:
concept.get_metadata().variables.append((var_name, None))
concept.init_key()
assert concept.key == expected
def test_i_can_compute_the_key_when_from_definition():
# if definition is not defined, use the name
concept = Concept()
concept.get_metadata().name = "hello a"
concept.get_metadata().variables = [("a", None)]
concept.init_key()
assert concept.key == "hello __var__0"
# if definition is defined, use it
concept = Concept()
concept.get_metadata().name = "greetings"
concept.get_metadata().definition = "hello a"
concept.get_metadata().definition_type = DEFINITION_TYPE_DEF
concept.get_metadata().variables = [("a", None)]
concept.init_key()
assert concept.key == "hello __var__0"
def test_key_does_not_use_variable_when_definition_is_set():
concept = Concept("plus").def_var('plus')
concept.init_key()
assert concept.get_metadata().key == "plus"
def test_i_can_serialize():
"""
Test concept.to_dict()
:return:
"""
concept = Concept(
name="concept_name a",
is_builtin=True,
is_unique=True,
key=None,
body="definition of the body",
where="definition of the where",
pre="definition of the pre",
post="definition of the post",
ret="concept to return",
definition="bnf definition",
definition_type="def type",
desc="this this the desc",
id="123456"
).def_var("a", "10").def_var("b", None)
concept.get_metadata().parameters = ["a"]
concept.init_key()
to_dict = concept.to_dict()
assert to_dict == {
'body': 'definition of the body',
'definition': 'bnf definition',
'definition_type': 'def type',
'desc': 'this this the desc',
'id': '123456',
'is_builtin': True,
'is_unique': True,
'key': 'concept_name __var__0',
'name': 'concept_name a',
'post': 'definition of the post',
'pre': 'definition of the pre',
'ret': "concept to return",
'props': {},
'variables': [('a', "10"), ('b', None)],
'where': 'definition of the where',
'parameters': ['a'],
}
def test_i_can_deserialize():
"""
Test concept.from_dict()
:return:
"""
from_dict = {
'body': 'definition of the body',
'definition': 'bnf definition',
'definition_type': 'def type',
'desc': 'this this the desc',
'id': '123456',
'is_builtin': True,
'is_unique': True,
'key': 'concept_key',
'name': 'concept_name',
'post': 'definition of the post',
'pre': 'definition of the pre',
'props': {},
'variables': [('a', "10"), ('b', None)],
'where': 'definition of the where'
}
concept = Concept().from_dict(from_dict)
freeze_concept_attrs(concept)
assert concept == Concept(
name="concept_name",
is_builtin=True,
is_unique=True,
key="concept_key",
body="definition of the body",
where="definition of the where",
pre="definition of the pre",
post="definition of the post",
definition="bnf definition",
definition_type="def type",
desc="this this the desc",
id="123456"
).def_var("a", "10").def_var("b", None)
def test_i_can_deserialize_variables_coming_from_sdp():
from_dict = {
'variables': [['a', "10"], ['b', None]], # JSON transform set into list
}
concept = Concept().from_dict(from_dict)
assert concept == Concept().def_var("a", "10").def_var("b", None)
def test_i_can_compare_concepts():
concept_a = Concept(
name="concept_name",
is_builtin=True,
is_unique=True,
key="concept_key",
body="definition of the body",
where="definition of the where",
pre="definition of the pre",
post="definition of the post",
definition="bnf definition",
definition_type="def type",
desc="this this the desc",
id="123456"
).def_var("a", "10").def_var("b", None).add_prop("prop", "prop_val")
concept_b = Concept(
name="concept_name",
is_builtin=True,
is_unique=True,
key="concept_key",
body="definition of the body",
where="definition of the where",
pre="definition of the pre",
post="definition of the post",
definition="bnf definition",
definition_type="def type",
desc="this this the desc",
id="123456"
).def_var("a", "10").def_var("b", None).add_prop("prop", "prop_val")
assert concept_a == concept_b
def test_i_can_detect_concept_differences():
ALL_ATTRIBUTES.clear()
assert Concept(name="concept_name") != Concept()
assert Concept(is_builtin=True) != Concept()
assert Concept(is_unique=True) != Concept()
assert Concept(key="concept_key") != Concept()
assert Concept(body="concept_body") != Concept()
assert Concept(where="concept_where") != Concept()
assert Concept(pre="concept_pre") != Concept()
assert Concept(post="concept_post") != Concept()
assert Concept(definition="def") != Concept()
assert Concept(definition_type="def type") != Concept()
assert Concept(desc="desc") != Concept()
assert Concept(id="concept_id") != Concept()
assert Concept().def_var("a") != Concept()
assert Concept().add_prop("a", "b") != Concept()
assert Concept().set_value("a", "b") != Concept()
def test_compiled_is_not_used_when_comparing_concepts():
ALL_ATTRIBUTES.clear()
concept = Concept()
concept.get_compiled()["foo"] = "value"
assert concept == Concept() # compiled is not used in the comparison
def test_i_can_compare_concept_with_circular_reference():
foo = Concept("foo")
foo.get_metadata().body = foo
assert foo == foo
def test_i_can_compare_concept_with_sophisticated_circular_reference():
foo = Concept("foo")
bar = Concept("foo", body=foo)
baz = Concept("foo", body=bar)
foo.get_metadata().body = baz
assert foo != bar
def test_i_can_compare_concept_with_sophisticated_circular_reference_in_other_metadata():
foo = Concept("foo")
bar = Concept("foo", pre=foo)
baz = Concept("foo", pre=bar)
foo.get_metadata().pre = baz
assert foo != bar
def test_i_can_update_from():
ALL_ATTRIBUTES.clear()
template = Concept(
name="concept_name",
is_builtin=True,
is_unique=True,
key="concept_key",
body="definition of the body",
where="definition of the where",
pre="definition of the pre",
post="definition of the post",
definition="bnf definition",
definition_type="def type",
desc="this this the desc",
id="123456"
).def_var("a", "10").def_var("b", None)
template.add_prop(BuiltinConcepts.ISA, Concept("foo").def_var("x", "value_x"))
template.add_prop(BuiltinConcepts.HASA, Concept("bar").def_var("x", "value_x"))
template.set_prop(BuiltinConcepts.AUTO_EVAL, True)
# make sure origin is preserved
setattr(template, "##origin##", "digest")
template.set_value(ConceptParts.BODY, "value in body")
template.set_value(ConceptParts.WHERE, "value in where")
template.set_value(ConceptParts.PRE, "value in pre")
template.set_value(ConceptParts.POST, "value in post")
template.set_value("a", 10)
template.set_value("b", 20)
concept = Concept().update_from(template)
assert concept == template
assert getattr(concept, "##origin##") == "digest"
def test_i_can_manage_concepts_attributes():
ALL_ATTRIBUTES.clear()
concept = Concept("foo")
assert get_concept_attrs(concept) == []
assert concept.values() == {}
concept.set_value(ConceptParts.BODY, "I have a body!")
assert concept.values() == {"#body#": "I have a body!"}
def test_i_can_manage_instance_attributes():
ALL_ATTRIBUTES.clear()
foo = Concept("foo", id="foo_id").def_var("x")
assert foo.values() == {"x": NotInit}
assert foo.get_all_attributes() is None
assert ALL_ATTRIBUTES == {"foo_id": ["x"]}
foo.set_value("x", "value for x")
assert foo.values() == {"x": "value for x"}
assert foo.get_all_attributes() is None
assert ALL_ATTRIBUTES == {"foo_id": ["x"]}
foo.set_value("y", "value for y")
assert foo.values() == {"x": "value for x", "y": "value for y"}
assert foo.get_all_attributes() == ["x", "y"]
assert ALL_ATTRIBUTES == {"foo_id": ["x"]}
def test_i_can_init_key_and_compute_parameters():
concept = Concept("foo x").def_var("x").init_key()
assert concept.key == "foo __var__0"
concept = Concept("foo").def_var("x").init_key()
assert concept.key == "foo"
concept = Concept("foo a b").def_var("a").def_var("b").init_key()
assert concept.key == "foo __var__0 __var__1"
concept = Concept("foo a b").def_var("b").def_var("a").init_key()
assert concept.key == "foo __var__1 __var__0"
concept = Concept("foo a b").def_var("a").init_key()
assert concept.key == "foo __var__0 b"
concept = Concept("foo a b").def_var("b").init_key()
assert concept.key == "foo a __var__0"
concept = Concept("foo a").def_var("a").def_var("b").init_key()
assert concept.key == "foo __var__0"
concept = Concept("foo b").def_var("a").def_var("b").init_key()
assert concept.key == "foo __var__1"
concept = Concept("plus", definition_type=DEFINITION_TYPE_DEF, definition="a plus b").def_var("a").def_var("b")
concept.init_key()
assert concept.key == "__var__0 plus __var__1"