Creating a database for testing

Now we will create the PostgreSQL database that we will use as a repository for our testing environment. Notice that the testing computer or server must have PostgreSQL 10.5 installed on it, as explained in the previous chapters for the development environment. I assume that you are running the tests on the same computer in which you worked with the previous examples.

Remember to make sure that the PostgreSQL bin folder is included in the PATH environmental variable. You should be able to execute the psql command-line utility from your current Terminal, Command Prompt, or Windows PowerShell.

We will use the PostgreSQL command-line tools to create a new database named test_flask_notifications. If you already have a PostgreSQL database with this name, make sure that you use another name in all the commands and configurations. You can perform the same task with any PostgreSQL GUI tool. If you are developing on Linux, it is necessary to run the commands as the postgres user. Run the following command in macOS or Windows to create a new database named test_flask_notifications. Notice that the command won't produce any output:

    createdb test_flask_notifications

In Linux, run the following command to use the postgres user:

    sudo -u postgres createdb test_flask_notifications
  

Now we will use the psql command-line tool to run some SQL statements to grant privileges on the database to a user. If you are using a different server than the development server, you will have to create the user before granting privileges. On macOS or Windows, run the following command to launch psql:

    psql

On Linux, run the following command to use the postgres user:

    sudo -u psql

Then, run the following SQL statements and finally enter q to exit the psql command-line tool. Replace your_user_name with your desired username to use in the new database and your_password with your chosen password. We will use the username and password in the Flask testing configuration. You don't need to run the steps if you are already working with a specific user in PostgreSQL and you have already granted privileges to the database for the user. You will see the output indicating that the permission was granted. The code file for the sample is included in the restful_python_2_04_01 folder, in the Flask01/configure_test_database.sql file:

    GRANT ALL PRIVILEGES ON DATABASE "test_flask_notifications" TO 
your_user_name;
q

Create a new test_config.py file within the service folder. The following lines show the code that declares variables that determine the configuration for Flask and SQLAlchemy for our testing environment. The SQL_ALCHEMY_DATABASE_URI variable generates an SQLAlchemy URI for the PostgreSQL database. Make sure you specify the desired database name in the value for DB_NAME and that you configure the user, password, host, and port based on your PostgreSQL configuration. If you followed the previous steps, use the settings specified in these steps. The code file for the sample is included in the restful_python_2_04_01 folder, in the Flask01/service/test_config.py file:

import os 
 
 
basedir = os.path.abspath(os.path.dirname(__file__)) 
SQLALCHEMY_ECHO = False 
SQLALCHEMY_TRACK_MODIFICATIONS = True 
# Replace your_user_name with the user name you configured for the test database 
# Replace your_password with the password you specified for the test database user 
SQLALCHEMY_DATABASE_URI = "postgresql://{DB_USER}:{DB_PASS}@{DB_ADDR}/{DB_NAME}".format(DB_USER="your_user_name", DB_PASS="your_password", DB_ADDR="127.0.0.1", DB_NAME="test_flask_notifications") 
SQLALCHEMY_MIGRATE_REPO = os.path.join(basedir, 'db_repository') 
# Pagination configuration 
PAGINATION_PAGE_SIZE = 4 
PAGINATION_PAGE_ARGUMENT_NAME = 'page' 
# Enable the TESTING flag 
TESTING = True 
# Disable CSRF protection in the testing configuration 
WTF_CSRF_ENABLED = False 
# Necessary for flask.url_for to build the URLs 
SERVER_NAME="127.0.0.1" 

As we did with the similar config.py file we created for our development environment, we will specify the previously created module, test_config, as an argument to a function that will create a Flask app that we will use for testing. This way, we have one module that specifies all the values for the different configuration variables for our development environment and another module that creates a Flask app for our testing environment.

It is also possible to create a class hierarchy with one class for each environment we want to use. However, in our sample case, it is easier to create a new configuration file for our testing environment.

The TESTING variable set to True enables a flag that indicates to Flask that we are running tests. This way, Flask extensions will also enable the test mode and the exceptions will propagate to the test client. The WTF_CSRF_ENABLED variable set to False disables the CSRF (short for Cross-Site Request Forgery) protection during test's execution. The SERVER_NAME variable specifies the host that is running the Flask server during tests. It is necessary to specify an appropriate value for this variable to make the flask.url_for method work OK when building the URLs for the different resources in our API.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.104.248