Ejemplo n.º 1
0
 def _create_db(self, url: str):
     """Create Spine DB at url
     """
     logging.info(f"Creating a new Spine DB at '{url}'")
     try:
         create_new_spine_database(url)
     except SQLAlchemyArgumentError as e:
         raise RuntimeError(e)
     except SpineDBAPIError as e:
         raise RuntimeError(e)
     else:
         self._open_db_writing(url)
    def test_import_data_integration(self):
        try:
            os.remove(TEMP_SQLITE_FILENAME)
        except OSError:
            pass

        # create a in memory database with objects, relationship, parameters and values
        create_new_spine_database("sqlite:///" + TEMP_SQLITE_FILENAME)
        db_map = DiffDatabaseMapping("sqlite:///" + TEMP_SQLITE_FILENAME,
                                     username="******")

        object_c = ["example_class", "other_class"]  # 2 items
        objects = [["example_class", "example_object"],
                   ["other_class", "other_object"]]  # 2 items
        relationship_c = [[
            "example_rel_class", ["example_class", "other_class"]
        ]]  # 1 item
        relationships = [[
            "example_rel_class", ["example_object", "other_object"]
        ]]  # 1 item
        obj_parameters = [["example_class", "example_parameter"]]  # 1 item
        rel_parameters = [["example_rel_class", "rel_parameter"]]  # 1 item
        object_p_values = [[
            "example_class", "example_object", "example_parameter", 3.14
        ]]  # 1 item
        rel_p_values = [[
            "example_rel_class", ["example_object", "other_object"],
            "rel_parameter", 2.718
        ]]  # 1

        num_imports, errors = import_data(
            db_map,
            object_classes=object_c,
            relationship_classes=relationship_c,
            object_parameters=obj_parameters,
            relationship_parameters=rel_parameters,
            objects=objects,
            relationships=relationships,
            object_parameter_values=object_p_values,
            relationship_parameter_values=rel_p_values,
        )

        self.assertEqual(num_imports, 10)
        self.assertEqual(len(errors), 0)

        try:
            os.remove(TEMP_SQLITE_FILENAME)
        except OSError:
            pass
Ejemplo n.º 3
0
 def setUp(self):
     if os.path.exists('TestDatabaseAPI.sqlite'):
         os.remove('TestDatabaseAPI.sqlite')
     self.object_number = 100
     self.object_class_number = 100
     self.number_wide_relationship = 100
     self.number_of_parameter = 100
     self.number_of_parameter_value = 100
     self.db = create_new_spine_database('sqlite:///TestDatabaseAPI.sqlite')
     self.db_map = DatabaseMapping('sqlite:///TestDatabaseAPI.sqlite')
 def setUpClass(cls):
     """Overridden method. Runs once before all tests in this class."""
     logging.basicConfig(
         stream=sys.stderr,
         level=logging.DEBUG,
         format="%(asctime)s %(levelname)s: %(message)s",
         datefmt="%Y-%m-%d %H:%M:%S",
     )
     try:
         os.remove("temp.sqlite")
     except OSError:
         pass
     db_url = "sqlite:///temp.sqlite"
     engine = create_new_spine_database(db_url)
     cls.db_map = DiffDatabaseMapping(db_url, username="******")
Ejemplo n.º 5
0
    def test_create_engine_and_session(self):

        db = create_new_spine_database(
            'sqlite:///test_create_engine_and_session.sqlite')
        db.connect()

        m = DatabaseMapping('sqlite:///test_create_engine_and_session.sqlite',
                            create_all=False)

        assert isinstance(m, DatabaseMapping)

        assert not isinstance(m.session, Session)

        m.create_engine_and_session()

        assert isinstance(m.session, Session)
Ejemplo n.º 6
0
 def test_create_db(self):
     # create a in memory database
     m = MetaData()
     db = create_new_spine_database('sqlite://')
     m.reflect(db.engine)
     assert len(m.tables.values()) == 9