Skip to content

Commit e6839ba

Browse files
Merge pull request #93 from baagaard-usgs/update-pytests
Update pytests to use load_tests. Streamline test setup.
2 parents a6203d3 + 6e1f766 commit e6839ba

36 files changed

+328
-287
lines changed

spatialdata/Makefile.am

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ EXTRA_DIST = \
4242
spatialdb/generator/Shaper.py \
4343
spatialdb/generator/Value.py \
4444
spatialdb/generator/__init__.py \
45+
testing/__init__.py \
46+
testing/UnitTestApp.py \
47+
testing/TestCases.py \
4548
units/NondimElasticDynamic.py \
4649
units/NondimElasticQuasistatic.py \
4750
units/Nondimensional.py \

spatialdata/spatialdb/SimpleDB.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ class SimpleDB(SpatialDBObj, ModuleSimpleDB):
3535
queryType.meta['tip'] = "Type of query to perform."
3636

3737
from .SimpleIOAscii import SimpleIOAscii
38-
iohandler = pythia.pyre.inventory.facility("iohandler", family="simpledb_io",
39-
factory=SimpleIOAscii)
38+
iohandler = pythia.pyre.inventory.facility("iohandler", family="simpledb_io", factory=SimpleIOAscii)
4039
iohandler.meta['tip'] = "I/O handler for database."
4140

4241
# PUBLIC METHODS /////////////////////////////////////////////////////

spatialdata/spatialdb/SimpleGridDB.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
# See https://mit-license.org/ and LICENSE.md and for license information.
99
# =================================================================================================
1010

11+
import pathlib
12+
1113
from .SpatialDBObj import SpatialDBObj
1214
from .spatialdb import SimpleGridDB as ModuleSimpleGridDB
1315

@@ -18,10 +20,8 @@ def validateFilename(value):
1820
"""
1921
if 0 == len(value):
2022
raise ValueError("Name of SimpleGridDB file must be specified.")
21-
try:
22-
fin = open(value, "r")
23-
except IOError:
24-
raise IOError("Spatial database file '{}' not found.".format(value))
23+
if not pathlib.Path(value).is_file():
24+
raise IOError(f"Spatial database file '{value}' not found.")
2525
return value
2626

2727

spatialdata/spatialdb/SimpleIO.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,7 @@
88
# See https://mit-license.org/ and LICENSE.md and for license information.
99
# =================================================================================================
1010

11-
# @file spatialdata/spatialdb/SimpleIO.py
12-
#
13-
# @brief Python I/O manager for simple spatial database (SimpleDB).
14-
#
15-
# Factory: simpledb_io
11+
import pathlib
1612

1713
from pythia.pyre.components.Component import Component
1814

@@ -23,10 +19,8 @@ def validateFilename(value):
2319
"""
2420
if 0 == len(value):
2521
raise ValueError("Filename for spatial database not specified.")
26-
try:
27-
fin = open(value, "r")
28-
except IOError:
29-
raise IOError("Spatial database file '{}' not found.".format(value))
22+
if not pathlib.Path(value).is_file():
23+
raise IOError(f"Spatial database file '{value}' not found.")
3024
return value
3125

3226

spatialdata/spatialdb/TimeHistory.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
# See https://mit-license.org/ and LICENSE.md and for license information.
99
# =================================================================================================
1010

11+
import pathlib
1112

1213
from pythia.pyre.components.Component import Component
1314
from .spatialdb import TimeHistory as ModuleTimeHistory
@@ -19,10 +20,8 @@ def validateFilename(value):
1920
"""
2021
if 0 == len(value):
2122
raise ValueError("Name of TimeHistoryDB file must be specified.")
22-
try:
23-
fin = open(value, "r")
24-
except IOError:
25-
raise IOError("Temporal database file '{}' not found.".format(value))
23+
if not pathlib.Path(value).is_file():
24+
raise IOError(f"Temporal database file '{value}' not found.")
2625
return value
2726

2827

spatialdata/testing/TestCases.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# =================================================================================================
2+
# This code is part of SpatialData, developed through the Computational Infrastructure
3+
# for Geodynamics (https://github.com/geodynamics/spatialdata).
4+
#
5+
# Copyright (c) 2010-2023, University of California, Davis and the SpatialData Development Team.
6+
# All rights reserved.
7+
#
8+
# See https://mit-license.org/ and LICENSE.md and for license information.
9+
# =================================================================================================
10+
"""PyLith module for unit test helper functions."""
11+
12+
import unittest
13+
14+
def make_suite(test_classes, loader=unittest.defaultTestLoader):
15+
suite = unittest.TestSuite()
16+
for cls in test_classes:
17+
suite.addTests(loader.loadTestsFromTestCase(cls))
18+
return suite
19+
20+
21+
# End of file

tests/pytests/UnitTestApp.py renamed to spatialdata/testing/UnitTestApp.py

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
#
88
# See https://mit-license.org/ and LICENSE.md and for license information.
99
# =================================================================================================
10-
# @brief Python application for Python unit tests.
11-
#
12-
# We place this script in tests/pytests because we must initialize coverage
13-
# *before* importing any spatialdata modules.
10+
"""Application for running Python unit tests with code coverage.
11+
12+
We place this script in tests/pytests so that we can import it into the individual scripts in the
13+
subdirectories.
14+
"""
15+
1416

1517
import unittest
1618

@@ -19,16 +21,18 @@ class UnitTestApp():
1921
"""
2022
Test application.
2123
"""
22-
cov = None
23-
try:
24-
import coverage
25-
cov = coverage.Coverage(source=["spatialdata"])
26-
except ImportError:
27-
pass
28-
29-
# PUBLIC METHODS /////////////////////////////////////////////////////
30-
31-
def main(self):
24+
test_modules = []
25+
26+
def __init__(self, test_modules, src_dirs=["spatialdata"]):
27+
self.cov = None
28+
self.test_modules = test_modules
29+
try:
30+
import coverage
31+
self.cov = coverage.Coverage(source=src_dirs)
32+
except ImportError:
33+
pass
34+
35+
def run(self):
3236
"""
3337
Run the application.
3438
"""
@@ -40,10 +44,19 @@ def main(self):
4044
if self.cov:
4145
self.cov.stop()
4246
self.cov.save()
47+
self.cov.report()
4348

4449
if not success:
4550
import sys
4651
sys.exit(1)
4752

4853

54+
def _suite(self):
55+
loader = unittest.defaultTestLoader
56+
suite = unittest.TestSuite()
57+
for mod in self.test_modules:
58+
suite.addTests(loader.loadTestsFromModule(mod))
59+
return suite
60+
61+
4962
# End of file

spatialdata/testing/__init__.py

Whitespace-only changes.

tests/pytests/Makefile.am

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,5 @@ SUBDIRS = \
1414
units \
1515
utils
1616

17-
noinst_PYTHON = UnitTestApp.py
18-
1917

2018
# End of file

tests/pytests/geocoords/TestCSCart.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import unittest
1414

15+
from spatialdata.testing.TestCases import make_suite
1516
from spatialdata.geocoords.CSCart import CSCart
1617

1718

@@ -32,4 +33,13 @@ def test_accessors(self):
3233
self.assertEqual(2, cs.getSpaceDim())
3334

3435

36+
def load_tests(loader, tests, pattern):
37+
TEST_CLASSES = [TestCSCart]
38+
return make_suite(TEST_CLASSES, loader)
39+
40+
41+
if __name__ == "__main__":
42+
unittest.main(verbosity=2)
43+
44+
3545
# End of file

0 commit comments

Comments
 (0)