Skip to content

Commit

Permalink
[Tests] Simplify access to work and data folders
Browse files Browse the repository at this point in the history
  • Loading branch information
ischoegl committed Jun 1, 2021
1 parent f6965b7 commit 7f58c26
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 42 deletions.
19 changes: 9 additions & 10 deletions interfaces/cython/cantera/test/test_composite.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import sys
from pathlib import Path

import numpy as np
from collections import OrderedDict
Expand All @@ -15,7 +14,7 @@ class TestModels(utilities.CanteraTest):
@classmethod
def setUpClass(cls):
utilities.CanteraTest.setUpClass()
cls.yml_file = Path(cls.test_data_dir).joinpath("thermo-models.yaml")
cls.yml_file = cls.test_data_path / "thermo-models.yaml"
cls.yml = utilities.load_yaml(cls.yml_file)

def test_load_thermo_models(self):
Expand Down Expand Up @@ -128,7 +127,7 @@ def setUpClass(cls):
def tearDown(self):
temp_files = ["solutionarray.csv", "solutionarray.h5"]
for f in temp_files:
fpath = Path(self.test_work_dir).joinpath(f)
fpath = self.test_work_path / f
if fpath.is_file():
fpath.unlink()

Expand All @@ -149,7 +148,7 @@ def test_write_csv(self):
states.TPX = np.linspace(300, 1000, 7), 2e5, 'H2:0.5, O2:0.4'
states.equilibrate('HP')

outfile = Path(self.test_work_dir).joinpath("solutionarray.csv")
outfile = self.test_work_path / "solutionarray.csv"
states.write_csv(outfile)

data = np.genfromtxt(outfile, names=True, delimiter=',')
Expand All @@ -166,7 +165,7 @@ def test_write_csv(self):
def test_write_csv_str_column(self):
states = ct.SolutionArray(self.gas, 3, extra={'spam': 'eggs'})

outfile = Path(self.test_work_dir).joinpath("solutionarray.csv")
outfile = self.test_work_path / "solutionarray.csv"
states.write_csv(outfile)

b = ct.SolutionArray(self.gas, extra={'spam'})
Expand All @@ -176,7 +175,7 @@ def test_write_csv_str_column(self):
def test_write_csv_multidim_column(self):
states = ct.SolutionArray(self.gas, 3, extra={'spam': np.zeros((3, 5,))})

outfile = Path(self.test_work_dir).joinpath("solutionarray.csv")
outfile = self.test_work_path / "solutionarray.csv"
with self.assertRaisesRegex(NotImplementedError, 'not supported'):
states.write_csv(outfile)

Expand All @@ -193,7 +192,7 @@ def test_to_pandas(self):

@utilities.unittest.skipIf(isinstance(_h5py, ImportError), "h5py is not installed")
def test_write_hdf(self):
outfile = Path(self.test_work_dir).joinpath("solutionarray.h5")
outfile = self.test_work_path / "solutionarray.h5"

extra = {'foo': range(7), 'bar': range(7)}
meta = {'spam': 'eggs', 'hello': 'world'}
Expand Down Expand Up @@ -236,7 +235,7 @@ def test_write_hdf(self):

@utilities.unittest.skipIf(isinstance(_h5py, ImportError), "h5py is not installed")
def test_write_hdf_str_column(self):
outfile = Path(self.test_work_dir).joinpath("solutionarray.h5")
outfile = self.test_work_path / "solutionarray.h5"

states = ct.SolutionArray(self.gas, 3, extra={'spam': 'eggs'})
states.write_hdf(outfile, mode='w')
Expand All @@ -247,7 +246,7 @@ def test_write_hdf_str_column(self):

@utilities.unittest.skipIf(isinstance(_h5py, ImportError), "h5py is not installed")
def test_write_hdf_multidim_column(self):
outfile = Path(self.test_work_dir).joinpath("solutionarray.h5")
outfile = self.test_work_path / "solutionarray.h5"

states = ct.SolutionArray(self.gas, 3, extra={'spam': [[1, 2], [3, 4], [5, 6]]})
states.write_hdf(outfile, mode='w')
Expand Down Expand Up @@ -496,7 +495,7 @@ def test_yaml_outunits(self):
units = {'length': 'cm', 'quantity': 'mol', 'energy': 'cal'}
gas.write_yaml('h2o2-generated.yaml', units=units)
generated = utilities.load_yaml("h2o2-generated.yaml")
original = utilities.load_yaml(Path(self.cantera_data).joinpath("h2o2.yaml"))
original = utilities.load_yaml(self.cantera_data_path / "h2o2.yaml")
self.assertEqual(generated['units'], units)

