Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to properly test a Python Flask system based on SQLAlchemy Declarative
    text
    copied!<p>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 <a href="http://flask.pocoo.org/docs/patterns/sqlalchemy/" rel="nofollow">http://flask.pocoo.org/docs/patterns/sqlalchemy/</a>. 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. </p> <p>I looked at <a href="http://flask.pocoo.org/docs/testing/" rel="nofollow">http://flask.pocoo.org/docs/testing/</a>, but I can't seem to make it work. </p> <p>I tried a mix of things from different websites, but am unable to find something that works correctly. </p> <pre><code>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() </code></pre> <p>Unfortunately, the best I can seem to get, causes the following error.</p> <pre><code>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) </code></pre> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload