Giter VIP home page Giter VIP logo

Comments (14)

miguelgrinberg avatar miguelgrinberg commented on September 8, 2024

The default app is used for a couple of things: (1) to enable the test command to be launched from manage.py; and (2) to enable the calls to url_for() within tests to work.

You can address (1) by running the tests from a separate script:

import unittest
tests = unittest.TestLoader().discover('tests')
unittest.TextTestRunner(verbosity=2).run(tests)

Unfortunately (2) is a bit harder to address, because url_for() normally wants a request context. I haven't tried this, but probably setting SERVER_NAME to some hostname in the testing configuration will allow url_for() to run. I'm not sure if the resulting URLs will work with the test client though, maybe a fix is needed there as well, since the test client expects relative URLs without hostname.

from flasky.

miguelgrinberg avatar miguelgrinberg commented on September 8, 2024

Actually, regarding (2) above, I think you can easily emulate what Flask-Script does, which is to run commands under a test request context. A test context can be created in the setUp() methods of the test cases.

from flasky.

iyawnis avatar iyawnis commented on September 8, 2024

Thank you for the pointers, I will investigate these options. Before closing, I feel I should say that your book is amazing, great piece of work!

from flasky.

v-ken avatar v-ken commented on September 8, 2024

Related to this issue, I was wondering if there was a way to use a different database URI for the unit test? Because an app is created (which initialises the db) using the 'default' config in the manage.py script, I am experiencing some difficulties changing the database URI when another app is created in the unit test self.app = create_app('testing'). I've set the SQLALCHEMY_DATABASE_URI to a different value in TestingConfig but database operations in the unit tests seems to still be performed on the default db URI.

note: Not sure if it makes a difference but i'm using mysql

from flasky.

iyawnis avatar iyawnis commented on September 8, 2024

@v-ken my solution to the default config problem was providing the system variable when I wanted a different config. So when I run tests, I set FLASK_CONFIG = testing on my system.

from flasky.

v-ken avatar v-ken commented on September 8, 2024

Thanks. That's definitely a solution. Was wondering if there was a way to reset the sqlalchemy DB to use the new URI? Seems like a much cleaner implementation as we can fully rely on self.app = create_app('testing') in the unit test.

from flasky.

miguelgrinberg avatar miguelgrinberg commented on September 8, 2024

@v-ken The app that is created as part of setting up Flask-Script is not related to the app that is created for the tests, so I'm not sure why the interfere with each other. Have you debugged your issue to understand what is exactly the problem?

from flasky.

v-ken avatar v-ken commented on September 8, 2024

@miguelgrinberg Thanks for your response. That's what I thought as well. But I think both apps might be referencing a single db = SQLAlchemy() instance. And I'm not sure if this is the reason but according to the Flask-SQLAlchemy docs:

Note that some of those cannot be modified after the engine was created so make sure to configure as early as possible and to not modify them at runtime.

I tested by cloning this repo and setting the following in the .env file:

FLASK_CONFIG=development
TEST_DATABASE_URL=mysql://username:password@localhost/db_test
DEV_DATABASE_URL=mysql://username:password@localhost/db

from flasky.

miguelgrinberg avatar miguelgrinberg commented on September 8, 2024

What happened when you tested the .env file above? Did you get any error?

from flasky.

v-ken avatar v-ken commented on September 8, 2024

I did get some errors when I ran the unit test but the point was that I noticed all the DB operations were performed on the DEV_DATABASE_URL instead of TEST_DATABASE_URL.
Correct me if I'm wrong but even though FLASK_CONFIG is set to development, I would've thought that the app created in the unit test should use the 'testing' config?

In test setUp:

self.app = create_app('testing')

from flasky.

v-ken avatar v-ken commented on September 8, 2024

@miguelgrinberg Can you confirm that this issue isn't happening on your end? Just want to make sure that it isn't a mistake on my side. Thanks!

from flasky.

v-ken avatar v-ken commented on September 8, 2024

Hi @miguelgrinberg , did you get the time to confirm if this is an issue on your end as well? Can I open this as a new issue?

from flasky.

miguelgrinberg avatar miguelgrinberg commented on September 8, 2024

Sorry I did not answer, @v-ken. This does not happen here, when I run the tests a data-test.sqlite file is created, and my debug database is unaffected. Are you using a pristine copy of the project or do you have changes?

from flasky.

ithinco avatar ithinco commented on September 8, 2024

I got the same problem as @v-ken met, after add the test case of "test_roles_and_permissions" and "test_anonymous_user", I got the error:

sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) UNIQUE constraint failed: users.email [SQL: u'INSERT INTO users (email, username, role_id, password_hash) VALUES (?, ?, ?, ?)'] [parameters: ('[email protected]', None, 3, 'pbkdf2:sha1:1000$R377YAwK$7ac350e7f748f8591774be665bd1dd1ce5d5bf79')]

and the data-test.sqlite is created but empty. I ran migrate and upgrade command but didn't work.

from flasky.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.