def test_get_content_path_is_root(self): # Arrange raw_xml = "<root><element>Hello</element></root>" # Act content = get_content_by_xpath(raw_xml, "/root") # Assert self.assertEquals(content, [raw_xml])
def test_get_content_path_does_not_exist(self): # Arrange raw_xml = "<root><element>Hello</element></root>" # Act content = get_content_by_xpath(raw_xml, "/root/test") # Assert self.assertEquals(content, [])
def test_get_content_and_path_exists(self): # Arrange raw_xml = "<root><element>Hello</element></root>" # Act content = get_content_by_xpath(raw_xml, "/root/element") # Assert self.assertEquals(content, ["<element>Hello</element>"])
def test_get_content_path_found_more_than_once(self): # Arrange raw_xml = "<root><element>Hello</element><element>World</element></root>" # Act content = get_content_by_xpath(raw_xml, "/root/element") # Assert self.assertEquals( content, ["<element>Hello</element>", "<element>World</element>"])
def build_response(self, data_list): """Build the response. Args: data_list: List of data Returns: The response paginated """ xpath = self.request.data.get("xpath", None) namespaces = self.request.data.get("namespaces", None) if "all" in self.request.data and to_bool(self.request.data["all"]): # Select values at xpath if provided if xpath: for data_object in data_list: data_object.xml_content = get_content_by_xpath( data_object.xml_content, xpath, namespaces=namespaces) # Serialize data list data_serializer = self.serializer(data_list, many=True) # Return response return Response(data_serializer.data) else: # Get paginator paginator = StandardResultsSetPagination() # Get requested page from list of results page = paginator.paginate_queryset(data_list, self.request) # Select values at xpath if provided if xpath: for data_object in page: data_object.xml_content = get_content_by_xpath( data_object.xml_content, xpath, namespaces=namespaces) # Serialize page data_serializer = self.serializer(page, many=True) # Return paginated response return paginator.get_paginated_response(data_serializer.data)
def test_get_list_attribute_path_with_namespace(self): # Arrange raw_xml = """<root xmlns:h="http://www.w3.org/TR/html4/"> <h:table> <h:tr> <h:td class="class1">Apples</h:td> <h:td class="class2">Bananas</h:td> </h:tr> </h:table> </root>""" # Act content = get_content_by_xpath( raw_xml, "/root/h:table/h:tr/h:td/@class", namespaces={"h": "http://www.w3.org/TR/html4/"}, ) # Assert self.assertEquals( content, ["class1", "class2"], )
def test_get_content_returns_list(self): # Arrange raw_xml = """<root xmlns:h="http://www.w3.org/TR/html4/"> <h:table> <h:tr> <h:td>Apples</h:td> <h:td>Bananas</h:td> </h:tr> </h:table> </root>""" # Act content = get_content_by_xpath( raw_xml, "/root/h:table/h:tr/h:td", namespaces={"h": "http://www.w3.org/TR/html4/"}, ) # Assert self.assertEquals( content, [ '<h:td xmlns:h="http://www.w3.org/TR/html4/">Apples</h:td>', '<h:td xmlns:h="http://www.w3.org/TR/html4/">Bananas</h:td>', ], )