From e51b14f6fd87b675833f34c609c0076e0ac994c9 Mon Sep 17 00:00:00 2001 From: Joakim Hove Date: Fri, 25 Oct 2019 09:48:30 +0200 Subject: [PATCH] Use utility function for path to testdata --- python/tests/test_connection.py | 8 +++++--- python/tests/test_grupnet.py | 24 ------------------------ python/tests/test_parse.py | 7 ++++--- python/tests/test_parser.py | 5 +++-- python/tests/test_props.py | 13 +++++++------ python/tests/test_schedule.py | 3 ++- python/tests/test_state.py | 9 +++++---- python/tests/test_time_vector.py | 30 +++++++++++++++--------------- python/tests/test_wells.py | 5 +++-- python/tests/utils.py | 6 ++++++ 10 files changed, 50 insertions(+), 60 deletions(-) delete mode 100644 python/tests/test_grupnet.py diff --git a/python/tests/test_connection.py b/python/tests/test_connection.py index 1ef648ba8..776457afe 100644 --- a/python/tests/test_connection.py +++ b/python/tests/test_connection.py @@ -3,7 +3,7 @@ import unittest from opm.io.parser import Parser from opm.io.ecl_state import EclipseState from opm.io.schedule import Schedule - +from utils import test_path def flowing(connection): return connection.state == 'OPEN' @@ -16,8 +16,10 @@ class TestWells(unittest.TestCase): @classmethod def setUpClass(cls): - deck = Parser().parse('tests/spe3/SPE3CASE1.DATA') + deck = Parser().parse(test_path('spe3/SPE3CASE1.DATA')) + print("Creating state") cls.state = EclipseState(deck) + print("State OK") cls.sch = Schedule(deck, cls.state) cls.timesteps = cls.sch.timesteps @@ -31,7 +33,7 @@ class TestWells(unittest.TestCase): self.assertEqual(p01, (6,6,3)) self.assertEqual(p10, (0,0,0)) self.assertEqual(p11, (0,0,1)) - + def test_connection_state(self): for timestep,_ in enumerate(self.timesteps): for well in self.sch.get_wells(timestep): diff --git a/python/tests/test_grupnet.py b/python/tests/test_grupnet.py deleted file mode 100644 index 4a77015f1..000000000 --- a/python/tests/test_grupnet.py +++ /dev/null @@ -1,24 +0,0 @@ -import sys -import unittest - -import opm.io - -class TestGrupnet(unittest.TestCase): - - """ - @classmethod - def setUpClass(cls): - norne = 'examples/data/norne/NORNE_ATW2013.DATA' - cls.sch = opm.io.parse(norne, [('PARSE_RANDOM_SLASH', opm.io.action.ignore)]).schedule - - def test_vfp_table(self): - self.assertEqual(0, self.sch.group(timestep=0)['PROD'].vfp_table_nr) - self.assertEqual(9, self.sch.group(timestep=0)['MANI-E2'].vfp_table_nr) - self.assertEqual(9, self.sch.group(timestep=247)['MANI-E2'].vfp_table_nr) - self.assertEqual(9999, self.sch.group(timestep=0)['MANI-K1'].vfp_table_nr) - self.assertEqual(0, self.sch.group(timestep=0)['INJE'].vfp_table_nr) - """ - - -if __name__ == '__main__': - unittest.main() diff --git a/python/tests/test_parse.py b/python/tests/test_parse.py index 104d46c9a..80fc411ab 100644 --- a/python/tests/test_parse.py +++ b/python/tests/test_parse.py @@ -5,6 +5,7 @@ import sys from opm.io.parser import Parser, ParseContext from opm.io.ecl_state import EclipseState +from utils import test_path class TestParse(unittest.TestCase): @@ -32,8 +33,8 @@ FIPNUM """ def setUp(self): - self.spe3fn = 'tests/spe3/SPE3CASE1.DATA' - self.norne_fname = os.path.abspath('examples/data/norne/NORNE_ATW2013.DATA') + self.spe3fn = test_path('spe3/SPE3CASE1.DATA') + self.norne_fname = test_path('../examples/data/norne/NORNE_ATW2013.DATA') def test_parse(self): deck = Parser().parse(self.spe3fn) @@ -75,7 +76,7 @@ FIPNUM parse_context = ParseContext( [('PARSE_RANDOM_SLASH', opm.io.action.ignore)] ) deck = Parser().parse(self.norne_fname, parse_context) es = EclipseState( deck ) - + self.assertEqual(46, es.grid().nx) self.assertEqual(112, es.grid().ny) self.assertEqual(22, es.grid().nz) diff --git a/python/tests/test_parser.py b/python/tests/test_parser.py index 71bfe13b3..1595fa258 100644 --- a/python/tests/test_parser.py +++ b/python/tests/test_parser.py @@ -5,6 +5,7 @@ import sys from opm.io.parser import Parser from opm.io.parser import ParseContext from opm.io.deck import DeckKeyword +from utils import test_path unit_foot = 0.3048 #meters @@ -34,8 +35,8 @@ FIPNUM """ def setUp(self): - self.spe3fn = 'tests/spe3/SPE3CASE1.DATA' - self.norne_fname = os.path.abspath('examples/data/norne/NORNE_ATW2013.DATA') + self.spe3fn = test_path('spe3/SPE3CASE1.DATA') + self.norne_fname = test_path('../examples/data/norne/NORNE_ATW2013.DATA') def test_create(self): parser = Parser() diff --git a/python/tests/test_props.py b/python/tests/test_props.py index 811d2da90..bd8fa53c8 100644 --- a/python/tests/test_props.py +++ b/python/tests/test_props.py @@ -3,6 +3,7 @@ import opm.io from opm.io.parser import Parser from opm.io.ecl_state import EclipseState +from utils import test_path class TestProps(unittest.TestCase): @@ -14,10 +15,10 @@ class TestProps(unittest.TestCase): def setUp(self): parser = Parser() - deck = parser.parse('tests/spe3/SPE3CASE1.DATA') - self.spe3 = EclipseState(deck) + deck = parser.parse(test_path('spe3/SPE3CASE1.DATA')) + self.spe3 = EclipseState(deck) self.props = self.spe3.props() - + def test_contains(self): p = self.props self.assertTrue('PORO' in p) @@ -39,7 +40,7 @@ class TestProps(unittest.TestCase): #millidarcy->SI return md * 1e-3 * 9.869233e-13 e3dp = self.props - + grid = self.spe3.grid() permx = e3dp['PERMX'] print('set(PERMX) = %s' % set(permx)) @@ -52,7 +53,7 @@ class TestProps(unittest.TestCase): perm = permx[g_idx] darcy = darcys[k] self.assertClose(darcy, perm) - + def test_volume(self): e3dp = self.props grid = self.spe3.grid() @@ -65,7 +66,7 @@ class TestProps(unittest.TestCase): if k == 0: self.assertClose(exp, grid.getCellVolume(g_idx)) self.assertEqual(grid.getCellVolume(g_idx), grid.getCellVolume(i, j, k)) - + if __name__ == "__main__": unittest.main() diff --git a/python/tests/test_schedule.py b/python/tests/test_schedule.py index 608219da9..eea1335c1 100644 --- a/python/tests/test_schedule.py +++ b/python/tests/test_schedule.py @@ -4,6 +4,7 @@ import datetime as dt from opm.io.parser import Parser from opm.io.ecl_state import EclipseState from opm.io.schedule import Schedule +from utils import test_path class TestSchedule(unittest.TestCase): @@ -11,7 +12,7 @@ class TestSchedule(unittest.TestCase): @classmethod def setUpClass(cls): - deck = Parser().parse('tests/spe3/SPE3CASE1.DATA') + deck = Parser().parse(test_path('spe3/SPE3CASE1.DATA')) state = EclipseState(deck) cls.sch = Schedule( deck, state ) diff --git a/python/tests/test_state.py b/python/tests/test_state.py index a8c6bd065..edcad6dff 100644 --- a/python/tests/test_state.py +++ b/python/tests/test_state.py @@ -5,6 +5,7 @@ from opm.io.parser import Parser from opm.io.ecl_state import EclipseState from opm.io.schedule import Schedule from opm.io.summary import SummaryConfig +from utils import test_path class TestState2(unittest.TestCase): @@ -55,12 +56,12 @@ SATNUM """ @classmethod - def setUpClass(cls): + def setUpClass(cls): parser = Parser() - cls.deck_cpa = parser.parse('tests/data/CORNERPOINT_ACTNUM.DATA') + cls.deck_cpa = parser.parse(test_path('data/CORNERPOINT_ACTNUM.DATA')) cls.cp_state = EclipseState(cls.deck_cpa) - cls.deck_spe3 = parser.parse('tests/spe3/SPE3CASE1.DATA') + cls.deck_spe3 = parser.parse(test_path('spe3/SPE3CASE1.DATA')) cls.state = EclipseState(cls.deck_spe3) cls.schedule = Schedule(cls.deck_spe3, cls.state) cls.summary_config = SummaryConfig(cls.deck_spe3, cls.state, cls.schedule) @@ -184,6 +185,6 @@ PROPS\nREGIONS self.assertTrue('SummaryConfig' in repr(smry)) self.assertTrue('WOPR' in smry) # hasKeyword self.assertFalse('NONO' in smry) # hasKeyword - + if __name__ == "__main__": unittest.main() diff --git a/python/tests/test_time_vector.py b/python/tests/test_time_vector.py index 0a962b877..9a51a01fe 100644 --- a/python/tests/test_time_vector.py +++ b/python/tests/test_time_vector.py @@ -3,7 +3,7 @@ import datetime from opm.tools import * from opm.io.parser import Parser -from .utils import tmp +from utils import test_path, tmp class TestTimeVector(unittest.TestCase): @@ -57,10 +57,10 @@ class TestTimeVector(unittest.TestCase): def test_load(self): - tv = TimeVector(datetime.date(1997, 11, 6), base_file = "tests/data/schedule/part1.sch") - tv.load("tests/data/schedule/part3.sch") - tv.load("tests/data/schedule/fragment_dates.sch") - tv.load("tests/data/schedule/part2.sch") + tv = TimeVector(datetime.date(1997, 11, 6), base_file = test_path("data/schedule/part1.sch")) + tv.load(test_path("data/schedule/part3.sch")) + tv.load(test_path("data/schedule/fragment_dates.sch")) + tv.load(test_path("data/schedule/part2.sch")) self.assertEqual(tv.dates, [datetime.datetime(1997, 11, 6), datetime.datetime(1997, 11, 14), @@ -80,9 +80,9 @@ class TestTimeVector(unittest.TestCase): datetime.datetime(1998, 8, 1)]) def test_str(self): - tv = TimeVector(datetime.date(1997, 11, 6), base_string = open("tests/data/schedule/part1.sch").read()) - tv.load("tests/data/schedule/part3.sch") - tv.load("tests/data/schedule/part2.sch") + tv = TimeVector(datetime.date(1997, 11, 6), base_string = open(test_path("data/schedule/part1.sch")).read()) + tv.load(test_path("data/schedule/part3.sch")) + tv.load(test_path("data/schedule/part2.sch")) s = str(tv) tv2 = TimeVector(datetime.date(1997, 11, 6)) @@ -93,17 +93,17 @@ class TestTimeVector(unittest.TestCase): def test_optional(self): - tv = TimeVector(datetime.date(1997, 11, 6), base_file = "tests/data/schedule/part1.sch") + tv = TimeVector(datetime.date(1997, 11, 6), base_file = test_path("data/schedule/part1.sch")) # Must have a starting date, either as first keyword in loaded file, # or alternatively as the optional date argument. with self.assertRaises(ValueError): - tv.load("tests/data/schedule/fragment.sch") + tv.load(test_path("data/schedule/fragment.sch")) with self.assertRaises(ValueError): - tv.load("tests/data/schedule/fragment_dates.sch", date = datetime.datetime(1998, 1,1)) + tv.load(test_path("data/schedule/fragment_dates.sch"), date = datetime.datetime(1998, 1,1)) - tv.load("tests/data/schedule/fragment.sch", date = datetime.datetime(1998, 1, 10)) + tv.load(test_path("data/schedule/fragment.sch"), date = datetime.datetime(1998, 1, 10)) ts = tv[-1] self.assertEqual(ts.dt, datetime.datetime(1998, 1 , 10)) self.assertEqual(ts.keywords[0].name, "WCONINJE") @@ -112,14 +112,14 @@ class TestTimeVector(unittest.TestCase): def test_user_test(self): tv=TimeVector(datetime.date(1999,12,31)) - tv.load('tests/data/schedule/TEMPLATE.SCH', date=datetime.datetime(1999,12,31)) + tv.load(test_path('data/schedule/TEMPLATE.SCH'), date=datetime.datetime(1999,12,31)) self.assertListEqual(tv.dates, [datetime.datetime(1999,12,31), datetime.datetime(2000,1,1), datetime.datetime(2000,2,1), datetime.datetime(2000,3,1)]) def test_no_leading_DATES(self): - tv = TimeVector(datetime.date(1997, 11, 6), base_file="tests/data/schedule/part1.sch") + tv = TimeVector(datetime.date(1997, 11, 6), base_file=test_path("data/schedule/part1.sch")) s = str(tv) d = Parser().parse_string(s) kw0 = d[0] @@ -129,7 +129,7 @@ class TestTimeVector(unittest.TestCase): self.assertEqual("", str(tv2)) def test_drop_dates(self): - tv = TimeVector(datetime.date(1997, 11, 6), base_file="tests/data/schedule/part1.sch") + tv = TimeVector(datetime.date(1997, 11, 6), base_file=test_path("data/schedule/part1.sch")) with self.assertRaises(KeyError): tv.delete(datetime.datetime(2019,1,1)) diff --git a/python/tests/test_wells.py b/python/tests/test_wells.py index 7d648dbbf..c816cb6d1 100644 --- a/python/tests/test_wells.py +++ b/python/tests/test_wells.py @@ -3,6 +3,7 @@ import opm from opm.io.parser import Parser from opm.io.ecl_state import EclipseState from opm.io.schedule import Schedule +from utils import test_path def injector(well): return well.isinjector() @@ -26,7 +27,7 @@ class TestWells(unittest.TestCase): @classmethod def setUpClass(cls): - deck = Parser().parse('tests/spe3/SPE3CASE1.DATA') + deck = Parser().parse(test_path('spe3/SPE3CASE1.DATA')) state = EclipseState(deck) cls.sch = Schedule( deck, state ) cls.timesteps = cls.sch.timesteps @@ -120,7 +121,7 @@ class TestWells(unittest.TestCase): num_steps = len( self.sch.timesteps ) w0 = self.sch.get_wells(num_steps - 1)[0] c0,c1 = w0.connections() - + self.assertEqual((6,6,2), c0.pos) self.assertEqual((6,6,3), c1.pos) diff --git a/python/tests/utils.py b/python/tests/utils.py index 55250df37..58297bd28 100644 --- a/python/tests/utils.py +++ b/python/tests/utils.py @@ -1,5 +1,6 @@ import contextlib import os +import os.path import shutil import tempfile import unittest @@ -20,6 +21,11 @@ def tmpdir(path, teardown=True): return real_decorator + +def test_path(path): + return os.path.join(os.path.dirname(__file__), path) + + @contextlib.contextmanager def tmp(path=None, teardown=True): """Create and go into tmp directory, returns the path.