Skip to content

Commit

Permalink
test all edge cases of Composition.__contains__
Browse files Browse the repository at this point in the history
- Species in Composition
- Element in Composition with Species
- str in Composition with Element
- int (atomic number) in Composition
- float in Composition
- DummySpecies in Composition
  • Loading branch information
janosh committed Jul 25, 2023
1 parent d86e774 commit 2595681
Showing 1 changed file with 46 additions and 14 deletions.
60 changes: 46 additions & 14 deletions pymatgen/core/tests/test_composition.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from pytest import approx

from pymatgen.core.composition import ChemicalPotential, Composition
from pymatgen.core.periodic_table import Element, Species
from pymatgen.core.periodic_table import DummySpecies, Element, Species
from pymatgen.util.testing import PymatgenTest


Expand Down Expand Up @@ -52,6 +52,7 @@ def test_immutable(self):
assert "'Composition' object does not support item deletion" in str(exc.value)

def test_in(self):
# test the Composition.__contains__ magic method
assert "Fe" in self.comps[0]
assert "Fe" not in self.comps[2]
assert Element("Fe") in self.comps[0]
Expand All @@ -62,6 +63,46 @@ def test_in(self):
with pytest.raises(KeyError, match="Invalid key='Vac'"):
self.comps[0]["Vac"]

# Test Species in Composition
comp = Composition({Species("Fe2+"): 2})
assert Species("Fe2+") in comp
assert Species("Fe3+") not in comp
assert "Fe" in comp
assert Element("Fe") in comp

# Test Element in Composition with Species
comp = Composition({Species("Fe2+"): 2})
assert Element("Fe") in comp
assert Element("O") not in comp
assert "Fe" in comp
assert "O" not in comp

# Test str in Composition with Element
comp = Composition({"Fe": 2})
assert "Fe" in comp
assert "O" not in comp
assert Element("Fe") in comp
assert Element("O") not in comp

# Test int (atomic number) in Composition
comp = Composition({Element("Fe"): 2})
assert 26 in comp # atomic number for Fe
assert 8 not in comp # atomic number for O

# Test float in Composition
comp = Composition({Element("Fe"): 2})
with pytest.raises(TypeError, match="expected string or bytes-like object, got 'float'"):
assert 1.5 in comp

# Test DummySpecies in Composition
comp = Composition({DummySpecies("X"): 2})
assert DummySpecies("X") in comp
assert DummySpecies("A") not in comp
assert "X" in comp
assert "Y" not in comp
assert Element("Fe") not in comp
assert Species("Fe2+") not in comp

def test_hill_formula(self):
c = Composition("CaCO3")
assert c.hill_formula == "C Ca O3"
Expand Down Expand Up @@ -528,21 +569,12 @@ def test_oxi_state_guesses(self):
{"V": 5, "O": -2},
)

expected_oxi_guesses = dict(Li=1, Fe=2, P=5, O=-2)
# max_sites for very large composition - should timeout if incorrect
assert Composition("Li10000Fe10000P10000O40000").oxi_state_guesses(max_sites=7)[0] == {
"Li": 1,
"Fe": 2,
"P": 5,
"O": -2,
}
assert Composition("Li10000Fe10000P10000O40000").oxi_state_guesses(max_sites=7)[0] == expected_oxi_guesses

# max_sites for very large composition - should timeout if incorrect
assert Composition("Li10000Fe10000P10000O40000").oxi_state_guesses(max_sites=-1)[0] == {
"Li": 1,
"Fe": 2,
"P": 5,
"O": -2,
}
assert Composition("Li10000Fe10000P10000O40000").oxi_state_guesses(max_sites=-1)[0] == expected_oxi_guesses

# negative max_sites less than -1 - should throw error if cannot reduce
# to under the abs(max_sites) number of sites. Will also timeout if
Expand Down Expand Up @@ -572,7 +604,7 @@ def test_oxi_state_decoration(self):
assert decorated.get(Species("Ni", 0)) == 1
assert decorated.get(Species("Al", 0)) == 1

def test_Metallofullerene(self):
def test_metallofullerene(self):
# Test: Parse Metallofullerene formula (e.g. Y3N@C80)
formula = "Y3N@C80"
sym_dict = {"Y": 3, "N": 1, "C": 80}
Expand Down

0 comments on commit 2595681

Please sign in to comment.