def test_callable_func_converters(parser): with pytest.raises(TypeError, match=("'float' object is not callable")): read_xml(xml_types, converters={"degrees": float()}, parser=parser)
def test_wrong_encoding(datapath, parser): filename = datapath("io", "data", "xml", "baby_names.xml") with pytest.raises(UnicodeDecodeError, match=("'utf-8' codec can't decode")): read_xml(filename, parser=parser)
def test_ascii_encoding(datapath, parser): filename = datapath("io", "data", "xml", "baby_names.xml") with pytest.raises(UnicodeDecodeError, match=("'ascii' codec can't decode byte")): read_xml(filename, encoding="ascii", parser=parser)
def test_missing_prefix_definition_etree(datapath): filename = datapath("io", "data", "xml", "cta_rail_lines.kml") with pytest.raises(SyntaxError, match=("you used an undeclared namespace prefix")): read_xml(filename, xpath=".//kml:Placemark", parser="etree")
def test_names_option_wrong_length(datapath, parser): filename = datapath("io", "data", "xml", "books.xml") with pytest.raises(ValueError, match=("names does not match length")): read_xml(filename, names=["Col1", "Col2", "Col3"], parser=parser)
def test_empty_xpath_lxml(datapath): filename = datapath("io", "data", "xml", "books.xml") with pytest.raises(ValueError, match=("xpath does not return any nodes")): read_xml(filename, xpath=".//python", parser="lxml")
def test_bad_xpath_lxml(datapath): from lxml.etree import XPathEvalError filename = datapath("io", "data", "xml", "books.xml") with pytest.raises(XPathEvalError, match=("Invalid expression")): read_xml(filename, xpath=".//[book]", parser="lxml")
def test_parser_consistency_file(datapath): filename = datapath("io", "data", "xml", "books.xml") df_file_lxml = read_xml(filename, parser="lxml") df_file_etree = read_xml(filename, parser="etree") tm.assert_frame_equal(df_file_lxml, df_file_etree)
def test_file_only_attrs(datapath, parser): filename = datapath("io", "data", "xml", "books.xml") df_file = read_xml(filename, attrs_only=True, parser=parser) df_expected = DataFrame({"category": ["cooking", "children", "web"]}) tm.assert_frame_equal(df_file, df_expected)
def test_wrong_encoding_option_lxml(datapath, parser, encoding): filename = datapath("io", "data", "xml", "baby_names.xml") df_file = read_xml(filename, encoding="ISO-8859-1", parser="lxml") with tm.ensure_clean("test.xml") as path: df_file.to_xml(path, index=False, encoding=encoding, parser=parser)
def test_wrong_compression_gz(parser, comp): with tm.ensure_clean() as path: geom_df.to_xml(path, parser=parser, compression=comp) with pytest.raises(OSError, match="Not a gzipped file"): read_xml(path, parser=parser, compression="gzip")
def test_correct_encoding_file(datapath): filename = datapath("io", "data", "xml", "baby_names.xml") df_file = read_xml(filename, encoding="ISO-8859-1", parser="lxml") with tm.ensure_clean("test.xml") as path: df_file.to_xml(path, index=False, encoding="ISO-8859-1", parser="lxml")
def test_wrong_parse_dates_type(parser): with pytest.raises( TypeError, match=("Only booleans, lists, and dictionaries are accepted")): read_xml(xml_dates, parse_dates={"date"}, parser=parser)
def test_callable_str_converters(parser): with pytest.raises(TypeError, match=("'str' object is not callable")): read_xml(xml_types, converters={"degrees": "float"}, parser=parser)
def test_empty_string_etree(val): from xml.etree.ElementTree import ParseError with pytest.raises(ParseError, match="no element found"): read_xml(val, parser="etree")
def test_parser_consistency_with_encoding(datapath): filename = datapath("io", "data", "xml", "baby_names.xml") df_lxml = read_xml(filename, parser="lxml", encoding="ISO-8859-1") df_etree = read_xml(filename, parser="etree", encoding="iso-8859-1") tm.assert_frame_equal(df_lxml, df_etree)
def test_wrong_url(parser): with pytest.raises(HTTPError, match=("HTTP Error 404: Not Found")): url = "https://www.w3schools.com/xml/python.xml" read_xml(url, xpath=".//book[count(*)=4]", parser=parser)
def test_wrong_compression_bz2(parser, comp): with tm.ensure_clean() as path: geom_df.to_xml(path, parser=parser, compression=comp) with pytest.raises(OSError, match="Invalid data stream"): read_xml(path, parser=parser, compression="bz2")
def test_bad_xpath_etree(datapath): filename = datapath("io", "data", "xml", "books.xml") with pytest.raises( SyntaxError, match=("You have used an incorrect or unsupported XPath")): read_xml(filename, xpath=".//[book]", parser="etree")
def test_wrong_dict_value(datapath, parser): filename = datapath("io", "data", "xml", "books.xml") with pytest.raises( TypeError, match="<class 'str'> is not a valid type for value in iterparse"): read_xml(filename, parser=parser, iterparse={"book": "category"})
def test_missing_prefix_with_default_namespace(datapath, parser): filename = datapath("io", "data", "xml", "books.xml") with pytest.raises(ValueError, match=("xpath does not return any nodes")): read_xml(filename, xpath=".//Placemark", parser=parser)
def test_unsuported_compression(parser): with pytest.raises(ValueError, match="Unrecognized compression type"): with tm.ensure_clean() as path: read_xml(path, parser=parser, compression="7z")
def test_missing_prefix_definition_lxml(datapath): from lxml.etree import XPathEvalError filename = datapath("io", "data", "xml", "cta_rail_lines.kml") with pytest.raises(XPathEvalError, match=("Undefined namespace prefix")): read_xml(filename, xpath=".//kml:Placemark", parser="lxml")
def read_xml_iterparse(data, **kwargs): with tm.ensure_clean() as path: with open(path, "w") as f: f.write(data) return read_xml(path, **kwargs)
def test_names_option_wrong_type(datapath, parser): filename = datapath("io", "data", "xml", "books.xml") with pytest.raises(TypeError, match=("is not a valid type for names")): read_xml(filename, names="Col1, Col2, Col3", parser=parser)
def read_xml_iterparse_comp(comp_path, compression_only, **kwargs): with get_handle(comp_path, "r", compression=compression_only) as handles: with tm.ensure_clean() as path: with open(path, "w") as f: f.write(handles.handle.read()) return read_xml(path, **kwargs)
def test_unknown_encoding(datapath, parser): filename = datapath("io", "data", "xml", "baby_names.xml") with pytest.raises(LookupError, match=("unknown encoding: UFT-8")): read_xml(filename, encoding="UFT-8", parser=parser)
def test_empty_string_lxml(val): from lxml.etree import XMLSyntaxError with pytest.raises(XMLSyntaxError, match="Document is empty"): read_xml(val, parser="lxml")
def test_wrong_parser(datapath): filename = datapath("io", "data", "xml", "books.xml") with pytest.raises(ValueError, match=("Values for parser can only be lxml or etree.")): read_xml(filename, parser="bs4")
def test_wrong_converters_type(parser): with pytest.raises(TypeError, match=("Type converters must be a dict or subclass")): read_xml(xml_types, converters={"degrees", str}, parser=parser)