How to use a separate database for testing?
See original GitHub issueI am trying to integrate Pony with Flask-Testing (unsuccessfully, so far–see #25 )–and I’m wondering how I can use a separate database for testing. In Flask-SQLAlchemy, for example, this would be achieved by setting the SQLALCHEMY_DATABASE_URI
config option in the test setup
from flask.ext.testing import TestCase
from app import app
class TestApp(TestCase):
TESTING = True
DEBUG = True
SQLALCHEMY_DATABASE_URI = 'sqlite:///tmp/test.db'
def create_app(self):
app.config.from_object(self)
return app
I am not sure how to achieve this with Pony, since each Entity
class must be declared from a Database
that is already configured.
Issue Analytics
- State:
- Created 10 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
How can I set a separate Database for testing? - Stack Overflow
If you're not running two different servers, then your first step is to fix that, and be running different servers. And different database...
Read more >Database Testing Complete Guide (Why, What, and How to ...
I hope this tutorial will help you to focus on why database testing is important and also provide all the details of what...
Read more >Multiple databases and testing - Laracasts
Create test versions of each database. They have to use the same database driver (eg. MySQL) as your real databases. · In your...
Read more >Database (Data) Testing Tutorial with Sample Test Cases
In this Database Testing tutorial, we will learn about different Database Testing concepts like: Differences between User-Interface Testing and ...
Read more >Configuring Separate Spring DataSource for Tests - Baeldung
Configuring Separate Spring DataSource for Tests ... we may want to set up a test data source to use a smaller, faster database...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hi Steven!
At this point we don’t have a preferred way for testing Pony-powered applications. I want to describe the way, which seems most convenient to me. If your have some comments or suggestions, don’t hesitate to tell. It would be good if other developers contribute to the discussion and tell their opinion about the best testing practices and what is missing in Pony in this regard.
1. How to separate database and entity creation from the module import
Although Pony standard examples demonstrate creation of the
Database
instance as a module variable, it may be convenient to create theDatabase
instance along with the corresponding entities inside a function. Something like this:This way you can create
Database
and entities multiple times with different config parameters. After an entity is defined, it can be accessed as a database attribute with the same name:2. How to separate database parameters from entity definitions
In some situations it may be convenient to separate entity definitions and mapping generation. To do this, just pass the database as a parameter to the function:
3. How to fill the database with test data
One of the ways to do this is to define a separate function, like this:
Then you can call this function any time you want to repopulate your database.
4. How to use Pony with Flask-Testing
Currently Pony doesn’t support database URI syntax. Although it is possible to add the database URI support in the SQLAlchemy-compatible way, right now I don’t see the point in it. It seems that specifying arguments of the
Database
in a dict is enough. In the next example I add PONY_DB_PARAMS dictionary to the configuration and then use it for passing parameters to thedefine_database
function which was described earlier. If you think that database URIs have some real benefits, please add a comment which describes these benefits.It is not necessary to store parameters of the test database inside Flask configuration. You can simplify this by hardcoding these parameters inside the setUp method:
Please let me know what do you think about it. Is there anything that we can implement in Pony in order to simplify the testing process?
models/brands.py
test_api.py
it mays be a trick one, and mocks db.bind when it was called