1"""Tests for data module"""
2import os
3import abipy.data as abidata
4
5from abipy.core.testing import AbipyTest
6
7
8class DataModuleTest(AbipyTest):
9    """Test data module."""
10
11    def test_data_api(self):
12        """Testing abipy.data API."""
13        ncpseudo_h = abidata.pseudo("01h.pspgth")
14        assert ncpseudo_h.isnc
15        paw_table = abidata.pseudos("1h.paw", "28ni.paw")
16        assert paw_table.allpaw
17        assert os.path.isfile(abidata.cif_file("al.cif"))
18        assert os.path.isfile(abidata.pyscript("plot_bz.py"))
19
20        d = abidata.get_mp_structures_dict()
21        assert isinstance(d, dict) and d is abidata.get_mp_structures_dict()
22
23        structure = abidata.structure_from_cif("gan2.cif")
24        assert hasattr(structure, "to_abivars")
25
26        structure = abidata.structure_from_mpid("mp-4820")
27        assert hasattr(structure, "to_abivars")
28        with self.assertRaises(KeyError):
29            abidata.structure_from_mpid("foobar")
30
31
32class FilesGeneratorTest(AbipyTest):
33
34    def test_abinit_files_generator(self):
35        """Testing AbinitFilesGenerator."""
36        class MyGenerator(abidata.AbinitFilesGenerator):
37            """This class generates the output files used in the unit tests and in the examples."""
38            # Subclasses must define the following class attributes:
39            # List of pseudos (basenames) in abipy/data/pseudos
40            pseudos = ["14si.pspnc"]
41
42            # Mapping old_name --> new_name for the output files that must be preserved.
43            files_to_save = {
44                "out_DS1_DEN.nc": "si_DEN.nc",
45                "out_DS2_GSR.nc": "si_nscf_GSR.nc",
46                "out_DS2_WFK.nc": "si_nscf_WFK.nc",
47                "out_DS1_GSR.nc": "si_scf_GSR.nc",
48                "out_DS1_WFK.nc": "si_scf_WFK.nc",
49            }
50
51        abgen = MyGenerator()
52        print(abgen)
53        assert abgen.executable == "abinit"
54        assert len(abgen.make_filesfile_str())
55
56    def test_anaddb_files_generator(self):
57        """Testing AnaddbFilesGenerator."""
58        class MyGenerator(abidata.AnaddbFilesGenerator):
59            """This class generates the output files used in the unit tests and in the examples."""
60
61            # Mapping old_name --> new_name for the output files that must be preserved.
62            files_to_save = {
63                "out_PHBST.nc": "trf2_5.out_PHBST.nc",
64                "out_PHDOS.nc": "trf2_5.out_PHDOS.nc",
65            }
66
67            in_ddb = "trf2_3.ddb.out"
68
69        anagen = MyGenerator()
70        print(anagen)
71        assert anagen.executable == "anaddb"
72        assert len(anagen.make_filesfile_str())
73