This web application is used to filter countries according to indexs provided by the user. It can be extended easily by adding new routes on the controllers module at app folder.
app/
|-> controllers.py
|-> models.py
templates/
|-> index.html
test/
|-> unit/
|-> pytest.ini
|-> test_countries.py
util/
|-> input_file.csv
|-> script.sql
db_setup.py
main.py
requirements.txt
We need Git tools to clone the project, PIP to install Python packages, a SQL server to make queries and a virtual environment for Python:
$ sudo apt install git
$ sudo apt install python3-pip
$ sudo apt install mysql-server
$ sudo apt-get install python3-venv
A new user account will allow us to bring data from a CSV file:
# Write the root password after run the following:
$ sudo mysql -u root -p
# In this example, 'mauri' is the new user and '280490mg' is its password:
mysql> CREATE USER 'mauri'@'localhost' IDENTIFIED BY '280490mg';
Query OK, 0 rows affected (0.00 sec)
# Finally, we need to give it permissions to create a database later:
mysql> GRANT ALL PRIVILEGES ON *.* TO 'mauri'@'localhost' WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)
To clone the repository to your local machine, perform the following:
# Create a new folder for your files:
$ mkdir ~/aivo
# Make sure you are in the above directory:
$ cd ~/aivo
# Clone the repository:
$ git clone https://github.com/mauriciolg90/aivo_test.git
A simple way to query the data is to load the CSV into a SQL table:
# Make sure the SQL script is on the below directory (downloaded previously):
mysql -u mauri -p < ~/aivo/aivo_test/util/script.sql
The previous command requires to insert the password, and if we didn't receive any error messages, then all were ok.
Creating a virtual environment will isolate the libraries for one project from another and is very useful when you have multiple Python applications running on a single server.
To create a virtual environment, perform the following:
# Make sure you are in the work directory:
$ cd ~/aivo
# Create the virtual environment:
$ python3 -m venv flask_env
For making your environment variables accessible to the application, you will need to modify your virtualenv activation file:
# Open the file using any text editor, for example:
$ nano flask_env/bin/activate
# Add the following lines according to the SQL parameters configured previously:
export DB_HOST='localhost'
export DB_PORT='3306'
export DB_NAME='flask_test'
export DB_USER='mauri'
export DB_PASS='280490mg'
# Save and exit
NOTE: by default, host and port are 'localhost' and '3306' respectively.
Finally, we can start to use the virtual environment doing:
# Activate the created virtualenv:
$ source flask_env/bin/activate
# Install flask and other dependencies:
$ pip3 install -r aivo_test/requirements.txt
# NOTE: when you are done with virtualenv, you can run:
$ deactivate
Simply, you should run the main module as the following:
# This configure the endpoints and launch the web server, waiting for requests:
$ python3 aivo_test/main.py
Then, you can make queries using a client like Postman or Curl. The easiest way is to write the URL on any browser:
# Replace the 'index' parameter with a float value greater than 0:
http://localhost:5000/countries/sw_lifs_gt/<float:index>
# For example:
http://localhost:5000/countries/sw_lifs_gt/5.0
The application will return a JSON containing the countries with their index value greater than the input value, using only life satisfaction index and total inequality.
If you want to build another queries, you should only add new endpoints and handlers on the controllers module.
When the web server is running (i.e. it's up), you have 3 ways to run testcases. On another console and activating the virtualenv:
# Run ALL testcases:
$ python3 -m pytest -s aivo_test/test/unit/test_countries.py
# Run MARKED testcases (i.e. testcases with labels):
$ python3 -m pytest -s aivo_test/test/unit/test_countries.py -m status_code
$ python3 -m pytest -s aivo_test/test/unit/test_countries.py -m countries_sw_lifs
# Run SPECIFIC testcase:
$ python3 -m pytest -s aivo_test/test/unit/test_countries.py::test_not_found_request