Exemple #1
0
    def test_stave_reader_project(self, project_name: str):
        build_ontology(self.sql_db, project_name)

        # Query packs in this project directly.
        pack_count: int = query(
            self.sql_db,
            f"SELECT Count(*) FROM {self.datapack_table}, {self.project_table} "
            f"WHERE {self.datapack_table}.project_id = {self.project_table}.id "
            f"AND {self.project_table}.name = '{project_name}'",
        ).fetchone()[0]

        # Read the data packs using the reader.
        nlp: Pipeline[DataPack] = Pipeline[DataPack]()
        nlp.set_reader(
            StaveDataPackSqlReader(),
            config={
                "stave_db_path": self.sql_db,
                "target_project_name": project_name,
            },
        )
        nlp.initialize()

        read_pack_count = 0
        for _ in nlp.process_dataset():
            read_pack_count += 1

        self.assertEqual(pack_count, read_pack_count)
Exemple #2
0
    def test_stave_reader_project(self, project_name: str):
        def build_ontology():
            onto_path = "./stave_test_onto"
            res = self._query(
                f'SELECT ontology FROM nlpviewer_backend_project '
                f'WHERE nlpviewer_backend_project.name = '
                f'"{project_name}"').fetchone()[0]
            with tempfile.NamedTemporaryFile('w') as onto_file:
                onto_file.write(res)
                OntologyCodeGenerator().generate(onto_file.name,
                                                 onto_path,
                                                 lenient_prefix=True)
            # Make sure the newly created path is in the python path.
            sys.path.append(onto_path)

            # Make sure we can import the newly generated modules.
            try:
                importlib.import_module('edu.cmu')
            except Exception:
                pass

        build_ontology()

        # Query packs in this project directly.
        pack_count: int = self._query(
            f"SELECT Count(*) FROM {self.datapack_table}, {self.project_table} "
            f"WHERE {self.datapack_table}.project_id = {self.project_table}.id "
            f"AND {self.project_table}.name = '{project_name}'").fetchone()[0]

        # Read the data packs using the reader.
        nlp: Pipeline[DataPack] = Pipeline[DataPack]()
        nlp.set_reader(StaveDataPackSqlReader(),
                       config={
                           "stave_db_path": self.sql_db,
                           "target_project_name": project_name
                       })
        nlp.initialize()

        read_pack_count = 0
        for _ in nlp.process_dataset():
            read_pack_count += 1

        self.assertEqual(pack_count, read_pack_count)