Esempio n. 1
0
 def testEmptyName(self):
     with self.assertRaises(BadSpecError):
         FilterFactory.from_spec({
             'type': 'property_match',
             'property_name': '',
             'property_value': 'bar',
         })
Esempio n. 2
0
 def test_empty_path(self):
     with self.assertRaises(BadSpecError):
         FilterFactory.from_spec({
             'type': 'property_match',
             'property_path': [],
             'property_value': 'bar',
         })
Esempio n. 3
0
 def test_invalid_slug(self):
     with self.assertRaises(BadSpecError):
         FilterFactory.from_spec({
             'type': 'property_match',
             'property_name': 'foo',
             'property_value': 'bar',
             'slug': 'this-is-bad',
         })
Esempio n. 4
0
 def test_literal_in_expression(self):
     filter_with_literal = FilterFactory.from_spec({
         'type': 'boolean_expression',
         'expression': 1,
         'operator': 'gt',
         'property_value': 2
     })
     self.assertFalse(filter_with_literal({}))
     filter_with_literal = FilterFactory.from_spec({
         'type': 'boolean_expression',
         'expression': 2,
         'operator': 'gt',
         'property_value': 1
     })
     self.assertTrue(filter_with_literal({}))
Esempio n. 5
0
 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))
Esempio n. 6
0
    def _get_filter(self, doc_types):
        if not doc_types:
            return None

        extras = (
            [self.configured_filter]
            if self.configured_filter else []
        )
        built_in_filters = [
            self._get_domain_filter_spec(),
            {
                'type': 'or',
                'filters': [
                    {
                        'type': 'property_match',
                        'property_name': 'doc_type',
                        'property_value': doc_type,
                    }
                    for doc_type in doc_types
                ],
            },
        ]
        return FilterFactory.from_spec(
            {
                'type': 'and',
                'filters': built_in_filters + extras,
            },
            context=self.named_filter_objects,
        )
Esempio n. 7
0
    def _get_filter(self, doc_types, include_configured=True):
        if not doc_types:
            return None

        extras = (
            [self.configured_filter]
            if include_configured and self.configured_filter else []
        )
        built_in_filters = [
            self._get_domain_filter_spec(),
            {
                'type': 'or',
                'filters': [
                    {
                        "type": "boolean_expression",
                        "expression": {
                            "type": "property_name",
                            "property_name": "doc_type",
                        },
                        "operator": "eq",
                        "property_value": doc_type,
                    }
                    for doc_type in doc_types
                ],
            },
        ]
        return FilterFactory.from_spec(
            {
                'type': 'and',
                'filters': built_in_filters + extras,
            },
            context=self.get_factory_context(),
        )
Esempio n. 8
0
def _build_boolean_indicator(spec, context):
    wrapped = BooleanIndicatorSpec.wrap(spec)
    return BooleanIndicator(
        wrapped.display_name,
        wrapped.column_id,
        FilterFactory.from_spec(wrapped.filter, context),
    )
Esempio n. 9
0
 def _validations(self):
     return [
         _Validation(
             validation.name,
             validation.error_message,
             FilterFactory.from_spec(validation.expression, context=self.get_factory_context())
         )
         for validation in self.validations
     ]
Esempio n. 10
0
def _conditional_expression(spec):
    # no way around this since the two factories inherently depend on each other
    from corehq.apps.userreports.filters.factory import FilterFactory
    wrapped = ConditionalExpressionSpec.wrap(spec)
    return ConditionalExpression(
        FilterFactory.from_spec(wrapped.test),
        ExpressionFactory.from_spec(wrapped.expression_if_true),
        ExpressionFactory.from_spec(wrapped.expression_if_false),
    )
Esempio n. 11
0
 def get_path_filter(self):
     return FilterFactory.from_spec({
         'type': 'boolean_expression',
         'expression': {
             'type': 'property_path',
             'property_path': ['path', 'to', 'foo'],
         },
         'operator': 'eq',
         'property_value': 'bar',
     })
Esempio n. 12
0
 def test_null_value(self):
     null_filter = FilterFactory.from_spec({
         'type': 'property_match',
         'property_name': 'foo',
         'property_value': None,
     })
     self.assertEqual(True, null_filter({'foo': None}))
     self.assertEqual(True, null_filter({}))
     self.assertEqual(False, null_filter({'foo': 'exists'}))
     self.assertEqual(False, null_filter({'foo': ''}))
