def test_query_construction_1(self):
        """Test query construction."""
        from aiida.tools.dbimporters.plugins.cod import CodDbImporter
        import re

        codi = CodDbImporter()
        q_sql = codi.query_sql(
            id=['1000000', 3000000],
            element=['C', 'H', 'Cl'],
            number_of_elements=5,
            chemical_name=['caffeine', 'serotonine'],
            formula=['C6 H6'],
            volume=[100, 120.005],
            spacegroup='P -1',
            a=[10.0 / 3, 1],
            alpha=[10.0 / 6, 0],
            measurement_temp=[0, 10.5],
            measurement_pressure=[1000, 1001],
            determination_method=['single crystal', None]
        )

        # Rounding errors occur in Python 3 thus they are averted using
        # the following precision stripping regular expressions.
        q_sql = re.sub(r'(\d\.\d{6})\d+', r'\1', q_sql)
        q_sql = re.sub(r'(120.00)39+', r'\g<1>4', q_sql)

        self.assertEqual(q_sql, \
                          'SELECT file, svnrevision FROM data WHERE '
                          "(status IS NULL OR status != 'retracted') AND "
                          '(a BETWEEN 3.332333 AND 3.334333 OR '
                          'a BETWEEN 0.999 AND 1.001) AND '
                          '(alpha BETWEEN 1.665666 AND 1.667666 OR '
                          'alpha BETWEEN -0.001 AND 0.001) AND '
                          "(chemname LIKE '%caffeine%' OR "
                          "chemname LIKE '%serotonine%') AND "
                          "(method IN ('single crystal') OR method IS NULL) AND "
                          "(formula REGEXP ' C[0-9 ]' AND "
                          "formula REGEXP ' H[0-9 ]' AND "
                          "formula REGEXP ' Cl[0-9 ]') AND "
                          "(formula IN ('- C6 H6 -')) AND "
                          '(file IN (1000000, 3000000)) AND '
                          '(cellpressure BETWEEN 999 AND 1001 OR '
                          'cellpressure BETWEEN 1000 AND 1002) AND '
                          '(celltemp BETWEEN -0.001 AND 0.001 OR '
                          'celltemp BETWEEN 10.499 AND 10.501) AND '
                          "(nel IN (5)) AND (sg IN ('P -1')) AND "
                          '(vol BETWEEN 99.999 AND 100.001 OR '
                          'vol BETWEEN 120.004 AND 120.006)')
Пример #2
0
    def test_query_construction_1(self):
        from aiida.tools.dbimporters.plugins.cod import CodDbImporter

        codi = CodDbImporter()
        q = codi.query_sql(id=["1000000", 3000000],
                           element=["C", "H", "Cl"],
                           number_of_elements=5,
                           chemical_name=["caffeine", "serotonine"],
                           formula=["C6 H6"],
                           volume=[100, 120.005],
                           spacegroup="P -1",
                           a=[10.0 / 3, 1],
                           alpha=[10.0 / 6, 0],
                           measurement_temp=[0, 10.5],
                           measurement_pressure=[1000, 1001],
                           determination_method=["single crystal", None])
        self.assertEquals(q, \
                          "SELECT file, svnrevision FROM data WHERE "
                          "(status IS NULL OR status != 'retracted') AND "
                          "(method IN ('single crystal') OR method IS NULL) AND "
                          "(file IN (1000000, 3000000)) AND "
                          "(chemname LIKE '%caffeine%' OR "
                          "chemname LIKE '%serotonine%') AND "
                          "(formula IN ('- C6 H6 -')) AND "
                          "(a BETWEEN 3.33233333333 AND 3.33433333333 OR "
                          "a BETWEEN 0.999 AND 1.001) AND "
                          "(celltemp BETWEEN -0.001 AND 0.001 OR "
                          "celltemp BETWEEN 10.499 AND 10.501) AND "
                          "(vol BETWEEN 99.999 AND 100.001 OR "
                          "vol BETWEEN 120.004 AND 120.006) AND "
                          "(alpha BETWEEN 1.66566666667 AND 1.66766666667 OR "
                          "alpha BETWEEN -0.001 AND 0.001) AND "
                          "(cellpressure BETWEEN 999 AND 1001 OR "
                          "cellpressure BETWEEN 1000 AND 1002) AND "
                          "(formula REGEXP ' C[0-9 ]' AND "
                          "formula REGEXP ' H[0-9 ]' AND "
                          "formula REGEXP ' Cl[0-9 ]') AND "
                          "(nel IN (5)) AND (sg IN ('P -1'))")