def test_totype_fromtype(): """Test behavior of totype() and fromtype(), which are static methods that return a copy of the input abundance pattern transformed to or from the specified abudnance pattern type. """ # Round trip tests that compare copy=fromtype(totype()) with original. orig = Abund(0, pattern_names[0])() for type in types: pattern = Abund.totype(orig, type) copy = Abund.fromtype(pattern, type) # Same elements in the same order for full dictionary. assert copy.keys() == orig.keys() # Same elements have abundance defined (!= None). o = OrderedDict((k, v) for k, v in orig.items() if not np.isnan(v)) c = OrderedDict((k, v) for k, v in copy.items() if not np.isnan(v)) assert c.keys() == o.keys() # Logarithmic abundances differ by less than 1e-10. assert all([abs(o[k] - c[k]) < 1e-10 for k in o.keys()]) # Lowercase type yields same result as mixed case type. # type_lc = type.lower() # assert copy == Abund.fromtype(Abund.totype(orig, type_lc), type_lc) # Invalid abundance pattern type raises error. with pytest.raises(ValueError): copy = Abund.totype(orig, "INVALID") with pytest.raises(ValueError): copy = Abund.fromtype(orig, "INVALID")
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()