def test_instantiation(self, test_name): """ Test that each IonizationState test case can be instantiated. """ try: self.instances[test_name] = IonizationState(**test_cases[test_name]) except Exception: pytest.fail(f"Unable to create IonizationState instance for test case {test_name}.")
def setup_class(self): self.element = 'H' self.valid_number_densities = u.Quantity([0.1, 0.2], unit=u.m**-3) self.expected_n_elem = np.sum(self.valid_number_densities) self.expected_ionic_fractions = self.valid_number_densities / self.expected_n_elem try: self.instance = IonizationState(self.element) except Exception: pytest.fail("Unable to instantiate IonizationState with no ionic fractions.")
def test_nans(): """ Test that when no ionic fractions or temperature are inputted, the result is an array full of `~numpy.nan` of the right size. """ element = 'He' nstates = atomic_number(element) + 1 instance = IonizationState(element) assert len(instance.ionic_fractions) == nstates, \ f"Incorrect number of ionization states for {element}" assert np.all([np.isnan(instance.ionic_fractions)]), ( f"The ionic fractions for IonizationState are not defaulting " f"to numpy.nan when not set by user.")
def test_setting_ionic_fractions(): instance = IonizationState('He') new_ionic_fractions = [0.2, 0.5, 0.3] instance.ionic_fractions = new_ionic_fractions assert np.allclose(instance.ionic_fractions, new_ionic_fractions)
'element': 'He', 'atomic_number': 2, 'Z_mean': 1.3, 'Z_rms': 1.51657508881031, 'n_e': 1.3e19 * u.m**-3, 'n_elem': 1e19 * u.m**-3, 'integer_charges': [0, 1, 2], 'ionic_fractions': np.array([0.2, 0.3, 0.5]), 'ionic_symbols': ['He-4 0+', 'He-4 1+', 'He-4 2+'], '_is_normalized()': True, 'number_densities': np.array([2e18, 3e18, 5e18]) * u.m**-3, 'tol': 2e-14, '__str__()': "<IonizationState instance for He-4>", } instance = IonizationState(**kwargs) @pytest.mark.parametrize('key', expected_properties.keys()) def test_IonizationState_attributes(key): """ Test a specific case that the `IonizationState` attributes are working as expected. """ expected = expected_properties[key] actual = eval(f'instance.{key}') if isinstance(expected, u.Quantity): assert expected.unit == actual.unit, f"Unit mismatch for IonizationState.{key}" assert np.allclose(expected, actual, atol=1e-15 * expected.unit), \ f"Quantity.value mismatch for IonizationState.{key}"