def test_schema_model_multiple_bases(self): """ Multiple declarative bases on same Connector should fail. """ BaseX = declarative_base() BaseY = declarative_base() class Cats(BaseX): __tablename__ = 'cat' id = Column(Integer, primary_key=True) name = Column(String(250)) class Dogs(BaseY): __tablename__ = 'dog' id = Column(Integer, primary_key=True) name = Column(String(250)) db_file = "{}/pets.db".format(self.working_directory()) c = SqlAlchemyDatabaseConnector(engine_url=f"sqlite:///{db_file}", schema_model=[Cats, Dogs], access=ayeaye.AccessMode.READWRITE ) with self.assertRaises(ValueError) as context: c.connect() self.assertIn("Models passed to `schema_model` must share the same declarative base", str(context.exception) )
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__()
def test_create_db_schema(self): c = SqlAlchemyDatabaseConnector(engine_url="sqlite://", schema_builder=fruit_schemas, access=ayeaye.AccessMode.WRITE, ) c.connect() # check there aren't any tables in the DB here with self.assertRaises(OperationalError) as context: c.session.query(c.schema.Pear).all() self.assertIn("no such table: pear", str(context.exception)) c.create_table_schema() # but there are tables now (but no data in them) all_the_pears = c.session.query(c.schema.Pear).all() self.assertIsInstance(all_the_pears, list) self.assertEqual(0, len(all_the_pears))
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))
def test_add_orm_data_multiple(self): c = SqlAlchemyDatabaseConnector(engine_url="sqlite://", schema_builder=fruit_schemas, access=ayeaye.AccessMode.READWRITE ) c.connect() c.create_table_schema() with self.assertRaises(ValueError) as context: c.add({'variety': 'Cavendish'}) self.assertIn("Dictionary can only be used in single schema mode", str(context.exception)) c.add(c.schema.Pear(variety="D'Anjou")) c.add(c.schema.Bananna(variety="Cavendish")) c.commit() # read back mixed types with primary key values belonging to each table (i.e. both are 1) mixed_records = [r.__tablename__ + str(r.id) for r in c] expected = "pear1 bananna1" self.assertEqual(expected, " ".join(mixed_records))