Esempio n. 13
0
 def get_filter(self, operator, value):
     return FilterFactory.from_spec({
         'type': 'boolean_expression',
         'expression': {
             'type': 'property_name',
             'property_name': 'foo',
         },
         'operator': operator,
         'property_value': value,
     })
Esempio n. 14
0
 def setUp(self):
     self.filter = FilterFactory.from_spec({
         "type": "not",
         "filter": {
             "type": "property_match",
             "property_name": "foo",
             "property_value": "bar"
         }
     })
     self.assertTrue(isinstance(self.filter, NOTFilter))
Esempio n. 15
0
    def test_complex_structure(self):
        # in slightly more compact format:
        # ((foo=bar) or (foo1=bar1 and foo2=bar2 and (foo3=bar3 or foo4=bar4)))
        filter = FilterFactory.from_spec({
            "type": "or",
            "filters": [
                {
                    "type": "property_match",
                    "property_name": "foo",
                    "property_value": "bar"
                },
                {
                    "type": "and",
                    "filters": [
                        {
                            "type": "property_match",
                            "property_name": "foo1",
                            "property_value": "bar1"
                        },
                        {
                            "type": "property_match",
                            "property_name": "foo2",
                            "property_value": "bar2"
                        },
                        {
                            "type": "or",
                            "filters": [
                                {
                                    "type": "property_match",
                                    "property_name": "foo3",
                                    "property_value": "bar3"
                                },
                                {
                                    "type": "property_match",
                                    "property_name": "foo4",
                                    "property_value": "bar4"
                                }
                            ]
                        },
                    ]
                },
            ]
        })
        # first level or
        self.assertTrue(filter(dict(foo='bar')))
        # first level and with both or's
        self.assertTrue(filter(dict(foo1='bar1', foo2='bar2', foo3='bar3')))
        self.assertTrue(filter(dict(foo1='bar1', foo2='bar2', foo4='bar4')))

        # first and not right
        self.assertFalse(filter(dict(foo1='not bar1', foo2='bar2', foo3='bar3')))
        # second and not right
        self.assertFalse(filter(dict(foo1='bar1', foo2='not bar2', foo3='bar3')))
        # last and not right
        self.assertFalse(filter(dict(foo1='bar1', foo2='bar2', foo3='not bar3', foo4='not bar4')))
Esempio n. 16
0
 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, {})
         )
Esempio n. 17
0
 def test_date_conversion(self):
     filter_with_date = FilterFactory.from_spec({
         "type": "boolean_expression",
         "expression": {
             "datatype": "date",
             "property_name": "visit_date",
             "type": "property_name"
         },
         "operator": "gt",
         "property_value": "2015-05-05"
     })
     self.assertFalse(filter_with_date({'visit_date': '2015-05-04'}))
     self.assertTrue(filter_with_date({'visit_date': '2015-05-06'}))
Esempio n. 18
0
 def test_expression_in_value(self):
     filter_with_exp = FilterFactory.from_spec({
         'type': 'boolean_expression',
         'expression': {
             'type': 'property_name',
             'property_name': 'foo',
         },
         'operator': 'gt',
         'property_value': {
             'type': 'property_name',
             'property_name': 'bar',
         },
     })
     self.assertTrue(filter_with_exp({'foo': 4, 'bar': 2}))
     self.assertFalse(filter_with_exp({'foo': 2, 'bar': 4}))
Esempio n. 19
0
 def _get_filter(self, doc_types):
     extras = [self.configured_filter] if self.configured_filter else []
     built_in_filters = [
         self._get_domain_filter_spec(),
         {
             "type": "or",
             "filters": [
                 {"type": "property_match", "property_name": "doc_type", "property_value": doc_type}
                 for doc_type in doc_types
             ],
         },
     ]
     return FilterFactory.from_spec(
         {"type": "and", "filters": built_in_filters + extras}, context=self.named_filter_objects
     )
Esempio n. 20
0
 def setUp(self):
     self.filter = FilterFactory.from_spec({
         "type": "or",
         "filters": [
             {
                 "type": "property_match",
                 "property_name": "foo",
                 "property_value": "bar"
             },
             {
                 "type": "property_match",
                 "property_name": "foo2",
                 "property_value": "bar2"
             }
         ]
     })
     self.assertTrue(isinstance(self.filter, ORFilter))
Esempio n. 21
0
def _make_filter(spec, context):
    # just pulled out here to keep the inline imports to a minimum
    # no way around this since the two factories inherently depend on each other
    from corehq.apps.userreports.filters.factory import FilterFactory

    return FilterFactory.from_spec(spec, context)
