2

I am new to programming in general and this is my first web application in python (flask, sqlalchemy, wtforms, etc). I have been using the realpython.com course 2 as my study material on this subject. I have gotten to the point where i am learning about unit testing and i having trouble getting it to work correctly. I have compared the course example to the examples i found online and i am not seeing the issue with my code.

The problem i am encountering is that the test.py script correctly creates my test.db database but when it attempts to insert a test customer and it puts it into my production db (madsenconcrete.db) instead of my test db (test.db). If i remove the production db from the script directory it will raise this error when it cant find the db because its looking for madsenconcrete.db not test.db.

OperationalError: (sqlite3.OperationalError) no such table: customer [SQL: u'INSERT INTO customer (name, email, telephone, created_date) VALUES (?, ?, ?, ?)'] [parameters: ('Acme Company', 'acme@domain.com', '6125551000', '2016-01-03')]

I am not sure how to troubleshoot this issue. I have doing a lot of stare and compares and i do not see the difference.

import os
import unittest
import datetime
import pytz

from views import app, db
from _config import basedir
from models import Customer


TEST_DB = 'test.db'


class AllTests(unittest.TestCase):
    ############################
    #### setup and teardown ####
    ############################

    # executed prior to each test
    def setUp(self):
        app.config['TESTING'] = True
        app.config['WTF_CSRF_ENABLED'] = False
        app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, TEST_DB)
        app.config['SQLALCHEMY_ECHO'] = True
        self.app = app.test_client()
        db.create_all()

    # executed after each test

    def tearDown(self):
        db.session.remove()
        db.drop_all()

    # each test should start with 'test'
    def test_customer_setup(self):
        new_customer = Customer("Acme Company", "acme@domain.com", "6125551000",
                                datetime.datetime.now(pytz.timezone('US/Central')))
        db.session.add(new_customer)
        db.session.commit()


if __name__ == "__main__":
    unittest.main()

There would be an extensive amount of code i would have to paste so show all the dependencies. You can find the source code here.

https://github.com/ande0581/madsenconcrete

Thanks

Jeff A
  • 73
  • 8
  • I don't think you're connecting to the database you think you are. `db = SQLAlchemy(app)` happens long before you set the URI to your test database. Are you sure your code matches the tutorial? Maybe you need to call `db.init_app(app)` in your setup. The course isn't public, so it's hard to know what you missed. – dirn Jan 03 '16 at 18:13
  • Thanks for the response. My code no longer matches the course. I used the course as a framework and now i am trying to expand on the project they were demonstrating with. The course source code can be found here https://github.com/realpython/book2-exercises/tree/master/flasktaskr-03/project. I am not sure i follow what you are saying about not using the correct database but so much of this is new to me i am not sure i am grasping the imports and inheritance correctly. – Jeff A Jan 03 '16 at 18:30
  • Hi I'm stuck at exactly the same place. Did you get this to work? What did you do? – scientific_explorer Dec 11 '18 at 15:37
  • @JeffA I figured out something. That I had to run `db_create.py` every time before running the test, then it works properly. Else it doesn't. Maybe I might encounter this later in the course as to why. – scientific_explorer Dec 11 '18 at 16:40

1 Answers1

0

Ultimately, the problem is that you are creating your db object from an already configured app:

# config
app = Flask(__name__)
app.config.from_object('_config')
db = SQLAlchemy(app)

If you use the create_app pattern (documented in more detail in this answer) you will be able to alter the configuration you are loading for your test application.

Community
  • 1
  • 1
Sean Vieira
  • 155,703
  • 32
  • 311
  • 293
  • Thanks for the help. I still am not entire sure how to implement this and blueprints are coming up in my course work. I did try one change to bring my script in line with the example and it resolved my problem. I have no idea why. The gist of what i did was this line: from forms import LoginForm, AddCustomerForm, AddAddressForm, AddJournalForm, AddBidForm, AddServiceForm,\ AddBidItemForm, EditBidForm To the top of my script, before they were below my flask app config and helper functions like database queries. – Jeff A Jan 03 '16 at 23:17