def testExceptions(self): with self.assertRaises(RuntimeError) as ex: root = pyhit.Node(None) root['type'] = 'foo' th = make_harness('.moosetest', root, None) self.assertIn("The 'type' parameter must NOT be defined", str(ex.exception)) with mock.patch('moosetools.factory.Factory.status', return_value=1): with self.assertRaises(RuntimeError) as ex, mock.patch( 'os.path.isdir', return_value=True), mock.patch('os.chdir'): th = make_harness('.moosetest', pyhit.Node(None), None) self.assertIn("An error occurred during registration of the TestHarness", str(ex.exception)) with mock.patch('moosetools.factory.Parser.status', return_value=1): with self.assertRaises(RuntimeError) as ex, mock.patch( 'os.path.isdir', return_value=True), mock.patch('os.chdir'): th = make_harness('.moosetest', pyhit.Node(None), None) self.assertIn("An error occurred during parsing of the", str(ex.exception)) with mock.patch('moosetools.core.MooseObject.status', side_effect=(0, 0, 1)): with self.assertRaises(RuntimeError) as ex, mock.patch( 'os.path.isdir', return_value=True), mock.patch('os.chdir'): th = make_harness('.moosetest', pyhit.Node(None), None) self.assertIn("An error occurred applying the command line", str(ex.exception))
def testDefault(self): root = pyhit.Node(None) with mock.patch('os.path.isdir', return_value=True), mock.patch('os.chdir') as mock_chdir: formatter = make_formatter('.moosetest', pyhit.Node(None), tuple()) self.assertEqual(mock_chdir.call_count, 2) mock_chdir.assert_called_with(os.getcwd()) self.assertIsInstance(formatter, BasicFormatter) self.assertEqual(formatter.getParam('print_state'), TestCase.Result.DIFF)
def testExceptions(self): with mock.patch('moosetools.factory.Factory.status', return_value=1): with self.assertRaises(RuntimeError) as ex, mock.patch( 'os.path.isdir', return_value=True), mock.patch('os.chdir'): make_controllers('.moosetest', pyhit.Node(None), tuple()) self.assertIn("An error occurred registering the Controller type", str(ex.exception)) with mock.patch('moosetools.factory.Parser.status', return_value=1): with self.assertRaises(RuntimeError) as ex, mock.patch( 'os.path.isdir', return_value=True), mock.patch('os.chdir'): make_controllers('.moosetest', pyhit.Node(None), tuple()) self.assertIn("An error occurred during parsing of the Controller block", str(ex.exception))
def testExceptions(self): with mock.patch('moosetools.factory.Factory.status', return_value=1): with self.assertRaises(RuntimeError) as ex, mock.patch( 'os.path.isdir', return_value=True), mock.patch('os.chdir'): formatter = make_formatter('.moosetest', pyhit.Node(None), tuple()) self.assertIn("An error occurred registering the Formatter type", str(ex.exception)) with mock.patch('moosetools.factory.Parser.status', return_value=1): with self.assertRaises(RuntimeError) as ex, mock.patch( 'os.path.isdir', return_value=True), mock.patch('os.chdir'): formatter = make_formatter('.moosetest', pyhit.Node(None), tuple()) self.assertIn( "An error occurred during parsing of the root level parameters for creation of the Formatter object", str(ex.exception))
def testOverride(self): root = pyhit.Node(None) root.append('Formatter', type='BasicFormatter', width=1980) with mock.patch('os.path.isdir', return_value=True), mock.patch('os.chdir'): formatter = make_formatter('.moosetest', root, tuple()) self.assertIsInstance(formatter, BasicFormatter) self.assertEqual(formatter.getParam('width'), 1980)
def build_hit(expr, name, **kwargs): """ Create a hit node containing a ParsedFunction of the given expression Inputs: expr[sympy.core.Expr]: The sympy expression to convert name[str]: The name of the input file block to create kwargs: Key, value pairs for val, vals input parameters (defaults to 1.0) if not provided """ from moosetools import pyhit if hasattr(expr, 'free_symbols'): symbols = set([str(s) for s in expr.free_symbols ]).difference(set(['R.x', 'R.y', 'R.z', 't'])) else: symbols = set() for symbol in symbols: kwargs.setdefault(symbol, 1.) root = pyhit.Node(None, name) root['type'] = 'ParsedFunction' root['value'] = "'{}'".format(str(fparser(expr))) if kwargs: pvars = ' '.join(kwargs.keys()) pvals = ' '.join([str(v) for v in kwargs.values()]) root['vars'] = "'{}'".format(pvars) root['vals'] = "'{}'".format(pvals) return root
def testDefault(self): root = pyhit.Node(None) with mock.patch('os.path.isdir', return_value=True), mock.patch('os.chdir') as mock_chdir: controllers = make_controllers('.moosetest', root, tuple()) self.assertEqual(mock_chdir.call_count, 2) mock_chdir.assert_called_with(os.getcwd()) for c in controllers: self.assertIsInstance(c, Controller) self.assertEqual(list(controllers)[0].getParam('prefix'), 'env')
def testOverride(self): root = pyhit.Node(None) c = root.append('Controllers') c.append('env', type='EnvironmentController', prefix='environment') with mock.patch('os.path.isdir', return_value=True), mock.patch('os.chdir'): controllers = make_controllers('.moosetest', root, tuple()) for c in controllers: self.assertIsInstance(c, Controller) self.assertEqual(list(controllers)[0].getParam('prefix'), 'environment')
def testDefault(self): root = pyhit.Node(None) root['n_threads'] = 1 with mock.patch('os.path.isdir', return_value=True), mock.patch('os.chdir') as mock_chdir: th = make_harness('.moosetest', root, None) self.assertEqual(mock_chdir.call_count, 2) mock_chdir.assert_called_with(os.getcwd()) self.assertIsInstance(th, TestHarness) self.assertEqual(th.getParam('n_threads'), 1)
def test(self, mock_find): root = pyhit.Node() root.append('Environment') root['SOME_DIR'] = '/foo' mock_find.return_value = root with mock.patch('os.path.isdir', return_value=True), mock.patch('os.chdir'), mock.patch('os.path.exists', return_value=True): setup_environment('filename', None) self.assertIn('SOME_DIR', os.environ) self.assertEqual(os.environ['SOME_DIR'], '/foo')
def testDifferError(self, pyhit_load): root = pyhit.Node(None) root.append('Tests') root(0).append('differ', type='TestDiffer') pyhit_load.return_value = root f = MooseTestFactory() f.load() with self.assertLogs(level='CRITICAL') as log, \ mock.patch('os.path.isdir', return_value=True), \ mock.patch('os.path.isabs', return_value=True): objs, status = _create_runners('foo/bar', 'foo/bar/testing/tests', ['Tests'], f) self.assertEqual(status, 1) self.assertEqual(len(log.output), 1) self.assertIn("The `Differ` object 'differ' is being added without", log.output[0])
def testSubBlocks(self): root = pyhit.Node(None, 'Tests') root.append('obj0', type='TestObject') root.append('obj1', type='TestObject') sub = root.append('sub') sub.append('obj2', type='TestObject') sub.append('obj3', type='TestObject') f = factory.Factory() f.load() w = factory.Warehouse() p = factory.Parser(f, w) with mock.patch('moosetools.pyhit.load') as load: load.return_value = root p.parse('test0.hit') self.assertEqual(len(w.objects), 4) self.assertEqual(w.objects[0].name(), 'obj0') self.assertEqual(w.objects[1].name(), 'obj1') self.assertEqual(w.objects[2].name(), 'obj2') self.assertEqual(w.objects[3].name(), 'obj3')
def testRunnerWithDiffers(self, pyhit_load): root = pyhit.Node(None) root.append('Tests') root(0).append('run0', type='TestRunner') root(0, 0).append('diff0-0', type='TestDiffer') root(0, 0).append('diff0-1', type='TestDiffer') root(0).append('run1', type='TestRunner') root(0, 1).append('diff1-0', type='TestDiffer') root(0, 1).append('diff1-1', type='TestDiffer') pyhit_load.return_value = root f = MooseTestFactory() f.load() with mock.patch('os.path.isfile', return_value=True), \ mock.patch('os.path.isdir', return_value=True), \ mock.patch('os.path.isabs', return_value=True): objs, status = _create_runners('foo/bar', 'foo/bar/testing/tests', ['Tests'], f) self.assertEqual(status, 0) self.assertIsInstance(objs[0], TestRunner) self.assertEqual(objs[0].name(), 'testing/tests:Tests/run0') differs = objs[0].getParam('differs') self.assertEqual(len(differs), 2) self.assertIsInstance(differs[0], TestDiffer) self.assertEqual(differs[0].name(), 'diff0-0') self.assertIsInstance(differs[1], TestDiffer) self.assertEqual(differs[1].name(), 'diff0-1') self.assertIsInstance(objs[1], TestRunner) self.assertEqual(objs[1].name(), 'testing/tests:Tests/run1') differs = objs[1].getParam('differs') self.assertEqual(len(differs), 2) self.assertIsInstance(differs[0], TestDiffer) self.assertEqual(differs[0].name(), 'diff1-0') self.assertIsInstance(differs[1], TestDiffer) self.assertEqual(differs[1].name(), 'diff1-1')
def testErrors(self): f = factory.Factory() f.load() w = factory.Warehouse() p = factory.Parser(f, w) # INVALID FILENAME with self.assertLogs(level='ERROR') as log: p.parse('wrong') self.assertEqual(len(log.output), 1) self.assertIn("The filename 'wrong' does not exist.", log.output[0]) # MISSING TYPE root = pyhit.Node(None, 'Tests') root.append('obj0') with self.assertLogs(level='ERROR') as log: p._parseNode('test0.hit', root) self.assertEqual(p.status(), 1) self.assertEqual(len(log.output), 1) self.assertIn("Missing 'type' in block", log.output[0]) # FAIL PYHIT.LOAD with mock.patch('moosetools.pyhit.load') as load: load.side_effect = Exception() with self.assertLogs(level='ERROR') as log: p.parse('test0.hit') self.assertEqual(p.status(), 1) self.assertEqual(len(log.output), 1) self.assertIn("Failed to load filename with pyhit: test0.hit", log.output[0]) # OBJECT FAILS VALIDPARAMS root = pyhit.Node(None, 'Tests') root.append('obj0', type='TestObjectBadParams') with mock.patch('moosetools.pyhit.load') as load: load.return_value = root with self.assertLogs(level='ERROR') as log: p.parse('test0.hit') self.assertEqual(p.status(), 1) self.assertEqual(len(log.output), 2) self.assertIn("Failed to evaluate validParams function of 'TestObjectBadParams'", log.output[0]) self.assertIn("Failed to extract parameters from 'TestObjectBadParams'", log.output[1]) # PARAM NO EXISTY root = pyhit.Node(None, 'Tests') root.append('obj0', type='TestObject', nope='1') with mock.patch('moosetools.pyhit.load') as load: load.return_value = root with self.assertLogs(level='ERROR') as log: p.parse('test0.hit') self.assertEqual(p.status(), 1) self.assertEqual(len(log.output), 1) self.assertIn("he parameter 'nope' does not exist", log.output[0]) # PARAM WRONG TYPE root = pyhit.Node(None, 'Tests') root.append('obj0', type='TestObject', par_int='abc') with mock.patch('moosetools.pyhit.load') as load: load.return_value = root with self.assertLogs(level='ERROR') as log: p.parse('test0.hit') self.assertEqual(p.status(), 1) self.assertEqual(len(log.output), 1) self.assertIn( "Failed to convert 'None' to the correct type(s) of '(<class 'int'>,)' for 'par_int' parameter", log.output[0]) # OBJECT FAILS __INIT__ root = pyhit.Node(None, 'Tests') root.append('obj0', type='TestObjectBadInit') with mock.patch('moosetools.pyhit.load') as load: load.return_value = root with self.assertLogs(level='ERROR') as log: p.parse('test0.hit') self.assertEqual(p.status(), 1) self.assertEqual(len(log.output), 2) self.assertIn("Failed to create 'TestObjectBadInit' object.", log.output[0]) self.assertIn( "Failed to create object of type 'TestObjectBadInit' in block 'Tests/obj0'", log.output[1]) # DUPLICATE BLOCKS/PARAMS root = pyhit.Node(None, 'Tests') root.append('obj0', type='TestObject') root.append('obj0', type='TestObject') with mock.patch('moosetools.pyhit.load') as load: load.return_value = root with self.assertLogs(level='ERROR') as log: p.parse('test0.hit') self.assertEqual(p.status(), 1) self.assertEqual(len(log.output), 2) self.assertIn("Duplicate section 'Tests/obj0'", log.output[0]) self.assertIn("Duplicate parameter 'Tests/obj0/type'", log.output[1])