Esempio n. 22
0
 def get_filter(self):
     return FilterFactory.from_spec({
         'type': 'property_match',
         'property_name': 'foo',
         'property_value': 'bar',
     })
Esempio n. 23
0
 def get_path_filter(self):
     return FilterFactory.from_spec({
         'type': 'property_match',
         'property_path': ['path', 'to', 'foo'],
         'property_value': 'bar',
     })
Esempio n. 24
0
 def get_path_filter(self):
     return FilterFactory.from_spec({
         'type': 'property_match',
         'property_path': ['path', 'to', 'foo'],
         'property_value': 'bar',
     })
Esempio n. 25
0
 def test_name_no_value(self):
     with self.assertRaises(BadSpecError):
         FilterFactory.from_spec({
             'type': 'property_match',
             'property_name': 'foo',
         })
Esempio n. 26
0
 def test_no_name_or_path(self):
     with self.assertRaises(BadSpecError):
         FilterFactory.from_spec({
             'type': 'property_match',
             'property_value': 'bar',
         })
Esempio n. 27
0
    def test_complex_structure(self):
        # in slightly more compact format:
        # ((foo=bar) or (foo1=bar1 and foo2=bar2 and (foo3=bar3 or foo4=bar4)))
        filter = FilterFactory.from_spec({
            "type":
            "or",
            "filters": [
                {
                    "type": "property_match",
                    "property_name": "foo",
                    "property_value": "bar"
                },
                {
                    "type":
                    "and",
                    "filters": [
                        {
                            "type": "property_match",
                            "property_name": "foo1",
                            "property_value": "bar1"
                        },
                        {
                            "type": "property_match",
                            "property_name": "foo2",
                            "property_value": "bar2"
                        },
                        {
                            "type":
                            "or",
                            "filters": [{
                                "type": "property_match",
                                "property_name": "foo3",
                                "property_value": "bar3"
                            }, {
                                "type": "property_match",
                                "property_name": "foo4",
                                "property_value": "bar4"
                            }]
                        },
                    ]
                },
            ]
        })
        # first level or
        self.assertTrue(filter(dict(foo='bar')))
        # first level and with both or's
        self.assertTrue(filter(dict(foo1='bar1', foo2='bar2', foo3='bar3')))
        self.assertTrue(filter(dict(foo1='bar1', foo2='bar2', foo4='bar4')))

        # first and not right
        self.assertFalse(
            filter(dict(foo1='not bar1', foo2='bar2', foo3='bar3')))
        # second and not right
        self.assertFalse(
            filter(dict(foo1='bar1', foo2='not bar2', foo3='bar3')))
        # last and not right
        self.assertFalse(
            filter(
                dict(foo1='bar1',
                     foo2='bar2',
                     foo3='not bar3',
                     foo4='not bar4')))
Esempio n. 28
0
 def named_filter_objects(self):
     return {name: FilterFactory.from_spec(filter, {})
             for name, filter in self.named_filters.items()}
Esempio n. 29
0
 def named_filter_objects(self):
     return {name: FilterFactory.from_spec(filter, {})
             for name, filter in self.named_filters.items()}
Esempio n. 30
0
 def test_name_no_value(self):
     with self.assertRaises(BadSpecError):
         FilterFactory.from_spec({
             'type': 'property_match',
             'property_name': 'foo',
         })
Esempio n. 31
0
 def readable_output(self, context):
     from corehq.apps.userreports.filters.factory import FilterFactory
     filter_object = FilterFactory.from_spec(self.filter, context)
     return str(filter_object)
Esempio n. 32
0
 def get_filter(self):
     return FilterFactory.from_spec({
         'type': 'property_match',
         'property_name': 'foo',
         'property_value': 'bar',
     })
Esempio n. 33
0
 def named_filter_objects(self):
     return {name: FilterFactory.from_spec(filter, FactoryContext(self.named_expression_objects, {}))
             for name, filter in self.named_filters.items()}
Esempio n. 34
0
def _make_filter(spec, context):
    # just pulled out here to keep the inline imports to a minimum
    # no way around this since the two factories inherently depend on each other
    from corehq.apps.userreports.filters.factory import FilterFactory
    return FilterFactory.from_spec(spec, context)
Esempio n. 35
0
 def named_filter_objects(self):
     return {name: FilterFactory.from_spec(filter, FactoryContext.empty())
             for name, filter in self.named_filters.items()}
Esempio n. 36
0
 def named_filter_objects(self):
     return {
         name: FilterFactory.from_spec(
             filter, FactoryContext(self.named_expression_objects, {}))
         for name, filter in self.named_filters.items()
     }