Exemplo n.º 1
0
def test_pattern_property_set_and_get():
    """Test setting and getting pattern property. Set is not allowed.
    """
    # Raise error is user tries to set pattern
    abund = Abund(0, "Empty")
    with pytest.raises(AttributeError):
        abund.pattern = 0.0
Exemplo n.º 2
0
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()