def test_filter_policy(): test_data = [ ( 'no filter with no attributes', {}, {}, True ), ( 'no filter with attributes', {}, {'filter': {'Type': 'String', 'Value': 'type1'}}, True ), ( 'exact string filter', {'filter': 'type1'}, {'filter': {'Type': 'String', 'Value': 'type1'}}, True ), ( 'exact string filter with no attributes', {'filter': 'type1'}, {}, False ), ( 'exact string filter with no match', {'filter': 'type1'}, {'filter': {'Type': 'String', 'Value': 'type2'}}, False ), ( 'or string filter with match', {'filter': ['type1', 'type2']}, {'filter': {'Type': 'String', 'Value': 'type1'}}, True ), ( 'or string filter with other match', {'filter': ['type1', 'type2']}, {'filter': {'Type': 'String', 'Value': 'type2'}}, True ), ( 'or string filter with no attributes', {'filter': ['type1', 'type2']}, {}, False ), ( 'or string filter with no match', {'filter': ['type1', 'type2']}, {'filter': {'Type': 'String', 'Value': 'type3'}}, False ), ( 'anything-but string filter with match', {'filter': [{'anything-but': 'type1'}]}, {'filter': {'Type': 'String', 'Value': 'type1'}}, False ), ( 'anything-but string filter with no match', {'filter': [{'anything-but': 'type1'}]}, {'filter': {'Type': 'String', 'Value': 'type2'}}, True ), ( 'prefix string filter with match', {'filter': [{'prefix': 'typ'}]}, {'filter': {'Type': 'String', 'Value': 'type1'}}, True ), ( 'prefix string filter with no match', {'filter': [{'prefix': 'test'}]}, {'filter': {'Type': 'String', 'Value': 'type2'}}, False ), ( 'numeric = filter with match', {'filter': [{'numeric': ['=', 300]}]}, {'filter': {'Type': 'Number', 'Value': 300}}, True ), ( 'numeric = filter with no match', {'filter': [{'numeric': ['=', 300]}]}, {'filter': {'Type': 'Number', 'Value': 301}}, False ), ( 'numeric > filter with match', {'filter': [{'numeric': ['>', 300]}]}, {'filter': {'Type': 'Number', 'Value': 301}}, True ), ( 'numeric > filter with no match', {'filter': [{'numeric': ['>', 300]}]}, {'filter': {'Type': 'Number', 'Value': 300}}, False ), ( 'numeric < filter with match', {'filter': [{'numeric': ['<', 300]}]}, {'filter': {'Type': 'Number', 'Value': 299}}, True ), ( 'numeric < filter with no match', {'filter': [{'numeric': ['<', 300]}]}, {'filter': {'Type': 'Number', 'Value': 300}}, False ), ( 'numeric >= filter with match', {'filter': [{'numeric': ['>=', 300]}]}, {'filter': {'Type': 'Number', 'Value': 300}}, True ), ( 'numeric >= filter with no match', {'filter': [{'numeric': ['>=', 300]}]}, {'filter': {'Type': 'Number', 'Value': 299}}, False ), ( 'numeric <= filter with match', {'filter': [{'numeric': ['<=', 300]}]}, {'filter': {'Type': 'Number', 'Value': 300}}, True ), ( 'numeric <= filter with no match', {'filter': [{'numeric': ['<=', 300]}]}, {'filter': {'Type': 'Number', 'Value': 301}}, False ), ( 'numeric filter with bad data', {'filter': [{'numeric': ['=', 300]}]}, {'filter': {'Type': 'String', 'Value': 'test'}}, False ), ( 'logical OR with match', {'filter': ['test1', 'test2', {'prefix': 'typ'}]}, {'filter': {'Type': 'String', 'Value': 'test2'}}, True ), ( 'logical OR with match', {'filter': ['test1', 'test2', {'prefix': 'typ'}]}, {'filter': {'Type': 'String', 'Value': 'test1'}}, True ), ( 'logical OR no match', {'filter': ['test1', 'test2', {'prefix': 'typ'}]}, {'filter': {'Type': 'String', 'Value': 'test3'}}, False ), ( 'logical AND with match', {'filter': [{'numeric': ['=', 300]}], 'other': [{'prefix': 'typ'}]}, {'filter': {'Type': 'Number', 'Value': 300}, 'other': {'Type': 'String', 'Value': 'type1'}}, True ), ( 'logical AND missing first attribute', {'filter': [{'numeric': ['=', 300]}], 'other': [{'prefix': 'typ'}]}, {'other': {'Type': 'String', 'Value': 'type1'}}, False ), ( 'logical AND missing second attribute', {'filter': [{'numeric': ['=', 300]}], 'other': [{'prefix': 'typ'}]}, {'filter': {'Type': 'Number', 'Value': 300}}, False ), ( 'logical AND no match', {'filter': [{'numeric': ['=', 300]}], 'other': [{'prefix': 'typ'}]}, {'filter': {'Type': 'Number', 'Value': 299}, 'other': {'Type': 'String', 'Value': 'type1'}}, False ), ( 'multiple numeric filters with first match', {'filter': [{'numeric': ['=', 300]}, {'numeric': ['=', 500]}]}, {'filter': {'Type': 'Number', 'Value': 300}}, True ), ( 'multiple numeric filters with second match', {'filter': [{'numeric': ['=', 300]}, {'numeric': ['=', 500]}]}, {'filter': {'Type': 'Number', 'Value': 500}}, True ), ( 'multiple prefix filters with first match', {'filter': [{'prefix': 'typ'}, {'prefix': 'tes'}]}, {'filter': {'Type': 'String', 'Value': 'type1'}}, True ), ( 'multiple prefix filters with second match', {'filter': [{'prefix': 'typ'}, {'prefix': 'tes'}]}, {'filter': {'Type': 'String', 'Value': 'test'}}, True ), ( 'multiple anything-but filters with second match', {'filter': [{'anything-but': 'type1'}, {'anything-but': 'type2'}]}, {'filter': {'Type': 'String', 'Value': 'type2'}}, True ), ( 'multiple numeric conditions', {'filter': [{'numeric': ['>', 0, '<=', 150]}]}, {'filter': {'Type': 'Number', 'Value': 122}}, True ), ( 'multiple numeric conditions', {'filter': [{'numeric': ['>', 0, '<=', 150]}]}, {'filter': {'Type': 'Number', 'Value': 200}}, False ), ( 'multiple numeric conditions', {'filter': [{'numeric': ['>', 0, '<=', 150]}]}, {'filter': {'Type': 'Number', 'Value': -1}}, False ) ] for test in test_data: test_name = test[0] filter_policy = test[1] attributes = test[2] expected = test[3] assert_equal(sns_listener.check_filter_policy(filter_policy, attributes), expected, test_name)
def test_filter_policy(self): test_data = [ ("no filter with no attributes", {}, {}, True), ( "no filter with attributes", {}, { "filter": { "Type": "String", "Value": "type1" } }, True, ), ( "exact string filter", { "filter": "type1" }, { "filter": { "Type": "String", "Value": "type1" } }, True, ), ( "exact string filter on an array", { "filter": "soccer" }, { "filter": { "Type": "String.Array", "Value": "['soccer', 'rugby', 'hockey']", } }, True, ), ("exact string filter with no attributes", { "filter": "type1" }, {}, False), ( "exact string filter with no match", { "filter": "type1" }, { "filter": { "Type": "String", "Value": "type2" } }, False, ), ( "or string filter with match", { "filter": ["type1", "type2"] }, { "filter": { "Type": "String", "Value": "type1" } }, True, ), ( "or string filter with other match", { "filter": ["type1", "type2"] }, { "filter": { "Type": "String", "Value": "type2" } }, True, ), ( "or string filter match with an array", { "filter": ["soccer", "basketball"] }, { "filter": { "Type": "String.Array", "Value": "['soccer', 'rugby', 'hockey']", } }, True, ), ( "or string filter with no attributes", { "filter": ["type1", "type2"] }, {}, False, ), ( "or string filter with no match", { "filter": ["type1", "type2"] }, { "filter": { "Type": "String", "Value": "type3" } }, False, ), ( "or string filter no match with an array", { "filter": ["volleyball", "basketball"] }, { "filter": { "Type": "String.Array", "Value": "['soccer', 'rugby', 'hockey']", } }, False, ), ( "anything-but string filter with match", { "filter": [{ "anything-but": "type1" }] }, { "filter": { "Type": "String", "Value": "type1" } }, False, ), ( "anything-but string filter with no match", { "filter": [{ "anything-but": "type1" }] }, { "filter": { "Type": "String", "Value": "type2" } }, True, ), ( "prefix string filter with match", { "filter": [{ "prefix": "typ" }] }, { "filter": { "Type": "String", "Value": "type1" } }, True, ), ( "prefix string filter match with an array", { "filter": [{ "prefix": "soc" }] }, { "filter": { "Type": "String.Array", "Value": "['soccer', 'rugby', 'hockey']", } }, True, ), ( "prefix string filter with no match", { "filter": [{ "prefix": "test" }] }, { "filter": { "Type": "String", "Value": "type2" } }, False, ), ( "numeric = filter with match", { "filter": [{ "numeric": ["=", 300] }] }, { "filter": { "Type": "Number", "Value": 300 } }, True, ), ( "numeric = filter with no match", { "filter": [{ "numeric": ["=", 300] }] }, { "filter": { "Type": "Number", "Value": 301 } }, False, ), ( "numeric > filter with match", { "filter": [{ "numeric": [">", 300] }] }, { "filter": { "Type": "Number", "Value": 301 } }, True, ), ( "numeric > filter with no match", { "filter": [{ "numeric": [">", 300] }] }, { "filter": { "Type": "Number", "Value": 300 } }, False, ), ( "numeric < filter with match", { "filter": [{ "numeric": ["<", 300] }] }, { "filter": { "Type": "Number", "Value": 299 } }, True, ), ( "numeric < filter with no match", { "filter": [{ "numeric": ["<", 300] }] }, { "filter": { "Type": "Number", "Value": 300 } }, False, ), ( "numeric >= filter with match", { "filter": [{ "numeric": [">=", 300] }] }, { "filter": { "Type": "Number", "Value": 300 } }, True, ), ( "numeric >= filter with no match", { "filter": [{ "numeric": [">=", 300] }] }, { "filter": { "Type": "Number", "Value": 299 } }, False, ), ( "numeric <= filter with match", { "filter": [{ "numeric": ["<=", 300] }] }, { "filter": { "Type": "Number", "Value": 300 } }, True, ), ( "numeric <= filter with no match", { "filter": [{ "numeric": ["<=", 300] }] }, { "filter": { "Type": "Number", "Value": 301 } }, False, ), ( "numeric filter with bad data", { "filter": [{ "numeric": ["=", 300] }] }, { "filter": { "Type": "String", "Value": "test" } }, False, ), ( "logical OR with match", { "filter": ["test1", "test2", { "prefix": "typ" }] }, { "filter": { "Type": "String", "Value": "test2" } }, True, ), ( "logical OR with match", { "filter": ["test1", "test2", { "prefix": "typ" }] }, { "filter": { "Type": "String", "Value": "test1" } }, True, ), ( "logical OR with match on an array", { "filter": ["test1", "test2", { "prefix": "typ" }] }, { "filter": { "Type": "String.Array", "Value": "['test1', 'other']" } }, True, ), ( "logical OR no match", { "filter": ["test1", "test2", { "prefix": "typ" }] }, { "filter": { "Type": "String", "Value": "test3" } }, False, ), ( "logical OR no match on an array", { "filter": ["test1", "test2", { "prefix": "typ" }] }, { "filter": { "Type": "String.Array", "Value": "['anything', 'something']", } }, False, ), ( "logical AND with match", { "filter": [{ "numeric": ["=", 300] }], "other": [{ "prefix": "typ" }] }, { "filter": { "Type": "Number", "Value": 300 }, "other": { "Type": "String", "Value": "type1" }, }, True, ), ( "logical AND missing first attribute", { "filter": [{ "numeric": ["=", 300] }], "other": [{ "prefix": "typ" }] }, { "other": { "Type": "String", "Value": "type1" } }, False, ), ( "logical AND missing second attribute", { "filter": [{ "numeric": ["=", 300] }], "other": [{ "prefix": "typ" }] }, { "filter": { "Type": "Number", "Value": 300 } }, False, ), ( "logical AND no match", { "filter": [{ "numeric": ["=", 300] }], "other": [{ "prefix": "typ" }] }, { "filter": { "Type": "Number", "Value": 299 }, "other": { "Type": "String", "Value": "type1" }, }, False, ), ( "multiple numeric filters with first match", { "filter": [{ "numeric": ["=", 300] }, { "numeric": ["=", 500] }] }, { "filter": { "Type": "Number", "Value": 300 } }, True, ), ( "multiple numeric filters with second match", { "filter": [{ "numeric": ["=", 300] }, { "numeric": ["=", 500] }] }, { "filter": { "Type": "Number", "Value": 500 } }, True, ), ( "multiple prefix filters with first match", { "filter": [{ "prefix": "typ" }, { "prefix": "tes" }] }, { "filter": { "Type": "String", "Value": "type1" } }, True, ), ( "multiple prefix filters with second match", { "filter": [{ "prefix": "typ" }, { "prefix": "tes" }] }, { "filter": { "Type": "String", "Value": "test" } }, True, ), ( "multiple anything-but filters with second match", { "filter": [{ "anything-but": "type1" }, { "anything-but": "type2" }] }, { "filter": { "Type": "String", "Value": "type2" } }, True, ), ( "multiple numeric conditions", { "filter": [{ "numeric": [">", 0, "<=", 150] }] }, { "filter": { "Type": "Number", "Value": 122 } }, True, ), ( "multiple numeric conditions", { "filter": [{ "numeric": [">", 0, "<=", 150] }] }, { "filter": { "Type": "Number", "Value": 200 } }, False, ), ( "multiple numeric conditions", { "filter": [{ "numeric": [">", 0, "<=", 150] }] }, { "filter": { "Type": "Number", "Value": -1 } }, False, ), ( "multiple conditions on an array", { "filter": ["test1", "test2", { "prefix": "som" }] }, { "filter": { "Type": "String.Array", "Value": "['anything', 'something']", } }, True, ), ( "exists with existing attribute", { "field": [{ "exists": True }] }, { "field": { "Type": "String", "Value": "anything" } }, True, ), ( "exists without existing attribute", { "field": [{ "exists": True }] }, { "other_field": { "Type": "String", "Value": "anything" } }, False, ), ( "does not exists without existing attribute", { "field": [{ "exists": False }] }, { "other_field": { "Type": "String", "Value": "anything" } }, True, ), ( "does not exists with existing attribute", { "field": [{ "exists": False }] }, { "field": { "Type": "String", "Value": "anything" } }, False, ), ] for test in test_data: test_name = test[0] filter_policy = test[1] attributes = test[2] expected = test[3] self.assertEqual( expected, sns_listener.check_filter_policy(filter_policy, attributes), test_name, )
def test_filter_policy(self): test_data = [('no filter with no attributes', {}, {}, True), ('no filter with attributes', {}, { 'filter': { 'Type': 'String', 'Value': 'type1' } }, True), ('exact string filter', { 'filter': 'type1' }, { 'filter': { 'Type': 'String', 'Value': 'type1' } }, True), ('exact string filter on an array', { 'filter': 'soccer' }, { 'filter': { 'Type': 'String.Array', 'Value': "['soccer', 'rugby', 'hockey']" } }, True), ('exact string filter with no attributes', { 'filter': 'type1' }, {}, False), ('exact string filter with no match', { 'filter': 'type1' }, { 'filter': { 'Type': 'String', 'Value': 'type2' } }, False), ('or string filter with match', { 'filter': ['type1', 'type2'] }, { 'filter': { 'Type': 'String', 'Value': 'type1' } }, True), ('or string filter with other match', { 'filter': ['type1', 'type2'] }, { 'filter': { 'Type': 'String', 'Value': 'type2' } }, True), ('or string filter match with an array', { 'filter': ['soccer', 'basketball'] }, { 'filter': { 'Type': 'String.Array', 'Value': "['soccer', 'rugby', 'hockey']" } }, True), ('or string filter with no attributes', { 'filter': ['type1', 'type2'] }, {}, False), ('or string filter with no match', { 'filter': ['type1', 'type2'] }, { 'filter': { 'Type': 'String', 'Value': 'type3' } }, False), ('or string filter no match with an array', { 'filter': ['volleyball', 'basketball'] }, { 'filter': { 'Type': 'String.Array', 'Value': "['soccer', 'rugby', 'hockey']" } }, False), ('anything-but string filter with match', { 'filter': [{ 'anything-but': 'type1' }] }, { 'filter': { 'Type': 'String', 'Value': 'type1' } }, False), ('anything-but string filter with no match', { 'filter': [{ 'anything-but': 'type1' }] }, { 'filter': { 'Type': 'String', 'Value': 'type2' } }, True), ('prefix string filter with match', { 'filter': [{ 'prefix': 'typ' }] }, { 'filter': { 'Type': 'String', 'Value': 'type1' } }, True), ('prefix string filter match with an array', { 'filter': [{ 'prefix': 'soc' }] }, { 'filter': { 'Type': 'String.Array', 'Value': "['soccer', 'rugby', 'hockey']" } }, True), ('prefix string filter with no match', { 'filter': [{ 'prefix': 'test' }] }, { 'filter': { 'Type': 'String', 'Value': 'type2' } }, False), ('numeric = filter with match', { 'filter': [{ 'numeric': ['=', 300] }] }, { 'filter': { 'Type': 'Number', 'Value': 300 } }, True), ('numeric = filter with no match', { 'filter': [{ 'numeric': ['=', 300] }] }, { 'filter': { 'Type': 'Number', 'Value': 301 } }, False), ('numeric > filter with match', { 'filter': [{ 'numeric': ['>', 300] }] }, { 'filter': { 'Type': 'Number', 'Value': 301 } }, True), ('numeric > filter with no match', { 'filter': [{ 'numeric': ['>', 300] }] }, { 'filter': { 'Type': 'Number', 'Value': 300 } }, False), ('numeric < filter with match', { 'filter': [{ 'numeric': ['<', 300] }] }, { 'filter': { 'Type': 'Number', 'Value': 299 } }, True), ('numeric < filter with no match', { 'filter': [{ 'numeric': ['<', 300] }] }, { 'filter': { 'Type': 'Number', 'Value': 300 } }, False), ('numeric >= filter with match', { 'filter': [{ 'numeric': ['>=', 300] }] }, { 'filter': { 'Type': 'Number', 'Value': 300 } }, True), ('numeric >= filter with no match', { 'filter': [{ 'numeric': ['>=', 300] }] }, { 'filter': { 'Type': 'Number', 'Value': 299 } }, False), ('numeric <= filter with match', { 'filter': [{ 'numeric': ['<=', 300] }] }, { 'filter': { 'Type': 'Number', 'Value': 300 } }, True), ('numeric <= filter with no match', { 'filter': [{ 'numeric': ['<=', 300] }] }, { 'filter': { 'Type': 'Number', 'Value': 301 } }, False), ('numeric filter with bad data', { 'filter': [{ 'numeric': ['=', 300] }] }, { 'filter': { 'Type': 'String', 'Value': 'test' } }, False), ('logical OR with match', { 'filter': ['test1', 'test2', { 'prefix': 'typ' }] }, { 'filter': { 'Type': 'String', 'Value': 'test2' } }, True), ('logical OR with match', { 'filter': ['test1', 'test2', { 'prefix': 'typ' }] }, { 'filter': { 'Type': 'String', 'Value': 'test1' } }, True), ('logical OR with match on an array', { 'filter': ['test1', 'test2', { 'prefix': 'typ' }] }, { 'filter': { 'Type': 'String.Array', 'Value': "['test1', 'other']" } }, True), ('logical OR no match', { 'filter': ['test1', 'test2', { 'prefix': 'typ' }] }, { 'filter': { 'Type': 'String', 'Value': 'test3' } }, False), ('logical OR no match on an array', { 'filter': ['test1', 'test2', { 'prefix': 'typ' }] }, { 'filter': { 'Type': 'String.Array', 'Value': "['anything', 'something']" } }, False), ('logical AND with match', { 'filter': [{ 'numeric': ['=', 300] }], 'other': [{ 'prefix': 'typ' }] }, { 'filter': { 'Type': 'Number', 'Value': 300 }, 'other': { 'Type': 'String', 'Value': 'type1' } }, True), ('logical AND missing first attribute', { 'filter': [{ 'numeric': ['=', 300] }], 'other': [{ 'prefix': 'typ' }] }, { 'other': { 'Type': 'String', 'Value': 'type1' } }, False), ('logical AND missing second attribute', { 'filter': [{ 'numeric': ['=', 300] }], 'other': [{ 'prefix': 'typ' }] }, { 'filter': { 'Type': 'Number', 'Value': 300 } }, False), ('logical AND no match', { 'filter': [{ 'numeric': ['=', 300] }], 'other': [{ 'prefix': 'typ' }] }, { 'filter': { 'Type': 'Number', 'Value': 299 }, 'other': { 'Type': 'String', 'Value': 'type1' } }, False), ('multiple numeric filters with first match', { 'filter': [{ 'numeric': ['=', 300] }, { 'numeric': ['=', 500] }] }, { 'filter': { 'Type': 'Number', 'Value': 300 } }, True), ('multiple numeric filters with second match', { 'filter': [{ 'numeric': ['=', 300] }, { 'numeric': ['=', 500] }] }, { 'filter': { 'Type': 'Number', 'Value': 500 } }, True), ('multiple prefix filters with first match', { 'filter': [{ 'prefix': 'typ' }, { 'prefix': 'tes' }] }, { 'filter': { 'Type': 'String', 'Value': 'type1' } }, True), ('multiple prefix filters with second match', { 'filter': [{ 'prefix': 'typ' }, { 'prefix': 'tes' }] }, { 'filter': { 'Type': 'String', 'Value': 'test' } }, True), ('multiple anything-but filters with second match', { 'filter': [{ 'anything-but': 'type1' }, { 'anything-but': 'type2' }] }, { 'filter': { 'Type': 'String', 'Value': 'type2' } }, True), ('multiple numeric conditions', { 'filter': [{ 'numeric': ['>', 0, '<=', 150] }] }, { 'filter': { 'Type': 'Number', 'Value': 122 } }, True), ('multiple numeric conditions', { 'filter': [{ 'numeric': ['>', 0, '<=', 150] }] }, { 'filter': { 'Type': 'Number', 'Value': 200 } }, False), ('multiple numeric conditions', { 'filter': [{ 'numeric': ['>', 0, '<=', 150] }] }, { 'filter': { 'Type': 'Number', 'Value': -1 } }, False), ('multiple conditions on an array', { 'filter': ['test1', 'test2', { 'prefix': 'som' }] }, { 'filter': { 'Type': 'String.Array', 'Value': "['anything', 'something']" } }, True)] for test in test_data: test_name = test[0] filter_policy = test[1] attributes = test[2] expected = test[3] self.assertEqual( sns_listener.check_filter_policy(filter_policy, attributes), expected, test_name)