Debian Codegraph @

python-scripts/getentityname/tests/test_catalogs.py

raw
1 from unittest import mock
2
3 import pytest
4 import py
5
6 # "gen" is the abbreviated name for "getentityname.py"
7 import gen
8
9 TESTDIR = py.path.local(__file__).parts()[-2]
10
11
12 def test_catalog_does_not_exist():
13 with pytest.raises(gen.XMLCatalogError, match=""):
14 gen.xmlcatalog("urn:not_important",
15 "catalog-not-exists.xml",
16 raise_on_error=True)
17
18
19 @pytest.mark.parametrize("identifier", [
20 "-//TOMS//DTD DocBook XML V7.0//EN",
21 "https://example.org/example",
22 ])
23 def test_xmlcatalog_identifiers(identifier):
24 catalog = str(TESTDIR / "catalog-publicid-systemid.xml")
25 result = gen.xmlcatalog(identifier, catalog, raise_on_error=True)
26 assert result == "/it/works/"
27
28
29 def test_xmlcatalog_with_system_entity(tmpdir):
30 # given
31 resultentity = "my-fake.ent"
32 catalogfile = (tmpdir / "catalog.xml")
33 catalogfile.write_text("""<catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog">
34 <system systemId="http://www.example.org/foo.ent" uri="%s"/>
35 </catalog>""" % resultentity,
36 encoding="UTF-8",
37 )
38 # when
39 result = gen.xmlcatalog("http://www.example.org/foo.ent", str(catalogfile))
40
41 # then
42 assert result == str(tmpdir / resultentity)
43
44
45 def test_xmlcatalog_with_system_entity_mock():
46 # Identical with test_xmlcatalog_with_system_entity, but without the
47 # real call
48
49 # given
50 mock_patch="gen.xmlcatalog"
51 entity = "fake.ent"
52 with mock.patch(mock_patch) as mck:
53 mck.return_value = entity
54
55 # when
56 result = gen.xmlcatalog("http://www.example.org", "fake-catalog.xml")
57
58 # then
59 assert result == entity
60 mck.assert_called_once()
61
62
63 def test_xmlcatalog_with_missing_catalog():
64 # given
65 mock_patch="gen.os.path.exists"
66
67 with mock.patch(mock_patch) as mck:
68 mck.return_value = False
69 # when/then
70 with pytest.raises(gen.XMLCatalogError):
71 gen.xmlcatalog("https://www.example.com", "fake-catalog.xml", True)
72
73
74 def test_xmlcatalog_with_error_from_xmlcatalog():
75 # given
76 mock_patch="gen.os.path.exists"
77
78 with mock.patch(mock_patch) as mck:
79 mck.return_value = True
80 #
81 with pytest.raises(gen.XMLCatalogError):
82 gen.xmlcatalog("https://www.example.com", "fake-catalog.xml", True)
83
84
85 def test_xmlcatalog_with_():
86 # given
87 mock_patch="gen.os.path.exists"
88 proc = gen.subprocess.CompletedProcess("fake", returncode=100,
89 stdout=b"", stderr=None)
90
91 with mock.patch(mock_patch) as mck:
92 with mock.patch("gen.subprocess.run") as mock_subprocess:
93 mck.return_value = True
94 mock_subprocess.return_value = proc
95 assert gen.xmlcatalog("https://www.example.com", "fake-catalog.xml") is None