Skip to content

Commit 4a3e56b

Browse files
committed
[test] Add and use common resource dir path
1 parent 2a12879 commit 4a3e56b

11 files changed

+62
-73
lines changed

test/test_doc.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from odml import Document, Section, Property
1313
from odml.doc import BaseDocument
1414
from odml.dtypes import FORMAT_DATE
15-
from .util import ODML_CACHE_DIR as CACHE_DIR
15+
from .util import ODML_CACHE_DIR as CACHE_DIR, TEST_RESOURCES_DIR as RES_DIR
1616

1717

1818
class TestSection(unittest.TestCase):
@@ -109,8 +109,7 @@ def test_date(self):
109109
doc.date = "some format"
110110

111111
def test_get_terminology_equivalent(self):
112-
dir_path = os.path.dirname(os.path.realpath(__file__))
113-
repo_file = os.path.join(dir_path, "resources", self.local_repo_file)
112+
repo_file = os.path.join(RES_DIR, self.local_repo_file)
114113
local_url = "file://%s" % pathname2url(repo_file)
115114

116115
doc = Document(repository=local_url)

test/test_fileio.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,44 @@
1-
import unittest
2-
import sys
31
import os
4-
import odml
2+
import sys
3+
import unittest
54

65
try:
76
from StringIO import StringIO
87
except ImportError:
98
from io import StringIO
109

10+
import odml
11+
12+
from .util import TEST_RESOURCES_DIR as RES_DIR
13+
1114

1215
class TestTypes(unittest.TestCase):
1316

1417
def setUp(self):
15-
self.dir_path = os.path.dirname(os.path.realpath(__file__))
16-
self.file = os.path.join(self.dir_path, 'resources', 'example.odml')
18+
self.file = os.path.join(RES_DIR, "example.odml")
1719
# Do not allow anything to be printed on STDOUT
1820
self.captured_stdout = StringIO()
1921
sys.stdout = self.captured_stdout
2022

2123
def test_load_save(self):
2224
doc = odml.load(self.file)
2325
self.assertTrue(isinstance(doc, odml.doc.BaseDocument))
24-
odml.save(doc, self.file + '_copy')
25-
os.remove(self.file + '_copy')
26+
file_name = "%s_copy" % self.file
27+
odml.save(doc, file_name)
28+
os.remove(file_name)
2629

2730
def test_display(self):
2831
doc = odml.load(self.file)
2932
odml.display(doc)
3033

3134
def test_invalid_parser(self):
3235
with self.assertRaises(NotImplementedError):
33-
odml.load(self.file, 'html')
36+
odml.load(self.file, "html")
3437

3538
doc = odml.load(self.file)
3639
with self.assertRaises(NotImplementedError):
37-
odml.save(doc, self.file + '_copy_html', 'html')
40+
file_name = "%s_copy_html" % self.file
41+
odml.save(doc, file_name, "html")
3842

3943
with self.assertRaises(NotImplementedError):
40-
odml.display(doc, 'html')
44+
odml.display(doc, "html")

test/test_parser_json.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from odml.tools import dict_parser
1212
from odml.tools.parser_utils import ParserException, InvalidVersionException
13-
from .util import create_test_dir
13+
from .util import create_test_dir, TEST_RESOURCES_DIR as RES_DIR
1414

1515

