Skip to content

Orm#176

Open
HenrySwanson wants to merge 6 commits intomasterfrom
orm
Open

Orm#176
HenrySwanson wants to merge 6 commits intomasterfrom
orm

Conversation

@HenrySwanson
Copy link
Contributor

This is a re-attempt at #172 .

TL;DR: Using the SqlAlchemy ORM makes testing easier, due to being able to construct the DB in-memory, and also making modifications to the DB before/during tests easier (fewer inline SQL queries).

Ideally we move all the db interactions through SqlAlchemy, but that's a long-term project.

Still some improvements to do, in particular around flask.g (should be loaded when the app-context is pushed, not the request-context).

IMO, this makes it significantly easier to maintain tests and write new ones.

Testing is still just `python -m pytest` (make sure you're in the virtualenv!)
Instead, create the engine in init, and create connections on each request.

It's a teeny bit faster, but the more important part is it makes the fixtures
I'm about to create in the next commit easier.
Most of helpers.py was about writing SQL queries in docstrings, and it
turns out that that can be all but eliminated by using the ORM. Wasn't
able to get mypy type checking to work out yet but I've heard it's
possible.

IMO this is much cleaner to read, and the minor performance overhead
doesn't really matter for us, so the tradeoff is worth it.

Also, if we convert all the modules to do this, we can clean up a lot
of the database/*.sql scripts (maybe remove them altogether).
This class makes form-parsing much easier. Could certainly be fleshed
out and generalized, but that's beyond the scope of this diff.
Note that the ORM setup makes this testing much nicer:
 - the declarative definition of the schema means that we can
   easily initialize the test db
 - the ORM doesn't care about sqlite vs. mariadb differences
 - ORM object construction > manual SQL INSERT queries
Otherwise how do we get beautiful f-strings?

Also, my own PYTHONPATH has /ruddock in it, which is why pytest works for me.
That doesn't seem to be the case on Travis. Not sure why it's succeeding for
other people.
@HenrySwanson HenrySwanson mentioned this pull request Jan 30, 2020
@HenrySwanson
Copy link
Contributor Author

And if anyone can figure out why test_constitution still fails for me, I will be your friend forever

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant