Refactored Concept class for better separation of metadata, compiled and values
This commit is contained in:
+92
-12
@@ -1,9 +1,18 @@
|
||||
import pytest
|
||||
|
||||
from core.concept import Concept
|
||||
from core.concept import Concept, ConceptParts
|
||||
|
||||
|
||||
@pytest.mark.parametrize("name, variables, expected", [
|
||||
@pytest.mark.parametrize("name, properties, 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"),
|
||||
@@ -11,17 +20,17 @@ from core.concept import Concept
|
||||
("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 test_i_can_get_concept_key(name, variables, expected):
|
||||
def test_i_can_compute_the_key(name, properties, expected):
|
||||
concept = Concept(name)
|
||||
for v in variables:
|
||||
concept.set_prop(v, None)
|
||||
|
||||
for prop in properties:
|
||||
concept.metadata.props.append((prop, None))
|
||||
concept.init_key()
|
||||
assert concept.metadata.key == expected
|
||||
|
||||
assert concept.key == expected
|
||||
|
||||
|
||||
def test_key_does_not_use_variable_when_definition_is_set():
|
||||
concept = Concept("plus").set_prop('plus')
|
||||
concept = Concept("plus").def_prop('plus')
|
||||
|
||||
concept.init_key()
|
||||
assert concept.metadata.key == "plus"
|
||||
@@ -45,7 +54,7 @@ def test_i_can_serialize():
|
||||
definition_type="def type",
|
||||
desc="this this the desc",
|
||||
id="123456"
|
||||
).set_prop("a", 10).set_prop("b", None)
|
||||
).def_prop("a", "10").def_prop("b", None)
|
||||
|
||||
to_dict = concept.to_dict()
|
||||
assert to_dict == {
|
||||
@@ -60,7 +69,7 @@ def test_i_can_serialize():
|
||||
'name': 'concept_name',
|
||||
'post': 'definition of the post',
|
||||
'pre': 'definition of the pre',
|
||||
'props': [('a', 10), ('b', None)],
|
||||
'props': [('a', "10"), ('b', None)],
|
||||
'where': 'definition of the where'
|
||||
}
|
||||
|
||||
@@ -83,7 +92,7 @@ def test_i_can_deserialize():
|
||||
'name': 'concept_name',
|
||||
'post': 'definition of the post',
|
||||
'pre': 'definition of the pre',
|
||||
'props': [('a', 10), ('b', None)],
|
||||
'props': [('a', "10"), ('b', None)],
|
||||
'where': 'definition of the where'
|
||||
}
|
||||
|
||||
@@ -102,7 +111,50 @@ def test_i_can_deserialize():
|
||||
definition_type="def type",
|
||||
desc="this this the desc",
|
||||
id="123456"
|
||||
).set_prop("a", 10).set_prop("b", None)
|
||||
).def_prop("a", "10").def_prop("b", None)
|
||||
|
||||
|
||||
def test_i_can_deserialize_props_coming_from_sdp():
|
||||
from_dict = {
|
||||
'props': [['a', "10"], ['b', None]], # JSON transform set into list
|
||||
}
|
||||
|
||||
concept = Concept().from_dict(from_dict)
|
||||
assert concept == Concept().def_prop("a", "10").def_prop("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_prop("a", "10").def_prop("b", None)
|
||||
|
||||
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_prop("a", "10").def_prop("b", None)
|
||||
|
||||
assert concept_a == concept_b
|
||||
|
||||
|
||||
def test_i_can_compare_concept_with_circular_reference():
|
||||
@@ -128,3 +180,31 @@ def test_i_can_compare_concept_with_sophisticated_circular_reference_in_other_me
|
||||
foo.metadata.pre = baz
|
||||
|
||||
assert foo != bar
|
||||
|
||||
|
||||
def test_i_can_update_from():
|
||||
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_prop("a", "10").def_prop("b", None)
|
||||
|
||||
template.values[ConceptParts.BODY] = "value in body"
|
||||
template.values[ConceptParts.WHERE] = "value in where"
|
||||
template.values[ConceptParts.PRE] = "value in pre"
|
||||
template.values[ConceptParts.POST] = "value in post"
|
||||
template.set_prop("a", 10)
|
||||
template.set_prop("b", 20)
|
||||
|
||||
concept = Concept().update_from(template)
|
||||
|
||||
assert concept == template
|
||||
|
||||
Reference in New Issue
Block a user