def get_expression(self, column_id, column_type): column = self.get_column(column_id) if column['type'] == 'boolean': return FilterFactory.from_spec(column['filter'], context=FactoryContext( self.named_expressions, {})) else: self.assertEqual(column['datatype'], column_type) return ExpressionFactory.from_spec(column['expression'], context=FactoryContext( self.named_expressions, {}))
def test_diff_days_expression(self, source_doc, to_date_expression, expected_value): from_date_expression = { 'type': 'property_name', 'property_name': 'dob', } expression = ExpressionFactory.from_spec({ 'type': 'diff_days', 'from_date_expression': from_date_expression, 'to_date_expression': to_date_expression }) self.assertEqual(expected_value, expression(source_doc)) # test named_expression for 'from_date_expression' context = FactoryContext( {"from_date_name": ExpressionFactory.from_spec(from_date_expression)}, {}) named_expression = ExpressionFactory.from_spec( { 'type': 'diff_days', 'from_date_expression': { "type": "named", "name": "from_date_name" }, 'to_date_expression': to_date_expression }, context) self.assertEqual(expected_value, named_expression(source_doc))
def test_regimen_property_private_treatment(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': 'not_new', 'treatment_regimen': 'test_regimen', 'treatment_status': 'yes_private', 'treatment_initiated': 'yes_private' } 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)), 'Outisde RNTCP')
def _get_expression(self, column_id, column_type): column = self._get_column(column_id) self.assertEqual(column['datatype'], column_type) return ExpressionFactory.from_spec( column['expression'], context=FactoryContext(self.named_expressions, {}) )
def test_follow_up_treatment_initiation_date_month_follow_up_drtb(self): episode_case = { '_id': 'episode_case_id', 'domain': 'enikshay-test', 'treatment_initiation_date': '2017-07-01', 'archive_reason': None, 'treatment_outcome': None, } test_case = { '_id': 'test_case_id', 'domain': 'enikshay-test', 'episode_case_id': 'episode_case_id', 'rft_general': 'follow_up_drtb', 'date_reported': '2017-10-01', 'rft_drtb_follow_up_treatment_month': '6' } self.database.mock_docs = { 'episode_case_id': episode_case, 'test_case_id': test_case } column = self._get_column('follow_up_treatment_initiation_date_month') self.assertEqual(column['datatype'], 'string') expression = ExpressionFactory.from_spec(column['expression'], context=FactoryContext( self.named_expressions, {})) self.assertEqual( expression(test_case, EvaluationContext(test_case, 0)), '3')
def setUp(self): self.filter = FilterFactory.from_spec( {'type': 'named', 'name': 'foo'}, FactoryContext({}, { 'foo': FilterFactory.from_spec({ "type": "not", "filter": { "type": "property_match", "property_name": "foo", "property_value": "bar" } }) }) ) self.assertTrue(isinstance(self.filter, NOTFilter))
def test_name_in_value(self): expression = ExpressionFactory.from_spec( { "type": "nested", "argument_expression": { "type": "identity", }, "value_expression": { "type": "named", "name": "three" } }, context=FactoryContext({'three': ExpressionFactory.from_spec(3)}, {})) self.assertEqual(3, expression({}))
def test_key_populations(self): episode_case = { '_id': 'episode_case_id', 'domain': 'enikshay-test', 'treatment_initiation_date': '2017-07-01', 'archive_reason': None, 'treatment_outcome': None, 'indices': [{ 'referenced_id': 'occurrence_case_id' }] } occurrence_case = { '_id': 'occurrence_case_id', 'domain': 'enikshay-test', 'indices': [{ 'referenced_id': 'person_case_id' }], 'key_populations': 'test test2 test3' } test_case = { '_id': 'test_case_id', 'domain': 'enikshay-test', 'episode_case_id': 'episode_case_id', 'rft_general': 'follow_up_dstb', 'date_reported': '2017-10-01', 'indices': [{ 'referenced_id': 'occurrence_case_id' }] } self.database.mock_docs = { 'episode_case_id': episode_case, 'test_case_id': test_case, 'occurrence_case_id': occurrence_case } column = self._get_column('key_populations_from_occurrence') self.assertEqual(column['datatype'], 'string') expression = ExpressionFactory.from_spec(column['expression'], context=FactoryContext( self.named_expressions, {})) self.assertEqual( expression(test_case, EvaluationContext(test_case, 0)), 'test, test2, test3')
def test_disease_classification_extra_pulmonary_site_choice_other(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', 'disease_classification': 'extra_pulmonary', 'site_choice': 'other', 'site_detail': 'test detail', 'indices': [ {'referenced_id': 'person_case_id'} ] } person_case = { '_id': 'person_case_id', 'domain': 'enikshay-test', 'owner_id': 'owner-id' } self.database.mock_docs = { 'episode_case_id': episode_case, 'occurrence_case_id': occurrence_case, 'person_case_id': person_case } column = self._get_column('disease_classification') self.assertEqual(column['datatype'], 'string') expression = ExpressionFactory.from_spec( column['expression'], context=FactoryContext(self.named_expressions, {}) ) self.assertEqual( expression(episode_case, EvaluationContext(episode_case, 0)), 'Extra Pulmonary, Other, test detail' )
def test_tu_name_property_when_clause_treatment_initiation_date_is_null( self): episode_case = { '_id': 'episode_case_id', 'domain': 'enikshay-test', 'treatment_initiation_date': None, 'archive_reason': None, 'treatment_outcome': None, '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', 'tu_name': 'test' } self.database.mock_docs = { 'episode_case_id': episode_case, 'occurrence_case_id': occurrence_case, 'person_case_id': person_case } column = self._get_column('treating_phi') self.assertEqual(column['datatype'], 'string') expression = ExpressionFactory.from_spec(column['expression'], context=FactoryContext( self.named_expressions, {})) self.assertIsNone( expression(episode_case, EvaluationContext(episode_case, 0)))
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 list(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 bad_spec_error: # maybe a nested name resolution issue, try again on the next pass spec_error = bad_spec_error 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 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 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' )
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 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')