Skip to main content

Testing a Flask App

 Main.py

from flask import Flask

from flask_restful import Api, Resource, reqparse

app = Flask(__name__)

api = Api(app)

@app.route("/")

def hello_world():

    return "<p>Hello, World!</p>"

class DemoApiEndpoint(Resource):

    def __init__(self):

        self.post_args = reqparse.RequestParser()

        self.post_args.add_argument("name",

                                    type=str,

                                    help="You must include a name string with this post request.",

                                    required=True)

    def get(self):

        return {

            "message": "This is a response from a GET request."

        }

    def post(self):

        args = self.post_args.parse_args()

        name = args['name']

        return {

            "message": f'Nice to meet you, {name}!'

        }

api.add_resource(DemoApiEndpoint, "/api/DemoApiEndpoint")

if __name__ == '__main__':

    app.run(host="0.0.0.0", port=80, debug=True)



tests/test_home_route.py

from main import app

def test_home_route():

    response = app.test_client().get('/')

    assert response.status_code == 200

    assert b"Hello, World!" in response.data



tests/test_demo_api_endpoint.py

from main import app

def test_get_api_endpoint():

    with app.test_client() as c:

        response = c.get('api/DemoApiEndpoint')

        assert response.status_code == 200

        json_response = response.get_json()

        assert json_response == {'message': 'This is a response from a GET request.'}


def test_post_api_endpoint():

    with app.test_client() as c:

        response = c.post('api/DemoApiEndpoint', json={

            "name": "Vincent"

        })

        assert response.status_code == 200

        json_response = response.get_json()

        assert json_response == {'message': 'Nice to meet you, Vincent!'}


# test_api_endpoint()

# test_post_api_endpoint()

Comments

Popular posts from this blog

Kubernetes2

  Challenge 2 ConfigMaps: Step-1: To create a ConfigMap named 'fresco-config' with the key-value pair SERVER_URL= https://www.fresco.me , use the following command: kubectl create configmap fresco-config --from-literal=SERVER_URL=https://www.fresco.me To verify if the ConfigMap is created, use the following command: kubectl get configmap fresco-config Step-2: To create an nginx pod with the environmental variable SERVER_URL_ENV using the ConfigMap created earlier, use the following YAML template: apiVersion: v1 kind: Pod metadata: name: fresco-nginx-pod spec: containers: - name: fresco-nginx-container image: nginx env: - name: SERVER_URL_ENV valueFrom: configMapKeyRef: name: fresco-config key: SERVER_URL Deploy the above file you can name it deployment.yaml and use the command: kubectl apply -f deployment.yaml To test your configuration by executing the following command: ...

Mini-Projects for Docker, Docker Swarm

 Mini-Projects for Docker Challenge 1 Welcome to the Docker challenge, your task is to follow the below steps and complete them successfully. Step 1: Pull latest nginx image Step 2: Create a new bridge 'bridge_sample' Step 3: Run a couple of images (Cont1 and Cont2) and connect these to the new bridge created. Now try to ping from cont1 to cont2 to verify connectivity. Step 4: Stop containers Step 5: Remove network, containers, and images using docker commands Note: Execute "history -w" in the terminal before submitting the scenario. Before moving to Answers try to solve yourself... Let's get started... Step 1: Pull latest nginx image docker pull nginx:latest Step 2: Create a new bridge 'bridge_sample' docker network create bridge_sample Step 3: Run containers and connect them to the new bridge docker run -d --name Cont1 --network bridge_sample nginx:latest docker run -d --name Cont2 --netw...

Hands-on with Docker and Docker-Compose

  Hands-on with Docker & Docker-Compose (1) Create a "Dockerfile" inside the path "-/Desktop/Project/docker-docker-compose working-with-multiple-services/app", with base image "python:3.7" to dockerize the given Flask application "app.py" (2) Create another "Dockerfile" inside the path "-/Desktop/Project/docker-docker-compose-working-with-multiple-services/db", with base image "postgres" and configure the environment variables postgres user "abc", postgres password "abc@123", postgres db "postgres". (3) Create one more "Dockerfile" inside the path "-/Desktop/Project/docker-docker-compose-working-with-multiple-services/cache", with base image as "redis". (4) Create a "docker-compose.yml" file inside the path "-/Desktop/Project/docker-docker-compose-working-with-multiple-services" with the below specifications,      (4.1) Create three s...