def test_expression_when_episode_does_not_exist(self): context = EvaluationContext({"domain": self.domain}) expression = ExpressionFactory.from_spec({ "type": "enikshay_episode_from_person", "person_id_expression": "some_random_id", }) self.assertEqual(expression({}, context), None)
def indexed_case_expression(spec, context): wrapped = IndexedCaseExpressionSpec.wrap(spec) wrapped.configure( ExpressionFactory.from_spec(wrapped.case_expression, context), context ) return wrapped
def _build_choice_list_indicator(spec, context): wrapped_spec = ChoiceListIndicatorSpec.wrap(spec) base_display_name = wrapped_spec.display_name def _construct_display(choice): return '{base} ({choice})'.format(base=base_display_name, choice=choice) def _construct_column(choice): return '{col}_{choice}'.format(col=spec['column_id'], choice=choice) choice_indicators = [ BooleanIndicator( display_name=_construct_display(choice), column_id=_construct_column(choice), filter=SinglePropertyValueFilter( expression=wrapped_spec.getter, operator=wrapped_spec.get_operator(), reference_expression=ExpressionFactory.from_spec(choice), ), wrapped_spec=None, ) for choice in spec['choices'] ] return CompoundIndicator(base_display_name, choice_indicators, wrapped_spec)
def test_expression_when_episode_exists(self): context = EvaluationContext({"domain": self.domain}) expression = ExpressionFactory.from_spec({ "type": "enikshay_episode_from_person", "person_id_expression": self.person_id, }) self.assertEqual(expression({}, context), self.cases[self.episode_id].to_json())
def test_expression_when_referral_pass_filters(self): referral_1_case_id = uuid.uuid4().hex referral = self.create_referral_case(referral_1_case_id)[0] context = EvaluationContext({"domain": self.domain}) expression = ExpressionFactory.from_spec({ "type": "enikshay_most_recent_referral_from_person", "person_id_expression": self.person_id, }) self.assertEqual(expression({}, context), referral.to_json())
def referral_test_expression(spec, context): """ Factory function for ReferralTestExpression """ wrapped = ReferralTestExpression.wrap(spec) wrapped.configure( ExpressionFactory.from_spec(wrapped.person_id_expression, context) ) return wrapped
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_expression_when_referral_does_not_pass_filters(self): referral_2_case_id = uuid.uuid4().hex self.create_referral_case(referral_2_case_id) self.update_referral(referral_2_case_id, { "referral_closed_reason": "duplicate_referral_reconciliation", }) context = EvaluationContext({"domain": self.domain}) expression = ExpressionFactory.from_spec({ "type": "enikshay_most_recent_referral_from_person", "person_id_expression": self.person_id, }) self.assertEqual(expression({}, context), None)
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 configure(self, case_expression, context): self._case_expression = case_expression index = self.index or 'parent' spec = { 'type': 'related_doc', 'related_doc_type': 'CommCareCase', 'doc_id_expression': { 'type': 'nested', 'argument_expression': self.case_expression, 'value_expression': { 'type': 'nested', 'argument_expression': { 'type': 'array_index', 'array_expression': { 'type': 'filter_items', 'items_expression': { 'datatype': 'array', 'type': 'property_name', 'property_name': 'indices' }, 'filter_expression': { 'type': 'boolean_expression', 'operator': 'eq', 'property_value': index, 'expression': { 'type': 'property_name', 'property_name': 'identifier' } } }, 'index_expression': { 'type': 'constant', 'constant': 0 } }, 'value_expression': { 'type': 'property_name', 'property_name': 'referenced_id' } } }, 'value_expression': { 'type': 'identity' } } self._expression = ExpressionFactory.from_spec(spec, context)
def _build_choice_list_indicator(spec, context): wrapped_spec = ChoiceListIndicatorSpec.wrap(spec) base_display_name = wrapped_spec.display_name def _construct_display(choice): return '{base} ({choice})'.format(base=base_display_name, choice=choice) def _construct_column(choice): return '{col}_{choice}'.format(col=spec['column_id'], choice=choice) choice_indicators = [ BooleanIndicator( display_name=_construct_display(choice), column_id=_construct_column(choice), filter=SinglePropertyValueFilter( expression=wrapped_spec.getter, operator=wrapped_spec.get_operator(), reference_expression=ExpressionFactory.from_spec(choice), ) ) for choice in spec['choices'] ] return CompoundIndicator(base_display_name, choice_indicators)
def evaluate_expression(request, domain): doc_type = request.POST['doc_type'] doc_id = request.POST['doc_id'] try: usable_type = { 'form': 'XFormInstance', 'case': 'CommCareCase', }.get(doc_type, 'Unknown') document_store = get_document_store(domain, usable_type) doc = document_store.get_document(doc_id) expression_text = request.POST['expression'] expression_json = json.loads(expression_text) parsed_expression = ExpressionFactory.from_spec(expression_json) result = parsed_expression(doc, EvaluationContext(doc)) return json_response({ "result": result, }) except DocumentNotFoundError: return json_response( { "error": _("{} with id {} not found in domain {}.").format( doc_type, doc_id, domain) }, status_code=404, ) except BadSpecError as e: return json_response( {"error": _("Problem with expression: {}.").format(e)}, status_code=400, ) except Exception as e: return json_response( {"error": unicode(e)}, status_code=500, )
def wrapped_expression(self): return ExpressionFactory.from_spec(self.expression)
def get_expressions_from_base_item_expression(self): return ExpressionFactory.from_spec( self.base_item_expression, context=FactoryContext(self.base_item_expression, {}) )