I have a project that I've been working on for a while, which is written in Flask, and uses SQLAlchemy with the Declarative extension http://flask.pocoo.org/docs/patterns/sqlalchemy/. I've recently decided to start unit testing my project, but for the life of me, I can't seem to figure out how to make it work. I looked at http://flask.pocoo.org/docs/testing/, but I can't seem to make it work. I also looked at a few questions on Stack Overflow, including: http://stackoverflow.com/questions/833626/i-need-a-sample-of-python-unit-testing-sqlalchemy-model-with-nose . I tried a mix of things from different websites, but am unable to find something that works correctly. class StopsTestCase(unittest.TestCase): def setUp(self): self.engine = create_engine('sqlite:///:memory:') self.session = scoped_session(sessionmaker(autocommit=False, autoflush=False, bind=self.engine)) models.Base = declarative_base() models.Base.query = self.session.query_property() models.Base.metadata.create_all(bind=self.engine) def test_empty_db(self): stops = session.query(models.Stop).all() assert len(stops) == 0 def tearDown(self): session.remove() if __name__ == '__main__': unittest.main() Unfortunately, the best I can seem to get, causes the following error. OperationalError: (OperationalError) no such table: stops u'SELECT stops.agency_id AS stops_agency_id, stops.id AS stops_id, stops.name AS stops_name, stops."desc" AS stops_desc, stops.lat AS stops_lat, stops.lon AS stops_lon, stops.zone_id AS stops_zone_id \nFROM stops' () ---------------------------------------------------------------------- Ran 1 test in 0.025s FAILED (errors=1) Any help on this would be greatly appreciated. If someone has ever been through this before, and made it work, I would like some pointers! Thanks in advance. Jamie Starke -- Software Developer Victoria, BC www.jamiestarke.com
Something that immediately jumps out at me here is that you are effectively monkey-patching your Base class with a new one: > models.Base = declarative_base() Since models.Stop would have to inherit the Base class, it should had been set prior to your test class setup. If you removed that line do you have more success? Everything else seems to be more or less the same as what I've done with my own SQLAlchemy tests. __________________________________ David McKeone On 2012-10-07, at 8:13 AM, Jamie Starke wrote: > I have a project that I've been working on for a while, which is written in Flask, and uses SQLAlchemy with the Declarative extension http://flask.pocoo.org/docs/patterns/sqlalchemy/. I've recently decided to start unit testing my project, but for the life of me, I can't seem to figure out how to make it work. > > I looked at http://flask.pocoo.org/docs/testing/, but I can't seem to make it work. I also looked at a few questions on Stack Overflow, including: http://stackoverflow.com/questions/833626/i-need-a-sample-of-python-unit-testing-sqlalchemy-model-with-nose. > > I tried a mix of things from different websites, but am unable to find something that works correctly. > > class StopsTestCase(unittest.TestCase): > > def setUp(self): > self.engine = create_engine('sqlite:///:memory:') > self.session = scoped_session(sessionmaker(autocommit=False, > autoflush=False, > bind=self.engine)) > models.Base = declarative_base() > models.Base.query = self.session.query_property() > > models.Base.metadata.create_all(bind=self.engine) > > def test_empty_db(self): > stops = session.query(models.Stop).all() > assert len(stops) == 0 > > def tearDown(self): > session.remove() > > if __name__ == '__main__': > unittest.main() > Unfortunately, the best I can seem to get, causes the following error. > > OperationalError: (OperationalError) no such table: stops u'SELECT stops.agency_id AS stops_agency_id, stops.id AS stops_id, stops.name AS stops_name, stops."desc" AS stops_desc, stops.lat AS stops_lat, stops.lon AS stops_lon, stops.zone_id AS stops_zone_id \nFROM stops' () > > ---------------------------------------------------------------------- > Ran 1 test in 0.025s > > FAILED (errors=1) > Any help on this would be greatly appreciated. If someone has ever been through this before, and made it work, I would like some pointers! Thanks in advance. > > Jamie Starke > -- > Software Developer > Victoria, BC > www.jamiestarke.com > >
Brilliant! Worked like a charm! Thank you very much for your help David! Jamie Starke -- Software Developer Victoria, BC www.jamiestarke.com On Sun, Oct 7, 2012 at 3:59 AM, David McKeone <david@artsman.com> wrote: > Something that immediately jumps out at me here is that you are > effectively monkey-patching your Base class with a new one: > > models.Base = declarative_base() > > > Since models.Stop would have to inherit the Base class, it should had been > set prior to your test class setup. If you removed that line do you have > more success? Everything else seems to be more or less the same as what > I've done with my own SQLAlchemy tests. > > __________________________________ > *David McKeone* > * > * > > On 2012-10-07, at 8:13 AM, Jamie Starke wrote: > > I have a project that I've been working on for a while, which is written > in Flask, and uses SQLAlchemy with the Declarative extension > http://flask.pocoo.org/docs/patterns/sqlalchemy/. I've recently decided > to start unit testing my project, but for the life of me, I can't seem to > figure out how to make it work. > > I looked at http://flask.pocoo.org/docs/testing/, but I can't seem to > make it work. I also looked at a few questions on Stack Overflow, > including: > http://stackoverflow.com/questions/833626/i-need-a-sample-of-python-unit-testing-sqlalchemy-model-with-nose > . > > I tried a mix of things from different websites, but am unable to find > something that works correctly. > > class StopsTestCase(unittest.TestCase): > > def setUp(self): > self.engine = create_engine('sqlite:///:memory:') > self.session = scoped_session(sessionmaker(autocommit=False, > autoflush=False, > bind=self.engine)) > models.Base = declarative_base() > models.Base.query = self.session.query_property() > > models.Base.metadata.create_all(bind=self.engine) > > def test_empty_db(self): > stops = session.query(models.Stop).all() > assert len(stops) == 0 > > def tearDown(self): > session.remove() > > if __name__ == '__main__': > unittest.main() > Unfortunately, the best I can seem to get, causes the following error. > > OperationalError: (OperationalError) no such table: stops u'SELECT > stops.agency_id AS stops_agency_id, stops.id AS stops_id, stops.name AS > stops_name, stops."desc" AS stops_desc, stops.lat AS stops_lat, stops.lon > AS stops_lon, stops.zone_id AS stops_zone_id \nFROM stops' () > > ---------------------------------------------------------------------- > Ran 1 test in 0.025s > > FAILED (errors=1) > Any help on this would be greatly appreciated. If someone has ever been > through this before, and made it work, I would like some pointers! Thanks > in advance. > > Jamie Starke > -- > Software Developer > Victoria, BC > www.jamiestarke.com > > > >