1616
_INVALID_ATTRIBUTE_HANDLING_DOC = """
@@ -110,11 +110,8 @@
110110
class TestJSONParser(unittest.TestCase):
111111

112112
def setUp(self):
113-
dir_path = os.path.dirname(os.path.realpath(__file__))
114-
self.basepath = os.path.join(dir_path, "resources")
115-
113+
self.base_path = RES_DIR
116114
self.json_reader = dict_parser.DictReader(show_warnings=False)
117-
118115
self.tmp_dir_path = create_test_dir(__file__)
119116

120117
def tearDown(self):
@@ -136,7 +133,7 @@ def test_missing_root(self):
136133
filename = "missing_root.json"
137134
message = "Missing root element"
138135

139-
with open(os.path.join(self.basepath, filename)) as json_data:
136+
with open(os.path.join(self.base_path, filename)) as json_data:
140137
parsed_doc = json.load(json_data)
141138

142139
with self.assertRaises(ParserException) as exc:
@@ -148,7 +145,7 @@ def test_missing_version(self):
148145
filename = "missing_version.json"
149146
message = "Could not find odml-version"
150147

151-
with open(os.path.join(self.basepath, filename)) as json_data:
148+
with open(os.path.join(self.base_path, filename)) as json_data:
152149
parsed_doc = json.load(json_data)
153150

154151
with self.assertRaises(ParserException) as exc:
@@ -159,7 +156,7 @@ def test_missing_version(self):
159156
def test_invalid_version(self):
160157
filename = "invalid_version.json"
161158

162-
with open(os.path.join(self.basepath, filename)) as json_data:
159+
with open(os.path.join(self.base_path, filename)) as json_data:
163160
parsed_doc = json.load(json_data)
164161

165162
with self.assertRaises(InvalidVersionException):

test/test_parser_odml.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,14 @@
99

1010
from odml import Document, Section, Property
1111
from odml.tools import odmlparser
12-
from .util import create_test_dir
12+
from .util import create_test_dir, TEST_RESOURCES_DIR as RES_DIR
1313

1414

1515
class TestOdmlParser(unittest.TestCase):
1616

1717
def setUp(self):
1818
# Set up test environment
19-
dir_path = os.path.dirname(os.path.realpath(__file__))
20-
self.basefile = os.path.join(dir_path, "resources", "example.odml")
21-
19+
base_file = os.path.join(RES_DIR, "example.odml")
2220
self.tmp_dir = create_test_dir(__file__)
2321

2422
self.json_file = os.path.join(self.tmp_dir, "test.json")
@@ -36,7 +34,7 @@ def setUp(self):
3634
self.json_writer = odmlparser.ODMLWriter(parser='JSON')
3735
self.rdf_writer = odmlparser.ODMLWriter(parser='RDF')
3836

39-
self.odml_doc = self.xml_reader.from_file(self.basefile)
37+
self.odml_doc = self.xml_reader.from_file(base_file)
4038

4139
def tearDown(self):
4240
if os.path.exists(self.tmp_dir):

test/test_parser_xml.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33

44
from odml.tools import xmlparser
55
from odml.tools.parser_utils import ParserException, InvalidVersionException
6+
from .util import TEST_RESOURCES_DIR as RES_DIR
67

78

89
class TestXMLParser(unittest.TestCase):
910

1011
def setUp(self):
11-
dir_path = os.path.dirname(os.path.realpath(__file__))
12-
self.basepath = os.path.join(dir_path, "resources")
12+
self.base_path = RES_DIR
1313

1414
self.xml_reader = xmlparser.XMLReader()
1515
self.xml_reader_ignore = xmlparser.XMLReader(ignore_errors=True)
@@ -19,7 +19,7 @@ def test_invalid_root(self):
1919
message = "Expecting <odML>"
2020

2121
with self.assertRaises(ParserException) as exc:
22-
_ = self.xml_reader.from_file(os.path.join(self.basepath, filename))
22+
_ = self.xml_reader.from_file(os.path.join(self.base_path, filename))
2323

2424
self.assertIn(message, str(exc.exception))
2525

@@ -28,21 +28,21 @@ def test_missing_version(self):
2828
message = "Could not find format version attribute"
2929

3030
with self.assertRaises(ParserException) as exc:
31-
_ = self.xml_reader.from_file(os.path.join(self.basepath, filename))
31+
_ = self.xml_reader.from_file(os.path.join(self.base_path, filename))
3232

3333
self.assertIn(message, str(exc.exception))
3434

3535
def test_invalid_version(self):
3636
filename = "invalid_version.xml"
3737

3838
with self.assertRaises(InvalidVersionException):
39-
_ = self.xml_reader.from_file(os.path.join(self.basepath, filename))
39+
_ = self.xml_reader.from_file(os.path.join(self.base_path, filename))
4040

4141
def test_ignore_errors(self):
4242
filename = "ignore_errors.xml"
4343

4444
with self.assertRaises(ParserException):
45-
_ = self.xml_reader.from_file(os.path.join(self.basepath, filename))
45+
_ = self.xml_reader.from_file(os.path.join(self.base_path, filename))
4646

47-
doc = self.xml_reader_ignore.from_file(os.path.join(self.basepath, filename))
47+
doc = self.xml_reader_ignore.from_file(os.path.join(self.base_path, filename))
4848
doc.pprint()

test/test_parser_yaml.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from odml.tools import dict_parser
1212
from odml.tools.parser_utils import ParserException, InvalidVersionException
13-
from .util import create_test_dir
13+
from .util import create_test_dir, TEST_RESOURCES_DIR as RES_DIR
1414

1515

1616
_INVALID_ATTRIBUTE_HANDLING_DOC = """
@@ -70,11 +70,8 @@
7070
class TestYAMLParser(unittest.TestCase):
7171

