def test_some_object_methods(): p = Property({"handle":"complaint"}) assert p t = Term({"value":"halitosis"}) assert t u = Term({"value":"ptomaine"}) assert u vs = ValueSet({"_id":"1"}) assert vs p.value_set = vs vs.terms['ptomaine'] = u assert p.terms['ptomaine'].value == 'ptomaine' p.terms['halitosis'] = t assert vs.terms['halitosis'].value == 'halitosis' vals = p.values assert isinstance(vals,list) assert 'ptomaine' in vals assert 'halitosis' in vals s = Node({'handle':"case"}) assert s d = Node({'handle':"cohort"}) assert d e = Edge({'handle':"member_of",'src':s,'dst':d}) assert e assert e.triplet == ('member_of','case','cohort')
def test_tags_on_objects(): nodeTag = Tag({"key":"name","value":"Neddy"}) relnTag = Tag({"key":"name","value":"Robby"}) conceptTag = Tag({"key":"name","value":"Catty"}) conceptTag2 = Tag({"key":"aka","value":"Jehoshaphat"}) termTag = Tag({"key":"name","value":"Termy"}) propTag = Tag({"key":"name","value":"Puppy"}) case = Node({"model":"test","handle":"case"}) of_sample = Edge({"model":"test","handle":"of_sample"}) sample = Node({"model":"test","handle":"sample"}) of_sample.src = sample of_sample.dst = case term = Term({"value":"sample"}) concept = Concept(); term.concept = concept concept.terms["sample"]=term sample.concept = concept sample.props['this'] = Property({"that":"this"}) case.tags[nodeTag.key] = nodeTag of_sample.tags[relnTag.key] = relnTag term.tags[termTag.key] = termTag concept.tags[conceptTag.key] = conceptTag concept.tags[conceptTag2.key] = conceptTag2 sample.props['this'].tags['name'] = propTag names = [x.tags['name'].value for x in [case,of_sample,term,concept,sample.props['this']]];
def calc_value_domain(self, typedef): if isinstance(typedef, dict): if typedef.get("pattern"): return { "value_domain": "regexp", "pattern": typedef["pattern"] } elif typedef.get("units"): return { "value_domain": typedef.get("value_type"), "units": ";".join(typedef.get("units")), } else: # punt warn("MDF type descriptor unrecognized: json looks like {j}". format(j=json.dumps(typedef))) return {"value_domain": json.dumps(typedef)} elif isinstance(typedef, list): # a valueset: create value set and term objs vs = ValueSet({"_id": str(uuid4())}) vs.handle = self.handle + vs._id[0:8] if re.match("^(?:https?|bolt)://", typedef[0]): # looks like url vs.url = typedef[0] else: # an enum for t in typedef: vs.terms[t] = Term({"value": t}) return {"value_domain": "value_set", "value_set": vs} else: return {"value_domain": MDF.default_type}
def add_terms(self, prop, *terms): """Add a list of :class:`Term` and/or strings to a :class:`Property` with a value domain of ``value_set`` :param Property prop: :class:`Property` to modify :param list terms: A list of :class:`Term` instances and/or str :class:`Term` instances are created for strings; `Term.value` is set to the string. """ if not isinstance(prop, Property): raise ArgError("arg1 must be Property") if not re.match("value_set|enum", prop.value_domain): raise AttributeError( "Property value domain is not value_set or enum, can't add terms" ) if not prop.value_set: warn("Creating ValueSet object for Property " + prop.handle) prop.value_set = ValueSet({"prop": prop, "_id": str(uuid4())}) prop.value_set.handle = self.handle + prop.value_set._id[0:8] for t in terms: if isinstance(t, str): warn("Creating Term object for string '{term}'".format(term=t)) t = Term({"value": t}) elif not isinstance(t, Term): raise ArgError( "encountered arg that was not a str or Term object") prop.value_set.terms[t.value] = t
def test_valuesets_are_different__f(): '''test using sets as input''' p_1 = Property({"handle": "States"}) p_2 = Property({"handle": "Estados"}) vs_1 = ValueSet({"_id": "1"}) vs_2 = ValueSet({"_id": "2"}) term_a = Term({"value": "Merida"}) term_b = Term({"value": "Cumana"}) term_c = Term({"value": "Maracaibo"}) term_d = Term({"value": "Ciudad Bolivar"}) term_e = Term({"value": "Barcelona"}) term_f = Term({"value": "Barquisimeto"}) vs_1.terms['Merida'] = term_a vs_1.terms['Cumana'] = term_b vs_1.terms['Maracaibo'] = term_c vs_1.terms['Ciudad Bolivar'] = term_d vs_2.terms['Merida'] = term_a vs_2.terms['Cumana'] = term_b vs_2.terms['Maracaibo'] = term_c vs_2.terms['Ciudad Bolivar'] = term_d p_1.value_set = vs_1 p_2.value_set = vs_2 actual = diff.valuesets_are_different(p_1.value_set, p_2.value_set) expected = False assert actual == expected
def test_create_model(): model = Model('test') case = Node({"handle": "case"}) # set_trace() case.props['days_to_enrollment'] = Property( {"handle": 'days_to_enrollment'}) model.add_node(case) assert isinstance(model.nodes['case'], Node) assert model.props[('case', 'days_to_enrollment')] model.add_node({"handle": "sample"}) assert model.nodes["sample"] assert isinstance(model.nodes["sample"], Node) assert model.nodes["sample"].model == 'test' case_id = Property({"handle": "case_id", "value_domain": "string"}) model.add_prop(case, case_id) assert model.props[("case", "case_id")] assert model.props[("case", "case_id")].value_domain == 'string' assert 'case_id' in model.nodes['case'].props sample = model.nodes["sample"] of_case = Edge({"handle": "of_case", "src": sample, "dst": case}) of_case.props['operator'] = Property({ "handle": "operator", "value_domain": "boolean" }) model.add_edge(of_case) assert model.edges[('of_case', 'sample', 'case')] assert model.contains(of_case.props['operator']) assert of_case.props['operator'].model == 'test' assert model.props[('of_case', 'sample', 'case', 'operator')] assert model.props[('of_case', 'sample', 'case', 'operator')].value_domain == 'boolean' dx = Property({"handle": "diagnosis", "value_domain": "value_set"}) tm = Term({"value": "CRS"}) model.add_prop(case, dx) model.add_terms(dx, tm, 'rockin_pneumonia', 'fungusamongus') assert {x.value for x in dx.terms.values() } == {'CRS', 'rockin_pneumonia', 'fungusamongus'}
def test_init_and_link_objects(): case = Node({"model":"test","handle":"case"}) assert case assert case.model == "test" assert case.handle == "case" sample = Node({"model":"test","handle":"sample"}) assert sample of_sample = Edge({"model":"test","handle":"of_sample"}) assert of_sample assert of_sample.model == "test" assert of_sample.handle == "of_sample" of_sample.src = sample of_sample.dst = case assert of_sample.dst == case assert of_sample.src == sample term = Term({"value":"sample"}) concept = Concept(); term.concept = concept other_concept = Concept() concept.terms["sample"]=term sample.concept = concept [o] = [x for x in term.belongs.values()] assert o == concept assert of_sample.src.concept.terms["sample"].value == "sample" pred = Predicate({"subject":concept, "object":other_concept, "handle":"isa"}) assert type(pred.subject) == Concept assert type(pred.object) == Concept def test_tags_on_objects(): nodeTag = Tag({"key":"name","value":"Neddy"}) relnTag = Tag({"key":"name","value":"Robby"})
def test_put_queries(): m = ObjectMap(cls=Node) n = Node({"handle": "test", "model": "test_model", "_commit": 1}) qry = m.put_q(n) assert qry == [ 'CREATE (n:node {_commit:1,handle:"test",model:"test_model"}) RETURN n,id(n)' ] n.neoid = 2 stmts = m.put_q(n) assert stmts[ 0] == 'MATCH (n:node) WHERE id(n)=2 SET n._commit=1,n.handle="test",n.model="test_model" RETURN n,id(n)' assert len(stmts[1:]) == len( [x for x in Node.attspec if Node.attspec[x] == 'simple']) - 3 for s in stmts[1:]: assert re.match( '^MATCH \\(n:node\\) WHERE id\\(n\\)=2 REMOVE n.[a-z_]+ RETURN n,id\\(n\\)$', s) n.neoid = None with pytest.raises(ArgError, match='object must be mapped'): m.put_attr_q(n, '_commit', 2) n.neoid = 1 c = Concept({"_id": "blarf"}) with pytest.raises( ArgError, match="'values' must be a list of mapped Entity objects"): m.put_attr_q(n, 'concept', c) with pytest.raises( ArgError, match="'values' must be a list of mapped Entity objects"): m.put_attr_q(n, 'concept', [c]) qry = m.put_attr_q(n, '_commit', [3]) assert qry == "MATCH (n:node) WHERE id(n)=1 SET _commit=3 RETURN id(n)" c.neoid = 2 stmts = m.put_attr_q(n, 'concept', [c]) assert stmts[ 0] == "MATCH (n:node),(a:concept) WHERE id(n)=1 AND id(a)=2 MERGE (n)-[:has_concept]->(a) RETURN id(a)" assert len(stmts) == 1 prps = [ Property(x) for x in ({ "model": "test", "handle": "prop1" }, { "model": "test", "handle": "prop2" }, { "model": "test", "handle": "prop3" }) ] i = 5 for p in prps: p.neoid = i i += 1 stmts = m.put_attr_q(n, 'props', prps) assert stmts[ 0] == "MATCH (n:node),(a:property) WHERE id(n)=1 AND id(a)=5 MERGE (n)-[:has_property]->(a) RETURN id(a)" assert len(stmts) == 3 m = ObjectMap(cls=FakeNode) n = FakeNode({"handle": "test", "model": "test_model", "category": 1}) n.neoid = 1 t = Term({"value": "boog"}) t.neoid = 6 stmts = m.put_attr_q(n, "concept", [t]) assert re.match( "MATCH \\(n:node\\),\\(a\\) WHERE id\\(n\\)=1 AND id\\(a\\)=6 AND \\('[a-z]+' IN labels\\(a\\) OR '[a-z]+' IN labels\\(a\\)\\) MERGE \\(n\\)-\\[:has_concept\\]->\\(a\\) RETURN id\\(a\\)", stmts[0]) qry = m.get_attr_q(n, "concept") assert re.match( "MATCH \\(n:node\\)-\\[:has_concept\\]->\\(a\\) WHERE id\\(n\\)=1 AND \\('[a-z]+' IN labels\\(a\\) OR '[a-z]+' IN labels\\(a\\)\\) RETURN a", qry) pass
def test_put_then_rm_queries(): """test adding then removing attr""" m = ObjectMap(cls=Node) n = Node({"handle": "test_", "model": "test_model_", "_commit": 1}) qry = m.put_q(n) assert qry == [ 'CREATE (n:node {_commit:1,handle:"test_",model:"test_model_"}) RETURN n,id(n)' ] # manually set neoid n.neoid = 2 stmts = m.put_q(n) assert stmts[ 0] == 'MATCH (n:node) WHERE id(n)=2 SET n._commit=1,n.handle="test_",n.model="test_model_" RETURN n,id(n)' assert len(stmts[1:]) == len( [x for x in Node.attspec if Node.attspec[x] == 'simple']) - 3 for s in stmts[1:]: assert re.match( '^MATCH \\(n:node\\) WHERE id\\(n\\)=2 REMOVE n.[a-z_]+ RETURN n,id\\(n\\)$', s) n.neoid = None with pytest.raises(ArgError, match='object must be mapped'): m.put_attr_q(n, '_commit', 2) n.neoid = 1 c = Concept({"_id": "blarfblark"}) with pytest.raises( ArgError, match="'values' must be a list of mapped Entity objects"): m.put_attr_q(n, 'concept', c) with pytest.raises( ArgError, match="'values' must be a list of mapped Entity objects"): m.put_attr_q(n, 'concept', [c]) qry = m.put_attr_q(n, '_commit', [3]) assert qry == "MATCH (n:node) WHERE id(n)=1 SET _commit=3 RETURN id(n)" c.neoid = 2 stmts = m.put_attr_q(n, 'concept', [c]) #assert stmts[0] == "MATCH (n:node),(a:concept) WHERE id(n)=1 AND id(a)=2 MERGE (n)-[:has_concept]->(a) RETURN id(a)" assert len(stmts) == 1 prps = [ Property(x) for x in ({ "model": "test_", "handle": "prop1" }, { "model": "test_", "handle": "prop2" }, { "model": "test_", "handle": "prop3" }) ] i = 5 for p in prps: p.neoid = i i += 1 stmts = m.put_attr_q(n, 'props', prps) assert stmts[ 0] == "MATCH (n:node),(a:property) WHERE id(n)=1 AND id(a)=5 MERGE (n)-[:has_property]->(a) RETURN id(a)" assert len(stmts) == 3 Node.mapspec_ = { "label": "node", "property": { "handle": "handle", "model": "model" }, "relationship": { "concept": { "rel": ":has_concept>", "end_cls": {"Concept", "Term"} }, "props": { "rel": ":has_property>", "end_cls": "Property" } } } (Node.attspec, Node._mapspec) = mergespec('Node', Node.attspec, Node.mapspec_) assert Node.mapspec()["relationship"]["concept"]["end_cls"] == { "Concept", "Term" } t = Term({"value": "boogblark"}) t.neoid = 6 stmts = m.put_attr_q(n, "concept", [t]) assert re.match( "MATCH \\(n:node\\),\\(a\\) WHERE id\\(n\\)=1 AND id\\(a\\)=6 AND \\('[a-z]+' IN labels\\(a\\) OR '[a-z]+' IN labels\\(a\\)\\) MERGE \\(n\\)-\\[:has_concept\\]->\\(a\\) RETURN id\\(a\\)", stmts[0]) qry = m.get_attr_q(n, "concept") assert re.match( "MATCH \\(n:node\\)-\\[:has_concept\\]->\\(a\\) WHERE id\\(n\\)=1 AND \\('[a-z]+' IN labels\\(a\\) OR '[a-z]+' IN labels\\(a\\)\\) RETURN a", qry) # now delete the attr I just added.... qry2 = m.rm_attr_q(n, "concept", [t]) assert re.match( "MATCH \\(n:node\\)-\\[r:has_concept\\]->\\(a\\) WHERE id\\(n\\)=1 AND id\\(a\\)=6 AND \\('[a-z]+' IN labels\\(a\\) OR '[a-z]+' IN labels\\(a\\)\\) DELETE r RETURN id\\(n\\),id\\(a\\)", qry2[0])
sys.path.extend(['.', '..']) import pytest #from pdb import set_trace from bento_meta.mdf import MDF from bento_meta.diff import Diff #from bento_meta.entity import ArgError from bento_meta.model import Model from bento_meta.objects import Node, Property, Edge, Term, ValueSet, Concept, Origin # compare sets of terms # a_att.terms # {'FFPE': <bento_meta.objects.Term object at 0x10..>, 'Snap Frozen': <bento_meta.objects.Term object at 0x10..>} # set(a_att.terms) # {'Snap Frozen', 'FFPE'} diff = Diff() term_a = Term({"value": "Merida"}) term_b = Term({"value": "Cumana"}) term_c = Term({"value": "Maracaibo"}) term_d = Term({"value": "Ciudad Bolivar"}) term_e = Term({"value": "Barcelona"}) term_f = Term({"value": "Barquisimeto"}) def test_valuesets_are_different__a(): '''test using sets as input''' vs_1 = ValueSet({"_id": "1"}) vs_2 = ValueSet({"_id": "2"}) vs_1.terms['Merida'] = term_a vs_1.terms['Cumana'] = term_b