<s> from django . test import SimpleTestCase <EOL> from corehq . apps . userreports . exceptions import BadSpecError <EOL> from corehq . apps . userreports . filters import ANDFilter , ORFilter , NOTFilter <EOL> from corehq . apps . userreports . filters . factory import FilterFactory <EOL> from corehq . apps . userreports . specs import FactoryContext <EOL> class PropertyMatchFilterTest ( SimpleTestCase ) : <EOL> def get_filter ( self ) : <EOL> return FilterFactory . from_spec ( { <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:foo>' , <EOL> '<STR_LIT>' : '<STR_LIT:bar>' , <EOL> } ) <EOL> def get_path_filter ( self ) : <EOL> return FilterFactory . from_spec ( { <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ '<STR_LIT:path>' , '<STR_LIT:to>' , '<STR_LIT:foo>' ] , <EOL> '<STR_LIT>' : '<STR_LIT:bar>' , <EOL> } ) <EOL> def test_normal_filter ( self ) : <EOL> self . get_filter ( ) <EOL> def test_filter_with_path ( self ) : <EOL> self . get_path_filter ( ) <EOL> def test_no_name_or_path ( self ) : <EOL> with self . assertRaises ( BadSpecError ) : <EOL> FilterFactory . from_spec ( { <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:bar>' , <EOL> } ) <EOL> def test_empty_name ( self ) : <EOL> with self . assertRaises ( BadSpecError ) : <EOL> FilterFactory . from_spec ( { <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:bar>' , <EOL> } ) <EOL> def test_name_no_value ( self ) : <EOL> with self . assertRaises ( BadSpecError ) : <EOL> FilterFactory . from_spec ( { <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:foo>' , <EOL> } ) <EOL> def test_empty_path ( self ) : <EOL> with self . assertRaises ( BadSpecError ) : <EOL> FilterFactory . from_spec ( { <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : '<STR_LIT:bar>' , <EOL> } ) <EOL> def test_filter_match ( self ) : <EOL> self . assertTrue ( self . get_filter ( ) ( dict ( foo = '<STR_LIT:bar>' ) ) ) <EOL> def test_filter_no_match ( self ) : <EOL> self . assertFalse ( self . get_filter ( ) ( dict ( foo = '<STR_LIT>' ) ) ) <EOL> def test_filter_missing ( self ) : <EOL> self . assertFalse ( self . get_filter ( ) ( dict ( not_foo = '<STR_LIT:bar>' ) ) ) <EOL> def test_filter_path_match ( self ) : <EOL> self . assertTrue ( self . get_path_filter ( ) ( { '<STR_LIT:path>' : { '<STR_LIT:to>' : { '<STR_LIT:foo>' : '<STR_LIT:bar>' } } } ) ) <EOL> def test_filter_path_no_match ( self ) : <EOL> self . assertFalse ( self . get_path_filter ( ) ( { '<STR_LIT:path>' : { '<STR_LIT:to>' : { '<STR_LIT:foo>' : '<STR_LIT>' } } } ) ) <EOL> def test_path_filter_missing ( self ) : <EOL> self . assertFalse ( self . get_path_filter ( ) ( { '<STR_LIT:path>' : { '<STR_LIT:to>' : { '<STR_LIT>' : '<STR_LIT:bar>' } } } ) ) <EOL> self . assertFalse ( self . get_path_filter ( ) ( { '<STR_LIT:foo>' : '<STR_LIT:bar>' } ) ) <EOL> class EqualityFilterTest ( PropertyMatchFilterTest ) : <EOL> def get_filter ( self ) : <EOL> return FilterFactory . from_spec ( { <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:foo>' , <EOL> } , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:bar>' , <EOL> } ) <EOL> def get_path_filter ( self ) : <EOL> return FilterFactory . from_spec ( { <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ '<STR_LIT:path>' , '<STR_LIT:to>' , '<STR_LIT:foo>' ] , <EOL> } , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:bar>' , <EOL> } ) <EOL> class PropertyMatchFilterTest ( SimpleTestCase ) : <EOL> def test_null_value ( self ) : <EOL> null_filter = FilterFactory . from_spec ( { <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:foo>' , <EOL> '<STR_LIT>' : None , <EOL> } ) <EOL> self . assertEqual ( True , null_filter ( { '<STR_LIT:foo>' : None } ) ) <EOL> self . assertEqual ( True , null_filter ( { } ) ) <EOL> self . assertEqual ( False , null_filter ( { '<STR_LIT:foo>' : '<STR_LIT>' } ) ) <EOL> self . assertEqual ( False , null_filter ( { '<STR_LIT:foo>' : '<STR_LIT>' } ) ) <EOL> class BooleanExpressionFilterTest ( SimpleTestCase ) : <EOL> def get_filter ( self , operator , value ) : <EOL> return FilterFactory . from_spec ( { <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:foo>' , <EOL> } , <EOL> '<STR_LIT>' : operator , <EOL> '<STR_LIT>' : value , <EOL> } ) <EOL> def test_equal ( self ) : <EOL> match = '<STR_LIT>' <EOL> filter = self . get_filter ( '<STR_LIT>' , match ) <EOL> self . assertTrue ( filter ( { '<STR_LIT:foo>' : match } ) ) <EOL> self . assertFalse ( filter ( { '<STR_LIT:foo>' : '<STR_LIT>' } ) ) <EOL> self . assertFalse ( filter ( { '<STR_LIT:foo>' : None } ) ) <EOL> def test_equal_null ( self ) : <EOL> null_filter = self . get_filter ( '<STR_LIT>' , None ) <EOL> self . assertEqual ( True , null_filter ( { '<STR_LIT:foo>' : None } ) ) <EOL> self . assertEqual ( True , null_filter ( { } ) ) <EOL> self . assertEqual ( False , null_filter ( { '<STR_LIT:foo>' : '<STR_LIT>' } ) ) <EOL> self . assertEqual ( False , null_filter ( { '<STR_LIT:foo>' : '<STR_LIT>' } ) ) <EOL> def test_in ( self ) : <EOL> values = [ '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' ] <EOL> filter = self . get_filter ( '<STR_LIT>' , values ) <EOL> for value in values : <EOL> self . assertTrue ( filter ( { '<STR_LIT:foo>' : value } ) ) <EOL> for value in [ '<STR_LIT:d>' , '<STR_LIT:e>' , '<STR_LIT:f>' ] : <EOL> self . assertFalse ( filter ( { '<STR_LIT:foo>' : value } ) ) <EOL> def test_in_multiselect ( self ) : <EOL> filter = self . get_filter ( '<STR_LIT>' , '<STR_LIT:a>' ) <EOL> self . assertTrue ( filter ( { '<STR_LIT:foo>' : '<STR_LIT:a>' } ) ) <EOL> self . assertTrue ( filter ( { '<STR_LIT:foo>' : '<STR_LIT>' } ) ) <EOL> self . assertTrue ( filter ( { '<STR_LIT:foo>' : '<STR_LIT>' } ) ) <EOL> self . assertFalse ( filter ( { '<STR_LIT:foo>' : '<STR_LIT:b>' } ) ) <EOL> self . assertFalse ( filter ( { '<STR_LIT:foo>' : '<STR_LIT:abc>' } ) ) <EOL> self . assertFalse ( filter ( { '<STR_LIT:foo>' : '<STR_LIT>' } ) ) <EOL> self . assertFalse ( filter ( { '<STR_LIT:foo>' : '<STR_LIT>' } ) ) <EOL> def test_less_than ( self ) : <EOL> filter = self . get_filter ( '<STR_LIT>' , <NUM_LIT:3> ) <EOL> for match in ( - <NUM_LIT:10> , <NUM_LIT:0> , <NUM_LIT:2> ) : <EOL> self . assertTrue ( filter ( { '<STR_LIT:foo>' : match } ) ) <EOL> for non_match in ( <NUM_LIT:3> , <NUM_LIT:11> , '<STR_LIT:2>' ) : <EOL> self . assertFalse ( filter ( { '<STR_LIT:foo>' : non_match } ) ) <EOL> def test_less_than_equal ( self ) : <EOL> filter = self . get_filter ( '<STR_LIT>' , <NUM_LIT:3> ) <EOL> for match in ( - <NUM_LIT:10> , <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:3> ) : <EOL> self . assertTrue ( filter ( { '<STR_LIT:foo>' : match } ) ) <EOL> for non_match in ( <NUM_LIT:4> , <NUM_LIT:11> , '<STR_LIT:2>' ) : <EOL> self . assertFalse ( filter ( { '<STR_LIT:foo>' : non_match } ) ) <EOL> def test_greater_than ( self ) : <EOL> filter = self . get_filter ( '<STR_LIT>' , <NUM_LIT:3> ) <EOL> for match in ( <NUM_LIT:4> , <NUM_LIT:11> , '<STR_LIT:2>' ) : <EOL> self . assertTrue ( filter ( { '<STR_LIT:foo>' : match } ) ) <EOL> for non_match in ( - <NUM_LIT:10> , <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:3> ) : <EOL> self . assertFalse ( filter ( { '<STR_LIT:foo>' : non_match } ) ) <EOL> def test_greater_than_equal ( self ) : <EOL> filter = self . get_filter ( '<STR_LIT>' , <NUM_LIT:3> ) <EOL> for match in ( <NUM_LIT:3> , <NUM_LIT:11> , '<STR_LIT:2>' ) : <EOL> self . assertTrue ( filter ( { '<STR_LIT:foo>' : match } ) ) <EOL> for non_match in ( - <NUM_LIT:10> , <NUM_LIT:0> , <NUM_LIT:2> ) : <EOL> self . assertFalse ( filter ( { '<STR_LIT:foo>' : non_match } ) ) <EOL> def test_date_conversion ( self ) : <EOL> filter_with_date = FilterFactory . from_spec ( { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT:date>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:type>" : "<STR_LIT>" <EOL> } , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } ) <EOL> self . assertFalse ( filter_with_date ( { '<STR_LIT>' : '<STR_LIT>' } ) ) <EOL> self . assertTrue ( filter_with_date ( { '<STR_LIT>' : '<STR_LIT>' } ) ) <EOL> def test_literal_in_expression ( self ) : <EOL> filter_with_literal = FilterFactory . from_spec ( { <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:2> <EOL> } ) <EOL> self . assertFalse ( filter_with_literal ( { } ) ) <EOL> filter_with_literal = FilterFactory . from_spec ( { <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:2> , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:1> <EOL> } ) <EOL> self . assertTrue ( filter_with_literal ( { } ) ) <EOL> def test_expression_in_value ( self ) : <EOL> filter_with_exp = FilterFactory . from_spec ( { <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:foo>' , <EOL> } , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:bar>' , <EOL> } , <EOL> } ) <EOL> self . assertTrue ( filter_with_exp ( { '<STR_LIT:foo>' : <NUM_LIT:4> , '<STR_LIT:bar>' : <NUM_LIT:2> } ) ) <EOL> self . assertFalse ( filter_with_exp ( { '<STR_LIT:foo>' : <NUM_LIT:2> , '<STR_LIT:bar>' : <NUM_LIT:4> } ) ) <EOL> class ConfigurableANDFilterTest ( SimpleTestCase ) : <EOL> def setUp ( self ) : <EOL> self . filter = FilterFactory . from_spec ( { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ <EOL> { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT:foo>" , <EOL> "<STR_LIT>" : "<STR_LIT:bar>" <EOL> } , <EOL> { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> ] <EOL> } ) <EOL> self . assertTrue ( isinstance ( self . filter , ANDFilter ) ) <EOL> def test_filter_match ( self ) : <EOL> self . assertTrue ( self . filter ( dict ( foo = '<STR_LIT:bar>' , foo2 = '<STR_LIT>' ) ) ) <EOL> def test_filter_partial_match ( self ) : <EOL> self . assertFalse ( self . filter ( dict ( foo = '<STR_LIT:bar>' , foo2 = '<STR_LIT>' ) ) ) <EOL> def test_filter_no_match ( self ) : <EOL> self . assertFalse ( self . filter ( dict ( foo = '<STR_LIT>' , foo2 = '<STR_LIT>' ) ) ) <EOL> def test_filter_missing_partial_match ( self ) : <EOL> self . assertFalse ( self . filter ( dict ( foo = '<STR_LIT:bar>' ) ) ) <EOL> def test_filter_missing_all ( self ) : <EOL> self . assertFalse ( self . filter ( dict ( notfoo = '<STR_LIT>' ) ) ) <EOL> class ConfigurableORFilterTest ( SimpleTestCase ) : <EOL> def setUp ( self ) : <EOL> self . filter = FilterFactory . from_spec ( { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ <EOL> { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT:foo>" , <EOL> "<STR_LIT>" : "<STR_LIT:bar>" <EOL> } , <EOL> { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> ] <EOL> } ) <EOL> self . assertTrue ( isinstance ( self . filter , ORFilter ) ) <EOL> def test_filter_match ( self ) : <EOL> self . assertTrue ( self . filter ( dict ( foo = '<STR_LIT:bar>' , foo2 = '<STR_LIT>' ) ) ) <EOL> def test_filter_partial_match ( self ) : <EOL> self . assertTrue ( self . filter ( dict ( foo = '<STR_LIT:bar>' , foo2 = '<STR_LIT>' ) ) ) <EOL> def test_filter_no_match ( self ) : <EOL> self . assertFalse ( self . filter ( dict ( foo = '<STR_LIT>' , foo2 = '<STR_LIT>' ) ) ) <EOL> def test_filter_missing_partial_match ( self ) : <EOL> self . assertTrue ( self . filter ( dict ( foo = '<STR_LIT:bar>' ) ) ) <EOL> def test_filter_missing_all ( self ) : <EOL> self . assertFalse ( self . filter ( dict ( notfoo = '<STR_LIT>' ) ) ) <EOL> class ComplexFilterTest ( SimpleTestCase ) : <EOL> def test_complex_structure ( self ) : <EOL> filter = FilterFactory . from_spec ( { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ <EOL> { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT:foo>" , <EOL> "<STR_LIT>" : "<STR_LIT:bar>" <EOL> } , <EOL> { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ <EOL> { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } , <EOL> { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } , <EOL> { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ <EOL> { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } , <EOL> { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> ] <EOL> } , <EOL> ] <EOL> } , <EOL> ] <EOL> } ) <EOL> self . assertTrue ( filter ( dict ( foo = '<STR_LIT:bar>' ) ) ) <EOL> self . assertTrue ( filter ( dict ( foo1 = '<STR_LIT>' , foo2 = '<STR_LIT>' , foo3 = '<STR_LIT>' ) ) ) <EOL> self . assertTrue ( filter ( dict ( foo1 = '<STR_LIT>' , foo2 = '<STR_LIT>' , foo4 = '<STR_LIT>' ) ) ) <EOL> self . assertFalse ( filter ( dict ( foo1 = '<STR_LIT>' , foo2 = '<STR_LIT>' , foo3 = '<STR_LIT>' ) ) ) <EOL> self . assertFalse ( filter ( dict ( foo1 = '<STR_LIT>' , foo2 = '<STR_LIT>' , foo3 = '<STR_LIT>' ) ) ) <EOL> self . assertFalse ( filter ( dict ( foo1 = '<STR_LIT>' , foo2 = '<STR_LIT>' , foo3 = '<STR_LIT>' , foo4 = '<STR_LIT>' ) ) ) <EOL> class ConfigurableNOTFilterTest ( SimpleTestCase ) : <EOL> def setUp ( self ) : <EOL> self . filter = FilterFactory . from_spec ( { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT:foo>" , <EOL> "<STR_LIT>" : "<STR_LIT:bar>" <EOL> } <EOL> } ) <EOL> self . assertTrue ( isinstance ( self . filter , NOTFilter ) ) <EOL> def test_filter_match ( self ) : <EOL> self . assertTrue ( self . filter ( dict ( foo = '<STR_LIT>' ) ) ) <EOL> def test_filter_no_match ( self ) : <EOL> self . assertFalse ( self . filter ( dict ( foo = '<STR_LIT:bar>' ) ) ) <EOL> class ConfigurableNamedFilterTest ( SimpleTestCase ) : <EOL> def setUp ( self ) : <EOL> self . filter = FilterFactory . from_spec ( <EOL> { '<STR_LIT:type>' : '<STR_LIT>' , '<STR_LIT:name>' : '<STR_LIT:foo>' } , <EOL> FactoryContext ( { } , { <EOL> '<STR_LIT:foo>' : FilterFactory . from_spec ( { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT:foo>" , <EOL> "<STR_LIT>" : "<STR_LIT:bar>" <EOL> } <EOL> } ) <EOL> } ) <EOL> ) <EOL> self . assertTrue ( isinstance ( self . filter , NOTFilter ) ) <EOL> def test_filter_match ( self ) : <EOL> self . assertTrue ( self . filter ( dict ( foo = '<STR_LIT>' ) ) ) <EOL> def test_filter_no_match ( self ) : <EOL> self . assertFalse ( self . filter ( dict ( foo = '<STR_LIT:bar>' ) ) ) </s>
