def test_XmlHandler_is_set(tmp_valid_xml): """Checks XmlHandler.is_set method :param py.path.local tmp_valid_xml: Fixture, pointing to a temporary XML file """ xml = XmlHandler(tmp_valid_xml.strpath) assert xml, "No XmlHandler could be instantiated" assert not xml.is_set("foo", "nix"), "Tag 'foo' shouldn't be here"
def test_docmanager_init_0(tmp_valid_xml): """ Test the init sub command without force """ tmpfile = tmp_valid_xml.strpath clicmd = 'init {}'.format(tmpfile) a = Actions(parsecli(shlex.split(clicmd))) a.parse() handler = XmlHandler(tmpfile) for i in DEFAULT_DM_PROPERTIES: assert handler.is_prop_set( i) == True, "property {} is not set.".format(i)
def test_overwrite(tmp_docmanager_overwrite): """Checks if it's possible to overwrite the value of a property in an XML document :param py.path.local tmp_docmanager_overwrite: Fixture, pointing to a temporary XML file """ handler = XmlHandler(tmp_docmanager_overwrite.strpath) handler.set({"hello": "world"}) assert handler.get("hello") == { "hello": "world" }, \ 'Could not override old value of property "hello" in file test.override.xml'
def test_XmlHandler_tree(tmp_valid_xml): """Checks tree property of XmlHandler class :param py.path.local tmp_valid_xml: Fixture, pointing to a temporary XML file """ xml = XmlHandler(tmp_valid_xml.strpath) assert xml, "No XmlHandler could be instantiated" assert xml.tree
def test_docmanager_init_1(tmp_valid_xml, option, value): """ Test the init sub command with pre defined values """ tmpfile = tmp_valid_xml.strpath ropt = option.replace("/", "-") clicmd = "init --{} \"{}\" {}".format(ropt, value, tmpfile) a = Actions(parsecli(shlex.split(clicmd))) a.parse() handler = XmlHandler(tmpfile) ret = handler.get(option) assert ret[ option] == value, "The file was initialized without the pre defined value for the option '{}'.".format( option)
def test_missing_info_element(tmp_missing_info_element): """Checks if docmanager behaves correctly if a given XML files does not provide an info element :param py.path.local tmp_missing_info_element: Fixture, pointing to a temporary XML file""" try: handler = XmlHandler(tmp_missing_info_element.strpath) except SystemExit as e: assert e.code == 7, "Expected exit code 7 but got " + str(e.code)
def test_docmanager_setattr0(tmp_valid_xml): ustr = 'Uf4C56WL' handler = XmlHandler(tmp_valid_xml.strpath) handler.set({"myprop": None}) handler.set_attr("myprop", {ustr: 'blub'}) handler.write() with open(tmp_valid_xml.strpath, 'r') as f: content = f.read() assert ustr + "=\"blub\"" in content, 'Attribute {}=blub could not be created.'.format( ustr)
def test_XmlHandler_findelem(tmp_valid_xml): """Unit test for find_elem """ xml = XmlHandler(tmp_valid_xml.strpath) xml.set({"a/b/c": None}) assert xml.find_elem("a/b/c") is not None assert xml.find_elem("a") is not None assert xml.find_elem("b") is None
def test_file_without_info(testdir, tmpdir): """Checks if XML file contains NO <info> element :param py.path.local testdir: Path to test directory :param tmpdir: temporary directory """ base = "valid_xml_file_without_info.xml" xmlfile = testdir / base assert xmlfile.exists(), "temp XML file '%s' does not exist" % base xmlfile.copy(tmpdir) xmlfile = tmpdir / base xml = XmlHandler(xmlfile.strpath) root = xml.root assert root is not None i = xml.tree.find("//d:info", namespaces=NS) assert i is not None
def init_xml_handlers(self, fname): """ Initializes an XmlHandler for a file. :param string fname: The file name """ handler = None try: handler = {"file": fname, "handler": XmlHandler(fname, True)} except (DMXmlParseError, DMInvalidXMLRootElement, DMFileNotFoundError, DMNotDocBook5File) as err: handler = { "file": fname, "errorstr": err.errorstr, "error": err.error } return handler
def test_docmanager_getattr0(format_type, expected, tmp_valid_xml, capsys): ustr = 'Uf4C56WL' handler = XmlHandler(tmp_valid_xml.strpath) handler.set({"myprop": None}) handler.set_attr("myprop", {ustr: 'blub'}) handler.write() if format_type: clicmd = "get-attr -p myprop -a {} --format {} {}".format( ustr, format_type, tmp_valid_xml.strpath) else: clicmd = "get-attr -p myprop -a {} {}".format(ustr, tmp_valid_xml.strpath) a = Actions(parsecli(shlex.split(clicmd))) res = a.parse() renderer = getrenderer(format_type) renderer(res, args=a.args) out, err = capsys.readouterr() expected = expected.replace("[REPLACE_FILENAME]", tmp_valid_xml.strpath) assert out == expected
def test_XmlHandler_set(tmp_valid_xml): """Checks if docmanager element is available :param py.path.local tmp_valid_xml: Fixture, pointing to a temporary XML file """ xml = XmlHandler(tmp_valid_xml.strpath) assert xml, "No XmlHandler could be instantiated" xml.set({"foo": "2"}) xml.write() dm = xml.tree.find("//dm:docmanager", namespaces=NS) assert len(dm), "expected child elements in docmanager" # Let's test the written XML file tree = etree.parse(tmp_valid_xml.strpath) dm = tree.find("//dm:docmanager", namespaces=NS) assert len(dm), "expected child elements in docmanager"
def test_docmanager_setattr1(tmp_valid_xml): ustr = "Uf4C56WL" handler = XmlHandler(tmp_valid_xml.strpath) handler.set({"myprop": None}) handler.write() clicmd = "set-attr -p myprop -a {}=blub {}".format(ustr, tmp_valid_xml.strpath) a = Actions(parsecli(shlex.split(clicmd))) a.parse() with open(tmp_valid_xml.strpath, 'r') as f: content = f.read() assert ustr + "=\"blub\"" in content, 'Attribute {}=blub could not be created.'.format( ustr)
def test_docmanager_delattr2(tmp_valid_xml): ustr = 'Uf4C56WL' handler = XmlHandler(tmp_valid_xml.strpath) handler.set({"a/b/c": None}) handler.set_attr("a/b/c", {ustr: 'blub'}) handler.write() with open(tmp_valid_xml.strpath, 'r') as f: content = f.read() assert "dm:c "+ustr+"=\"blub\"" in content, 'Attribute {}=blub could not be created.'.format(ustr) handler.del_attr("a/b/c", [ustr]) handler.write() with open(tmp_valid_xml.strpath, 'r') as f: content = f.read() assert "dm:c "+ustr+"=\"blub\"" not in content, 'Attribute {}=blub could not be deleted.'.format(ustr)