def testEmptyName(self): with self.assertRaises(BadSpecError): FilterFactory.from_spec({ 'type': 'property_match', 'property_name': '', 'property_value': 'bar', })
def test_empty_path(self): with self.assertRaises(BadSpecError): FilterFactory.from_spec({ 'type': 'property_match', 'property_path': [], 'property_value': 'bar', })
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', })
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({}))
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 _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, )
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(), )
def _build_boolean_indicator(spec, context): wrapped = BooleanIndicatorSpec.wrap(spec) return BooleanIndicator( wrapped.display_name, wrapped.column_id, FilterFactory.from_spec(wrapped.filter, context), )
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 ]
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), )
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', })
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': ''}))
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, })
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))
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')))
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_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'}))
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}))
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 )
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))
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)
def get_filter(self): return FilterFactory.from_spec({ 'type': 'property_match', 'property_name': 'foo', 'property_value': 'bar', })
def get_path_filter(self): return FilterFactory.from_spec({ 'type': 'property_match', 'property_path': ['path', 'to', 'foo'], 'property_value': 'bar', })
def test_name_no_value(self): with self.assertRaises(BadSpecError): FilterFactory.from_spec({ 'type': 'property_match', 'property_name': 'foo', })
def test_no_name_or_path(self): with self.assertRaises(BadSpecError): FilterFactory.from_spec({ 'type': 'property_match', 'property_value': 'bar', })
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')))
def named_filter_objects(self): return {name: FilterFactory.from_spec(filter, {}) for name, filter in self.named_filters.items()}
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)
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_filter_objects(self): return {name: FilterFactory.from_spec(filter, FactoryContext.empty()) for name, filter in self.named_filters.items()}
def named_filter_objects(self): return { name: FilterFactory.from_spec( filter, FactoryContext(self.named_expression_objects, {})) for name, filter in self.named_filters.items() }