for r1, r2 in zip(original['reactions'], generated['reactions']):
Expand Down
24 changes: 12 additions & 12 deletions interfaces/cython/cantera/test/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ def convert(self, inputFile, thermo=None, transport=None,
if output is None:
output = Path(inputFile).stem # strip '.inp'
if inputFile is not None:
inputFile = Path(self.test_data_dir).joinpath(inputFile)
inputFile = self.test_data_path / inputFile
if thermo is not None:
thermo = Path(self.test_data_dir).joinpath(thermo)
thermo = self.test_data_path / thermo
if transport is not None:
transport = Path(self.test_data_dir).joinpath(transport)
transport = self.test_data_path / transport
if surface is not None:
surface = Path(self.test_data_dir).joinpath(surface)
surface = self.test_data_path / surface
if extra is not None:
extra = Path(self.test_data_dir).joinpath(extra)
output = Path(self.test_work_dir).joinpath(output + self.ext)
extra = self.test_data_path / extra
output = self.test_work_path / (output + self.ext)
if output.is_file():
output.unlink()
self._convert(inputFile, thermo=thermo, transport=transport,
Expand Down Expand Up @@ -359,14 +359,14 @@ def test_empty_reaction_section(self):

def test_reaction_comments1(self):
output = self.convert('pdep-test.inp')
text = Path(output).read_text()
text = output.read_text()
self.assertIn('Generic mechanism header', text)
self.assertIn('Single PLOG reaction', text)
self.assertIn('Multiple PLOG expressions at the same pressure', text)

def test_reaction_comments2(self):
output = self.convert('explicit-third-bodies.inp', thermo='dummy-thermo.dat')
text = Path(output).read_text()
text = output.read_text()
self.assertIn('An end of line comment', text)
self.assertIn('A comment after the last reaction', text)

Expand Down Expand Up @@ -470,7 +470,7 @@ def test_extra(self):
transport='gri30_tran.dat', output='gri30_extra',
extra='extra.yaml')

output = Path(self.test_work_dir).joinpath("gri30_extra" + self.ext)
output = self.test_work_path / ("gri30_extra" + self.ext)
yml = utilities.load_yaml(output)

desc = yml['description'].split('\n')[-1]
Expand All @@ -481,7 +481,7 @@ def test_extra(self):

def test_sri_zero(self):
self.convert('sri_convert_test.txt')
output = Path(self.test_work_dir).joinpath("sri_convert_test" + self.ext)
output = self.test_work_path / ("sri_convert_test" + self.ext)
mech = utilities.load_yaml(output)
D = mech['reactions'][0]['SRI']['D']
E = mech['reactions'][0]['SRI']['E']
Expand Down Expand Up @@ -569,7 +569,7 @@ def test_long_source_input(self):

gas = ct.Solution('pdep-test.yaml')

with open(Path(self.test_data_dir).joinpath("pdep-test.cti"), "r") as f:
with open(self.test_data_path / "pdep-test.cti", "r") as f:
data = f.read()
data_size_2048kB = data + ' '*2048*1024
gas2 = ct.Solution(source=data_size_2048kB)
Expand All @@ -584,7 +584,7 @@ def test_short_source_input(self):

gas = ct.Solution('pdep-test.yaml')

with open(Path(self.test_data_dir).joinpath("pdep-test.cti"), "r") as f:
with open(self.test_data_path / "pdep-test.cti", "r") as f:
data = f.read()
data_size_32kB = data + ' '*18000
gas2 = ct.Solution(source=data_size_32kB)
Expand Down
45 changes: 25 additions & 20 deletions interfaces/cython/cantera/test/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import tempfile
import unittest
import errno
from pathlib import Path

try:
import ruamel_yaml as yaml
Expand All @@ -31,36 +32,40 @@ def setUpClass(cls):
# an in-source test, create the directory in the root
# test/work directory. Otherwise, create a system level
# temporary directory
root_dir = os.path.abspath(os.path.join(os.path.dirname(__file__),
'..', '..', '..', '..'))
if os.path.exists(os.path.join(root_dir, 'SConstruct')):
cls.test_work_dir = os.path.join(root_dir, 'test', 'work', 'python')
root_dir = Path(__file__).parents[4].resolve()
if (root_dir / "Sconstruct").is_file():
cls.test_work_path = root_dir / "test" / "work" / "python"
cls.test_work_dir = str(cls.test_work_path)
cls.using_tempfile = False
try:
os.makedirs(cls.test_work_dir)
except OSError as e:
if e.errno == errno.EEXIST:
pass
elif e.errno == errno.EACCES:
cls.test_work_dir = tempfile.mkdtemp()
else:
raise
cls.test_work_dir.mkdir()
except FileExistsError:
pass
except FileNotFoundError:
cls.test_work_path = Path(tempfile.mkdtemp())
except:
raise
else:
cls.test_work_dir = tempfile.mkdtemp()
cls.test_work_path = Path(tempfile.mkdtemp())
cls.using_tempfile = True

cantera.make_deprecation_warnings_fatal()
cantera.add_directory(cls.test_work_dir)
cls.test_data_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), 'data'))
cls.cantera_data = os.path.abspath(os.path.join(
os.path.dirname(__file__), '..', 'data'))
cantera.add_directory(cls.test_work_path)
cls.test_data_path = Path(__file__).parent / "data"
cls.cantera_data_path = Path(__file__).parents[1] / "data"

# retain old path strings
cls.test_work_dir = str(cls.test_work_path)
cls.test_data_dir = str(cls.test_data_path)
cls.cantera_data = str(cls.cantera_data_path)


@classmethod
def tearDownClass(cls):
# Remove the working directory after testing, but only if its a temp directory
if getattr(cls, "using_tempfile", False):
try:
shutil.rmtree(cls.test_work_dir)
shutil.rmtree(str(cls.test_work_path))
except OSError:
pass

Expand Down Expand Up @@ -101,7 +106,7 @@ def compare(self, data, reference_file, rtol=1e-8, atol=1e-12):
file if it does not exist.
"""
data = np.array(data)
if os.path.exists(reference_file):
if Path(reference_file).is_file():
# Compare with existing output file
ref = np.genfromtxt(reference_file)
self.assertEqual(data.shape, ref.shape)
Expand All @@ -110,7 +115,7 @@ def compare(self, data, reference_file, rtol=1e-8, atol=1e-12):
else:
# Generate the output file for the first time
warnings.warn('Generating test data file:' +
os.path.abspath(reference_file))
Path(reference_file).resolve())
np.savetxt(reference_file, data, fmt='%.10e')


Expand Down

0 comments on commit 7f58c26

Please sign in to comment.