peewee-async
peewee-async copied to clipboard
Test database
How I can create database for testing without real database?
Like:
database = SqliteDatabase(':memory:')
I needed the same thing, so I took the liberty of creating my own SqliteDatabase class (it might exist, but couldn't find anything). Thought someone might find it useful.
https://gist.github.com/xsduan/09fb145da3da3a78f5ca844b155f27aa
Some caveats:
- Since I only needed it to play around with during development without setting up an entire mysql/postgres server, it's a very "works on my machine" deal, e.g. connection pooling is just creating new connections, though I made my best effort to make it presentable.
- I just copied most of the stuff from the AsyncPostgres* and AsyncMysql* classes until they worked more or less, and only verified that it works on the readme example. I may very well have missed a few things.
-
:memory:
Does Not Work. No idea why (maybe something about concurrency?), but writing to file seems like a fair enough compromise to me.
I dunno if sqlite is "useful" enough to make it part of the core, so I'm happy to let it stay a recipe.
@xsduan Each new connection to sqlite with :memory: database creates its own database, distinct from the others, that is removed upon disconnect. Haven't tried the gist yet but may this be the reason for "does not work"?
Async driver for sqlite is not provided actually.