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 2, 2021
1 parent 7922a81 commit 48de1ee
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 110 deletions.
30 changes: 14 additions & 16 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 @@ -125,13 +124,6 @@ def setUpClass(cls):
utilities.CanteraTest.setUpClass()
cls.gas = ct.Solution('h2o2.yaml', transport_model=None)

def tearDown(self):
temp_files = ["solutionarray.csv", "solutionarray.h5"]
for f in temp_files:
fpath = Path(self.test_work_dir).joinpath(f)
if fpath.is_file():
fpath.unlink()

def test_collect_data(self):
states = ct.SolutionArray(self.gas)
collected = states.collect_data(tabular=True)
Expand All @@ -149,7 +141,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 +158,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 +168,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 +185,9 @@ 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"
if outfile.is_file():
outfile.unlink()

extra = {'foo': range(7), 'bar': range(7)}
meta = {'spam': 'eggs', 'hello': 'world'}
Expand Down Expand Up @@ -236,7 +230,9 @@ 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"
if outfile.is_file():
outfile.unlink()

states = ct.SolutionArray(self.gas, 3, extra={'spam': 'eggs'})
states.write_hdf(outfile, mode='w')
Expand All @@ -247,7 +243,9 @@ 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"
if outfile.is_file():
outfile.unlink()

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 +494,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
7 changes: 3 additions & 4 deletions interfaces/cython/cantera/test/test_equilibrium.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import unittest
from os.path import join as pjoin

import numpy as np

Expand Down Expand Up @@ -153,7 +152,7 @@ def test_equil_TP(self):

data[i,1:] = self.mix.species_moles

self.compare(data, pjoin(self.test_data_dir, 'koh-equil-TP.csv'))
self.compare(data, self.test_data_path / "koh-equil-TP.csv")

@utilities.slow_test
def test_equil_HP(self):
Expand All @@ -177,7 +176,7 @@ def test_equil_HP(self):
data[i,1] = self.mix.T # equilibrated temperature
data[i,2:] = self.mix.species_moles

self.compare(data, pjoin(self.test_data_dir, 'koh-equil-HP.csv'))
self.compare(data, self.test_data_path / "koh-equil-HP.csv")


class TestEquil_GasCarbon(utilities.CanteraTest):
Expand Down Expand Up @@ -207,7 +206,7 @@ def solve(self, solver, **kwargs):
data[i,:2] = (phi[i], mix.T)
data[i,2:] = mix.species_moles

self.compare(data, pjoin(self.test_data_dir, 'gas-carbon-equil.csv'))
self.compare(data, self.test_data_path / "gas-carbon-equil.csv")

@utilities.slow_test
def test_gibbs(self):
Expand Down
13 changes: 4 additions & 9 deletions interfaces/cython/cantera/test/test_kinetics.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import numpy as np
import re
import itertools
from os.path import join as pjoin
import os

import cantera as ct
from . import utilities
Expand Down Expand Up @@ -816,7 +814,7 @@ def cathode_curr(E):
cathode_bulk.electric_potential -
anode_bulk.electric_potential])

self.compare(data, pjoin(self.test_data_dir, 'sofc-test.csv'), rtol=1e-7)
self.compare(data, self.test_data_path / "sofc-test.csv", rtol=1e-7)


class TestDuplicateReactions(utilities.CanteraTest):
Expand Down Expand Up @@ -889,8 +887,7 @@ def test_fromCti(self):

def test_fromXml(self):
import xml.etree.ElementTree as ET
p = os.path.dirname(__file__)
root = ET.parse(pjoin(p, '..', 'data', 'h2o2.xml')).getroot()
root = ET.parse(self.cantera_data_path / "h2o2.xml").getroot()
rxn_node = root.find('.//reaction[@id="0001"]')
r = ct.Reaction.fromXml(ET.tostring(rxn_node))

Expand Down Expand Up @@ -925,17 +922,15 @@ def test_listFromFile(self):

def test_listFromCti(self):
gas = ct.Solution("h2o2.xml", transport_model=None)
p = os.path.dirname(__file__)
with open(pjoin(p, '..', 'data', 'h2o2.cti')) as f:
with open(self.cantera_data_path / "h2o2.cti") as f:
R = ct.Reaction.listFromCti(f.read())
eq1 = [r.equation for r in R]
eq2 = [r.equation for r in gas.reactions()]
self.assertEqual(eq1, eq2)

def test_listFromXml(self):
gas = ct.Solution("h2o2.xml", transport_model=None)
p = os.path.dirname(__file__)
with open(pjoin(p, '..', 'data', 'h2o2.xml')) as f:
with open(self.cantera_data_path / "h2o2.xml") as f:
R = ct.Reaction.listFromXml(f.read())
eq1 = [r.equation for r in R]
eq2 = [r.equation for r in gas.reactions()]
Expand Down
Loading

0 comments on commit 48de1ee

Please sign in to comment.