def test_good_structures(self):
        test_structures_path = (Path(__file__).resolve().parent.joinpath(
            "../../optimade/server/data/test_structures.json"))
        with open(test_structures_path, "r") as f:
            good_structures = json.load(f)
            # adjust dates as mongo would
        for doc in good_structures:
            doc["last_modified"] = doc["last_modified"]["$date"]

        for structure in good_structures:
            StructureResource(**StructureMapper.map_back(structure))
    def test_list_length_aliases(self):
        from optimade.server.mappers import StructureMapper

        transformer = MongoTransformer(mapper=StructureMapper())
        parser = LarkParser(version=self.version, variant=self.variant)

        self.assertEqual(
            transformer.transform(parser.parse("elements LENGTH 3")),
            {"nelements": 3})

        self.assertEqual(
            transformer.transform(
                parser.parse('elements HAS "Li" AND elements LENGTH = 3')),
            {"$and": [{
                "elements": {
                    "$in": ["Li"]
                }
            }, {
                "nelements": 3
            }]},
        )

        self.assertEqual(
            transformer.transform(parser.parse("elements LENGTH > 3")),
            {"nelements": {
                "$gt": 3
            }},
        )
        self.assertEqual(
            transformer.transform(parser.parse("elements LENGTH < 3")),
            {"nelements": {
                "$lt": 3
            }},
        )
        self.assertEqual(
            transformer.transform(parser.parse("elements LENGTH = 3")),
            {"nelements": 3})
        self.assertEqual(
            transformer.transform(
                parser.parse("cartesian_site_positions LENGTH <= 3")),
            {"nsites": {
                "$lte": 3
            }},
        )
        self.assertEqual(
            transformer.transform(
                parser.parse("cartesian_site_positions LENGTH >= 3")),
            {"nsites": {
                "$gte": 3
            }},
        )
    def test_bad_structures(self):
        test_structures_path = (Path(__file__).resolve().parent.joinpath(
            "test_bad_structures.json"))
        with open(test_structures_path, "r") as f:
            bad_structures = json.load(f)
        for doc in bad_structures:
            doc["last_modified"] = doc["last_modified"]["$date"]

        for ind, structure in enumerate(bad_structures):
            with self.assertRaises(
                    ValidationError,
                    msg=
                    "Bad test structure {} failed to raise an error\nContents: {}"
                    .format(ind, json.dumps(structure, indent=2)),
            ):
                StructureResource(**StructureMapper.map_back(structure))
Beispiel #4
0
def test_list_length_aliases():
    """Check LENGTH aliases for lists"""
    from optimade.server.mappers import StructureMapper

    transformer = AiidaTransformer(mapper=StructureMapper())
    parser = LarkParser(version=VERSION, variant=VARIANT)

    assert transformer.transform(parser.parse("elements LENGTH 3")) == {
        "nelements": 3
    }

    assert transformer.transform(
        parser.parse('elements HAS "Li" AND elements LENGTH = 3')) == {
            "and": [{
                "elements": {
                    "contains": ["Li"]
                }
            }, {
                "nelements": 3
            }]
        }

    assert transformer.transform(parser.parse("elements LENGTH > 3")) == ({
        "nelements": {
            ">": 3
        }
    })
    assert transformer.transform(parser.parse("elements LENGTH < 3")) == ({
        "nelements": {
            "<": 3
        }
    })
    assert transformer.transform(parser.parse("elements LENGTH = 3")) == {
        "nelements": 3
    }
    assert transformer.transform(
        parser.parse("cartesian_site_positions LENGTH <= 3")) == {
            "nsites": {
                "<=": 3
            }
        }
    assert transformer.transform(
        parser.parse("cartesian_site_positions LENGTH >= 3")) == {
            "nsites": {
                ">=": 3
            }
        }
Beispiel #5
0
def transformer():
    from optimade.server.mappers import StructureMapper

    return ElasticTransformer(mapper=StructureMapper())
Beispiel #6
0
    def test_more_good_structures(self):
        good_structures = optimade.server.data.structures

        for structure in good_structures:
            StructureResource(**StructureMapper.map_back(structure))