Skip to content

Commit 9839dff

Browse files
committed
Add missing unit tests and get them passing (#10)
1 parent 208c4ef commit 9839dff

4 files changed

Lines changed: 77 additions & 43 deletions

File tree

neuxml/schema_data/fixtures/mods-3-4.xsd

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

pytest.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
[pytest]
2-
python_files = "test/**/*.py"
2+
python_files = "test/**.py"

test/test_catalog.py

Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Unit Test for Catalog.py. Tests for download of schemas and catalog generation"""
2+
23
# file test_xmlmap/test_core.py
34
#
45
# Copyright 2011 Emory University Libraries
@@ -19,21 +20,47 @@
1920

2021
import os
2122
import unittest
23+
from unittest.mock import patch
2224
import tempfile
2325
import glob
2426
import shutil
2527
from datetime import date
2628
import neuxml
2729
from lxml import etree
28-
from neuxml.catalog import download_schema, refresh_catalog
30+
from neuxml import catalog
31+
from requests.exceptions import HTTPError
32+
33+
34+
CORRECT_SCHEMA = "http://www.loc.gov/standards/mods/v3/mods-3-4.xsd"
35+
WRONG_SCHEMA = "http://www.loc.gov/standards/mods/v3/mods34.xsd"
36+
37+
38+
def mocked_requests_get(*args, **kwargs):
39+
class MockResponse:
40+
def __init__(self, status_code):
41+
self.status_code = status_code
42+
43+
def iter_content(self, chunk_size):
44+
with open("neuxml/schema_data/fixtures/mods-3-4.xsd", "rb") as file:
45+
while True:
46+
chunk = file.read(chunk_size)
47+
if not chunk:
48+
break
49+
yield chunk
50+
51+
def raise_for_status(self):
52+
if self.status_code != 200:
53+
raise HTTPError(response=self)
54+
55+
if args[0] == CORRECT_SCHEMA:
56+
return MockResponse(200)
57+
return MockResponse(404)
2958

3059

3160
class TestGenerateSchema(unittest.TestCase):
3261
""":class:`TestGenerateSchema` class for Catalog testing"""
3362

3463
def setUp(self):
35-
self.correct_schema = "http://www.loc.gov/standards/mods/v3/mods-3-4.xsd"
36-
self.wrong_schema = "http://www.loc.gov/standards/mods/v3/mods34.xsd"
3764
self.comment = "Downloaded by neuxml %s on %s" % (
3865
neuxml.__version__,
3966
date.today().isoformat(),
@@ -45,21 +72,24 @@ def tearDown(self):
4572
if os.path.isdir(self.path):
4673
shutil.rmtree(self.path)
4774

48-
def test_download_xml_schemas(self):
75+
@patch("requests.get", side_effect=mocked_requests_get)
76+
def test_download_xml_schemas(self, mock_get):
4977
"""Check if xsd schemas exist and download fresh copies"""
50-
filename = os.path.basename(self.correct_schema)
78+
filename = os.path.basename(CORRECT_SCHEMA)
5179
schema_path = os.path.join(self.path, filename)
5280
# do files already exist
5381
check_xsds = len(glob.glob("".join([self.path, "*.xsd"])))
5482
self.assertEqual(0, check_xsds)
5583

5684
# downloading the wrong schema
57-
response_wrong = download_schema(self.wrong_schema, schema_path, comment=None)
85+
response_wrong = catalog.download_schema(
86+
WRONG_SCHEMA, schema_path, comment=None
87+
)
5888
self.assertFalse(response_wrong)
5989

6090
# downloading the right schemas
61-
response_correct = download_schema(
62-
self.correct_schema, schema_path, comment=None
91+
response_correct = catalog.download_schema(
92+
CORRECT_SCHEMA, schema_path, comment=None
6393
)
6494
self.assertTrue(response_correct)
6595

@@ -70,7 +100,7 @@ def test_download_xml_schemas(self):
70100
self.assertFalse(b"by neuxml" in schema_string_no_comment)
71101

72102
# Add comment and check if it is there now
73-
download_schema(self.correct_schema, schema_path, comment=self.comment)
103+
catalog.download_schema(CORRECT_SCHEMA, schema_path, comment=self.comment)
74104
tree = etree.parse(schema_path)
75105
schema_string_with_comment = etree.tostring(tree)
76106
self.assertTrue(b"by neuxml" in schema_string_with_comment)
@@ -85,28 +115,31 @@ def test_generate_xml_catalog(self):
85115
check_catalog = len(glob.glob("".join([self.path, "/catalog.xml"])))
86116
self.assertEqual(0, check_catalog)
87117
catalog_file = os.path.join(self.path, "catalog.xml")
88-
filename = os.path.basename(self.correct_schema)
118+
filename = os.path.basename(CORRECT_SCHEMA)
89119
# generate empty catalog xml object
90-
catalog = refresh_catalog(
91-
xsd_schemas=[self.correct_schema],
92-
xmlcatalog_dir=self.path,
93-
xmlcatalog_file=catalog_file,
94-
)
120+
with patch.object(catalog, "download_schema", return_value=True):
121+
gen_catalog = catalog.refresh_catalog(
122+
xsd_schemas=[CORRECT_SCHEMA],
123+
xmlcatalog_dir=self.path,
124+
xmlcatalog_file=catalog_file,
125+
)
95126

96127
# check if catalog was generated
97128
check_catalog = len(glob.glob("".join([self.path, "/catalog.xml"])))
98129
self.assertEqual(1, check_catalog)
99130

100131
# check elements of generated catalog
101-
self.assertEqual("catalog", catalog.ROOT_NAME)
102-
self.assertEqual("urn:oasis:names:tc:entity:xmlns:xml:catalog", catalog.ROOT_NS)
103-
self.assertEqual({"c": catalog.ROOT_NS}, catalog.ROOT_NAMESPACES)
104-
self.assertEqual(1, len(catalog.uri_list))
132+
self.assertEqual("catalog", gen_catalog.ROOT_NAME)
133+
self.assertEqual(
134+
"urn:oasis:names:tc:entity:xmlns:xml:catalog", gen_catalog.ROOT_NS
135+
)
136+
self.assertEqual({"c": gen_catalog.ROOT_NS}, gen_catalog.ROOT_NAMESPACES)
137+
self.assertEqual(1, len(gen_catalog.uri_list))
105138

106139
# check correct name attribute
107-
self.assertEqual(self.correct_schema, catalog.uri_list[0].name)
140+
self.assertEqual(CORRECT_SCHEMA, gen_catalog.uri_list[0].name)
108141
# check correct uri attribute
109-
self.assertEqual(filename, catalog.uri_list[0].uri)
142+
self.assertEqual(filename, gen_catalog.uri_list[0].uri)
110143

111144
# check how many uris we have in catalog
112-
self.assertEqual(len(catalog.uri_list), 1)
145+
self.assertEqual(len(gen_catalog.uri_list), 1)

test/test_xpath.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818

1919
import unittest
2020

21-
from neuxml import xpath
22-
from neuxml.xpath import ast, serialize
21+
from neuxml.xpath import ast
22+
from neuxml.xpath.core import parse, serialize
2323

2424

2525
class ParseTest(unittest.TestCase):
2626
def test_nametest_step(self):
27-
xp = xpath.parse("""author""")
27+
xp = parse("""author""")
2828
self.assertTrue(isinstance(xp, ast.Step))
2929
self.assertTrue(xp.axis is None) # or should this be 'child', the default?
3030
self.assertTrue(isinstance(xp.node_test, ast.NameTest))
@@ -33,20 +33,20 @@ def test_nametest_step(self):
3333
self.assertEqual(0, len(xp.predicates))
3434

3535
def test_nodetype_step(self):
36-
xp = xpath.parse("""text()""")
36+
xp = parse("""text()""")
3737
self.assertTrue(isinstance(xp, ast.Step))
3838
self.assertTrue(isinstance(xp.node_test, ast.NodeType))
3939
self.assertEqual("text", xp.node_test.name)
4040

4141
def test_axis(self):
42-
xp = xpath.parse("""ancestor::lib:book""")
42+
xp = parse("""ancestor::lib:book""")
4343
self.assertTrue(isinstance(xp, ast.Step))
4444
self.assertEqual("ancestor", xp.axis)
4545
self.assertEqual("lib", xp.node_test.prefix)
4646
self.assertEqual("book", xp.node_test.name)
4747

4848
def test_relative_path(self):
49-
xp = xpath.parse("""book//author/first-name""")
49+
xp = parse("""book//author/first-name""")
5050
self.assertTrue(isinstance(xp, ast.BinaryExpression))
5151
self.assertTrue(isinstance(xp.left, ast.BinaryExpression))
5252
self.assertEqual("book", xp.left.left.node_test.name)
@@ -56,19 +56,19 @@ def test_relative_path(self):
5656
self.assertEqual("first-name", xp.right.node_test.name)
5757

5858
def test_absolute_path(self):
59-
xp = xpath.parse("""/book//author""")
59+
xp = parse("""/book//author""")
6060
self.assertTrue(isinstance(xp, ast.AbsolutePath))
6161
self.assertEqual("/", xp.op)
6262
self.assertEqual("book", xp.relative.left.node_test.name)
6363

6464
def test_step_predicate(self):
65-
xp = xpath.parse("""book[author]""")
65+
xp = parse("""book[author]""")
6666
self.assertEqual("book", xp.node_test.name)
6767
self.assertEqual(1, len(xp.predicates))
6868
self.assertEqual("author", xp.predicates[0].node_test.name)
6969

7070
def test_function(self):
71-
xp = xpath.parse("""author[position() = 1]""")
71+
xp = parse("""author[position() = 1]""")
7272
self.assertTrue(isinstance(xp.predicates[0], ast.BinaryExpression))
7373
self.assertEqual("=", xp.predicates[0].op)
7474
self.assertTrue(isinstance(xp.predicates[0].left, ast.FunctionCall))
@@ -77,7 +77,7 @@ def test_function(self):
7777
self.assertEqual(1, xp.predicates[0].right)
7878

7979
def test_variable(self):
80-
xp = xpath.parse("""title[substring-after(text(), $pre:separator) = "world"]""")
80+
xp = parse("""title[substring-after(text(), $pre:separator) = "world"]""")
8181
self.assertEqual("title", xp.node_test.name)
8282
self.assertTrue(isinstance(xp.predicates[0], ast.BinaryExpression))
8383
self.assertEqual("=", xp.predicates[0].op)
@@ -93,7 +93,7 @@ def test_variable(self):
9393
self.assertEqual(("pre", "separator"), xp.predicates[0].left.args[1].name)
9494

9595
def test_predicated_expression(self):
96-
xp = xpath.parse("""(book or article)[author/last-name = "Jones"]""")
96+
xp = parse("""(book or article)[author/last-name = "Jones"]""")
9797
self.assertTrue(isinstance(xp, ast.PredicatedExpression))
9898
self.assertTrue(isinstance(xp.base, ast.BinaryExpression))
9999
self.assertEqual("book", xp.base.left.node_test.name)
@@ -110,7 +110,7 @@ def test_predicated_expression(self):
110110
def test_lex_exceptions(self):
111111
# http://www.w3.org/TR/xpath/#exprlex describes several unusual
112112
# lexing rules. Verify them here.
113-
xp = xpath.parse("""***""")
113+
xp = parse("""***""")
114114
self.assertTrue(isinstance(xp, ast.BinaryExpression))
115115
self.assertEqual("*", xp.op)
116116
self.assertTrue(isinstance(xp.left, ast.Step))
@@ -120,7 +120,7 @@ def test_lex_exceptions(self):
120120
self.assertTrue(isinstance(xp.right.node_test, ast.NameTest))
121121
self.assertEqual("*", xp.right.node_test.name)
122122

123-
xp = xpath.parse("""div div div""")
123+
xp = parse("""div div div""")
124124
self.assertTrue(isinstance(xp, ast.BinaryExpression))
125125
self.assertEqual("div", xp.op)
126126
self.assertTrue(isinstance(xp.left, ast.Step))
@@ -130,12 +130,12 @@ def test_lex_exceptions(self):
130130
self.assertTrue(isinstance(xp.right.node_test, ast.NameTest))
131131
self.assertEqual("div", xp.right.node_test.name)
132132

133-
xp = xpath.parse("""div:div""")
133+
xp = parse("""div:div""")
134134
self.assertTrue(isinstance(xp, ast.Step))
135135
self.assertEqual("div", xp.node_test.prefix)
136136
self.assertEqual("div", xp.node_test.name)
137137

138-
xp = xpath.parse("""node/node()""")
138+
xp = parse("""node/node()""")
139139
self.assertTrue(isinstance(xp, ast.BinaryExpression))
140140
self.assertEqual("/", xp.op)
141141
self.assertTrue(isinstance(xp.left, ast.Step))
@@ -145,28 +145,28 @@ def test_lex_exceptions(self):
145145
self.assertTrue(isinstance(xp.right.node_test, ast.NodeType))
146146
self.assertEqual("node", xp.right.node_test.name)
147147

148-
xp = xpath.parse("""boolean(boolean)""")
148+
xp = parse("""boolean(boolean)""")
149149
self.assertTrue(isinstance(xp, ast.FunctionCall))
150150
self.assertEqual("boolean", xp.name)
151151
self.assertEqual(1, len(xp.args))
152152
self.assertTrue(isinstance(xp.args[0], ast.Step))
153153
self.assertEqual("boolean", xp.args[0].node_test.name)
154154

155-
xp = xpath.parse("""parent::parent/parent:parent""")
155+
xp = parse("""parent::parent/parent:parent""")
156156
self.assertEqual("parent", xp.left.axis)
157157
self.assertEqual("parent", xp.left.node_test.name)
158158
self.assertEqual("parent", xp.right.node_test.prefix)
159159
self.assertEqual("parent", xp.right.node_test.name)
160160

161161
def test_syntax_error(self):
162162
# try to parse invalid xpath and make sure we get an exception
163-
self.assertRaises(RuntimeError, xpath.parse, """bogus-(""")
164-
self.assertRaises(RuntimeError, xpath.parse, """/bogus-(""")
163+
self.assertRaises(RuntimeError, parse, """bogus-(""")
164+
self.assertRaises(RuntimeError, parse, """/bogus-(""")
165165

166166

167167
class TestSerializeRoundTrip(unittest.TestCase):
168168
def round_trip(self, xpath_str):
169-
xp = xpath.parse(xpath_str)
169+
xp = parse(xpath_str)
170170
self.assertEqual(xpath_str, serialize(xp))
171171

172172
def test_nametest(self):

0 commit comments

Comments
 (0)