@@ -120,6 +120,23 @@ def test___copy__(self):
120120 # """check Structure.getLastAtom()"""
121121 # return
122122
123+ def test_addNewAtom (self ):
124+ """Duplicate test for the deprecated addNewAtom method.
125+
126+ Remove this test in version 4.0.0
127+ """
128+ s_lat = Lattice ()
129+ structure = Structure (lattice = s_lat )
130+
131+ length = len (structure )
132+ structure .addNewAtom (atype = "C" , xyz = [0.1 , 0.2 , 0.3 ])
133+ expected = len (structure )
134+ actual = length + 1
135+ assert expected == actual
136+ atom_object = structure [- 1 ]
137+ assert atom_object .element == "C"
138+ assert numpy .allclose (atom_object .xyz , [0.1 , 0.2 , 0.3 ])
139+
123140 def test_assignUniqueLabels (self ):
124141 """Check Structure.assignUniqueLabels()"""
125142 self .assertEqual ("" , "" .join ([a .label for a in self .stru ]))
@@ -609,7 +626,59 @@ def test_pickling(self):
609626
610627# End of class TestStructure
611628
629+
612630# ----------------------------------------------------------------------------
631+ @pytest .mark .parametrize (
632+ "existing, atype, xyz, expected_len, expected_element, expected_xyz" ,
633+ [
634+ # Case 1: valid atom added to an empty structure.
635+ # Expect the atom list length to go from 0 to 1.
636+ # Expect the atom attributes are successfully loaded.
637+ (
638+ None ,
639+ "C" ,
640+ [0.1 , 0.2 , 0.3 ],
641+ 1 ,
642+ "C" ,
643+ [0.1 , 0.2 , 0.3 ],
644+ ),
645+ # Case 2: valid atom added to existing atom list.
646+ # Expect the atom list length to go from 1 to 2.
647+ # Expect the atom attributes are successfully loaded.
648+ (
649+ [Atom ("C" , [0 , 0 , 0 ])],
650+ "Ni" ,
651+ [0.8 , 1.2 , 0.9 ],
652+ 2 ,
653+ "Ni" ,
654+ [0.8 , 1.2 , 0.9 ],
655+ ),
656+ ],
657+ )
658+ def test_add_new_atom (existing , atype , xyz , expected_len , expected_element , expected_xyz ):
659+ """Check Structure.add_new_item()"""
660+ structure = Structure (existing , lattice = Lattice ())
661+ structure .add_new_atom (atype = atype , xyz = xyz )
662+ actual_length = len (structure )
663+ assert expected_len == actual_length
664+ atom_object = structure [- 1 ]
665+ assert atom_object .element == expected_element
666+ assert numpy .allclose (atom_object .xyz , expected_xyz )
667+
668+
669+ def test_add_new_atom_duplicate ():
670+ # Case 3: duplicated atom added to the existing atom list.
671+ # Expect the atom to be added and gives a UserWarning.
672+ structure = Structure (
673+ [Atom ("C" , [0.1 , 0.2 , 0.3 ]), Atom ("Ni" , [0.8 , 1.2 , 0.9 ])],
674+ lattice = Lattice (),
675+ )
676+ with pytest .warns (UserWarning ):
677+ structure .add_new_atom (atype = "C" , xyz = [0.1 , 0.2 , 0.3 ])
678+ assert len (structure ) == 3
679+ assert structure [- 1 ].element == "C"
680+ assert numpy .allclose (structure [- 1 ].xyz , [0.1 , 0.2 , 0.3 ])
681+
613682
614683if __name__ == "__main__" :
615684 unittest .main ()
0 commit comments