Merge pull request #3 from joakim-hove/python-fixup

Use python pushd
This commit is contained in:
Håkon Hægland 2020-11-05 09:37:09 +01:00 committed by GitHub
commit eee2a92e39
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,6 +4,16 @@ from contextlib import contextmanager
from pathlib import Path from pathlib import Path
from opm2.simulators import BlackOilSimulator from opm2.simulators import BlackOilSimulator
@contextmanager
def pushd(path):
cwd = os.getcwd()
if not os.path.isdir(path):
os.makedirs(path)
os.chdir(path)
yield
os.chdir(cwd)
class TestBasic(unittest.TestCase): class TestBasic(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
@ -44,23 +54,15 @@ class TestBasic(unittest.TestCase):
# in which each test_xxx() method is called by unittest is not defined). # in which each test_xxx() method is called by unittest is not defined).
# However, as noted above this is not currently possible. # However, as noted above this is not currently possible.
# #
test_dir = Path(os.path.dirname(__file__)) cls.data_dir = os.path.join( os.path.dirname(__file__), "test_data/SPE1CASE1")
cls.data_dir = test_dir.parent.joinpath("test_data/SPE1CASE1")
with TestBasic.pushd(cls.data_dir):
cls.sim = BlackOilSimulator( 'SPE1CASE1.DATA' )
def setUp(self):
self.saved_cwd = os.getcwd()
os.chdir(self.data_dir)
self.sim.step_init()
self.sim.step()
def tearDown(self):
self.sim.step_cleanup()
os.chdir(self.saved_cwd)
def test_all(self): def test_all(self):
sim = self.sim with self.pushd(cls.data_dir):
sim = BlackOilSimulator("SPE1CASE1.DATA")
sim.step_init()
self.sim.step()
poro = sim.get_porosity() poro = sim.get_porosity()
self.assertEqual(len(poro), 300, 'length of porosity vector') self.assertEqual(len(poro), 300, 'length of porosity vector')
self.assertAlmostEqual(poro[0], 0.3, places=7, msg='value of porosity') self.assertAlmostEqual(poro[0], 0.3, places=7, msg='value of porosity')
@ -70,12 +72,3 @@ class TestBasic(unittest.TestCase):
poro2 = sim.get_porosity() poro2 = sim.get_porosity()
self.assertAlmostEqual(poro2[0], 0.285, places=7, msg='value of porosity 2') self.assertAlmostEqual(poro2[0], 0.285, places=7, msg='value of porosity 2')
@classmethod
@contextmanager
def pushd(cls, path):
cwd = os.getcwd()
if not os.path.isdir(path):
os.makedirs(path)
os.chdir(path)
yield
os.chdir(cwd)