def test_monh_property_set_and_get(): """Test setting and getting monh property. Set converts input to float. """ # Input str convertable to float yields a float with the specified value. abund = Abund("-6e-1", pattern_names[0]) assert isinstance(abund.monh, float) assert abund.monh == -0.6 # Input int yields a float with the specified value. abund.monh = -2 assert isinstance(abund.monh, float) assert abund.monh == -2.0 # Input float yields a float with the specified value. abund.monh = 0.3 assert isinstance(abund.monh, float) assert abund.monh == 0.3 # Input str that cannot be converted to float raises an error. with pytest.raises(ValueError): abund = Abund("ABC", pattern_names[0]) # Input that is not a string or a number raises an error. with pytest.raises(TypeError): abund.monh = []
def test_abund(): """Code coverage tests for Abund() class and methods. """ abund = Abund(0, pattern_names[0]) for name in pattern_names: a1 = Abund(0, name) a2 = Abund(0, name.lower()) a3 = Abund(-0.1, Abund(0.1, name).abund, 'H=12') a1.__repr__() a2.__str__() abund.compare(a3) a3.compare(abund) assert len(a1) == len(a1.elements) == 99 assert a1 is not a2 assert a1 == a2 assert tuple(a1.keys()) == a1.elements assert list(a1.values()) == [a1[k] for k, v in a1.items()] if a2['Fe'] is not None: for norm in norms: a1 == to_H12(a1.normalized(norm), norm) a1 == to_H12(a1.normalized(norm, prune=True), norm) a2.pattern['Fe'] = a1.pattern['Fe'] + 0.999e-4 assert a1['Fe'] != a2['Fe'] assert a1 == a2 assert not a1 != a2 a2.pattern['Fe'] = a1.pattern['Fe'] + 1.001e-4 assert a1 != a2 a2.pattern['Fe'] = None assert a1 != a2 assert a1 != 'wrong object type' assert all( [abs(a1[k] - a3[k]) < 1e-8 for k in a1.elements if a1[k]]) assert a1 != a3 with raises(AbundError, match='set monh and pattern separately'): abund['C'] = 8.2 with raises(ValueError, match='could not convert string'): abund.monh = 'text' with raises(AbundError, match='must be an AbundPattern object'): abund.pattern = {'H': 12, 'He': 11, 'Li': 1} with raises(AbundError, match='unknown element key'): abund['Water'] with raises(AbundError, match='unknown element key'): abund.pattern._pattern['Water'] = 5 abund.abund()