@@ -62,15 +62,24 @@ def test_identifier_normalization_and_cache():
6262 assert ConceptHierarchy ._normalize_identifier ("1+2+2" ) == "1+2"
6363
6464 # fake minimal results to build hierarchy
65- results = [
65+ results1 = [
6666 {"ancestor_concept_id" : 1 , "descendant_concept_id" : 1 ,
6767 "concept_name" : "Diabetes" , "concept_code" : "DIA" ,
6868 "count_in_cohort" : 5 , "prevalence" : 0.5 }
6969 ]
70- h1 = ConceptHierarchy .build_concept_hierarchy_from_results (1 , results )
71- h2 = ConceptHierarchy .build_concept_hierarchy_from_results (1 , results )
72- assert h1 is h2 # cache reuse
73- assert h1 .identifier == "1"
70+ results2 = [
71+ {"ancestor_concept_id" : 1 , "descendant_concept_id" : 1 ,
72+ "concept_name" : "Diabetes2" , "concept_code" : "DIA" ,
73+ "count_in_cohort" : 15 , "prevalence" : 0.15 }
74+ ]
75+ h1 = ConceptHierarchy .build_concept_hierarchy_from_results (1 , 'condition_occurrence' , results1 )
76+ h2 = ConceptHierarchy .build_concept_hierarchy_from_results (1 , 'condition_occurrence' , results2 )
77+ assert h1 is h2 # cache reuse even though results2 is different from results1
78+ assert h1 .identifier == "1-condition_occurrence-0-None"
79+ h2 = ConceptHierarchy .build_concept_hierarchy_from_results (1 , 'drug_exposure' , results2 )
80+ assert not h1 is h2 # cache is not used since drug_exposure concept_name is different than the cached
81+ # condition_occurrence
82+ assert h2 .identifier == "1-drug_exposure-0-None"
7483
7584def test_union_and_cache_behavior ():
7685 ConceptHierarchy .clear_cache ()
@@ -85,14 +94,14 @@ def test_union_and_cache_behavior():
8594 "count_in_cohort" : 3 , "prevalence" : 0.3 }
8695 ]
8796
88- h1 = ConceptHierarchy .build_concept_hierarchy_from_results (1 , results1 )
89- h2 = ConceptHierarchy .build_concept_hierarchy_from_results (2 , results2 )
90- assert "1" in ConceptHierarchy ._graph_cache
91- assert "2" in ConceptHierarchy ._graph_cache
97+ h1 = ConceptHierarchy .build_concept_hierarchy_from_results (1 , 'condition_occurrence' , results1 )
98+ h2 = ConceptHierarchy .build_concept_hierarchy_from_results (2 , 'condition_occurrence' , results2 )
99+ assert "1-condition_occurrence-0-None " in ConceptHierarchy ._graph_cache
100+ assert "2-condition_occurrence-0-None " in ConceptHierarchy ._graph_cache
92101 h12 = h1 .union (h2 )
93102 h21 = h2 .union (h1 )
94- assert h12 .identifier == "1+2 "
95- assert h21 .identifier == "1+2 "
103+ assert h12 .identifier == "1-condition_occurrence-0-None+2-condition_occurrence-0-None "
104+ assert h21 .identifier == "1-condition_occurrence-0-None+2-condition_occurrence-0-None "
96105 assert h12 is h21
97106
98107def test_traversal_and_serialization ():
@@ -105,7 +114,7 @@ def test_traversal_and_serialization():
105114 "concept_name" : "Child" , "concept_code" : "C" ,
106115 "count_in_cohort" : 2 , "prevalence" : 0.2 }
107116 ]
108- h = ConceptHierarchy .build_concept_hierarchy_from_results (1 , results )
117+ h = ConceptHierarchy .build_concept_hierarchy_from_results (1 , 'condition_occurrence' , results )
109118
110119 # roots
111120 roots = h .get_root_nodes ()
0 commit comments