def named_expression_objects(self): named_expression_specs = deepcopy(self.named_expressions) named_expressions = {} spec_error = None while named_expression_specs: number_generated = 0 for name, expression in named_expression_specs.items(): try: named_expressions[name] = ExpressionFactory.from_spec( expression, FactoryContext(named_expressions=named_expressions, named_filters={})) number_generated += 1 del named_expression_specs[name] except BadSpecError as spec_error: # maybe a nested name resolution issue, try again on the next pass pass if number_generated == 0 and named_expression_specs: # we unsuccessfully generated anything on this pass and there are still unresolved # references. we have to fail. assert spec_error is not None raise spec_error return named_expressions
def test_regimen_property_confirmed_tb(self): person_case = { '_id': 'person-case-id', 'domain': 'enikshay-test', } episode_case = { '_id': 'episode-case-id', 'adherence_schedule_id': '', 'case_definition': '', 'date_of_diagnosis': '', 'domain': 'enikshay-test', 'enikshay_enabled': '', 'episode_type': 'confirmed_tb', 'patient_type_choice': 'new', 'treatment_regimen': 'test_regimen' } self.database.mock_docs = { 'episode-case-id': episode_case, 'person-case-id': person_case } column = self.get_column('regimen') self.assertEqual(column['datatype'], 'string') expression = ExpressionFactory.from_spec( column['expression'], context=FactoryContext(self.named_expressions, {}) ) with mock.patch.object(MostRecentEpisodeCaseFromPerson, '__call__', lambda *args: episode_case): self.assertEqual(expression(episode_case, EvaluationContext(episode_case, 0)), '') episode_case['treatment_initiated'] = 'yes_phi' with mock.patch.object(MostRecentEpisodeCaseFromPerson, '__call__', lambda *args: episode_case): self.assertEqual(expression(episode_case, EvaluationContext(episode_case, 0)), 'New')
def get_factory_context(self): return FactoryContext(self.named_expression_objects, self.named_filter_objects)
def named_filter_objects(self): return { name: FilterFactory.from_spec( filter, FactoryContext(self.named_expression_objects, {})) for name, filter in self.named_filters.items() }
def named_expression_objects(self): return { name: ExpressionFactory.from_spec(expression, FactoryContext.empty()) for name, expression in self.named_expressions.items() }
def parsed_expression(self): return ExpressionFactory.from_spec(self.configured_expression, FactoryContext.empty())
def parsed_filter(self): return FilterFactory.from_spec(self.configured_filter, FactoryContext.empty())
def named_filter_objects(self): return {name: FilterFactory.from_spec(filter, FactoryContext.empty()) for name, filter in self.named_filters.items()}
def named_expression_objects(self): return {name: ExpressionFactory.from_spec(expression, FactoryContext.empty()) for name, expression in self.named_expressions.items()}
def get_expressions_from_base_item_expression(self): return ExpressionFactory.from_spec( self.base_item_expression, context=FactoryContext(self.base_item_expression, {}) )
def test_caching(self): doc = { "indices": [ { "doc_type": "CommCareCaseIndex", "identifier": "parent", "referenced_type": "pregnancy", "referenced_id": "my_parent_id" }, { "doc_type": "CommCareCaseIndex", "identifier": "parent", "referenced_type": "pregnancy", "referenced_id": "my_parent_id2" } ] } named_expression = { "type": "property_name", "property_name": "referenced_id" } evaluation_context = EvaluationContext(doc) factory_context = FactoryContext({ 'test_named_expression': ExpressionFactory.from_spec(named_expression) }, evaluation_context) expression1 = ExpressionFactory.from_spec( { "type": "nested", "argument_expression": { "type": "array_index", "array_expression": { "type": "property_name", "property_name": "indices" }, "index_expression": { "type": "constant", "constant": 0 } }, "value_expression": { "type": "named", "name": "test_named_expression" } }, context=factory_context ) expression2 = ExpressionFactory.from_spec( { "type": "nested", "argument_expression": { "type": "array_index", "array_expression": { "type": "property_name", "property_name": "indices" }, "index_expression": { "type": "constant", "constant": 1 } }, "value_expression": { "type": "named", "name": "test_named_expression" } }, context=factory_context ) self.assertEqual(expression1(doc, evaluation_context), 'my_parent_id') self.assertEqual(expression2(doc, evaluation_context), 'my_parent_id2')
def test_not_microbiological_result(self): episode_case = { '_id': 'episode_case_id', 'domain': 'enikshay-test', 'treatment_initiation_date': '2017-09-28', 'archive_reason': None, 'treatment_outcome': 'test', 'indices': [ {'referenced_id': 'occurrence_case_id'} ] } occurrence_case = { '_id': 'occurrence_case_id', 'domain': 'enikshay-test', 'indices': [ {'referenced_id': 'person_case_id'} ] } person_case = { '_id': 'person_case_id', 'domain': 'enikshay-test', 'owner_id': 'owner-id' } subcases = [ { 'domain': 'enikshay-test', 'type': 'test', 'is_direct_test_entry': 'no', 'rft_dstb_followup': 'end_of_ip', 'test_requested_date': '2017-09-28', 'date_tested': '2017-08-10', 'date_reported': '2017-08-10', 'test_type_value': 'cytopathology', 'test_type_label': 'Cytopathology', 'result_grade': 'result_grade', 'result_recorded': 'yes', 'result_summary_display': 'result_cytopathology', 'result': 'result_cytopathology', }, { 'domain': 'enikshay-test', 'type': 'test', 'is_direct_test_entry': 'yes', 'rft_dstb_followup': 'end_of_cp', 'test_requested_date': '2017-09-28', 'date_tested': '2017-09-10', 'date_reported': '2017-09-10', 'test_type_value': 'igra', 'result': 'result', 'result_recorded': 'yes', 'result_summary_display': 'result_igra' }, { 'domain': 'enikshay-test', 'type': 'test', 'rft_dstb_followup': 'end_of_cp', 'test_requested_date': '2017-10-10', 'date_reported': '2017-10-10', 'test_type_value': 'other_clinical', 'test_type_label': 'Other clinical', 'result_summary_display': 'result_other_clinical' } ] self.database.mock_docs = { 'episode_case_id': episode_case, 'occurrence_case_id': occurrence_case, 'person_case_id': person_case } not_microbiological = self._get_column('not_microbiological_result') self.assertEqual(not_microbiological['datatype'], 'string') not_microbiological_result_expression = ExpressionFactory.from_spec( not_microbiological['expression'], context=FactoryContext(self.named_expressions, {}) ) with mock.patch.object(SubcasesExpressionSpec, '__call__', lambda *args: subcases): self.assertEqual( not_microbiological_result_expression(episode_case, EvaluationContext(episode_case, 0)), 'Cytopathology, result_cytopathology' )
def test_microbiological_endofip_test_requested_date(self): episode_case = { '_id': 'episode_case_id', 'domain': 'enikshay-test', 'treatment_initiation_date': '2017-09-28', 'archive_reason': None, 'treatment_outcome': 'test', 'indices': [ {'referenced_id': 'occurrence_case_id'} ] } occurrence_case = { '_id': 'occurrence_case_id', 'domain': 'enikshay-test', 'indices': [ {'referenced_id': 'person_case_id'} ] } person_case = { '_id': 'person_case_id', 'domain': 'enikshay-test', 'owner_id': 'owner-id' } subcases = [ { 'domain': 'enikshay-test', 'type': 'test', 'is_direct_test_entry': 'no', 'rft_dstb_followup': 'end_of_ip', 'test_requested_date': '2017-09-28', 'date_tested': '2017-08-10', 'test_type_value': 'microscopy-zn', 'result_recorded': 'yes' }, { 'domain': 'enikshay-test', 'type': 'test', 'is_direct_test_entry': 'yes', 'rft_dstb_followup': 'end_of_cp', 'test_requested_date': '2017-09-28', 'date_tested': '2017-08-10', 'test_type_value': 'microscopy-zn', 'result_recorded': 'yes' }, { 'domain': 'enikshay-test', 'type': 'test', 'rft_dstb_followup': 'end_of_cp', 'test_requested_date': '2017-09-28', 'test_type_value': 'microscopy-zn', } ] self.database.mock_docs = { 'episode_case_id': episode_case, 'occurrence_case_id': occurrence_case, 'person_case_id': person_case } endofip_test_requested_date = self._get_column('endofip_test_requested_date') self.assertEqual(endofip_test_requested_date['datatype'], 'date') endofip_test_requested_date_expression = ExpressionFactory.from_spec( endofip_test_requested_date['expression'], context=FactoryContext(self.named_expressions, {}) ) endofcp_test_requested_date = self._get_column('endofcp_test_requested_date') self.assertEqual(endofcp_test_requested_date['datatype'], 'date') endofcp_test_requested_date_expression = ExpressionFactory.from_spec( endofcp_test_requested_date['expression'], context=FactoryContext(self.named_expressions, {}) ) with mock.patch.object(SubcasesExpressionSpec, '__call__', lambda *args: subcases): self.assertEqual( endofip_test_requested_date_expression(episode_case, EvaluationContext(episode_case, 0)), '2017-09-28' ) self.assertEqual( endofcp_test_requested_date_expression(episode_case, EvaluationContext(episode_case, 0)), '2017-08-10' )