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: ...

Kubernetes1

  Challenge 1 Welcome to the Kubernetes challenge, your task is to follow the below steps and complete them successfully. Environment Setup Check whether docker & minikube are properly installed and configured. Start Minikube and execute this command to sync host docker with minikube docker minikube -p minikube docker-env and eval $(minikube docker-env) Step-1 Create a pod object using kubectl run command with google's sample image: gcr.io/google-samples/kubernetes-bootcamp:v1 and expose it on port 8080, name the pod as firstapp. Check if the pod creation is successful by running the command: kubectl get pod firstapp Step-2 Expose the application to the local VM by creating a Service object of type NodePort. Check if the service is created by running the command: kubectl get svc firstapp Step-3 Create another deployment using a 'YAML' file, create a deployment.yaml file that contains information of the number of replicas and the images to be ...

Docker + Kubernetes + Ansible

  Docker + Kubernetes + Ansible ----------------------------------------------------------------------------------------------------------------------------- Web Application Deployment using Kubernetes and Ansible A Flask application named "application.py" is given in the path Run the given setup.sh file given in the path to install the required dependencies be the challenge. 1. Start Minikube and sync host Docker with Minikube Docker. Note: If you get any errors while starting Minikube, please do try again by running the command to start Minikube. 2. Create a Dockerfile in the path and dockerize the given 'Flask' application as 'webapp-img' using 'python:alpine3.7' as its base image. 3. Create a 'deployment.yml' file in the path to create a Kubernetes deployment object named "web-deployment" with 3 replicas which uses the 'webapp-img' and runs a container named "webapp-container". Add Label "app=webapp" an...