<div dir="ltr"><div>Nicolas, thanks. That helped a lot and I managed to write a full example that shows this: <a href="https://code-maven.com/flask-counter-sqlite-sqlalchemy">https://code-maven.com/flask-counter-sqlite-sqlalchemy</a></div><div></div><div><br></div><div><br></div><div>However I still have an issue. We use yoyo to manage migrations and I wanted to try the automap feature of SQLAlchemy.  I could not solve it elegantly just this way:</div><div><a href="https://code-maven.com/flask-counter-sqlite-sqlalchemy-yoyo">https://code-maven.com/flask-counter-sqlite-sqlalchemy-yoyo</a></div><div><br></div><div>* There is a "global" in the model.py <br></div><div>* I could not figure out how to create the classes for the table(s) so I added them to a "base" class.</div><div>* I had to change the query code from <span class="gmail-typ">Counter</span><span class="gmail-pun">.</span><span class="gmail-pln">query</span><span class="gmail-pun">.</span><span class="gmail-pln">all</span><span class="gmail-pun">() <br></span></div><div><span class="gmail-pun"><br></span></div><div><span class="gmail-pun">Any suggestions there?</span></div><div><span class="gmail-pun"><br></span></div><div><span class="gmail-pun">Gabor<br></span></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, May 12, 2021 at 12:12 PM Nicolas Le Manchet <<a href="mailto:nicolas@lemanchet.fr">nicolas@lemanchet.fr</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi,<br>
<br>
You should take a look at application factories (<a href="https://flask.palletsprojects.com/en/1.1.x/patterns/appfactories/" rel="noreferrer" target="_blank">https://flask.palletsprojects.com/en/1.1.x/patterns/appfactories/</a>). It's a pattern that allows to have multiple applications configured differently in the same interpreter, which tends to make testing easier.<br>
<br>
On Wed, May 12, 2021, at 11:00, Gabor Szabo wrote:<br>
> Hi,<br>
> <br>
> I am working on an application using Flask-sqlalchemy and I am not sure <br>
> how to write tests properly.<br>
> <br>
> In all the examples I have seen so far the main flask file has the line<br>
> <br>
>     db = SQLAlcehmy(app)<br>
> <br>
> meaning this is executed when the main file of Flask is loaded. so if <br>
> the test file has<br>
> <br>
>     import app<br>
> <br>
> that would still happen at compile time. This way I have only one <br>
> chance to set up a test-database , right before that import-statement.<br>
> <br>
> In order to make it easier to use different test database we moved the <br>
>     db = SQLAlcehmy(app)<br>
> call inside a function decorated with before_first_request.<br>
> This ensures the code is called for regular use and we can call the <br>
> before_first_request method for each test. Because we don't have db at <br>
> load time we also had to move all the class-declarations in this <br>
> function (because they all inherit from db.Model).<br>
> <br>
> The problem is that now all the model classes are scoped to this <br>
> function. We had to use "global" to make them accessible to the rest of <br>
> the code.<br>
> <br>
> That does not look right.<br>
> <br>
> The code can be found here: <a href="https://github.com/rnewstead1/workout-app/" rel="noreferrer" target="_blank">https://github.com/rnewstead1/workout-app/</a> <br>
> (together with the tests)<br>
> And you can actually see our struggle in this video: <br>
> <a href="https://code-maven.com/workout-app-2" rel="noreferrer" target="_blank">https://code-maven.com/workout-app-2</a><br>
> <br>
> I'd appreciate your insight on how to make this nicer or even just a <br>
> link to a code-base that has such tests.<br>
> <br>
> Gabor<br><br>
</blockquote></div></div>