1
1
import os
2
2
import unittest
3
- import json
4
- from flask_sqlalchemy import SQLAlchemy
5
3
6
4
from flaskr import create_app
7
- from models import setup_db , Question , Category
5
+ from models import db , Question , Category
8
6
9
7
10
8
class TriviaTestCase (unittest .TestCase ):
@@ -13,18 +11,28 @@ class TriviaTestCase(unittest.TestCase):
13
11
def setUp (self ):
14
12
"""Define test variables and initialize app."""
15
13
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
18
20
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
20
24
})
25
+ self .client = self .app .test_client ()
21
26
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 ()
23
30
24
-
25
31
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 ()
28
36
29
37
"""
30
38
TODO
@@ -34,4 +42,4 @@ def tearDown(self):
34
42
35
43
# Make the tests conveniently executable
36
44
if __name__ == "__main__" :
37
- unittest .main ()
45
+ unittest .main ()
0 commit comments