Exemple #1
0
    def test_schema_model_multiple(self):
        """
        see :method:`` but with a list, same idea as how the schema_builder argument can return a
        single schema or list.
        """
        Base = declarative_base()

        class Cats(Base):
            __tablename__ = 'cat'
            id = Column(Integer, primary_key=True)
            name = Column(String(250))

        class Dogs(Base):
            __tablename__ = 'dog'
            id = Column(Integer, primary_key=True)
            name = Column(String(250))

        db_file = "{}/pets.db".format(self.working_directory())
        pets = SqlAlchemyDatabaseConnector(engine_url=f"sqlite:///{db_file}",
                                           schema_model=[Cats, Dogs],
                                           access=ayeaye.AccessMode.READWRITE
                                           )
        pets.create_table_schema()

        with self.assertRaises(ValueError) as context:
            pets.add({'name': 'Lady'})
        self.assertIn("Dictionary can only be used in single schema mode", str(context.exception))

        pets.add(pets.schema.Cats(name="Lady"))
        pets.add(pets.schema.Dogs(name="Lady"))

        pets.commit()
        pets.close_connection()
Exemple #2
0
    def test_double_close_sqlite(self):
        """
        TODO - can't reproduce the "Cannot operate on a closed database." Sqlite error.
        """
        db_file = "{}/fruit.db".format(self.working_directory())
        c = SqlAlchemyDatabaseConnector(engine_url=f"sqlite:///{db_file}",
                                        schema_builder=fruit_schemas,
                                        access=ayeaye.AccessMode.READWRITE
                                        )
        c.connect()
        c.create_table_schema()

        c.add(c.schema.Pear(variety="Williams"))
        # c.commit()

        c.close_connection()
        c.__del__()
Exemple #3
0
    def test_schema_model_single(self):
        """
        Instead of passing a callable (i.e. schema_builder argument) pass an SqlAlchemy model
        which already has a declarative base.
        """
        Base = declarative_base()

        class Rodents(Base):
            __tablename__ = 'rodent'
            id = Column(Integer, primary_key=True)
            species = Column(String(250), nullable=False)

        db_file = "{}/rodents.db".format(self.working_directory())
        rodents = SqlAlchemyDatabaseConnector(engine_url=f"sqlite:///{db_file}",
                                              schema_model=Rodents,
                                              access=ayeaye.AccessMode.READWRITE
                                              )
        rodents.create_table_schema()
        rodents.add({'species': 'Yellow-necked mouse'})
        rodents.commit()
        rodents.close_connection()
Exemple #4
0
    def test_on_disk(self):
        """
        All the other tests are in memory. Ensure to disk works.

        This test is also being created because windows is refusing to delete open files so
        confirmation that close_connection() is working was experimented with using lsof under
        OSX. But the file handle isn't left open so can't be part of this test.
        """
        db_file = "{}/fruit.db".format(self.working_directory())
        c = SqlAlchemyDatabaseConnector(engine_url=f"sqlite:///{db_file}",
                                        schema_builder=fruit_schemas,
                                        access=ayeaye.AccessMode.READWRITE
                                        )
        c.connect()
        c.create_table_schema()

        c.add(c.schema.Pear(variety="Comice"))
        c.commit()

        c.close_connection()

        self.assertTrue(os.access(db_file, os.R_OK))
Exemple #5
0
    def test_two_databases(self):
        """
        The declarative base is created by each SqlAlchemyDatabaseConnector. Ensure models passed
        to each Connector stay in their own engines.
        """
        db_file = "{}/fruit.db".format(self.working_directory())
        fruit = SqlAlchemyDatabaseConnector(engine_url=f"sqlite:///{db_file}",
                                            schema_builder=fruit_schemas,
                                            access=ayeaye.AccessMode.READWRITE
                                            )
        fruit.create_table_schema()

        db_file = "{}/people.db".format(self.working_directory())
        people = SqlAlchemyDatabaseConnector(engine_url=f"sqlite:///{db_file}",
                                             schema_builder=people_schema,
                                             access=ayeaye.AccessMode.READWRITE
                                             )
        people.create_table_schema()

        # Tables creates in correct DB
        # ----------------------
        inspector = inspect(fruit.engine)
        fruit_tables = {table_name for table_name in inspector.get_table_names()}
        self.assertEqual({'bananna', 'pear'}, fruit_tables)

        inspector = inspect(people.engine)
        people_tables = {table_name for table_name in inspector.get_table_names()}
        self.assertEqual({'person'}, people_tables)

        # Tables can be used in the normal way
        # ----------------------
        fruit.add(fruit.schema.Pear(variety="Comice"))
        fruit.commit()
        fruit.close_connection()

        people.add({'surname': 'Attenborough'})
        people.commit()
        people.close_connection()