def __init__(self, statepoint_name, tallies_present=False, mg=False): super(PyAPITestHarness, self).__init__(statepoint_name, tallies_present) self.parser.add_option('--build-inputs', dest='build_only', action='store_true', default=False) if mg: self._input_set = MGInputSet() else: self._input_set = InputSet()
def __init__(self, statepoint_name, tallies_present=False): TestHarness.__init__(self, statepoint_name, tallies_present) self._input_set = InputSet()
class PyAPITestHarness(TestHarness): def __init__(self, statepoint_name, tallies_present=False, mg=False): super(PyAPITestHarness, self).__init__(statepoint_name, tallies_present) self.parser.add_option('--build-inputs', dest='build_only', action='store_true', default=False) if mg: self._input_set = MGInputSet() else: self._input_set = InputSet() def main(self): """Accept commandline arguments and either run or update tests.""" (self._opts, self._args) = self.parser.parse_args() if self._opts.build_only: self._build_inputs() elif self._opts.update: self.update_results() else: self.execute_test() def execute_test(self): """Build input XMLs, run OpenMC, and verify correct results.""" try: self._build_inputs() inputs = self._get_inputs() self._write_inputs(inputs) self._compare_inputs() self._run_openmc() self._test_output_created() results = self._get_results() self._write_results(results) self._compare_results() finally: self._cleanup() def update_results(self): """Update results_true.dat and inputs_true.dat""" try: self._build_inputs() inputs = self._get_inputs() self._write_inputs(inputs) self._overwrite_inputs() self._run_openmc() self._test_output_created() results = self._get_results() self._write_results(results) self._overwrite_results() finally: self._cleanup() def _build_inputs(self): """Write input XML files.""" self._input_set.build_default_materials_and_geometry() self._input_set.build_default_settings() self._input_set.export() def _get_inputs(self): """Return a hash digest of the input XML files.""" xmls = ('geometry.xml', 'tallies.xml', 'materials.xml', 'settings.xml', 'plots.xml') xmls = [os.path.join(os.getcwd(), fname) for fname in xmls] outstr = '\n'.join([open(fname).read() for fname in xmls if os.path.exists(fname)]) sha512 = hashlib.sha512() sha512.update(outstr.encode('utf-8')) outstr = sha512.hexdigest() return outstr def _write_inputs(self, input_digest): """Write the digest of the input XMLs to an ASCII file.""" with open('inputs_test.dat', 'w') as fh: fh.write(input_digest) def _overwrite_inputs(self): """Overwrite inputs_true.dat with inputs_test.dat""" shutil.copyfile('inputs_test.dat', 'inputs_true.dat') def _compare_inputs(self): """Make sure the current inputs agree with the _true standard.""" compare = filecmp.cmp('inputs_test.dat', 'inputs_true.dat') if not compare: f = open('inputs_test.dat') for line in f.readlines(): print(line) f.close() os.rename('inputs_test.dat', 'inputs_error.dat') assert compare, 'Input files are broken.' def _cleanup(self): """Delete XMLs, statepoints, tally, and test files.""" super(PyAPITestHarness, self)._cleanup() output = [os.path.join(os.getcwd(), 'materials.xml')] output.append(os.path.join(os.getcwd(), 'geometry.xml')) output.append(os.path.join(os.getcwd(), 'settings.xml')) output.append(os.path.join(os.getcwd(), 'inputs_test.dat')) output.append(os.path.join(os.getcwd(), 'summary.h5')) for f in output: if os.path.exists(f): os.remove(f)
class PyAPITestHarness(TestHarness): def __init__(self, statepoint_name, tallies_present=False): TestHarness.__init__(self, statepoint_name, tallies_present) self._input_set = InputSet() def execute_test(self): """Build input XMLs, run OpenMC, and verify correct results.""" try: self._build_inputs() inputs = self._get_inputs() self._write_inputs(inputs) self._compare_inputs() self._run_openmc() self._test_output_created() results = self._get_results() self._write_results(results) self._compare_results() finally: self._cleanup() def update_results(self): """Update results_true.dat and inputs_true.dat""" try: self._build_inputs() inputs = self._get_inputs() self._write_inputs(inputs) self._overwrite_inputs() self._run_openmc() self._test_output_created() results = self._get_results() self._write_results(results) self._overwrite_results() finally: self._cleanup() def _build_inputs(self): """Write input XML files.""" self._input_set.build_default_materials_and_geometry() self._input_set.build_default_settings() self._input_set.export() def _get_inputs(self): """Return a hash digest of the input XML files.""" xmls = ('geometry.xml', 'tallies.xml', 'materials.xml', 'settings.xml') xmls = [os.path.join(os.getcwd(), fname) for fname in xmls] outstr = '\n'.join([open(fname).read() for fname in xmls if os.path.exists(fname)]) sha512 = hashlib.sha512() sha512.update(outstr.encode('utf-8')) outstr = sha512.hexdigest() return outstr def _write_inputs(self, input_digest): """Write the digest of the input XMLs to an ASCII file.""" with open('inputs_test.dat', 'w') as fh: fh.write(input_digest) def _overwrite_inputs(self): """Overwrite inputs_true.dat with inputs_test.dat""" shutil.copyfile('inputs_test.dat', 'inputs_true.dat') def _compare_inputs(self): """Make sure the current inputs agree with the _true standard.""" compare = filecmp.cmp('inputs_test.dat', 'inputs_true.dat') if not compare: f = open('inputs_test.dat') for line in f.readlines(): print(line) f.close() os.rename('inputs_test.dat', 'inputs_error.dat') assert compare, 'Input files are broken.' def _cleanup(self): """Delete XMLs, statepoints, tally, and test files.""" TestHarness._cleanup(self) output = [os.path.join(os.getcwd(), 'materials.xml')] output.append(os.path.join(os.getcwd(), 'geometry.xml')) output.append(os.path.join(os.getcwd(), 'settings.xml')) output.append(os.path.join(os.getcwd(), 'inputs_test.dat')) output.append(os.path.join(os.getcwd(), 'summary.h5')) for f in output: if os.path.exists(f): os.remove(f)