def test_filtering_with_a_value(self): content = dict_2_etree({ "data": { "configuration": [ {"element": { "element-key": "MY-KEY", "attribute": {"sub-attribute": {}} }}, {"element": { "element-key": "MY-OTHER-KEY", "other-attribute": {"sub-attribute": {}} }}, ] } }) content_filter = dict_2_etree({ "filter": { "configuration": { "element": { "element-key": "MY-KEY" }, } } }) filter_content(content, content_filter) assert_that(content.xpath("//data/configuration/element"), has_length(1)) assert_that(content.xpath("//data/configuration/element/*"), has_length(2)) assert_that(content.xpath("//data/configuration/element/attribute/*"), has_length(1))
def test_filtering_should_only_consider_non_empty_text_nodes(self): content = dict_2_etree({ "data": { "configuration": { "interfaces": { "interface": { "name": "key1" } } } } }) content_filter = to_ele(""" <filter> <configuration> <interfaces> <interface> <name>key1</name> </interface> </interfaces> </configuration> </filter> """) filter_content(content, content_filter) assert_that(content.xpath("//data/configuration/interfaces/interface"), has_length(1))
def test_filtering_multi_with_intermediate_without_identification_layers( self): content = dict_2_etree({ "data": { "configuration": { "interfaces": { "interface": [{ "name": "irb" }, { "unit": { "name": "123", "family": { "inet": { "address": { "name": "1.1.1.1/27" } } } } }] } } } }) content_filter = dict_2_etree({ "filter": { "configuration": { "interfaces": { "interface": { "name": "irb", "unit": { "name": "123", "family": { "inet": { "address": { "name": "1.1.1.1/27" } } } } } } } } }) filter_content(content, content_filter) assert_that( content.xpath( "//data/configuration/interfaces/interface/unit/family/inet/address/name" ), has_length(1))
def test_filtering(self): content = dict_2_etree({ "data": { "configuration": [{ "shizzle": { "whizzle": {} } }, { "shizzle": { "whizzle": { "howdy": {} }, "not-whizzle": { "not-howdy": {} } } }, { "zizzle": { "nothing": {} } }, { "outzzle": { "nothing": {} } }] } }) content_filter = dict_2_etree({ "filter": { "configuration": { "shizzle": { "whizzle": {} }, "zizzle": {}, } } }) filter_content(content, content_filter) assert_that(content.xpath("//data/configuration/shizzle"), has_length(2)) assert_that(content.xpath("//data/configuration/shizzle/*"), has_length(2)) assert_that( content.xpath("//data/configuration/shizzle/whizzle/howdy"), has_length(1)) assert_that(content.xpath("//data/configuration/zizzle"), has_length(1)) assert_that(content.xpath("//data/configuration/outzzle"), has_length(0))
def test_filtering_multiple_identification_is_weirdly_supported(self): content = dict_2_etree({ "data": { "configuration": { "interfaces": [{ "interface": [{ "name": "key1" }, { "name1": "key2" }, { "unit": { "name": "123" } }] }, { "interface": [{ "name": "key1" }, { "name1": "NOTKEY2" }, { "unit": { "name": "123" } }] }] } } }) content_filter = dict_2_etree({ "filter": { "configuration": { "interfaces": { "interface": [{ "name": "key1" }, { "name1": "key2" }, { "unit": { "name": "123" } }] } } } }) filter_content(content, content_filter) assert_that( content.xpath("//data/configuration/interfaces/interface/unit"), has_length(1))
def test_filtering_with_a_value(self): content = dict_2_etree({ "data": { "configuration": [ { "element": { "element-key": "MY-KEY", "attribute": { "sub-attribute": {} } } }, { "element": { "element-key": "MY-OTHER-KEY", "other-attribute": { "sub-attribute": {} } } }, ] } }) content_filter = dict_2_etree({ "filter": { "configuration": { "element": { "element-key": "MY-KEY" }, } } }) filter_content(content, content_filter) assert_that(content.xpath("//data/configuration/element"), has_length(1)) assert_that(content.xpath("//data/configuration/element/*"), has_length(2)) assert_that(content.xpath("//data/configuration/element/attribute/*"), has_length(1))
def test_filtering(self): content = dict_2_etree({ "data": { "configuration": [ {"shizzle": { "whizzle": {} }}, {"shizzle": { "whizzle": { "howdy": {} }, "not-whizzle": { "not-howdy": {} } }}, {"zizzle": { "nothing": {} }}, {"outzzle": { "nothing": {} }} ] } }) content_filter = dict_2_etree({ "filter": { "configuration": { "shizzle": {"whizzle": {}}, "zizzle": {}, } } }) filter_content(content, content_filter) assert_that(content.xpath("//data/configuration/shizzle"), has_length(2)) assert_that(content.xpath("//data/configuration/shizzle/*"), has_length(2)) assert_that(content.xpath("//data/configuration/shizzle/whizzle/howdy"), has_length(1)) assert_that(content.xpath("//data/configuration/zizzle"), has_length(1)) assert_that(content.xpath("//data/configuration/outzzle"), has_length(0))
def test_filtering_multi_level_should_not_return_siblings(self): content = dict_2_etree({ "data": { "configuration": { "interfaces": { "interface": [{ "name": "irb" }, { "unit": { "name": "123" } }] } } } }) content_filter = dict_2_etree({ "filter": { "configuration": { "interfaces": { "interface": { "name": "irb", "unit": { "name": "456" } } } } } }) filter_content(content, content_filter) assert_that( content.xpath("//data/configuration/interfaces/interface/unit"), has_length(0))