# IODATA is an input and output module for quantum chemistry.# Copyright (C) 2011-2019 The IODATA Development Team## This file is part of IODATA.## IODATA is free software; you can redistribute it and/or# modify it under the terms of the GNU General Public License# as published by the Free Software Foundation; either version 3# of the License, or (at your option) any later version.## IODATA is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU General Public License for more details.## You should have received a copy of the GNU General Public License# along with this program; if not, see <http://www.gnu.org/licenses/># --"""Unit tests for iodata.__main__."""importosimportsubprocessimportsysfromfunctoolsimportpartialfromimportlib.resourcesimportas_file,filesfromtypingimportOptionalfromwarningsimportwarnimportpytestfromnumpy.testingimportassert_allclose,assert_equalfrom..__main__importconvertasconvfnfrom..apiimportload_many,load_onefrom..utilsimportFileFormatError,PrepareDumpError,PrepareDumpWarningdef_convscript(infn:str,outfn:str,many:bool=False,infmt:Optional[str]=None,outfmt:Optional[str]=None,allow_changes:bool=False,):"""Simulate the convert function by calling iodata-convert in a subprocess."""args=[sys.executable,"-m","iodata.__main__",infn,outfn]ifmany:args.append("-m")ifinfmtisnotNone:args.append(f"--infmt={infmt}")ifoutfmtisnotNone:args.append(f"--outfmt={outfmt}")ifallow_changes:args.append("-c")cp=subprocess.run(args,capture_output=True,check=False,encoding="utf8")ifcp.returncode==0:ifallow_changesand"PrepareDumpWarning"incp.stderr:warn(PrepareDumpWarning(cp.stderr),stacklevel=2)else:if"PrepareDumpError"incp.stderr:raisePrepareDumpError(cp.stderr)if"FileFormatError"incp.stderr:raiseFileFormatError(cp.stderr)raiseRuntimeError(f"Failure not processed.\n{cp.stderr}")def_check_convert_one(myconvert,tmpdir):outfn=os.path.join(tmpdir,"tmp.xyz")withas_file(files("iodata.test.data").joinpath("hf_sto3g.fchk"))asinfn:myconvert(infn,outfn,allow_changes=False)iodata=load_one(outfn)assertiodata.natom==2assert_equal(iodata.atnums,[9,1])assert_allclose(iodata.atcoords,[[0.0,0.0,0.190484394],[0.0,0.0,-1.71435955]])def_check_convert_one_changes(myconvert,tmpdir):outfn=os.path.join(tmpdir,"tmp.mkl")withas_file(files("iodata.test.data").joinpath("hf_sto3g.fchk"))asinfn:withpytest.raises(PrepareDumpError):myconvert(infn,outfn,allow_changes=False)assertnotos.path.isfile(outfn)withpytest.warns(PrepareDumpWarning):myconvert(infn,outfn,allow_changes=True)iodata=load_one(outfn)assertiodata.natom==2assert_equal(iodata.atnums,[9,1])assert_allclose(iodata.atcoords,[[0.0,0.0,0.190484394],[0.0,0.0,-1.71435955]])