def test_get_default_name(self): class MyClass(object): pass parent = MyClass() pname = get_default_name(parent, None) self.assertEqual(pname, 'myclass1') for i in range(3): obj = MyClass() oname = get_default_name(obj, parent) setattr(parent, oname, obj) self.assertEqual(oname, "myclass%s" % (i+1))
def test_get_default_name(self): class MyClass(object): pass parent = MyClass() pname = get_default_name(parent, None) self.assertEqual(pname, 'myclass1') for i in range(3): obj = MyClass() oname = get_default_name(obj, parent) setattr(parent, oname, obj) self.assertEqual(oname, "myclass%s" % (i + 1))
def test_default_naming(self): cont = Container() cont.add('container1', Container()) cont.add('container2', Container()) self.assertEqual(get_default_name(Container(), cont), 'container3') self.assertEqual(get_default_name(Container(), None), 'container1')
def test_save_load(self): logging.debug('') logging.debug('test_save_load') self.model.vehicle.bore = 95 self.model.vehicle.spark_angle = -35.368341874 self.model.driver.itmax = 1 # Set local dir in case we're running in a different directory. py_dir = pkg_resources.resource_filename( 'openmdao.examples.enginedesign', 'test') python = openmdao.util.testutil.find_python() name = self.model.name or get_default_name(self.model, self) egg_info = self.model.save_to_egg(name, '0', py_dir=py_dir) egg_name = egg_info[0] orig_dir = os.getcwd() test_dir = 'EggTest' if os.path.exists(test_dir): shutil.rmtree(test_dir) os.mkdir(test_dir) os.chdir(test_dir) egg_path = os.path.join('..', egg_name) try: logging.debug('Unpacking in subprocess...') logging.debug(' python %s' % python) out = open('unpack.py', 'w') out.write("""\ from openmdao.main.api import Component try: Component.load_from_eggfile('%s') except Exception, err: import openmdao.util.log openmdao.util.log.logger.exception(str(err)) raise """ % egg_path) out.close() retcode = subprocess.call([python, 'unpack.py']) self.assertEqual(retcode, 0) logging.debug('Load state and run test in subprocess...') logging.debug(' python %s' % python) os.chdir(name) out = open('test.py', 'w') out.write("""\ import sys if not '.' in sys.path: sys.path.append('.') import unittest class TestCase(unittest.TestCase): def test_load(self): loader = __import__('%s_loader') model = loader.load() model.run() self.assertAlmostEqual(model.sim_acc.accel_time, 5.5999999999999961, places=6) self.assertAlmostEqual(model.sim_EPA_city.fuel_economy, 25.203, places=3) self.assertAlmostEqual(model.sim_EPA_highway.fuel_economy, 32.8139, places=4) if __name__ == '__main__': unittest.main() """ % name) out.close() out = open('test.out', 'w') retcode = subprocess.call([python, 'test.py'], stdout=out, stderr=subprocess.STDOUT) out.close() inp = open('test.out', 'r') for line in inp.readlines(): logging.debug(line.rstrip()) inp.close() self.assertEqual(retcode, 0) finally: os.chdir(orig_dir) shutil.rmtree(test_dir) if egg_name and os.path.exists(egg_name): os.remove(egg_name)
def check_save_load(comp, py_dir=None, test_dir='test_dir', cleanup=True, logfile=None): """Convenience routine to check that saving & reloading `comp` works. comp: Component The component to check. py_dir: string or None The directory in which to find local Python modules. test_dir: string Name of a scratch directory to unpack in. cleanup: bool If True, the scratch directory will be removed after the test. logfile: string or None Name of file for logging progress. Creates an egg in the current directory, unpacks it in `test_dir` via a separate process, and then loads and runs the component in another subprocess. Returns the first non-zero subprocess exit code, or zero if everything succeeded. """ assert isinstance(comp, Component) old_level = comp.log_level comp.log_level = LOG_DEBUG name = comp.name or get_default_name(comp, comp.parent) start = time.time() egg_info = comp.save_to_egg(name, 'CSL.1', py_dir=py_dir) egg_name = egg_info[0] elapsed = time.time() - start size = os.path.getsize(egg_name) print '\nSaved %d bytes in %.2f seconds (%.2f bytes/sec)' % \ (size, elapsed, size / elapsed) orig_dir = os.getcwd() if os.path.exists(test_dir): shutil.rmtree(test_dir, onerror=onerror) os.mkdir(test_dir) os.chdir(test_dir) egg_path = os.path.join('..', egg_name) try: print '\nUnpacking %s in subprocess...' % egg_name if logfile: stdout = open(logfile, 'w') else: stdout = None stderr = subprocess.STDOUT python = find_python() print ' python:', python unpacker = 'unpack.py' out = open(unpacker, 'w') out.write("""\ from openmdao.main.api import Component Component.load_from_eggfile(r'%s') """ % egg_path) out.close() args = [python, unpacker] retcode = subprocess.call(args, env=os.environ, stdout=stdout, stderr=stderr) print ' retcode', retcode if retcode == 0: print '\nRunning in subprocess...' os.chdir(name) retcode = subprocess.call([python, name + '_loader.py'], stdout=stdout, stderr=stderr) print ' retcode', retcode if logfile: stdout.close() finally: os.chdir(orig_dir) comp.log_level = old_level if cleanup: os.remove(egg_name) shutil.rmtree(test_dir, onerror=onerror) return retcode
def check_save_load(comp, py_dir=None, test_dir='test_dir', cleanup=True, logfile=None): """Convenience routine to check that saving & reloading `comp` works. comp: Component The component to check. py_dir: string or None The directory in which to find local Python modules. test_dir: string Name of a scratch directory to unpack in. cleanup: bool If True, the scratch directory will be removed after the test. logfile: string or None Name of file for logging progress. Creates an egg in the current directory, unpacks it in `test_dir` via a separate process, and then loads and runs the component in another subprocess. Returns the first non-zero subprocess exit code, or zero if everything succeeded. """ assert isinstance(comp, Component) old_level = comp.log_level comp.log_level = LOG_DEBUG name = comp.name or get_default_name(comp, comp.parent) start = time.time() egg_info = comp.save_to_egg(name, 'CSL.1', py_dir=py_dir) egg_name = egg_info[0] elapsed = time.time() - start size = os.path.getsize(egg_name) print '\nSaved %d bytes in %.2f seconds (%.2f bytes/sec)' % \ (size, elapsed, size/elapsed) orig_dir = os.getcwd() if os.path.exists(test_dir): shutil.rmtree(test_dir, onerror=onerror) os.mkdir(test_dir) os.chdir(test_dir) egg_path = os.path.join('..', egg_name) try: print '\nUnpacking %s in subprocess...' % egg_name if logfile: stdout = open(logfile, 'w') else: stdout = None stderr = subprocess.STDOUT python = find_python() print ' python:', python unpacker = 'unpack.py' out = open(unpacker, 'w') out.write("""\ from openmdao.main.api import Component Component.load_from_eggfile(r'%s') """ % egg_path) out.close() args = [python, unpacker] retcode = subprocess.call(args, env=os.environ, stdout=stdout, stderr=stderr) print ' retcode', retcode if retcode == 0: print '\nRunning in subprocess...' os.chdir(name) retcode = subprocess.call([python, name+'_loader.py'], stdout=stdout, stderr=stderr) print ' retcode', retcode if logfile: stdout.close() finally: os.chdir(orig_dir) comp.log_level = old_level if cleanup: os.remove(egg_name) shutil.rmtree(test_dir, onerror=onerror) return retcode
def test_save_load(self): logging.debug('') logging.debug('test_save_load') self.model.vehicle.bore = 95 self.model.vehicle.spark_angle = -35.368341874 self.model.driver.itmax = 1 # Set local dir in case we're running in a different directory. py_dir = pkg_resources.resource_filename('openmdao.examples.enginedesign', 'test') python = openmdao.util.testutil.find_python() name = self.model.name or get_default_name(self.model, self) egg_info = self.model.save_to_egg(name, '0', py_dir=py_dir) egg_name = egg_info[0] orig_dir = os.getcwd() test_dir = 'EggTest' if os.path.exists(test_dir): shutil.rmtree(test_dir) os.mkdir(test_dir) os.chdir(test_dir) egg_path = os.path.join('..', egg_name) try: logging.debug('Unpacking in subprocess...') logging.debug(' python %s' % python) out = open('unpack.py', 'w') out.write("""\ from openmdao.main.api import Component try: Component.load_from_eggfile('%s') except Exception, err: import openmdao.util.log openmdao.util.log.logger.exception(str(err)) raise """ % egg_path) out.close() retcode = subprocess.call([python, 'unpack.py']) self.assertEqual(retcode, 0) logging.debug('Load state and run test in subprocess...') logging.debug(' python %s' % python) os.chdir(name) out = open('test.py', 'w') out.write("""\ import sys if not '.' in sys.path: sys.path.append('.') import unittest class TestCase(unittest.TestCase): def test_load(self): loader = __import__('%s_loader') model = loader.load() model.run() self.assertAlmostEqual(model.sim_acc.accel_time, 5.5999999999999961, places=6) self.assertAlmostEqual(model.sim_EPA_city.fuel_economy, 25.203, places=3) self.assertAlmostEqual(model.sim_EPA_highway.fuel_economy, 32.8139, places=4) if __name__ == '__main__': unittest.main() """ % name) out.close() out = open('test.out', 'w') retcode = subprocess.call([python, 'test.py'], stdout=out, stderr=subprocess.STDOUT) out.close() inp = open('test.out', 'r') for line in inp.readlines(): logging.debug(line.rstrip()) inp.close() self.assertEqual(retcode, 0) finally: os.chdir(orig_dir) shutil.rmtree(test_dir) if egg_name and os.path.exists(egg_name): os.remove(egg_name)