def test_exception_in_context(): with pytest.raises(AccumulatedError) as exception: with ErrorAccumulator() as erracc: erracc.add_exception(ValueError("test")) raise ValueError("test2") assert len(exception.causes) == 2
def test_validate_sample_sphere_invalid(exporter, options): sample = SphereSample(VACUUM, -1.0) erracc = ErrorAccumulator() exporter._validate_sample_sphere(sample, options, erracc) assert len(erracc.exceptions) == 2 assert len(erracc.warnings) == 0
def test_validate_program_invalid(exporter, options): program = ProgramMock("bar", ElasticCrossSectionModel.ELSEPA2005) erracc = ErrorAccumulator() exporter._validate_program(program, options, erracc) assert len(erracc.exceptions) == 1 assert len(erracc.warnings) == 0
def test_validate_sample_substrate_invalid(exporter, options): sample = SubstrateSample(VACUUM, float("inf"), float("nan")) erracc = ErrorAccumulator() exporter._validate_sample_substrate(sample, options, erracc) assert len(erracc.exceptions) == 3 assert len(erracc.warnings) == 0
def test_validate_material_invalid(exporter, options): material = Material(" ", {120: 1.1}, -1.0, "blah") erracc = ErrorAccumulator() exporter._validate_material(material, options, erracc) assert len(erracc.exceptions) == 6 assert len(erracc.warnings) == 0
def test_validate_sample_inclusion_invalid(exporter, options): sample = InclusionSample(VACUUM, VACUUM, 0.0) erracc = ErrorAccumulator() exporter._validate_sample_inclusion(sample, options, erracc) assert len(erracc.exceptions) == 3 assert len(erracc.warnings) == 0
def test_validate_detector_photon_invalid(exporter, options): detector = PhotonDetector("", 2.0, -1.0) erracc = ErrorAccumulator() exporter._validate_detector_photon(detector, options, erracc) assert len(erracc.exceptions) == 3 assert len(erracc.warnings) == 0
def test_validate_beam_cylindrical_invalid(exporter, options): beam = CylindricalBeam(0.0, -1.0, "particle", float("inf"), float("nan")) erracc = ErrorAccumulator() exporter._validate_beam_cylindrical(beam, options, erracc) assert len(erracc.exceptions) == 6 assert len(erracc.warnings) == 0
def test_validate_material_nodensity(exporter, options): material = Material("Pure Cu", {29: 1.0}) erracc = ErrorAccumulator() exporter._validate_material(material, options, erracc) assert material.density_kg_per_m3 is not None assert len(erracc.exceptions) == 0 assert len(erracc.warnings) == 0
def test_validate_analysis_photonintensity(exporter, options): detector = PhotonDetector("test", 1.0, 1.0) analysis = PhotonIntensityAnalysis(detector) erracc = ErrorAccumulator() exporter._validate_analysis_photonintensity(analysis, options, erracc) assert len(erracc.exceptions) == 0 assert len(erracc.warnings) == 0
def test_validate_program_invalid2(exporter, options): program = ProgramMock("bar", MassAbsorptionCoefficientModel.POUCHOU_PICHOIR1991) erracc = ErrorAccumulator() exporter._validate_program(program, options, erracc) assert len(erracc.exceptions) == 1 assert len(erracc.warnings) == 0
async def import_(self, options, dirpath): """ Imports the results and returns a :class:`list` of :class:`Result`. :arg options: options used for the simulation :arg dirpath: path containing the simulation files """ with ErrorAccumulator(ImportWarning, ImportError) as erracc: return await self._import(options, dirpath, erracc)
async def test_exporter_write_material(event_loop, exporter, options, tmp_path): penmaterial = Penmaterial( name="test", composition={29: 0.5, 30: 0.5}, density_g_per_cm3=8.7 ) erracc = ErrorAccumulator() await exporter._write_material(penmaterial, options, tmp_path, erracc) assert tmp_path.joinpath(penmaterial.filename).exists()
def test_validate_sample_verticallayers_invalid(exporter, options): sample = VerticalLayerSample(VACUUM, VACUUM) sample.add_layer(ZINC, -1.0) erracc = ErrorAccumulator() exporter._validate_sample_verticallayers(sample, options, erracc) assert len(erracc.exceptions) == 3 assert len(erracc.warnings) == 0
def test_validate_sample_horizontallayers_invalid(exporter, options): sample = HorizontalLayerSample(COPPER) sample.add_layer(ZINC, -1.0) erracc = ErrorAccumulator() exporter._validate_sample_horizontallayers(sample, options, erracc) assert len(erracc.exceptions) == 1 assert len(erracc.warnings) == 0
def test_exception_and_warning(): with pytest.raises(AccumulatedError) as exception: with pytest.warns(AccumulatedWarning) as warning: with ErrorAccumulator() as erracc: erracc.add_warning(RuntimeWarning("test")) erracc.add_exception(ValueError("test")) assert len(warning) == 1 assert len(exception.causes) == 1
def test_validate_analysis_kratio_invalid(exporter, options): detector = PhotonDetector("test", 1.0, 1.0) analysis = KRatioAnalysis(detector) analysis.add_standard_material(14, Material.pure(13)) erracc = ErrorAccumulator() exporter._validate_analysis_kratio(analysis, options, erracc) assert len(erracc.exceptions) == 1 assert len(erracc.warnings) == 0
async def export(self, options, dirpath, dry_run=False): """ Exports options to the specified output directory. Args: options (Options): options to export dirpath (str): full path to output directory dry_run: if true no file is written on disk """ with ErrorAccumulator(ExportWarning, ExportError) as erracc: await self._export(options, dirpath, erracc, dry_run)
async def runasync(self): options_count = self.model.optionsCount() self.update.emit(0, options_count) self.erraccs.clear() for i, options in enumerate(self.model.iterOptions(), 1): erracc = self.erraccs.setdefault(options.program.name, ErrorAccumulator()) with tempfile.TemporaryDirectory() as dirpath: exporter = options.program.exporter await exporter._export(options, dirpath, erracc, dry_run=True) self.update.emit(i, options_count)
def test_warning_only(): with pytest.warns(AccumulatedWarning) as warning: with ErrorAccumulator() as erracc: erracc.add_warning(RuntimeWarning("test")) assert len(warning) == 1
def test_exception_only(): with pytest.raises(AccumulatedError) as exception: with ErrorAccumulator() as erracc: erracc.add_exception(ValueError("test")) assert len(exception.causes) == 1