7272
def setUp(self):
73-
dir_path = os.path.dirname(os.path.realpath(__file__))
74-
self.basepath = os.path.join(dir_path, "resources")
75-
73+
self.base_path = RES_DIR
7674
self.yaml_reader = dict_parser.DictReader(show_warnings=False)
77-
7875
self.tmp_dir_path = create_test_dir(__file__)
7976

8077
def tearDown(self):
@@ -96,7 +93,7 @@ def test_missing_root(self):
9693
filename = "missing_root.yaml"
9794
message = "Missing root element"
9895

99-
with open(os.path.join(self.basepath, filename)) as raw_data:
96+
with open(os.path.join(self.base_path, filename)) as raw_data:
10097
parsed_doc = yaml.safe_load(raw_data)
10198

10299
with self.assertRaises(ParserException) as exc:
@@ -108,7 +105,7 @@ def test_missing_version(self):
108105
filename = "missing_version.yaml"
109106
message = "Could not find odml-version"
110107

111-
with open(os.path.join(self.basepath, filename)) as raw_data:
108+
with open(os.path.join(self.base_path, filename)) as raw_data:
112109
parsed_doc = yaml.safe_load(raw_data)
113110

114111
with self.assertRaises(ParserException) as exc:
@@ -119,7 +116,7 @@ def test_missing_version(self):
119116
def test_invalid_version(self):
120117
filename = "invalid_version.yaml"
121118

122-
with open(os.path.join(self.basepath, filename)) as raw_data:
119+
with open(os.path.join(self.base_path, filename)) as raw_data:
123120
parsed_doc = yaml.safe_load(raw_data)
124121

125122
with self.assertRaises(InvalidVersionException):

test/test_validation.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import odml.validation
77
import odml.terminology
88
from . import test_samplefile as samplefile
9+
from .util import TEST_RESOURCES_DIR as RES_DIR
910

1011
try:
1112
from StringIO import StringIO
@@ -19,7 +20,6 @@ class TestValidation(unittest.TestCase):
1920

2021
def setUp(self):
2122
self.doc = samplefile.SampleFileCreator().create_document()
22-
self.dir_path = os.path.dirname(os.path.realpath(__file__))
2323

2424
@staticmethod
2525
def filter_repository_errors(errors):
@@ -405,7 +405,7 @@ def test_load_section_xml(self):
405405
Test if loading xml document raises validation errors for Sections with undefined type.
406406
"""
407407

408-
path = os.path.join(self.dir_path, "resources", "validation_section.xml")
408+
path = os.path.join(RES_DIR, "validation_section.xml")
409409
doc = odml.load(path)
410410

411411
self.load_section_validation(doc)
@@ -415,7 +415,7 @@ def test_load_section_json(self):
415415
Test if loading json document raises validation errors for Sections with undefined type.
416416
"""
417417

418-
path = os.path.join(self.dir_path, "resources", "validation_section.json")
418+
path = os.path.join(RES_DIR, "validation_section.json")
419419
doc = odml.load(path, "JSON")
420420

421421
self.load_section_validation(doc)
@@ -425,7 +425,7 @@ def test_load_section_yaml(self):
425425
Test if loading yaml document raises validation errors for Sections with undefined type.
426426
"""
427427

428-
path = os.path.join(self.dir_path, "resources", "validation_section.yaml")
428+
path = os.path.join(RES_DIR, "validation_section.yaml")
429429
doc = odml.load(path, "YAML")
430430

431431
self.load_section_validation(doc)
@@ -459,7 +459,7 @@ def test_load_dtypes_xml(self):
459459
for Properties with undefined dtypes.
460460
"""
461461

462-
path = os.path.join(self.dir_path, "resources", "validation_dtypes.xml")
462+
path = os.path.join(RES_DIR, "validation_dtypes.xml")
463463
doc = odml.load(path)
464464
self.load_dtypes_validation(doc)
465465

@@ -469,7 +469,7 @@ def test_load_dtypes_json(self):
469469
for Properties with undefined dtypes.
470470
"""
471471

472-
path = os.path.join(self.dir_path, "resources", "validation_dtypes.json")
472+
path = os.path.join(RES_DIR, "validation_dtypes.json")
473473
doc = odml.load(path, "JSON")
474474
self.load_dtypes_validation(doc)
475475

@@ -479,6 +479,6 @@ def test_load_dtypes_yaml(self):
479479
for Properties with undefined dtypes.
480480
"""
481481

482-
path = os.path.join(self.dir_path, "resources", "validation_dtypes.yaml")
482+
path = os.path.join(RES_DIR, "validation_dtypes.yaml")
483483
doc = odml.load(path, "YAML")
484484
self.load_dtypes_validation(doc)

0 commit comments

Comments
 (0)