11import os
22import unittest
3- import json
4- from flask_sqlalchemy import SQLAlchemy
53
64from flaskr import create_app
7- from models import setup_db , Question , Category
5+ from models import db , Question , Category
86
97
108class TriviaTestCase (unittest .TestCase ):
@@ -13,18 +11,28 @@ class TriviaTestCase(unittest.TestCase):
1311 def setUp (self ):
1412 """Define test variables and initialize app."""
1513 self .database_name = "trivia_test"
16- self .database_path = "postgres://{}/{}" .format ('localhost:5432' , self .database_name )
17-
14+ self .database_user = "postgres"
15+ self .database_password = "password"
16+ self .database_host = "localhost:5432"
17+ self .database_path = f"postgresql://{ self .database_user } :{ self .database_password } @{ self .database_host } /{ self .database_name } "
18+
19+ # Create app with the test configuration
1820 self .app = create_app ({
19- "SQLALCHEMY_DATABASE_URI" : self .database_path
21+ "SQLALCHEMY_DATABASE_URI" : self .database_path ,
22+ "SQLALCHEMY_TRACK_MODIFICATIONS" : False ,
23+ "TESTING" : True
2024 })
25+ self .client = self .app .test_client ()
2126
22- self .client = self .app .test_client
27+ # Bind the app to the current context and create all tables
28+ with self .app .app_context ():
29+ db .create_all ()
2330
24-
2531 def tearDown (self ):
26- """Executed after reach test"""
27- pass
32+ """Executed after each test"""
33+ with self .app .app_context ():
34+ db .session .remove ()
35+ db .drop_all ()
2836
2937 """
3038 TODO
@@ -34,4 +42,4 @@ def tearDown(self):
3442
3543# Make the tests conveniently executable
3644if __name__ == "__main__" :
37- unittest .main ()
45+ unittest .main ()
0 commit comments